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

it.tidalwave.bluebill.taxonomy.elmo.impl.ElmoTaxonomy Maven / Gradle / Ivy

There is a newer version: 1.0.22
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Core - open source birding
 * Copyright (C) 2009-2011 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://bluebill.tidalwave.it
 * SCM: https://kenai.com/hg/bluebill~core-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.elmo.impl;

import javax.annotation.Nonnull;
import java.util.Set;
import javax.xml.namespace.QName;
import org.openide.util.Lookup;
import org.openrdf.repository.Repository;
import org.openrdf.elmo.Entity;
import org.openrdf.elmo.annotations.rdf;
import org.openrdf.concepts.skos.core.Concept;
import org.openrdf.concepts.skos.core.ConceptScheme;
import it.tidalwave.util.Initializer;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.semantic.EntityFactory;
import it.tidalwave.semantic.Wrapper;
import it.tidalwave.openrdf.elmo.RDFUtils;
import it.tidalwave.openrdf.elmo.impl.ElmoEntityFactory;
import it.tidalwave.bluebill.taxonomy.Finder;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.elmo.ElmoTaxonomyVocabulary;
import it.tidalwave.bluebill.taxonomy.io.Exporter;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
@rdf({ElmoTaxonomyVocabulary.URI_TAXONOMY})
public class ElmoTaxonomy extends Support implements InternalElmoTaxonomy
  {
    private static final String CLASS = ElmoTaxonomy.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

//    @Nonnull
//    private Lookup lookup;
//
    @Nonnull
    private Repository repository;

    protected final EntityFactory conceptFactory =
            new ElmoEntityFactory(InternalElmoTaxon.class,
                                                                ElmoTaxon.class,
                                                                Concept.class);
    
    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public ElmoTaxonomy (final @Nonnull Entity entity)
      {
        super(entity);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull @Override
    public Taxon.Builder createTaxon()
      {
        return new Taxon.Builder()
          {
            @Override @Nonnull
            public Taxon build()
              {
                logger.fine("createTaxon().build(%s)", this);

                final QName qName = new QName(id.stringValue());
                final InternalElmoTaxon taxon = conceptFactory.create(qName);
                taxon.setBindings(repository, ElmoTaxonomy.this);
                taxon.init(this, ElmoTaxonomy.this);

                Taxon taxon2 = taxon;

                for (final Initializer initializer : getInitializers())
                  {
                    taxon2 = initializer.initialize(taxon2);
                  }

                return taxon2;
              }
          };
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull @Override
    public Taxon.Builder createTopTaxon()
      {
        return createTaxon().withInitializer(new Initializer()
          {
            @Override @Nonnull
            public Taxon initialize (final @Nonnull Taxon taxon)
              {
                getLookup().lookup(ConceptScheme.class).getSkosHasTopConcepts().add(taxon.getLookup().lookup(Concept.class));
//                as(ConceptScheme.class).getSkosHasTopConcepts().add(taxon.as(Concept.class));

                return taxon;
              }
          });
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull @Override
    public Taxon getTopTaxon()
      {
        logger.fine("getTopConcept() - @%x", System.identityHashCode(this));

        final Set topConcepts = as(ConceptScheme.class).getSkosHasTopConcepts();
        final InternalElmoTaxon concept = (InternalElmoTaxon)topConcepts.iterator().next();
        concept.setBindings(repository, this);

//        return concept;
        return Wrapper.wrap(concept); // FIXME: shouldn't be wrapped by the Proxy?
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull @Override
    public Finder findTaxa()
      {
        return new ElmoTaxonomyFinder(Taxon.class);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public synchronized Taxon findOrCreateAnonymousTaxon (final @Nonnull String id)
      {
        logger.fine("findOrCreateAnonymousTaxon(%s)", id);

        final QName qName = new QName(id);
        InternalElmoTaxon taxon = null; // FIXME anonymousTaxonFactory.find(qName);
//        InternalElmoAnonymousTaxon taxon = null; // FIXME anonymousTaxonFactory.find(qName);

        if (taxon == null)
          {
            taxon = conceptFactory.create(qName);
//            taxon = anonymousTaxonFactory.create(qName);
            taxon.setBindings(repository, this);
//            taxon.getLookup().lookup(Concept.class).getSkosInSchemes().add(getLookup().lookup(ConceptScheme.class)); // FIXME: use as()
          }

        return taxon;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void init (final @Nonnull Taxonomy.Builder builder)
      {
        logger.finest("init(%s) - @%x", builder, System.identityHashCode(this));
        RDFUtils.setDisplayName(entity, builder.getDisplayable().getDisplayName());
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void setBindings (final @Nonnull Repository repository)
      {
        logger.finest("setBindings(%s) - @%x", repository, System.identityHashCode(this));
        this.repository = repository;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public  T as (final @Nonnull Class clazz)
      {
        if (Exporter.class.equals(clazz))
          {
            return clazz.cast(new ElmoExporter(repository));
          }

        return super.as(clazz);
      }

    @Override
    public Taxon _findConcept(String id)
      throws NotFoundException
      {
        return conceptFactory.find(new QName(id));
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy