use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CayenneContextClientChannelEventsIT method testSyncSimpleProperty.
@Test
public void testSyncSimpleProperty() throws Exception {
tMtTable1.insert(1, "g1", "s1");
CayenneContext c1 = (CayenneContext) runtime.newContext();
CayenneContext c2 = (CayenneContext) runtime.newContext();
assertNotSame(c1, c2);
ClientMtTable1 o1 = (ClientMtTable1) Cayenne.objectForQuery(c1, new ObjectIdQuery(new ObjectId("MtTable1", "TABLE1_ID", 1)));
ClientMtTable1 o2 = (ClientMtTable1) Cayenne.objectForQuery(c2, new ObjectIdQuery(new ObjectId("MtTable1", "TABLE1_ID", 1)));
assertEquals("g1", o1.getGlobalAttribute1());
assertEquals("g1", o2.getGlobalAttribute1());
o1.setGlobalAttribute1("X");
c1.commitChanges();
// let the events propagate to peers
Thread.sleep(500);
assertEquals("X", o2.getGlobalAttribute1());
assertFalse(c1.internalGraphManager().hasChanges());
assertFalse(c2.internalGraphManager().hasChanges());
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CayenneContextClientChannelEventsIT method testSyncNewObjectIntoDirtyContext.
@Test
public void testSyncNewObjectIntoDirtyContext() throws Exception {
CayenneContext c1 = (CayenneContext) runtime.newContext();
CayenneContext c2 = (CayenneContext) runtime.newContext();
assertNotSame(c1, c2);
// make sure c2 has uncommitted changes
c2.newObject(ClientMtTable1.class);
ClientMtTable1 o1 = c1.newObject(ClientMtTable1.class);
o1.setGlobalAttribute1("X");
c1.commitChanges();
ClientMtTable1 o2 = (ClientMtTable1) c2.getGraphManager().getNode(o1.getObjectId());
assertNull(o2);
// now fetch it fresh
o2 = (ClientMtTable1) c2.performQuery(new ObjectIdQuery(o1.getObjectId())).get(0);
assertNotNull(o2);
assertEquals("X", o2.getGlobalAttribute1());
assertEquals(PersistenceState.COMMITTED, o2.getPersistenceState());
assertFalse(c1.internalGraphManager().hasChanges());
assertTrue(c2.internalGraphManager().hasChanges());
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class CayenneContextMapRelationshipIT method testReadToMany.
@Test
public void testReadToMany() throws Exception {
createTwoMapToManysWithTargetsDataSet();
ObjectId id = new ObjectId("IdMapToMany", IdMapToMany.ID_PK_COLUMN, 1);
ClientIdMapToMany o1 = (ClientIdMapToMany) Cayenne.objectForQuery(context, new ObjectIdQuery(id));
Map<Object, ClientIdMapToManyTarget> targets = o1.getTargets();
assertTrue(((ValueHolder) targets).isFault());
assertNotNull(targets);
assertEquals(3, targets.size());
assertNotNull(targets.get(1));
assertNotNull(targets.get(2));
assertNotNull(targets.get(3));
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class BaseContext method prepareForAccess.
@Override
public void prepareForAccess(Persistent object, String property, boolean lazyFaulting) {
if (object.getPersistenceState() == PersistenceState.HOLLOW) {
ObjectId oid = object.getObjectId();
List<?> objects = performQuery(new ObjectIdQuery(oid, false, ObjectIdQuery.CACHE));
if (objects.size() == 0) {
throw new FaultFailureException("Error resolving fault, no matching row exists in the database for ObjectId: " + oid);
} else if (objects.size() > 1) {
throw new FaultFailureException("Error resolving fault, more than one row exists in the database for ObjectId: " + oid);
}
// 5/28/2013 - Commented out this block to allow for modifying
// objects in the postLoad callback
// sanity check...
// if (object.getPersistenceState() != PersistenceState.COMMITTED) {
//
// String state =
// PersistenceState.persistenceStateName(object.getPersistenceState());
//
// // TODO: andrus 4/13/2006, modified and deleted states are
// // possible due to
// // a race condition, should we handle them here?
// throw new
// FaultFailureException("Error resolving fault for ObjectId: " +
// oid + " and state (" + state
// +
// "). Possible cause - matching row is missing from the database.");
// }
}
// resolve relationship fault
if (lazyFaulting && property != null) {
ClassDescriptor classDescriptor = getEntityResolver().getClassDescriptor(object.getObjectId().getEntityName());
PropertyDescriptor propertyDescriptor = classDescriptor.getProperty(property);
// be used.
if (propertyDescriptor == null) {
final StringBuilder errorMessage = new StringBuilder();
errorMessage.append(String.format("Property '%s' is not declared for entity '%s'.", property, object.getObjectId().getEntityName()));
errorMessage.append(" Declared properties are: ");
// Grab each of the declared properties.
final List<String> properties = new ArrayList<>();
classDescriptor.visitProperties(new PropertyVisitor() {
@Override
public boolean visitAttribute(final AttributeProperty property) {
properties.add(property.getName());
return true;
}
@Override
public boolean visitToOne(final ToOneProperty property) {
properties.add(property.getName());
return true;
}
@Override
public boolean visitToMany(final ToManyProperty property) {
properties.add(property.getName());
return true;
}
});
// Now add the declared property names to the error message.
boolean first = true;
for (String declaredProperty : properties) {
if (first) {
errorMessage.append(String.format("'%s'", declaredProperty));
first = false;
} else {
errorMessage.append(String.format(", '%s'", declaredProperty));
}
}
errorMessage.append(".");
throw new CayenneRuntimeException(errorMessage.toString());
}
// this should trigger fault resolving
propertyDescriptor.readProperty(object);
}
}
use of org.apache.cayenne.query.ObjectIdQuery in project cayenne by apache.
the class DeletedDiffProcessor method nodeRemoved.
@Override
public void nodeRemoved(Object nodeId) {
ObjectId id = (ObjectId) nodeId;
final MutableObjectChange objectChangeSet = changeSet.getOrCreate(id, ObjectChangeType.DELETE);
// TODO: rewrite with SelectById query after Cayenne upgrade
ObjectIdQuery query = new ObjectIdQuery(id, true, ObjectIdQuery.CACHE);
QueryResponse result = channel.onQuery(null, query);
@SuppressWarnings("unchecked") List<DataRow> rows = result.firstList();
if (rows.isEmpty()) {
LOGGER.warn("No DB snapshot for object to be deleted, no changes will be recorded. ID: " + id);
return;
}
final DataRow row = rows.get(0);
ClassDescriptor descriptor = channel.getEntityResolver().getClassDescriptor(id.getEntityName());
final CommitLogEntity entity = entityFactory.getEntity(id);
descriptor.visitProperties(new PropertyVisitor() {
@Override
public boolean visitAttribute(AttributeProperty property) {
if (!entity.isIncluded(property.getName())) {
return true;
}
Object value;
if (entity.isConfidential(property.getName())) {
value = Confidential.getInstance();
} else {
String key = property.getAttribute().getDbAttributeName();
value = row.get(key);
}
if (value != null) {
objectChangeSet.attributeChanged(property.getName(), value, null);
}
return true;
}
@Override
public boolean visitToOne(ToOneProperty property) {
// TODO record FK changes?
return true;
}
@Override
public boolean visitToMany(ToManyProperty property) {
return true;
}
});
}
Aggregations