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 Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: ElmoTaxonomy.java,v 31ee4e8a62bc 2010/07/17 14:44:24 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.elmo.impl;

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

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: ElmoTaxonomy.java,v 31ee4e8a62bc 2010/07/17 14:44:24 fabrizio $
 *
 ******************************************************************************/
@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(InternalElmoTaxonomyConcept.class,
                                                                                ElmoTaxonomyConcept.class,
                                                                                Concept.class);

    protected final EntityFactory anonymousTaxonFactory =
            new ElmoEntityFactory(InternalElmoAnonymousTaxon.class,
                                                                  ElmoAnonymousTaxon.class,
                                                                  Concept.class);

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public ElmoTaxonomy (@Nonnull final Entity entity)
      {
        super(entity);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull
    @Override
    public TaxonomyConcept createConcept (@Nonnull final String displayName,
                                          @Nonnull final String id)
      {
        logger.fine("createConcept(%s, %s)", displayName, id);
        
        final QName qName = new QName(id);
        final InternalElmoTaxonomyConcept taxonomyConcept = conceptFactory.create(qName);
        taxonomyConcept.setBindings(repository, this); 
        taxonomyConcept.init(displayName, this);
                
        return taxonomyConcept;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull
    @Override
    public TaxonomyConcept createTopConcept (@Nonnull final String displayName,
                                             @Nonnull final String id)
      {
        logger.fine("createTopConcept(%s, %s)", displayName, id);

        final TaxonomyConcept taxonomyConcept = createConcept(displayName, id);
        final ConceptScheme skosConceptScheme = getLookup().lookup(ConceptScheme.class);
        final Concept skosConcept = taxonomyConcept.getLookup().lookup(Concept.class);
        skosConceptScheme.getSkosHasTopConcepts().add(skosConcept);
//        as(ConceptScheme.class).getSkosHasTopConcepts().add(taxonomyConcept.as(Concept.class));

        return taxonomyConcept;
      }

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

        final Set topConcepts = as(ConceptScheme.class).getSkosHasTopConcepts();
        final InternalElmoTaxonomyConcept concept = (InternalElmoTaxonomyConcept)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 findConcepts()
      {
        return new ElmoTaxonomyFinder(TaxonomyConcept.class);
      }

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

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

        if (taxon == null)
          {
            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 (@Nonnull final String displayName)
      {
        logger.finest("init(%s) - @%x", displayName, System.identityHashCode(this));
        RDFUtils.setDisplayName(entity, displayName);
      }

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

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

    public Map getDisplayNames()
      {
        throw new UnsupportedOperationException("Not supported yet.");
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy