info.sswap.impl.empire.model
Class ImplFactory

java.lang.Object
  extended by info.sswap.impl.empire.model.ImplFactory

public class ImplFactory
extends java.lang.Object

Factory class for creating and initializing concrete implementations of SSWAP interfaces.

Author:
Blazej Bulka

Field Summary
private  javax.persistence.EntityManagerFactory entityManagerFactory
          The factory for creating Empire EntityManagers (one manager for each Jena model/data source).
private static java.lang.String GET_TYPES_QUERY
           
private static ImplFactory instance
          The instance singleton instance of this class.
 
Constructor Summary
private ImplFactory()
          Private constructor (following the Singleton design pattern) that initializes Empire and creates Empire EntityManagerFactory.
 
Method Summary
<I extends ModelImpl,A extends SSWAPModel>
I
assertImplementation(A apiObject, java.lang.Class<I> implementationClass)
          Asserts that the parameter typed via an API interface has the expected implementation.
 SourceModel assertSourceModel(ModelImpl modelImpl)
          Gets the source model of a dereferenced ModelImpl
<T extends ModelImpl>
boolean
canAs(SourceModel sourceModel, java.net.URI uri, java.lang.Class<T> clazz)
          Checks whether the given individual can be viewed using the specified Empire class.
<T extends ModelImpl>
T
castDependentModel(SourceModel sourceModel, java.net.URI uri, java.lang.Class<T> clazz)
           
 void copy(SSWAPIndividual srcInd, SSWAPIndividual dstInd, SSWAPDocument dstDoc, java.util.Collection<java.lang.String> excludedTypes)
           
<T extends ModelImpl>
T
createDependentObject(SourceModel sourceModel, java.net.URI uri, java.lang.Class<T> clazz)
          Creates a dependent object for a SourceModel (e.g., a SSWAPResource for an RDG).
 SSWAPElement createElement(SourceModel sourceModel, com.hp.hpl.jena.rdf.model.RDFNode rdfNode)
          Creates a SSWAPElement based on an RDF node.
<T extends SourceModelImpl>
T
createEmptySourceModel(java.net.URI uri, java.lang.Class<T> clazz)
          Creates a SourceModel based on an empty Jena model.
<T extends ModelImpl>
T
createEmptySSWAPDataObject(java.net.URI uri, java.lang.Class<T> clazz)
          Creates an empty Empire-generated object.
 javax.persistence.EntityManager createEntityManager(com.hp.hpl.jena.rdf.model.Model model)
          Creates an EntityManager for a specified Jena model
 javax.persistence.EntityManager createEntityManager(java.lang.Object stream, java.lang.String format)
          Creates a new entity manager for a Jena data source.
 IndividualImpl createIndividual(SourceModel sourceModel, com.hp.hpl.jena.rdf.model.Resource resource)
          Creates a SSWAPIndividual that for the Jena resource.
 IndividualImpl createIndividual(SourceModel sourceModel, java.net.URI uri)
          Creates a generic (non-Empire managed) SSWAP Entity.
 ListImpl createList(SourceModel sourceModel, com.hp.hpl.jena.rdf.model.RDFList list)
          Creates a SSWAPList implementation for an RDFList
 Literal createLiteral(com.hp.hpl.jena.rdf.model.Literal literal)
          Creates a SSWAP implementation of a literal based on a Jena literal.
 com.hp.hpl.jena.rdf.model.Literal createLiteral(SourceModel sourceModel, Literal literal)
          Creates a Jena literal for a SSWAPLiteral
 Literal createLiteral(java.lang.String value, SourceModel sourceModel)
           
 Literal createLiteral(java.lang.String value, java.lang.String language, SourceModel sourceModel)
           
 Literal createLiteral(java.lang.String value, java.net.URI datatypeURI, SourceModel sourceModel)
           
 Literal createLiteral(java.lang.String value, java.net.URI datatypeURI, java.lang.String language, SourceModel sourceModel)
          Creates a literal with the specified value
 PropertyImpl createProperty(SourceModel sourceModel, SSWAPIndividual individual, SSWAPElement value, java.net.URI uri)
          Creates an implementation of SSWAPProperty with the specified URI
 com.hp.hpl.jena.rdf.model.RDFList createRDFList(SourceModel sourceModel, SSWAPList list)
          Creates an RDFList for a SSWAPList.
 com.hp.hpl.jena.rdf.model.RDFNode createRDFNode(SourceModel sourceModel, SSWAPElement element)
          Creates an RDFNode for a SSWAPElement.
 com.hp.hpl.jena.rdf.model.Resource createResource(SourceModel sourceModel, SSWAPIndividual individual)
          Creates a corresponding Jena Resource for the SSWAPIndividual
 SSWAPElement deepCopyElement(SourceModel newModel, SSWAPElement originalElement, java.util.Set<java.net.URI> copiedIndividuals)
          Performs a deep copy of a SSWAPElement from one source model to another
 void deepCopyIndividual(SourceModel newModel, SSWAPIndividual originalIndividual, IndividualImpl dstIndividual, java.util.Set<java.net.URI> copiedIndividuals)
           
private  SSWAPIndividual deepCopyIndividual(SourceModel newModel, SSWAPIndividual originalIndividual, java.util.Set<java.net.URI> copiedIndividuals)
          Perform a deep copy of a SSWAPIndividual into another source model
private  SSWAPList deepCopyList(SourceModel newModel, SSWAPList originalList, java.util.Set<java.net.URI> copiedIndividuals)
          Perform a deep copy of a SSWAPList into another source model
private  boolean existsTyped(SourceModel sourceModel, java.net.URI individualURI)
           
static ImplFactory get()
          The getter method for the singleton instance.
private  java.lang.Throwable getRootCause(java.lang.Throwable e)
          Gets the root cause for the exception
private  java.util.Collection<java.net.URI> getTypes(SourceModel sourceModel, java.net.URI individualURI)
           
static void initURI(ModelImpl object)
          Initializes the URI of a SSWAPModel based on its RDF identifier.
private  boolean isTypedAs(SourceModel sourceModel, java.net.URI individualURI, java.net.URI typeURI)
           
<T> T
readSSWAPDataObject(javax.persistence.EntityManager aManager, java.lang.Class<T> clazz)
          Reads a single Empire-annotated object from the data source managed by the specified entity manager.
<T> T
readSSWAPDataObject(javax.persistence.EntityManager aManager, java.lang.Class<T> clazz, java.net.URI uri)
           
<T extends SourceModel>
T
readSSWAPDataObject(java.lang.Object stream, java.lang.String format, java.lang.Class<T> clazz)
          Reads a single Empire-annotated object from a data source (usually a stream).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET_TYPES_QUERY

private static final java.lang.String GET_TYPES_QUERY
See Also:
Constant Field Values

entityManagerFactory

private javax.persistence.EntityManagerFactory entityManagerFactory
The factory for creating Empire EntityManagers (one manager for each Jena model/data source).


instance

private static ImplFactory instance
The instance singleton instance of this class.

Constructor Detail

ImplFactory

private ImplFactory()
Private constructor (following the Singleton design pattern) that initializes Empire and creates Empire EntityManagerFactory.

Method Detail

get

public static ImplFactory get()
The getter method for the singleton instance.

Returns:
the instance of the implementation

createEmptySSWAPDataObject

public <T extends ModelImpl> T createEmptySSWAPDataObject(java.net.URI uri,
                                                          java.lang.Class<T> clazz)
                                               throws DataAccessException
Creates an empty Empire-generated object. The classes for Empire-generated objects (like ProviderImpl) are abstract, and therefore cannot be instantiated directly since Empire has to provide the implementations for the annotated methods. This method wraps the call to Empire to provide such implementation, and instantiates the object. The object is not initially populated with any data (there is no data source to populate the object). The object has the URI set. For example, to create an empty object representing a SSWAPProvider, one would call createEmptySSWAPDataObject(uri, ProviderImpl.class)

Type Parameters:
T - the Java class with Empire annotations
Parameters:
uri - the URI that will be assigned to this SSWAPModel
clazz - the class for the implementation (it should contain Empire annotations)
Returns:
a new instance of the requested class or null if it was not possible to instantiate (most likely because the class is not properly annotated by empire or has other abstract methods than the ones to be filled by Empire)
Throws:
DataAccessException

createEntityManager

public javax.persistence.EntityManager createEntityManager(java.lang.Object stream,
                                                           java.lang.String format)
                                                    throws DataAccessException
Creates a new entity manager for a Jena data source.

Parameters:
stream - the place to read the data from. Typically it is an InputStream, but it can be any Empire-supported object (e.g., Reader, File, or URL).
format - the string containing the format of the data (e.g., "RDF/XML")
Returns:
the entity manager for the given data source
Throws:
DataAccessException - if an error should occur when accessing the data source (e.g., I/O error or malformed data)

getRootCause

private java.lang.Throwable getRootCause(java.lang.Throwable e)
Gets the root cause for the exception

Parameters:
e - exception
Returns:
the root cause

createEntityManager

public javax.persistence.EntityManager createEntityManager(com.hp.hpl.jena.rdf.model.Model model)
Creates an EntityManager for a specified Jena model

Parameters:
model - the Jena model for which the EntityManager should be created
Returns:
the newly created EntityManager

createEmptySourceModel

public <T extends SourceModelImpl> T createEmptySourceModel(java.net.URI uri,
                                                            java.lang.Class<T> clazz)
Creates a SourceModel based on an empty Jena model.

Type Parameters:
T - the type parameter
Parameters:
uri - the URI of the SourceModel
clazz - the actual implementation class
Returns:
the newly created SourceModel with a properly created empty Jena model and associated EntityManager

createDependentObject

public <T extends ModelImpl> T createDependentObject(SourceModel sourceModel,
                                                     java.net.URI uri,
                                                     java.lang.Class<T> clazz)
Creates a dependent object for a SourceModel (e.g., a SSWAPResource for an RDG).

Type Parameters:
T - the type parameter for the object to be created
Parameters:
sourceModel - the SourceModel in which the dependent object should be created
uri - the URI of the newly created model
clazz - the actual implementation class
Returns:
the newly created object that is dependent for this SourceModel

getTypes

private java.util.Collection<java.net.URI> getTypes(SourceModel sourceModel,
                                                    java.net.URI individualURI)

isTypedAs

private boolean isTypedAs(SourceModel sourceModel,
                          java.net.URI individualURI,
                          java.net.URI typeURI)

existsTyped

private boolean existsTyped(SourceModel sourceModel,
                            java.net.URI individualURI)

canAs

public <T extends ModelImpl> boolean canAs(SourceModel sourceModel,
                                           java.net.URI uri,
                                           java.lang.Class<T> clazz)
Checks whether the given individual can be viewed using the specified Empire class. This method checks @RdfsClass annotation on the class and compares it with with the asserted types in the underlying model.

Type Parameters:
T - Empire class
Parameters:
sourceModel - SSWAP Model to which the individual belongs
uri - URI of individual to check
clazz - Class to which to check the annotation RdfsClass.class
Returns:
true if the individual does belong to the class; false otherwise

castDependentModel

public <T extends ModelImpl> T castDependentModel(SourceModel sourceModel,
                                                  java.net.URI uri,
                                                  java.lang.Class<T> clazz)

readSSWAPDataObject

public <T> T readSSWAPDataObject(javax.persistence.EntityManager aManager,
                                 java.lang.Class<T> clazz)
Reads a single Empire-annotated object from the data source managed by the specified entity manager. The returned object is populated with data. If the data source contains data for multiple objects, only the first object will be returned. (This method should generally be used for data sources that contain only information about one object; e.g., like a PDG contains information about only one SSWAPProvider).

Type Parameters:
T - Empire-annotated type of the object to be returned by the Empire (the annotation on the object will allow Empire to identify which triples in the data source correspond to the object's fields).
Parameters:
aManager - the entity manager that manages that data source
clazz - the Empire-annotated class
Returns:
a single Empire-annotated object populated with data, or null, if there is no data matching the class annotations.

readSSWAPDataObject

public <T> T readSSWAPDataObject(javax.persistence.EntityManager aManager,
                                 java.lang.Class<T> clazz,
                                 java.net.URI uri)

readSSWAPDataObject

public <T extends SourceModel> T readSSWAPDataObject(java.lang.Object stream,
                                                     java.lang.String format,
                                                     java.lang.Class<T> clazz)
Reads a single Empire-annotated object from a data source (usually a stream). This method supports any Empire-supported data source: InputStream, Reader, File, or URL. The returned object is populated with data from the data source. If the data source contains data for multiple objects, only the first object will be returned. (This method should generally be used for data sources that contain only information about one object; e.g., like a PDG contains information about only one SSWAPProvider). Since this method creates an underlying Jena model, it can only be used for creating SourceModels (i.e., PDGs and SSWAP protocol graphs)

Type Parameters:
T - Empire-annotated type of the object to be returned by the Empire (the annotation on the object will allow Empire to identify which triples in the data source correspond to the object's fields). This type has to implement SourceModel to pass the underlying Jena model to the object.
Parameters:
stream - the place to read the data from. Typically it is an InputStream, but it can be any Empire-supported object (e.g., Reader, File, or URL).
format - the string containing the format of the data (e.g., "RDF/XML")
clazz - the Empire-annotated class
Returns:
a single Empire-annotated object populated with data, or null, if there is no data matching the class annotations.

initURI

public static void initURI(ModelImpl object)
Initializes the URI of a SSWAPModel based on its RDF identifier.

Parameters:
object - an object that does not correspond to a blank node in RDF

createElement

public SSWAPElement createElement(SourceModel sourceModel,
                                  com.hp.hpl.jena.rdf.model.RDFNode rdfNode)
Creates a SSWAPElement based on an RDF node. The actual subtype of the SSWAPElement will be determined based on the type/content of the rdf node (e.g., SSWAPIndividual, SSWAPList or just a literal SSWAPElement).

Parameters:
sourceModel - the source model for which the SSWAPElement should be created
rdfNode - the RDF node based on which the SSWAPElement will be created
Returns:
the SSWAPElement appropriately cast to an SSWAPIndividual, SSWAPList, just a literal SSWAPElement

createLiteral

public Literal createLiteral(java.lang.String value,
                             SourceModel sourceModel)

createLiteral

public Literal createLiteral(java.lang.String value,
                             java.net.URI datatypeURI,
                             SourceModel sourceModel)
                      throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

createLiteral

public Literal createLiteral(java.lang.String value,
                             java.lang.String language,
                             SourceModel sourceModel)

createLiteral

public Literal createLiteral(java.lang.String value,
                             java.net.URI datatypeURI,
                             java.lang.String language,
                             SourceModel sourceModel)
                      throws java.lang.IllegalArgumentException
Creates a literal with the specified value

Parameters:
value - the lexical representation of the value
datatypeURI - the URI of the XSD datatype (may be null)
language - the declared language of the datatype (may be null)
sourceModel - the source the source model where the literal should be created
Returns:
the created literal
Throws:
java.lang.IllegalArgumentException - if the lexical representation of the value is not valid according to the declared datatypeURI

createLiteral

public Literal createLiteral(com.hp.hpl.jena.rdf.model.Literal literal)
Creates a SSWAP implementation of a literal based on a Jena literal.

Parameters:
literal - the Jena literal
Returns:
the created implementation of a SSWAP literal.

createList

public ListImpl createList(SourceModel sourceModel,
                           com.hp.hpl.jena.rdf.model.RDFList list)
Creates a SSWAPList implementation for an RDFList

Parameters:
sourceModel - the source model where the SSWAPList should be created
list - the RDFList that should be represented as a SSWAPList
Returns:
the created implementation of the SSWAPList

createIndividual

public IndividualImpl createIndividual(SourceModel sourceModel,
                                       com.hp.hpl.jena.rdf.model.Resource resource)
Creates a SSWAPIndividual that for the Jena resource.

Parameters:
sourceModel - the source model where the entity should be created
resource - the Jena resource for which the entity should be created
Returns:
the created SSWAPIndividual

createIndividual

public IndividualImpl createIndividual(SourceModel sourceModel,
                                       java.net.URI uri)
Creates a generic (non-Empire managed) SSWAP Entity.

Parameters:
sourceModel - a source model where the entity should be created
uri - the URI of the entity
Returns:
the SSWAPIndividual

createProperty

public PropertyImpl createProperty(SourceModel sourceModel,
                                   SSWAPIndividual individual,
                                   SSWAPElement value,
                                   java.net.URI uri)
Creates an implementation of SSWAPProperty with the specified URI

Parameters:
sourceModel - the source model for which the SSWAPProperty should be created
uri - the URI of the property
Returns:
the created SSWAPProperty implementation

createRDFNode

public com.hp.hpl.jena.rdf.model.RDFNode createRDFNode(SourceModel sourceModel,
                                                       SSWAPElement element)
Creates an RDFNode for a SSWAPElement.

Parameters:
sourceModel - the source model where the RDFNode should be created
element - the SSWAPElement which should be reflected as the RDFNode
Returns:
the created RDFNode

createLiteral

public com.hp.hpl.jena.rdf.model.Literal createLiteral(SourceModel sourceModel,
                                                       Literal literal)
Creates a Jena literal for a SSWAPLiteral

Parameters:
sourceModel - the source model for which the Jena literal should be created
literal - the SSWAP literal
Returns:
the created Jena literal

createRDFList

public com.hp.hpl.jena.rdf.model.RDFList createRDFList(SourceModel sourceModel,
                                                       SSWAPList list)
Creates an RDFList for a SSWAPList.

Parameters:
sourceModel - the source model in which the RDFList should be created
list - the list for which the RDFList should be created
Returns:
the created RDFList

createResource

public com.hp.hpl.jena.rdf.model.Resource createResource(SourceModel sourceModel,
                                                         SSWAPIndividual individual)
Creates a corresponding Jena Resource for the SSWAPIndividual

Parameters:
sourceModel - the source model in which the Jena resource should be created
individual - the entity for which the resource should be created
Returns:
the created Jena resource

assertImplementation

public <I extends ModelImpl,A extends SSWAPModel> I assertImplementation(A apiObject,
                                                                         java.lang.Class<I> implementationClass)
Asserts that the parameter typed via an API interface has the expected implementation.

Type Parameters:
I - the expected implementation type
A - the API interface
Parameters:
apiObject - the object to be casted
implementationClass - the expected implementation class
Returns:
apiObject cast to type (I)

assertSourceModel

public SourceModel assertSourceModel(ModelImpl modelImpl)
                              throws java.lang.IllegalArgumentException
Gets the source model of a dereferenced ModelImpl

Parameters:
modelImpl - the implementation of a SSWAPModel that should be dereferenced
Returns:
a source model of the passed model (never null)
Throws:
java.lang.IllegalArgumentException - if the model has not been dereferenced, and therefore does not have a source model

deepCopyList

private SSWAPList deepCopyList(SourceModel newModel,
                               SSWAPList originalList,
                               java.util.Set<java.net.URI> copiedIndividuals)
Perform a deep copy of a SSWAPList into another source model

Parameters:
newModel - the destination model into which the list should be copied
originalList - the original list (in the source model) that should be copied
Returns:
the new copy of the SSWAPList

deepCopyIndividual

private SSWAPIndividual deepCopyIndividual(SourceModel newModel,
                                           SSWAPIndividual originalIndividual,
                                           java.util.Set<java.net.URI> copiedIndividuals)
Perform a deep copy of a SSWAPIndividual into another source model

Parameters:
newModel - the destination model into which the individual should be copied
originalIndividual - the original individual (in the source model) that should be copied
Returns:
a deep copy of SSWAPIndividual into the newModel

deepCopyIndividual

public void deepCopyIndividual(SourceModel newModel,
                               SSWAPIndividual originalIndividual,
                               IndividualImpl dstIndividual,
                               java.util.Set<java.net.URI> copiedIndividuals)

deepCopyElement

public SSWAPElement deepCopyElement(SourceModel newModel,
                                    SSWAPElement originalElement,
                                    java.util.Set<java.net.URI> copiedIndividuals)
Performs a deep copy of a SSWAPElement from one source model to another

Parameters:
newModel - the new source model to which the element should be copied
originalElement - the original element (in the source model) to be copied
Returns:
a new SSWAPElement in the newModel that is a copy of originalElement

copy

public void copy(SSWAPIndividual srcInd,
                 SSWAPIndividual dstInd,
                 SSWAPDocument dstDoc,
                 java.util.Collection<java.lang.String> excludedTypes)


Copyright (c) 2011, iPlant Collaborative, University of Arizona, Cold Spring Harbor Laboratories, University of Texas at Austin.