use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class EntityIdCoderTest method testIdEncoding.
@Test
public void testIdEncoding() {
DbEntity dbEntity = new DbEntity("X");
DbAttribute pk = new DbAttribute("ID");
pk.setType(Types.VARCHAR);
pk.setPrimaryKey(true);
dbEntity.addAttribute(pk);
ObjEntity entity = mock(ObjEntity.class);
when(entity.getName()).thenReturn("x");
when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
when(entity.getDbEntity()).thenReturn(dbEntity);
EntityIdCoder coder = new EntityIdCoder(entity);
ObjectId id = ObjectId.of("x", "ID", "Ab:C");
assertEquals("x:Ab%3AC", coder.toStringId(id));
ObjectId parsedId = coder.toObjectId("x:Ab%3AC");
assertEquals(id, parsedId);
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class EJBQLIdentifierColumnsTranslator method addPrefetchedColumnsIfAny.
private void addPrefetchedColumnsIfAny(final String visitedIdentifier) {
PrefetchTreeNode prefetchTree = context.getCompiledExpression().getPrefetchTree();
if (prefetchTree != null) {
for (PrefetchTreeNode prefetch : prefetchTree.adjacentJointNodes()) {
ClassDescriptor descriptor = context.getEntityDescriptor(prefetch.getEjbqlPathEntityId());
if (visitedIdentifier.equals(prefetch.getEjbqlPathEntityId())) {
DbEntity table = descriptor.getRootDbEntities().iterator().next();
ObjEntity objectEntity = descriptor.getEntity();
prefetch.setEntityName(objectEntity.getName());
Expression prefetchExp = ExpressionFactory.exp(prefetch.getPath());
Expression dbPrefetch = objectEntity.translateToDbPath(prefetchExp);
DbRelationship r = null;
for (PathComponent<DbAttribute, DbRelationship> component : table.resolvePath(dbPrefetch, context.getMetadata().getPathSplitAliases())) {
r = component.getRelationship();
}
if (r == null) {
throw new CayenneRuntimeException("Invalid joint prefetch '%s' for entity: %s", prefetch, objectEntity.getName());
}
for (DbAttribute attribute : r.getTargetEntity().getAttributes()) {
appendColumn(prefetch.getEjbqlPathEntityId() + "." + prefetch.getPath(), attribute, "", prefetch.getPath() + "." + attribute.getName(), null);
}
}
}
}
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class EJBQLPathTranslator method resolveJoin.
protected void resolveJoin() {
EJBQLJoinAppender joinAppender = context.getTranslatorFactory().getJoinAppender(context);
String newPath = idPath + '.' + lastPathComponent;
String oldPath = joinAppender.registerReusableJoin(idPath, lastPathComponent, newPath);
this.fullPath = fullPath + '.' + lastPathComponent;
if (oldPath != null) {
this.idPath = oldPath;
Relationship lastRelationship = currentEntity.getRelationship(lastPathComponent);
if (lastRelationship != null) {
ObjEntity targetEntity = (ObjEntity) lastRelationship.getTargetEntity();
this.lastAlias = context.getTableAlias(fullPath, context.getQuotingStrategy().quotedFullyQualifiedName(targetEntity.getDbEntity()));
} else {
String tableName = context.getQuotingStrategy().quotedFullyQualifiedName(currentEntity.getDbEntity());
this.lastAlias = context.getTableAlias(oldPath, tableName);
}
} else {
Relationship lastRelationship = currentEntity.getRelationship(lastPathComponent);
ObjEntity targetEntity = null;
if (lastRelationship != null) {
targetEntity = (ObjEntity) lastRelationship.getTargetEntity();
} else {
targetEntity = currentEntity;
}
// register join
if (innerJoin) {
joinAppender.appendInnerJoin(joinMarker, new EJBQLTableId(idPath), new EJBQLTableId(fullPath));
} else {
joinAppender.appendOuterJoin(joinMarker, new EJBQLTableId(idPath), new EJBQLTableId(fullPath));
}
this.lastAlias = context.getTableAlias(fullPath, context.getQuotingStrategy().quotedFullyQualifiedName(targetEntity.getDbEntity()));
this.idPath = newPath;
}
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class EJBQLIdColumnsTranslator method visitIdentifier.
@Override
public boolean visitIdentifier(EJBQLExpression expression) {
Map<String, String> fields = null;
if (context.isAppendingResultColumns()) {
fields = context.nextEntityResult().getFields();
}
String idVar = expression.getText();
ClassDescriptor descriptor = context.getEntityDescriptor(idVar);
ObjEntity oe = descriptor.getEntity();
for (ObjAttribute oa : oe.getPrimaryKeys()) {
DbAttribute t = oe.getDbEntity().getAttribute(oa.getDbAttributeName());
appendColumn(idVar, oa, t, fields, oa.getType());
}
return false;
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class CustomColumnSetExtractor method extractFullObject.
private void extractFullObject(String prefix, Property<?> property) {
prefix = calculatePrefix(prefix, property);
ensureJoin(prefix);
ObjEntity entity = context.getResolver().getObjEntity(property.getType());
ColumnExtractor extractor;
if (context.getMetadata().getPageSize() > 0) {
extractor = new IdColumnExtractor(context, entity);
} else {
ClassDescriptor descriptor = context.getResolver().getClassDescriptor(entity.getName());
extractor = new DescriptorColumnExtractor(context, descriptor);
}
int index = context.getResultNodeList().size();
// extract required columns of entity
extractor.extract(prefix);
// Maybe we should change resolver, as it seems cleaner to have path from root as prefix in data row key.
for (int i = index; i < context.getResultNodeList().size(); i++) {
context.getResultNodeList().get(i).setDataRowKey(null);
}
}
Aggregations