org.apache.cayenne
Class CayenneContext

java.lang.Object
  extended by org.apache.cayenne.BaseContext
      extended by org.apache.cayenne.CayenneContext
All Implemented Interfaces:
Serializable, DataChannel, ObjectContext

public class CayenneContext
extends BaseContext

A default generic implementation of ObjectContext suitable for accessing Cayenne from either an ORM or a client tiers. Communicates with Cayenne via a DataChannel.

Since:
1.2
See Also:
Serialized Form

Field Summary
protected  EntityResolver entityResolver
           
 
Fields inherited from class org.apache.cayenne.BaseContext
channel, queryCache, threadObjectContext, userProperties
 
Fields inherited from interface org.apache.cayenne.DataChannel
FLUSH_CASCADE_SYNC, FLUSH_NOCASCADE_SYNC, GRAPH_CHANGED_SUBJECT, GRAPH_FLUSHED_SUBJECT, GRAPH_ROLLEDBACK_SUBJECT, ROLLBACK_CASCADE_SYNC
 
Constructor Summary
CayenneContext()
          Creates a new CayenneContext with no channel and disabled graph events.
CayenneContext(DataChannel channel)
          Creates a new CayenneContext, initializing it with a channel instance.
CayenneContext(DataChannel channel, boolean changeEventsEnabled, boolean syncEventsEnabled)
          Creates a new CayenneContext, initializing it with a channel.
 
Method Summary
 void commitChanges()
          Commits changes to uncommitted objects.
 void commitChangesToParent()
          Flushes all changes to objects in this context to the parent DataChannel.
 ObjectContext createChildContext()
          Creates and returns a new child ObjectContext.
 Collection<?> deletedObjects()
          Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.DELETED
 void deleteObject(Object object)
          Deletes an object locally, scheduling it for future deletion from the external data store.
 void deleteObjects(Collection<?> objects)
          Deletes a collection of objects by repeatedly calling deleteObject safely (avoiding a concurrent modification exception).
 EntityResolver getEntityResolver()
          Returns an EntityResolver that provides mapping information needed for CayenneContext operation.
 GraphManager getGraphManager()
          Returns GraphManager that manages object graph associated with this context.
 boolean hasChanges()
          Returns true if there are any modified, deleted or new objects registered with this CayenneContext, false otherwise.
 boolean isChangeEventsEnabled()
          Returns true if this context posts individual object modification events.
 boolean isLifecycleEventsEnabled()
          Returns true if this context posts lifecycle events.
 Persistent localObject(ObjectId id, Object prototype)
          Converts a list of Persistent objects registered in some other ObjectContext to a list of objects local to this ObjectContext.
 Collection<?> modifiedObjects()
          Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.MODIFIED
<T> T
newObject(Class<T> persistentClass)
          Creates and registers a new Persistent object instance.
 Collection<?> newObjects()
          Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.NEW
protected  GraphDiff onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade)
           
 QueryResponse onQuery(ObjectContext context, Query query)
          Executes a query, using provided context to register persistent objects if query returns any objects.
 QueryResponse performGenericQuery(Query query)
          Executes any kind of query providing the result in a form of QueryResponse.
 List performQuery(Query query)
          Runs a query, returning result as list.
 void propertyChanged(Persistent object, String property, Object oldValue, Object newValue)
          A callback method that child Persistent objects are expected to call from inside the setter after modifying a value of a persistent property, including "simple" and "arc" properties.
 void registerNewObject(Object object)
          Registers a transient object with the context.
 void rollbackChanges()
          Resets all uncommitted changes made to the objects in this ObjectContext, cascading rollback operation all the way through the stack.
 void rollbackChangesLocally()
          Resets all uncommitted changes made to the objects in this ObjectContext.
 void setChannel(DataChannel channel)
          Sets the context channel, setting up a listener for channel events.
 void setEntityResolver(EntityResolver entityResolver)
           
 Collection<?> uncommittedObjects()
          Returns a collection of MODIFIED, DELETED or NEW objects.
 
Methods inherited from class org.apache.cayenne.BaseContext
bindThreadObjectContext, fireDataChannelChanged, fireDataChannelCommitted, fireDataChannelRolledback, getChannel, getEventManager, getQueryCache, getThreadObjectContext, getUserProperties, getUserProperty, injectInitialValue, invalidateObjects, onSync, prepareForAccess, prepareForAccess, setQueryCache, setUserProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

entityResolver

protected EntityResolver entityResolver
Constructor Detail

CayenneContext

public CayenneContext()
Creates a new CayenneContext with no channel and disabled graph events.


CayenneContext

public CayenneContext(DataChannel channel)
Creates a new CayenneContext, initializing it with a channel instance. CayenneContext created using this constructor WILL NOT broadcast graph change events.


CayenneContext

public CayenneContext(DataChannel channel,
                      boolean changeEventsEnabled,
                      boolean syncEventsEnabled)
Creates a new CayenneContext, initializing it with a channel. If graphEventsEnabled is true, this context will broadcast GraphEvents using ObjectContext.GRAPH_CHANGE_SUBJECT.

Method Detail

setChannel

public void setChannel(DataChannel channel)
Sets the context channel, setting up a listener for channel events.


isChangeEventsEnabled

public boolean isChangeEventsEnabled()
Returns true if this context posts individual object modification events. Subject used for these events is ObjectContext.GRAPH_CHANGED_SUBJECT.


isLifecycleEventsEnabled

public boolean isLifecycleEventsEnabled()
Returns true if this context posts lifecycle events. Subjects used for these events are ObjectContext.GRAPH_COMMIT_STARTED_SUBJECT, ObjectContext.GRAPH_COMMITTED_SUBJECT, ObjectContext.GRAPH_COMMIT_ABORTED_SUBJECT, ObjectContext.GRAPH_ROLLEDBACK_SUBJECT. .


getEntityResolver

public EntityResolver getEntityResolver()
Returns an EntityResolver that provides mapping information needed for CayenneContext operation. If EntityResolver is not set, this method would obtain and cache one from the underlying DataChannel.

Specified by:
getEntityResolver in interface DataChannel
Specified by:
getEntityResolver in interface ObjectContext
Specified by:
getEntityResolver in class BaseContext

setEntityResolver

public void setEntityResolver(EntityResolver entityResolver)

getGraphManager

public GraphManager getGraphManager()
Description copied from interface: ObjectContext
Returns GraphManager that manages object graph associated with this context.

Specified by:
getGraphManager in interface ObjectContext
Specified by:
getGraphManager in class BaseContext

commitChanges

public void commitChanges()
Commits changes to uncommitted objects. First checks if there are changes in this context and if any changes are detected, sends a commit message to remote Cayenne service via an internal instance of CayenneConnector.

Specified by:
commitChanges in interface ObjectContext
Specified by:
commitChanges in class BaseContext

commitChangesToParent

public void commitChangesToParent()
Description copied from interface: ObjectContext
Flushes all changes to objects in this context to the parent DataChannel. Same as ObjectContext.commitChanges(), but no cascading flush occurs.

Specified by:
commitChangesToParent in interface ObjectContext
Specified by:
commitChangesToParent in class BaseContext

rollbackChanges

public void rollbackChanges()
Description copied from interface: ObjectContext
Resets all uncommitted changes made to the objects in this ObjectContext, cascading rollback operation all the way through the stack.

Specified by:
rollbackChanges in interface ObjectContext
Specified by:
rollbackChanges in class BaseContext

rollbackChangesLocally

public void rollbackChangesLocally()
Description copied from interface: ObjectContext
Resets all uncommitted changes made to the objects in this ObjectContext. Same as ObjectContext.rollbackChanges()(), but rollback is local to this context and no cascading changes undoing occurs.

Specified by:
rollbackChangesLocally in interface ObjectContext
Specified by:
rollbackChangesLocally in class BaseContext

deleteObject

public void deleteObject(Object object)
Deletes an object locally, scheduling it for future deletion from the external data store.

Specified by:
deleteObject in interface ObjectContext
Specified by:
deleteObject in class BaseContext

deleteObjects

public void deleteObjects(Collection<?> objects)
                   throws DeleteDenyException
Description copied from interface: ObjectContext
Deletes a collection of objects by repeatedly calling deleteObject safely (avoiding a concurrent modification exception).

Throws:
DeleteDenyException

newObject

public <T> T newObject(Class<T> persistentClass)
Creates and registers a new Persistent object instance.

Specified by:
newObject in interface ObjectContext
Specified by:
newObject in class BaseContext

registerNewObject

public void registerNewObject(Object object)
Description copied from interface: ObjectContext
Registers a transient object with the context. The difference with ObjectContext.newObject(Class) is that a user creates an object herself, before attaching it to the context, instead of relying on Cayenne to do that.

Specified by:
registerNewObject in interface ObjectContext
Specified by:
registerNewObject in class BaseContext
Parameters:
object - new object that needs to be made persistent.
Since:
3.0

performQuery

public List performQuery(Query query)
Runs a query, returning result as list.

Specified by:
performQuery in interface ObjectContext
Specified by:
performQuery in class BaseContext

performGenericQuery

public QueryResponse performGenericQuery(Query query)
Description copied from interface: ObjectContext
Executes any kind of query providing the result in a form of QueryResponse.

Specified by:
performGenericQuery in interface ObjectContext
Specified by:
performGenericQuery in class BaseContext

onQuery

public QueryResponse onQuery(ObjectContext context,
                             Query query)
Description copied from interface: DataChannel
Executes a query, using provided context to register persistent objects if query returns any objects.

Parameters:
context - an ObjectContext that originated the query, used to register result objects.
Returns:
a generic response object that encapsulates result of the execution.

localObject

public Persistent localObject(ObjectId id,
                              Object prototype)
Converts a list of Persistent objects registered in some other ObjectContext to a list of objects local to this ObjectContext.

Current limitation: all objects in the source list must be either in COMMITTED or in HOLLOW state.

Specified by:
localObject in interface ObjectContext
Specified by:
localObject in class BaseContext

propertyChanged

public void propertyChanged(Persistent object,
                            String property,
                            Object oldValue,
                            Object newValue)
Description copied from interface: ObjectContext
A callback method that child Persistent objects are expected to call from inside the setter after modifying a value of a persistent property, including "simple" and "arc" properties.

Specified by:
propertyChanged in interface ObjectContext
Specified by:
propertyChanged in class BaseContext

uncommittedObjects

public Collection<?> uncommittedObjects()
Description copied from interface: ObjectContext
Returns a collection of MODIFIED, DELETED or NEW objects.

Specified by:
uncommittedObjects in interface ObjectContext
Specified by:
uncommittedObjects in class BaseContext

deletedObjects

public Collection<?> deletedObjects()
Description copied from interface: ObjectContext
Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.DELETED

Specified by:
deletedObjects in interface ObjectContext
Specified by:
deletedObjects in class BaseContext

modifiedObjects

public Collection<?> modifiedObjects()
Description copied from interface: ObjectContext
Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.MODIFIED

Specified by:
modifiedObjects in interface ObjectContext
Specified by:
modifiedObjects in class BaseContext

newObjects

public Collection<?> newObjects()
Description copied from interface: ObjectContext
Returns a collection of objects that are registered with this ObjectContext and have a state PersistenceState.NEW

Specified by:
newObjects in interface ObjectContext
Specified by:
newObjects in class BaseContext

createChildContext

public ObjectContext createChildContext()
Creates and returns a new child ObjectContext.

Since:
3.0

onContextFlush

protected GraphDiff onContextFlush(ObjectContext originatingContext,
                                   GraphDiff changes,
                                   boolean cascade)
Specified by:
onContextFlush in class BaseContext

hasChanges

public boolean hasChanges()
Returns true if there are any modified, deleted or new objects registered with this CayenneContext, false otherwise.



Copyright © 2001-2011 Apache Cayenne. All Rights Reserved.