Search in sources :

Example 6 with ObjectIdQuery

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());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 7 with ObjectIdQuery

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());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 8 with ObjectIdQuery

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));
}
Also used : ClientIdMapToManyTarget(org.apache.cayenne.testdo.map_to_many.ClientIdMapToManyTarget) ClientIdMapToMany(org.apache.cayenne.testdo.map_to_many.ClientIdMapToMany) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Example 9 with ObjectIdQuery

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);
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ArrayList(java.util.ArrayList) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 10 with ObjectIdQuery

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;
        }
    });
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) CommitLogEntity(org.apache.cayenne.commitlog.meta.CommitLogEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) QueryResponse(org.apache.cayenne.QueryResponse) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Aggregations

ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)42 Test (org.junit.Test)34 ObjectId (org.apache.cayenne.ObjectId)15 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)11 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 ToOneFK2 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK2)6 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)6 DataRow (org.apache.cayenne.DataRow)5 Artist (org.apache.cayenne.testdo.testmap.Artist)5 ToOneFK1 (org.apache.cayenne.testdo.relationships_to_one_fk.ToOneFK1)4 List (java.util.List)3 ObjectContext (org.apache.cayenne.ObjectContext)3 Persistent (org.apache.cayenne.Persistent)3 QueryResponse (org.apache.cayenne.QueryResponse)3 SQLTemplate (org.apache.cayenne.query.SQLTemplate)3 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)3 Date (java.util.Date)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2