use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class ChildDiffLoader method nodePropertyChanged.
@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
// this change is for simple property, so no need to convert targets to
// server
// objects...
Persistent object = findObject(nodeId);
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
setExternalChange(Boolean.TRUE);
try {
descriptor.getProperty(property).writeProperty(object, oldValue, newValue);
} catch (Exception e) {
throw new CayenneRuntimeException("Error setting property: " + property, e);
} finally {
setExternalChange(Boolean.FALSE);
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class SQLResult method getResolvedComponents.
public List<Object> getResolvedComponents(EntityResolver resolver) {
if (resultDescriptors == null) {
return Collections.emptyList();
}
List<Object> resolvedComponents = new ArrayList<>(resultDescriptors.size());
int offset = 0;
for (Object component : getComponents()) {
if (component instanceof String) {
resolvedComponents.add(new DefaultScalarResultSegment((String) component, offset));
offset = offset + 1;
} else if (component instanceof EntityResult) {
EntityResult entityResult = (EntityResult) component;
Map<String, String> fields = entityResult.getDbFields(resolver);
String entityName = entityResult.getEntityName();
if (entityName == null) {
entityName = resolver.getObjEntity(entityResult.getEntityClass()).getName();
}
ClassDescriptor classDescriptor = resolver.getClassDescriptor(entityName);
resolvedComponents.add(new DefaultEntityResultSegment(classDescriptor, fields, offset));
offset = offset + fields.size();
} else if (component instanceof EmbeddedResult) {
EmbeddedResult embeddedResult = (EmbeddedResult) component;
Map<String, String> fields = embeddedResult.getFields();
resolvedComponents.add(new DefaultEmbeddableResultSegment(embeddedResult.getEmbeddable(), fields, offset));
offset = offset + fields.size();
} else {
throw new IllegalArgumentException("Unsupported result descriptor component: " + component);
}
}
return resolvedComponents;
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class ShallowMergeOperation method merge.
public <T extends Persistent> T merge(T peerInParentContext) {
if (peerInParentContext == null) {
throw new IllegalArgumentException("Null peerInParentContext");
}
// handling of HOLLOW peer state is here for completeness... Wonder if we ever
// have a case where it is applicable.
int peerState = peerInParentContext.getPersistenceState();
ObjectId id = peerInParentContext.getObjectId();
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(id.getEntityName());
GraphManager graphManager = context.getGraphManager();
// messing up dataobjects per CAY-845.
synchronized (graphManager) {
T object = (T) graphManager.getNode(id);
// merge into an existing object
if (object == null) {
object = (T) descriptor.createObject();
object.setObjectContext(context);
object.setObjectId(id);
if (peerState == PersistenceState.HOLLOW) {
object.setPersistenceState(PersistenceState.HOLLOW);
} else {
object.setPersistenceState(PersistenceState.COMMITTED);
}
graphManager.registerNode(id, object);
}
// TODO: Andrus, 1/24/2006 implement smart merge for modified objects...
if (peerState != PersistenceState.HOLLOW && object.getPersistenceState() != PersistenceState.MODIFIED && object.getPersistenceState() != PersistenceState.DELETED) {
descriptor.shallowMerge(peerInParentContext, object);
if (object.getPersistenceState() == PersistenceState.HOLLOW) {
object.setPersistenceState(PersistenceState.COMMITTED);
}
}
return object;
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class ObjectContextQueryAction method interceptRelationshipQuery.
protected boolean interceptRelationshipQuery() {
if (query instanceof RelationshipQuery) {
RelationshipQuery relationshipQuery = (RelationshipQuery) query;
if (!relationshipQuery.isRefreshing()) {
if (targetContext == null && relationshipQuery.getRelationship(actingContext.getEntityResolver()).isToMany()) {
return !DONE;
}
ObjectId id = relationshipQuery.getObjectId();
Object object = actingContext.getGraphManager().getNode(id);
if (object != null) {
ClassDescriptor descriptor = actingContext.getEntityResolver().getClassDescriptor(id.getEntityName());
if (!descriptor.isFault(object)) {
ArcProperty property = (ArcProperty) descriptor.getProperty(relationshipQuery.getRelationshipName());
if (!property.isFault(object)) {
Object related = property.readPropertyDirectly(object);
List result;
// null to-one
if (related == null) {
result = new ArrayList(1);
} else // to-many List
if (related instanceof List) {
result = (List) related;
} else // to-many Set
if (related instanceof Set) {
result = new ArrayList((Set) related);
} else // to-many Map
if (related instanceof Map) {
result = new ArrayList(((Map) related).values());
} else // non-null to-one
{
result = new ArrayList(1);
result.add(related);
}
this.response = new ListResponse(result);
return DONE;
}
/**
* Workaround for CAY-1183. If a Relationship query is being sent
* from child context, we assure that local object is not NEW and
* relationship - unresolved (this way exception will occur). This
* helps when faulting objects that were committed to parent
* context (this), but not to database. Checking type of context's
* channel is the only way to ensure that we are on the top level
* of context hierarchy (there might be more than one-level-deep
* nested contexts).
*/
if (((Persistent) object).getPersistenceState() == PersistenceState.NEW && !(actingContext.getChannel() instanceof BaseContext)) {
this.response = new ListResponse();
return DONE;
}
}
}
}
}
return !DONE;
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class PrefetchProcessorTreeBuilderIT method testBuildTreeWithPrefetches.
@Test
public void testBuildTreeWithPrefetches() {
final ClassDescriptor descriptor = resolver.getClassDescriptor("Artist");
ObjEntity e2 = resolver.getObjEntity("Painting");
ObjEntity e3 = resolver.getObjEntity("Gallery");
ObjEntity e4 = resolver.getObjEntity("Exhibit");
ObjEntity e5 = resolver.getObjEntity("ArtistExhibit");
List<DataRow> mainRows = new ArrayList<>();
Map<String, List<?>> extraRows = new HashMap<>();
PrefetchTreeNode tree = new PrefetchTreeNode();
tree.addPath(Artist.PAINTING_ARRAY.getName()).setPhantom(false);
tree.addPath(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).dot(Gallery.EXHIBIT_ARRAY).getName()).setPhantom(false);
tree.addPath(Artist.ARTIST_EXHIBIT_ARRAY.getName()).setPhantom(false);
QueryMetadata metadata = new MockQueryMetadata() {
@Override
public ClassDescriptor getClassDescriptor() {
return descriptor;
}
@Override
public ObjEntity getObjEntity() {
return descriptor.getEntity();
}
@Override
public DbEntity getDbEntity() {
return getObjEntity().getDbEntity();
}
@Override
public DataMap getDataMap() {
return getObjEntity().getDataMap();
}
@Override
public boolean isRefreshingObjects() {
return true;
}
@Override
public boolean isResolvingInherited() {
return true;
}
};
HierarchicalObjectResolver resolver = new HierarchicalObjectResolver(context, metadata);
PrefetchProcessorTreeBuilder builder = new PrefetchProcessorTreeBuilder(resolver, mainRows, extraRows);
PrefetchProcessorNode n1 = builder.buildTree(tree);
assertSame(mainRows, n1.getDataRows());
assertSame(descriptor.getEntity(), n1.getResolver().getEntity());
PrefetchProcessorNode n2 = (PrefetchProcessorNode) n1.getNode("paintingArray");
assertNotNull(n2);
assertSame(e2, n2.getResolver().getEntity());
assertFalse(n2.isPhantom());
assertTrue(n2.isPartitionedByParent());
PrefetchProcessorNode n3 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery");
assertNotNull(n3);
assertSame(e3, n3.getResolver().getEntity());
assertTrue(n3.isPhantom());
assertTrue(n3.isPartitionedByParent());
PrefetchProcessorNode n4 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery.exhibitArray");
assertNotNull(n4);
assertSame(e4, n4.getResolver().getEntity());
assertFalse(n4.isPhantom());
assertTrue(n4.isPartitionedByParent());
PrefetchProcessorNode n5 = (PrefetchProcessorNode) n1.getNode("artistExhibitArray");
assertNotNull(n5);
assertSame(e5, n5.getResolver().getEntity());
assertFalse(n5.isPhantom());
assertTrue(n5.isPartitionedByParent());
}
Aggregations