use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class ASTObjPathIT method testEvaluate_ObjEntity_Outer.
@Test
public void testEvaluate_ObjEntity_Outer() {
ASTObjPath node = new ASTObjPath("paintingArray+.paintingTitle");
ObjEntity ae = context.getEntityResolver().getObjEntity(Artist.class);
Object target = node.evaluate(ae);
assertTrue(target instanceof ObjAttribute);
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class DataContext method currentSnapshot.
/**
* Returns a DataRow reflecting current, possibly uncommitted, object state.
* <p>
* <strong>Warning:</strong> This method will return a partial snapshot if
* an object or one of its related objects that propagate their keys to this
* object have temporary ids. DO NOT USE this method if you expect a DataRow
* to represent a complete object state.
* </p>
*
* @since 1.1
*/
public DataRow currentSnapshot(final Persistent object) {
// for a HOLLOW object return snapshot from cache
if (object.getPersistenceState() == PersistenceState.HOLLOW && object.getObjectContext() != null) {
return getObjectStore().getSnapshot(object.getObjectId());
}
ObjEntity entity = getEntityResolver().getObjEntity(object);
final ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
final DataRow snapshot = new DataRow(10);
snapshot.setEntityName(entity.getName());
descriptor.visitProperties(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
ObjAttribute objAttr = property.getAttribute();
// processing compound attributes correctly
snapshot.put(objAttr.getDbAttributePath(), property.readPropertyDirectly(object));
return true;
}
public boolean visitToMany(ToManyProperty property) {
// do nothing
return true;
}
public boolean visitToOne(ToOneProperty property) {
ObjRelationship rel = property.getRelationship();
// if target doesn't propagates its key value, skip it
if (rel.isSourceIndependentFromTargetChange()) {
return true;
}
Object targetObject = property.readPropertyDirectly(object);
if (targetObject == null) {
return true;
}
// to avoid unneeded fault triggering
if (targetObject instanceof Fault) {
DataRow storedSnapshot = getObjectStore().getSnapshot(object.getObjectId());
if (storedSnapshot == null) {
throw new CayenneRuntimeException("No matching objects found for ObjectId %s" + ". Object may have been deleted externally.", object.getObjectId());
}
DbRelationship dbRel = rel.getDbRelationships().get(0);
for (DbJoin join : dbRel.getJoins()) {
String key = join.getSourceName();
snapshot.put(key, storedSnapshot.get(key));
}
return true;
}
// target is resolved and we have an FK->PK to it,
// so extract it from target...
Persistent target = (Persistent) targetObject;
Map<String, Object> idParts = target.getObjectId().getIdSnapshot();
// this method.
if (idParts.isEmpty()) {
return true;
}
DbRelationship dbRel = rel.getDbRelationships().get(0);
Map<String, Object> fk = dbRel.srcFkSnapshotWithTargetSnapshot(idParts);
snapshot.putAll(fk);
return true;
}
});
// process object id map
// we should ignore any object id values if a corresponding attribute
// is a part of relationship "toMasterPK", since those values have been
// set above when db relationships where processed.
Map<String, Object> thisIdParts = object.getObjectId().getIdSnapshot();
if (thisIdParts != null) {
// put only those that do not exist in the map
for (Map.Entry<String, Object> entry : thisIdParts.entrySet()) {
String nextKey = entry.getKey();
if (!snapshot.containsKey(nextKey)) {
snapshot.put(nextKey, entry.getValue());
}
}
}
return snapshot;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class DataDomainInsertBucket method createPermIds.
void createPermIds(DbEntityClassDescriptor descriptor, Collection<Persistent> objects) {
if (objects.isEmpty()) {
return;
}
ObjEntity objEntity = descriptor.getEntity();
DbEntity entity = descriptor.getDbEntity();
DataNode node = parent.getDomain().lookupDataNode(entity.getDataMap());
boolean supportsGeneratedKeys = node.getAdapter().supportsGeneratedKeys();
PkGenerator pkGenerator = node.getAdapter().getPkGenerator();
for (Persistent object : objects) {
ObjectId id = object.getObjectId();
if (id == null || !id.isTemporary()) {
continue;
}
// modify replacement id directly...
Map<String, Object> idMap = id.getReplacementIdMap();
boolean autoPkDone = false;
for (DbAttribute dbAttr : entity.getPrimaryKeys()) {
String dbAttrName = dbAttr.getName();
if (idMap.containsKey(dbAttrName)) {
continue;
}
// handle meaningful PK
ObjAttribute objAttr = objEntity.getAttributeForDbAttribute(dbAttr);
if (objAttr != null) {
Object value = descriptor.getClassDescriptor().getProperty(objAttr.getName()).readPropertyDirectly(object);
if (value != null) {
Class<?> javaClass = objAttr.getJavaClass();
if (javaClass.isPrimitive() && value instanceof Number && ((Number) value).intValue() == 0) {
// primitive 0 has to be treated as NULL, or
// otherwise we can't generate PK for POJO's
} else {
idMap.put(dbAttrName, value);
continue;
}
}
}
// skip db-generated
if (supportsGeneratedKeys && dbAttr.isGenerated()) {
continue;
}
// skip propagated
if (isPropagated(dbAttr)) {
continue;
}
// already in this loop, we must bail out.
if (autoPkDone) {
throw new CayenneRuntimeException("Primary Key autogeneration only works for a single attribute.");
}
// finally, use database generation mechanism
try {
Object pkValue = pkGenerator.generatePk(node, dbAttr);
idMap.put(dbAttrName, pkValue);
autoPkDone = true;
} catch (Exception ex) {
throw new CayenneRuntimeException("Error generating PK: %s", ex, ex.getMessage());
}
}
}
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class DataDomainSyncBucket method groupObjEntitiesBySpannedDbEntities.
private void groupObjEntitiesBySpannedDbEntities() {
dbEntities = new ArrayList<>(objectsByDescriptor.size());
descriptorsByDbEntity = new HashMap<>(objectsByDescriptor.size() * 2);
for (ClassDescriptor descriptor : objectsByDescriptor.keySet()) {
// root DbEntity
{
DbEntityClassDescriptor dbEntityDescriptor = new DbEntityClassDescriptor(descriptor);
DbEntity dbEntity = dbEntityDescriptor.getDbEntity();
Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
if (descriptors == null) {
descriptors = new ArrayList<>(1);
dbEntities.add(dbEntity);
descriptorsByDbEntity.put(dbEntity, descriptors);
}
if (!containsClassDescriptor(descriptors, descriptor)) {
descriptors.add(dbEntityDescriptor);
}
}
// Note that this logic won't allow flattened attributes to span multiple databases...
for (ObjAttribute objAttribute : descriptor.getEntity().getAttributes()) {
if (objAttribute.isFlattened()) {
DbEntityClassDescriptor dbEntityDescriptor = new DbEntityClassDescriptor(descriptor, objAttribute);
DbEntity dbEntity = dbEntityDescriptor.getDbEntity();
Collection<DbEntityClassDescriptor> descriptors = descriptorsByDbEntity.get(dbEntity);
if (descriptors == null) {
descriptors = new ArrayList<>(1);
dbEntities.add(dbEntity);
descriptorsByDbEntity.put(dbEntity, descriptors);
}
if (!containsClassDescriptor(descriptors, descriptor)) {
descriptors.add(dbEntityDescriptor);
}
}
}
}
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class MixedResultIncrementalFaultList method buildIdQualifier.
Expression buildIdQualifier(int index, Object[] data) {
Map<String, Object> map;
if (data[index] instanceof Map) {
map = (Map<String, Object>) data[index];
} else {
map = new HashMap<>();
int i = 0;
for (ObjAttribute attribute : indexToEntity.get(index).getPrimaryKeys()) {
map.put(attribute.getDbAttributeName(), data[index + i++]);
}
}
return ExpressionFactory.matchAllDbExp(map, Expression.EQUAL_TO);
}
Aggregations