use of org.apache.cayenne.DataObject in project cayenne by apache.
the class DataContext method registerNewObject.
/**
* Registers a transient object with the context, recursively registering
* all transient persistent objects attached to this object via
* relationships.
* <p>
* <i>Note that since 3.0 this method takes Object as an argument instead of
* a {@link DataObject}.</i>
*
* @param object
* new object that needs to be made persistent.
*/
@Override
public void registerNewObject(Object object) {
if (object == null) {
throw new NullPointerException("Can't register null object.");
}
ObjEntity entity = getEntityResolver().getObjEntity((Persistent) object);
if (entity == null) {
throw new IllegalArgumentException("Can't find ObjEntity for Persistent class: " + object.getClass().getName() + ", class is likely not mapped.");
}
final Persistent persistent = (Persistent) object;
// sanity check - maybe already registered
if (persistent.getObjectId() != null) {
if (persistent.getObjectContext() == this) {
// already registered, just ignore
return;
} else if (persistent.getObjectContext() != null) {
throw new IllegalStateException("Persistent is already registered with another DataContext. " + "Try using 'localObjects()' instead.");
}
} else {
persistent.setObjectId(new ObjectId(entity.getName()));
}
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
if (descriptor == null) {
throw new IllegalArgumentException("Invalid entity name: " + entity.getName());
}
injectInitialValue(object);
// now we need to find all arc changes, inject missing value holders and
// pull in
// all transient connected objects
descriptor.visitProperties(new PropertyVisitor() {
public boolean visitToMany(ToManyProperty property) {
property.injectValueHolder(persistent);
if (!property.isFault(persistent)) {
Object value = property.readProperty(persistent);
Collection<Map.Entry> collection = (value instanceof Map) ? ((Map) value).entrySet() : (Collection) value;
Iterator<Map.Entry> it = collection.iterator();
while (it.hasNext()) {
Object target = it.next();
if (target instanceof Persistent) {
Persistent targetDO = (Persistent) target;
// make sure it is registered
registerNewObject(targetDO);
getObjectStore().arcCreated(persistent.getObjectId(), targetDO.getObjectId(), property.getName());
}
}
}
return true;
}
public boolean visitToOne(ToOneProperty property) {
Object target = property.readPropertyDirectly(persistent);
if (target instanceof Persistent) {
Persistent targetDO = (Persistent) target;
// make sure it is registered
registerNewObject(targetDO);
getObjectStore().arcCreated(persistent.getObjectId(), targetDO.getObjectId(), property.getName());
}
return true;
}
public boolean visitAttribute(AttributeProperty property) {
return true;
}
});
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectStore method processInvalidatedIDs.
/**
* @since 1.1
*/
void processInvalidatedIDs(Collection<ObjectId> invalidatedIDs) {
if (invalidatedIDs != null && !invalidatedIDs.isEmpty()) {
for (ObjectId oid : invalidatedIDs) {
DataObject object = (DataObject) getNode(oid);
if (object == null) {
continue;
}
switch(object.getPersistenceState()) {
case PersistenceState.COMMITTED:
object.setPersistenceState(PersistenceState.HOLLOW);
break;
case PersistenceState.MODIFIED:
DataContext context = (DataContext) object.getObjectContext();
DataRow diff = getSnapshot(oid);
// consult delegate if it exists
DataContextDelegate delegate = context.nonNullDelegate();
if (delegate.shouldMergeChanges(object, diff)) {
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(oid.getEntityName());
DataRowUtils.forceMergeWithSnapshot(context, descriptor, object, diff);
delegate.finishedMergeChanges(object);
}
case PersistenceState.HOLLOW:
// do nothing
break;
case PersistenceState.DELETED:
// TODO: Do nothing? Or treat as merged?
break;
}
}
}
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class ObjectStore method processDeletedID.
/**
* Requires external synchronization.
*
* @since 1.2
*/
void processDeletedID(ObjectId nodeId) {
// access object map directly - the method should be called in a synchronized context...
Persistent object = objectMap.get(nodeId);
if (object != null) {
DataObject dataObject = (object instanceof DataObject) ? (DataObject) object : null;
DataContextDelegate delegate;
switch(object.getPersistenceState()) {
case PersistenceState.COMMITTED:
case PersistenceState.HOLLOW:
case PersistenceState.DELETED:
// consult delegate
delegate = context.nonNullDelegate();
if (dataObject == null || delegate.shouldProcessDelete(dataObject)) {
objectMap.remove(nodeId);
changes.remove(nodeId);
// setting DataContext to null will also set
// state to transient
object.setObjectContext(null);
if (dataObject != null) {
delegate.finishedProcessDelete(dataObject);
}
}
break;
case PersistenceState.MODIFIED:
// consult delegate
delegate = context.nonNullDelegate();
if (dataObject != null && delegate.shouldProcessDelete(dataObject)) {
object.setPersistenceState(PersistenceState.NEW);
changes.remove(nodeId);
registerNode(nodeId, object);
nodeCreated(nodeId);
delegate.finishedProcessDelete(dataObject);
}
break;
}
}
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class CayenneSQLTemplateProcessorTest method testProcessTemplateNotEqualID.
@Test
public void testProcessTemplateNotEqualID() throws Exception {
String sqlTemplate = "SELECT * FROM ME WHERE " + "COLUMN1 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN1')) " + "AND COLUMN2 #bindNotEqual($helper.cayenneExp($a, 'db:ID_COLUMN2'))";
Map<String, Object> idMap = new HashMap<>();
idMap.put("ID_COLUMN1", 3);
idMap.put("ID_COLUMN2", "aaa");
ObjectId id = new ObjectId("T", idMap);
DataObject dataObject = new CayenneDataObject();
dataObject.setObjectId(id);
Map<String, Object> map = Collections.singletonMap("a", dataObject);
SQLStatement compiled = processor.processTemplate(sqlTemplate, map);
assertEquals("SELECT * FROM ME WHERE COLUMN1 <> ? AND COLUMN2 <> ?", compiled.getSql());
assertEquals(2, compiled.getBindings().length);
assertBindingValue(3, compiled.getBindings()[0]);
assertBindingValue("aaa", compiled.getBindings()[1]);
}
use of org.apache.cayenne.DataObject in project cayenne by apache.
the class SimpleIdIncrementalFaultListIT method testIterator.
@Test
public void testIterator() throws Exception {
SimpleIdIncrementalFaultList<?> list = prepareList(6);
Iterator<?> it = list.iterator();
int counter = 0;
while (it.hasNext()) {
Object obj = it.next();
assertNotNull(obj);
assertTrue(obj instanceof DataObject);
// iterator must be resolved page by page
int expectedResolved = list.pageIndex(counter) * list.getPageSize() + list.getPageSize();
if (expectedResolved > list.size()) {
expectedResolved = list.size();
}
assertEquals(list.size() - expectedResolved, list.getUnfetchedObjects());
counter++;
}
}
Aggregations