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

it.tidalwave.bluebill.mobile.resources.RdfUtils Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Resources - 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://java.net/hg/bluebill~resources-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.resources;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.Resource;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import it.tidalwave.util.Id;
import static it.tidalwave.bluebill.taxonomy.elmo.ElmoTaxonomyVocabulary.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class RdfUtils 
  {
    public static final String NS_SKOS = "http://www.w3.org/2004/02/skos/core#";
    /*******************************************************************************************************************
     *
     * Adds a bunch of statements to the repository.
     * 
     * @param  connection  the repository connection
     * @param  statements  the statements
     * 
     ******************************************************************************************************************/
    public static void addStatements (final @Nonnull RepositoryConnection connection,
                                      final @Nonnull List statements)
      throws RepositoryException
      {
        for (final Statement statement : statements)
          {
            connection.add(statement);
          }
      }

    /*******************************************************************************************************************
     *
     * Returns a list of statements with the given subject id.
     * 
     * @param  connection  the repository connection
     * @param  subjectId   the subject id
     *
     ******************************************************************************************************************/
    @Nonnull
    public static List findStatementsWithSubject (final @Nonnull RepositoryConnection connection,
                                                             final @Nonnull Id subjectId)
      throws RepositoryException
      {
        final ValueFactory valueFactory = connection.getValueFactory();
        return connection.getStatements(valueFactory.createURI(subjectId.stringValue()), null, null, false).asList();
      }

    /*******************************************************************************************************************
     *
     * Finds all the id of objects that are relevant to the given taxon. They are:
     * 
     * 
    *
  1. the taxon id itself;
  2. *
  3. the ids of objects that refers to the taxon;
  4. *
  5. the ids of objects that are referred by the taxon;
  6. *
* * @param taxonId the taxon id * @param connection the repository connection * @return a set of ids * ******************************************************************************************************************/ @Nonnull public static Set findAllRelevantSubjectIds (final @Nonnull Id taxonId, final @Nonnull RepositoryConnection connection) throws RepositoryException { final Set objectIds = new TreeSet(); objectIds.add(taxonId); objectIds.addAll(findObjectIdsReferring(connection, taxonId)); for (final Id id : new ArrayList(objectIds)) { objectIds.addAll(findObjectIdsReferredBy(connection, id)); } return objectIds; } /******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private static List findObjectIdsReferring (final @Nonnull RepositoryConnection connection, final @Nonnull Id objectId) throws RepositoryException { final List referingIds = new ArrayList(); final ValueFactory valueFactory = connection.getValueFactory(); for (final Statement statement : connection.getStatements(null, null, valueFactory.createLiteral(objectId.stringValue()), false).asList()) { final String stringValue = statement.getPredicate().stringValue(); if (!stringValue.equals(URI_SCIENTIFIC_NAME_ID) && !stringValue.startsWith(NS_SKOS)) { referingIds.add(new Id(statement.getSubject().stringValue())); } } for (final Statement statement : connection.getStatements(null, null, valueFactory.createURI(objectId.stringValue()), false).asList()) { final String stringValue = statement.getPredicate().stringValue(); if (!stringValue.equals(URI_SCIENTIFIC_NAME_ID) && !stringValue.startsWith(NS_SKOS)) { referingIds.add(new Id(statement.getSubject().stringValue())); } } return referingIds; } /******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private static List findObjectIdsReferredBy (final @Nonnull RepositoryConnection conn, final @Nonnull Id subject) throws RepositoryException { final List result = new ArrayList(); final ValueFactory valueFactory = conn.getValueFactory(); for (final Statement statement : conn.getStatements(valueFactory.createURI(subject.stringValue()), null, null, false).asList()) { final Value object = statement.getObject(); if (object instanceof Resource) { result.add(new Id(object.stringValue())); } } return result; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy