All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.geo.location.elmo.impl.ElmoGeoSchema Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * forceTen - open source geography
 * Copyright (C) 2007-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://forceten.tidalwave.it
 * SCM: https://bitbucket.org/tidalwave/forceten-src
 *
 **********************************************************************************************************************/
package it.tidalwave.geo.location.elmo.impl;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.openrdf.elmo.annotations.rdf;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.semantic.LazyPropertyHolder;
import it.tidalwave.semantic.Wrapper;
import it.tidalwave.openrdf.elmo.ElmoUtils;
import it.tidalwave.openrdf.elmo.ElmoVolatile;
import static it.tidalwave.geo.GeoContainer.GeoContainer;
import it.tidalwave.geo.geocoding.GeoCoder;
import it.tidalwave.geo.geocoding.GeoCoderEntity;
import static it.tidalwave.geo.geocoding.GeoCoderEntity.GeoCoderEntity;
import it.tidalwave.geo.location.GeoLocation;
import it.tidalwave.geo.location.GeoLocation.Finder;
import it.tidalwave.geo.location.GeoSchema;
import it.tidalwave.geo.location.elmo.GeoVocabulary;
import static it.tidalwave.geo.geocoding.GeoCoderManager.GeoCoderManager;

/***********************************************************************************************************************
 *
 * @author Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@rdf(GeoVocabulary.URI_GEO_SCHEMA)
public class ElmoGeoSchema extends Support implements GeoSchema
  {
    private final static String CLASS = ElmoGeoSchema.class.getName();
    private final static Logger logger = Logger.getLogger(CLASS);
    
    protected String geoCoderName;
    
    @ElmoVolatile @CheckForNull
    private GeoLocation rootLocation;

    @CheckForNull @ElmoVolatile
    private Lookup lookup;

    @ElmoVolatile
    protected final LazyPropertyHolder geoCoder = new LazyPropertyHolder()
      {
        @Override @Nonnull
        protected GeoCoder create()
          {
            try
              {
                geoCoderName ="GeoNames"; // FIXME: it's always null
//                assert geoCoderName != null : "null geoCoderName";
                return Locator.find(GeoCoderManager).findGeoCoderByName(geoCoderName);
              }
            catch (NotFoundException e)
              {
                throw new RuntimeException(e);
              }
          }
      };

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull @Override
    public synchronized GeoLocation getRoot()
      {
        logger.fine("getRoot() - %s", this);
        assert geoCoder.get() != null : "Null geoCoder";

        if (rootLocation == null)
          {
            final GeoCoderEntityProxy gcEntityProxy = ElmoGeoCoderEntityProxyFactory.findOrCreate(this, geoCoder.get().getRoot());
            rootLocation = ElmoGeoLocationFactory.findOrCreate(this, gcEntityProxy);
          }

        logger.finer(">>>> returning %s", rootLocation);
        
        return rootLocation;
      }


    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Finder findLocations()
      {
        assert geoCoder.get() != null : "Null geoCoder";
        
        return new FinderSupport("findLocations()")
          {
            @Override
            @Nonnull
            protected List computeResults()
              {
                final List result = new ArrayList();

                if (displayName != null)
                  {
                    throw new IllegalArgumentException("name criterion on supported");
                  }

                else if (range != null)
                  {
                    for (final GeoCoderEntity gcEntity : range.as(GeoContainer).find(GeoCoderEntity).with(geoCoder).results())
                      {
                        final GeoCoderEntityProxy gcEntityProxy =
                            ElmoGeoCoderEntityProxyFactory.findOrCreate(ElmoGeoSchema.this, gcEntity);
                        result.add(ElmoGeoLocationFactory.findOrCreate(ElmoGeoSchema.this, gcEntityProxy));
                      }
                  }

                else if (path != null)
                  {
                    try
                      {
                        GeoLocation geoLocation = getRoot();
                        final StringTokenizer st = new StringTokenizer(path, "/");

                        while (st.hasMoreTokens())
                          {
                            geoLocation = geoLocation.findChildren(ElmoGeoSchema.this).withDisplayName(st.nextToken().trim()).result();
                          }

                        result.add(geoLocation);
                      }
                    catch (NotFoundException e)
                      {
                        doNothing(); // ok, empty result
                      }
                  }

                return Wrapper.wrap(result);
              }
          };
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public synchronized Lookup getLookup()
      {
        if (lookup == null)
          {
            lookup = new ProxyLookup(super.getLookup(), Lookups.fixed(geoCoder.get()));
          }

        return lookup;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String toString()
      {
        return String.format("ElmoGeoSchema@%x[%s]", System.identityHashCode(this), ElmoUtils.toString(this));
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy