com.viceversatech.rdfbeans
Class RDFBeanManager

java.lang.Object
  extended by com.viceversatech.rdfbeans.RDFBeanManager

public class RDFBeanManager
extends java.lang.Object

RDFBeans databinding functions are accessible as methods of a single RDFBeanManager class. An RDFBeanManager instance is created with a RDF2Go Model which provides an abstraction layer to access an underlying physical RDF storage. Currently, RDF2Go project provides implementations of Model interface (adapters) for Sesame 2.x and Jena frameworks. A Model instance is passed as an argument to the RDFBeanManager constructor. The Model implementations may require the model to be opened (initialized) before and closed after use. The following example illustrates how to setup RDFBeans databinding with a model adapter determined automatically via RDF2Go ModelFactory mechanism:

 import com.viceversatech.rdfbeans.RDFBeanManager; 
 import org.ontoware.rdf2go.ModelFactory; 
 import org.ontoware.rdf2go.RDF2Go; 
 import org.ontoware.rdf2go.model.Model; 
 ...
 
 ModelFactory modelFactory = RDF2Go.getModelFactory(); 
 Model model = modelFactory.createModel(); 
 model.open(); 
 RDFBeanManager manager = new RDFBeanManager(model); 
 ... 
 model.close();
 
An example with hardcoded Sesame 2.x NativeStore model implementation:
 import com.viceversatech.rdfbeans.RDFBeanManager; 
 import org.ontoware.rdf2go.model.Model; 
 import org.openrdf.rdf2go.RepositoryModel;
 import org.openrdf.repository.Repository; 
 import org.openrdf.repository.sail.SailRepository; 
 import org.openrdf.sail.nativerdf.NativeStore; 
 ...
 
 Repository repository = new SailRepository(new NativeStore(new File("~/.sesame/test"))); 
 repository.initialize(); 
 Model model = new RepositoryModel(repository); 
 model.open(); 
 RDFBeanManager manager = new RDFBeanManager(model); 
 ... 
 model.close();
 
For detailed information on RDF2Go configuration for specific triple store adapters, please refer to RDF2Go documentation.

Version:
$Id:$
Author:
alex

Field Summary
static org.ontoware.rdf2go.model.node.URI BINDINGCLASS_PROPERTY
           
static org.ontoware.rdf2go.model.node.URI BINDINGIFACE_PROPERTY
           
 
Constructor Summary
RDFBeanManager(org.ontoware.rdf2go.model.Model model)
          Creates new RDFBeanManager instance upon the given RDF2Go model.
 
Method Summary
 org.ontoware.rdf2go.model.node.Resource add(java.lang.Object o)
          Marshall the state of an RDFBean object to an RDF resource (a set of triple statements) in the underlying RDF model.
 java.lang.Object create(org.ontoware.rdf2go.model.node.Resource r)
          Create new dynamic proxy instance backed by an existing RDF resource.
<T> T
create(org.ontoware.rdf2go.model.node.Resource r, java.lang.Class<T> iface)
          Create new dynamic proxy instance that implements the specified RDFBean interface and backed by the specified RDF resource in the underlying RDF model.
<T> T
create(java.lang.String id, java.lang.Class<T> iface)
          Create new dynamic proxy instance that implements the specified RDFBean interface and backed by an RDF resource matching to the given RDFBean ID.
 void delete(org.ontoware.rdf2go.model.node.Resource r)
          Delete the RDF resource from underlying model.
 void delete(java.lang.String stringId, java.lang.Class rdfBeanClass)
          Delete an RDF resource matching the specified RDFBean identifier from underlying model.
 java.lang.Object get(org.ontoware.rdf2go.model.node.Resource r)
          Unmarshall an RDF resource to an RDFBean instance.
<T> T
get(org.ontoware.rdf2go.model.node.Resource r, java.lang.Class<T> rdfBeanClass)
          Unmarshall an RDF resource to an instance of the specified RDFBean class.
<T> T
get(java.lang.String stringId, java.lang.Class<T> rdfBeanClass)
          Unmarshall an RDF resource matching specified RDFBean identifier to an instance of the specified RDFBean class.
<T> org.ontoware.aifbcommons.collection.ClosableIterator<T>
getAll(java.lang.Class<T> rdfBeanClass)
          Obtain an iterator over all instances of specified RDFBean class stored in the RDF model
protected  java.lang.Class<?> getBindingIface(org.ontoware.rdf2go.model.node.Resource r)
           
protected  java.lang.Class<?> getBindingIface(org.ontoware.rdf2go.model.node.URI rdfType)
           
 java.lang.ClassLoader getClassLoader()
          Return the current ClassLoader for loading RDFBean classes.
 DatatypeMapper getDatatypeMapper()
          Return a current DatatypeMapper implementation.
 org.ontoware.rdf2go.model.Model getModel()
          Return the underlying RDF model.
 org.ontoware.rdf2go.model.node.Resource getResource(java.lang.String stringId, java.lang.Class rdfBeanClass)
          Resolve the RDFBean identifier to an RDF resource URI.
 boolean isAutocommit()
          Check if autocommit mode is on
 boolean isResourceExist(org.ontoware.rdf2go.model.node.Resource r)
          Check if a RDF resource exists in the underlying model.
 void setAutocommit(boolean autocommit)
          Set autocommit mode.
 void setClassLoader(java.lang.ClassLoader classLoader)
          Set a custom ClassLoader instance for loading RDFBean classes.
 void setDatatypeMapper(DatatypeMapper datatypeMapper)
          Set a DatatypeMapper implementation.
 org.ontoware.rdf2go.model.node.Resource update(java.lang.Object o)
          Marshall the state of an RDFBean object to update an existing RDF resource in the underlying RDF model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BINDINGCLASS_PROPERTY

public static final org.ontoware.rdf2go.model.node.URI BINDINGCLASS_PROPERTY

BINDINGIFACE_PROPERTY

public static final org.ontoware.rdf2go.model.node.URI BINDINGIFACE_PROPERTY
Constructor Detail

RDFBeanManager

public RDFBeanManager(org.ontoware.rdf2go.model.Model model)
Creates new RDFBeanManager instance upon the given RDF2Go model.

Parameters:
model -
Method Detail

add

public org.ontoware.rdf2go.model.node.Resource add(java.lang.Object o)
                                            throws RDFBeanException
Marshall the state of an RDFBean object to an RDF resource (a set of triple statements) in the underlying RDF model.

If the RDFBean object has not-null property, annotated with RDFSubject, the method returns absolute URI of RDF resource. Otherwise, RDF BlankNode is returned.

If an RDF representation of the given unanonymous object already exists in the model, the method immediately returns the RDF resource without changing the model.

If autocommit mode is on (see setAutocommit(boolean)), the statements are commited into the RDF model in a single transaction. Otherwise, the transaction is delayed until the commit() method of the underlying Model implementation is invoked.

Parameters:
o - RDFBean to add
Returns:
Resource URI or BlankNode for anonymous RDFBean
Throws:
RDFBeanException - If the object is not a valid RDFBean
See Also:
update(Object), setAutocommit(boolean)

update

public org.ontoware.rdf2go.model.node.Resource update(java.lang.Object o)
                                               throws RDFBeanException
Marshall the state of an RDFBean object to update an existing RDF resource in the underlying RDF model.

If no resource for the given object exists, or the object is anonymous RDFBean represented with a BlankNode, the method works like add().

If autocommit mode is on (see setAutocommit(boolean)), the statements are commited into the RDF model in a single transaction. Otherwise, the transaction is delayed until the commit() method of the underlying Model implementation is invoked.

Parameters:
o - RDFBean to update
Returns:
Resource URI or BlankNode for anonymous RDFBean
Throws:
RDFBeanException - If the object is not a valid RDFBean
See Also:
add(Object), setAutocommit(boolean)

get

public <T> T get(org.ontoware.rdf2go.model.node.Resource r,
                 java.lang.Class<T> rdfBeanClass)
      throws RDFBeanException
Unmarshall an RDF resource to an instance of the specified RDFBean class.

Parameters:
r - Resource URI (or BlankNode for anonymous RDFBeans).
rdfBeanClass - Java class of RDFBean
Returns:
Unmarshalled RDFBean object, or null if the resource does not exists
Throws:
RDFBeanException - If the class is not a valid RDFBean or an instance of this class cannot be created
See Also:
get(Resource), get(String,Class), getAll(Class)

get

public java.lang.Object get(org.ontoware.rdf2go.model.node.Resource r)
                     throws RDFBeanException
Unmarshall an RDF resource to an RDFBean instance.

The method tries to autodetect an RDFBean Java class using information added to the model at marshalling. If a binding class information is not found, RDFBeanException is thrown.

Parameters:
r - Resource URI or BlankNode for anonymous RDFBeans.
Returns:
Unmarshalled RDFBean object, or null if the resource does not exists in the model
Throws:
RDFBeanException - If the binding class cannot be detected, is not a valid RDFBean or an instance of this class cannot be created
See Also:
get(Resource,Class), get(String,Class), getAll(Class)

get

public <T> T get(java.lang.String stringId,
                 java.lang.Class<T> rdfBeanClass)
      throws RDFBeanException
Unmarshall an RDF resource matching specified RDFBean identifier to an instance of the specified RDFBean class.

If a namespace prefix is defined in RDFSubject declaration for this RDFBean class, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource URI). Otherwise, the fully qualified name must be provided.

Parameters:
stringId - RDFBean ID value
rdfBeanClass - Java class of RDFBean
Returns:
The unmarshalled Java object, or null no resource matching the given ID is found exists
Throws:
RDFBeanException - If the class is not a valid RDFBean or an instance of this class cannot be created
See Also:
get(Resource), get(Resource,Class), getAll(Class)

getAll

public <T> org.ontoware.aifbcommons.collection.ClosableIterator<T> getAll(java.lang.Class<T> rdfBeanClass)
                                                               throws RDFBeanException
Obtain an iterator over all instances of specified RDFBean class stored in the RDF model

The returned Iterator performs lazy unmarshalling of RDFBean objects (on every next() call) without any specific order. When iterating is done, the caller must invoke the close() method of ClosableIterator to release the resources of the underlying RDF model implementation.

Parameters:
rdfBeanClass - Java class of RDFBeans
Returns:
Iterator over RDFBean instances
Throws:
RDFBeanException - If the class is not a valid RDFBean

isResourceExist

public boolean isResourceExist(org.ontoware.rdf2go.model.node.Resource r)
Check if a RDF resource exists in the underlying model.

Parameters:
r - Resource URI or BlankNode
Returns:
true, if the model contains the statements with the given subject.

getResource

public org.ontoware.rdf2go.model.node.Resource getResource(java.lang.String stringId,
                                                           java.lang.Class rdfBeanClass)
                                                    throws RDFBeanException
Resolve the RDFBean identifier to an RDF resource URI.

If a namespace prefix is defined in RDFSubject declaration for this RDFBean class, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource URI). Otherwise, the fully qualified name must be provided.

Parameters:
stringId - RDFBean ID value
rdfBeanClass - Java class of RDFBean
Returns:
Resource URI, or null if no resource matching the given RDFBean ID found.
Throws:
RDFBeanException - If the class is not a valid RDFBean

delete

public void delete(org.ontoware.rdf2go.model.node.Resource r)
Delete the RDF resource from underlying model.

If autocommit mode is on (see setAutocommit(boolean)), the statements are removed from the RDF model as a single transaction. Otherwise, the transaction is delayed until the commit() method of the underlying Model implementation is invoked.

Parameters:
r - Resource URI
See Also:
delete(String,Class), setAutocommit(boolean)

delete

public void delete(java.lang.String stringId,
                   java.lang.Class rdfBeanClass)
            throws RDFBeanException
Delete an RDF resource matching the specified RDFBean identifier from underlying model.

If autocommit mode is on (see setAutocommit(boolean)), the statements are removed from the RDF model as a single transaction. Otherwise, the transaction is delayed until the commit() method of the underlying Model implementation is invoked.

Parameters:
stringId - RDFBean ID value
rdfBeanClass - Java class of RDFBean
Throws:
RDFBeanException - If the class is not a valid RDFBean
See Also:
delete(Resource), setAutocommit(boolean)

create

public <T> T create(org.ontoware.rdf2go.model.node.Resource r,
                    java.lang.Class<T> iface)
         throws RDFBeanException
Create new dynamic proxy instance that implements the specified RDFBean interface and backed by the specified RDF resource in the underlying RDF model.

Parameters:
r - Resource URI
iface - RDFBean interface
Returns:
New RDFBean dynamic proxy object with the specified interface
Throws:
RDFBeanException - If iface is not valid RDFBean interface
See Also:
create(String,Class), create(Resource)

create

public <T> T create(java.lang.String id,
                    java.lang.Class<T> iface)
         throws RDFBeanException
Create new dynamic proxy instance that implements the specified RDFBean interface and backed by an RDF resource matching to the given RDFBean ID.

If a namespace prefix is defined in RDFSubject declaration for this RDFBean interface, the provided identifier value is interpreted as a local part of fully qualified RDFBean name (RDF resource URI). Otherwise, the fully qualified name must be provided.

Parameters:
id - RDFBean ID value
iface - RDFBean interface
Returns:
New RDFBean dynamic proxy object with the specified interface
Throws:
RDFBeanException - if iface is not valid RDFBean interface or there is an error resolving RDFBean identifier
See Also:
create(Resource,Class), create(Resource)

create

public java.lang.Object create(org.ontoware.rdf2go.model.node.Resource r)
                        throws RDFBeanException
Create new dynamic proxy instance backed by an existing RDF resource.

The method tries to autodetect an RDFBean Java interface using information retrieved from the model. If a binding interface information is not found, RDFBeanException is thrown.

Parameters:
r - Resource URI
Returns:
New RDFBean dynamic proxy object or null if the resource does not exist or is not bound to any RDFBean interface
Throws:
RDFBeanException - If interface bound to the RDF type is not found
See Also:
create(Resource,Class), create(String,Class)

getBindingIface

protected java.lang.Class<?> getBindingIface(org.ontoware.rdf2go.model.node.Resource r)
                                      throws RDFBeanException
Throws:
RDFBeanException

getBindingIface

protected java.lang.Class<?> getBindingIface(org.ontoware.rdf2go.model.node.URI rdfType)
                                      throws RDFBeanException
Throws:
RDFBeanException

getModel

public org.ontoware.rdf2go.model.Model getModel()
Return the underlying RDF model.

Returns:
the model

isAutocommit

public boolean isAutocommit()
Check if autocommit mode is on

If autocommit mode is on, the transactions with the RDF model will be immediately commited on invocation of add(Object), update(Object) and delete(Resource) methods, as well as of the setter methods of the dynamic proxy objects. Otherwise, the transactions must be commited by explicit invocation of the commit() method of the Model implementation.

By default, the autocommit mode is on.

Returns:
True if autocommit mode is on.
See Also:
setAutocommit(boolean)

setAutocommit

public void setAutocommit(boolean autocommit)
Set autocommit mode.

If autocommit mode is on, the transactions with the RDF model will be immediately commited on invocation of add(Object), update(Object) and delete(Resource) methods, as well as of the setter methods of the dynamic proxy objects. Otherwise, the transactions must be commited by explicit invocation of the commit() method of the Model implementation.

By default, the autocommit mode is on.

Parameters:
autocommit - false to set the autocommit mode off or true to on
See Also:
isAutocommit()

getClassLoader

public java.lang.ClassLoader getClassLoader()
Return the current ClassLoader for loading RDFBean classes.

By default, the classes are loaded by the ClassLoader of this RDFBeanManager.

Returns:
the current ClassLoader instance
See Also:
setClassLoader(ClassLoader)

setClassLoader

public void setClassLoader(java.lang.ClassLoader classLoader)
Set a custom ClassLoader instance for loading RDFBean classes.

By default, the classes are loaded by the ClassLoader of this RDFBeanManager.

Parameters:
classLoader - the ClassLoader instance to set
See Also:
getClassLoader()

getDatatypeMapper

public DatatypeMapper getDatatypeMapper()
Return a current DatatypeMapper implementation.

Returns:
the datatypeMapper
See Also:
setDatatypeMapper(DatatypeMapper)

setDatatypeMapper

public void setDatatypeMapper(DatatypeMapper datatypeMapper)
Set a DatatypeMapper implementation.

Parameters:
datatypeMapper - the datatypeMapper to set
See Also:
getDatatypeMapper()


Copyright © 2011. All Rights Reserved.