<commit_before>/*
 *  Copyright 2013 <NAME>
 *
 *  Licensed under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package com.danhaywood.isis.wicket.gmap3.ui.collectioncontents;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.SpecificationLoader;
import org.apache.isis.core.runtime.system.context.IsisContext;
import org.apache.isis.viewer.wicket.model.models.EntityCollectionModel;
import org.apache.isis.viewer.wicket.ui.ComponentFactoryAbstract;
import org.apache.isis.viewer.wicket.ui.ComponentType;

import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;

import com.danhaywood.isis.wicket.gmap3.applib.Locatable;

public class CollectionOfEntitiesAsLocatablesFactory extends ComponentFactoryAbstract {

    private static final long serialVersionUID = 1L;

    private static final String ID_MAP = "map";

    private boolean determinedWhetherWebserviceAvailable;
    private boolean webserviceAvailable;

    public CollectionOfEntitiesAsLocatablesFactory() {
        super(ComponentType.COLLECTION_CONTENTS, ID_MAP);
    }

    @Override
    public ApplicationAdvice appliesTo(IModel<?> model) {
        
        if(!webserviceAvailable()) {
            return ApplicationAdvice.DOES_NOT_APPLY;
        }
        
        if (!(model instanceof EntityCollectionModel)) {
            return ApplicationAdvice.DOES_NOT_APPLY;
        }

        EntityCollectionModel entityCollectionModel = (EntityCollectionModel) model;

        ObjectSpecification typeOfSpec = entityCollectionModel.getTypeOfSpecification();
        ObjectSpecification locatableSpec = getSpecificationLoader().loadSpecification(Locatable.class);
        return appliesIf(typeOfSpec.isOfType(locatableSpec));
    }

    private boolean webserviceAvailable() {
        if(!determinedWhetherWebserviceAvailable) {
            webserviceAvailable = isInternetReachable();
            determinedWhetherWebserviceAvailable = true;
        }
        return webserviceAvailable;
    }
    
    /**
     * Tries to retrieve some content, 1 second timeout.
     */
    private static boolean isInternetReachable()<commit_msg>#6 check if internet available<commit_after>/*
 *  Copyright 2013 <NAME>
 *
 *  Licensed under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package com.danhaywood.isis.wicket.gmap3.ui.collectioncontents;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.SpecificationLoader;
import org.apache.isis.core.runtime.system.context.IsisContext;
import org.apache.isis.viewer.wicket.model.models.EntityCollectionModel;
import org.apache.isis.viewer.wicket.ui.ComponentFactoryAbstract;
import org.apache.isis.viewer.wicket.ui.ComponentType;

import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;

import com.danhaywood.isis.wicket.gmap3.applib.Locatable;

public class CollectionOfEntitiesAsLocatablesFactory extends ComponentFactoryAbstract {

    private static final long serialVersionUID = 1L;

    private static final String ID_MAP = "map";

    private boolean determinedWhetherInternetReachable;
    private boolean internetReachable;

    public CollectionOfEntitiesAsLocatablesFactory() {
        super(ComponentType.COLLECTION_CONTENTS, ID_MAP);
    }

    @Override
    public ApplicationAdvice appliesTo(IModel<?> model) {
        
        if(!internetReachable()) {
            return ApplicationAdvice.DOES_NOT_APPLY;
        }
        
        if (!(model instanceof EntityCollectionModel)) {
            return ApplicationAdvice.DOES_NOT_APPLY;
        }

        EntityCollectionModel entityCollectionModel = (EntityCollectionModel) model;

        ObjectSpecification typeOfSpec = entityCollectionModel.getTypeOfSpecification();
        ObjectSpecification locatableSpec = getSpecificationLoader().loadSpecification(Locatable.class);
        return appliesIf(typeOfSpec.isOfType(locatableSpec));
    }

    private boolean internetReachable() {
        if(!determinedWhetherInternetReachable) {
            internetReachable = isInternetReachable();
            determinedWhetherInternetReachable = true;
        }
        return internetReachable;
    }
    
    /**
     * Tries to retrieve some content, 1 second timeout.
     */
    private static boolean isInternetReachable()
{'fraction_non_alphanumeric': 0.07849708943376257, 'fraction_numerical': 0.0044099488445934025, 'mean_word_length': 3.776748104465038, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5402044', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1535, 'n_words': 377}