use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class QualifierTranslator method extractQualifier.
protected Expression extractQualifier() {
// if additional qualifier is set, use it
if (this.qualifier != null) {
return this.qualifier;
}
Query q = queryAssembler.getQuery();
Expression qualifier = ((SelectQuery<?>) q).getQualifier();
// append Entity qualifiers, taking inheritance into account
ObjEntity entity = getObjEntity();
if (entity != null) {
ClassDescriptor descriptor = queryAssembler.getEntityResolver().getClassDescriptor(entity.getName());
Expression entityQualifier = descriptor.getEntityInheritanceTree().qualifierForEntityAndSubclasses();
if (entityQualifier != null) {
qualifier = (qualifier != null) ? qualifier.andExp(entityQualifier) : entityQualifier;
}
}
// Attaching root Db entity's qualifier
if (getDbEntity() != null) {
Expression dbQualifier = getDbEntity().getQualifier();
if (dbQualifier != null) {
dbQualifier = dbQualifier.transform(new DbEntityQualifierTransformer());
qualifier = qualifier == null ? dbQualifier : qualifier.andExp(dbQualifier);
}
}
return qualifier;
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class AshwoodEntitySorter method sortObjectsForEntity.
@Override
public void sortObjectsForEntity(ObjEntity objEntity, List<?> objects, boolean deleteOrder) {
indexSorter();
List<Persistent> persistent = (List<Persistent>) objects;
DbEntity dbEntity = objEntity.getDbEntity();
// if no sorting is required
if (!isReflexive(dbEntity)) {
return;
}
int size = persistent.size();
if (size == 0) {
return;
}
EntityResolver resolver = persistent.get(0).getObjectContext().getEntityResolver();
ClassDescriptor descriptor = resolver.getClassDescriptor(objEntity.getName());
List<DbRelationship> reflexiveRels = reflexiveDbEntities.get(dbEntity);
String[] reflexiveRelNames = new String[reflexiveRels.size()];
for (int i = 0; i < reflexiveRelNames.length; i++) {
DbRelationship dbRel = reflexiveRels.get(i);
ObjRelationship objRel = (dbRel != null ? objEntity.getRelationshipForDbRelationship(dbRel) : null);
reflexiveRelNames[i] = (objRel != null ? objRel.getName() : null);
}
List<Persistent> sorted = new ArrayList<>(size);
Digraph<Persistent, Boolean> objectDependencyGraph = new MapDigraph<>();
Object[] masters = new Object[reflexiveRelNames.length];
for (int i = 0; i < size; i++) {
Persistent current = (Persistent) objects.get(i);
objectDependencyGraph.addVertex(current);
int actualMasterCount = 0;
for (int k = 0; k < reflexiveRelNames.length; k++) {
String reflexiveRelName = reflexiveRelNames[k];
if (reflexiveRelName == null) {
continue;
}
masters[k] = descriptor.getProperty(reflexiveRelName).readProperty(current);
if (masters[k] == null) {
masters[k] = findReflexiveMaster(current, objEntity.getRelationship(reflexiveRelName), current.getObjectId().getEntityName());
}
if (masters[k] != null) {
actualMasterCount++;
}
}
int mastersFound = 0;
for (int j = 0; j < size && mastersFound < actualMasterCount; j++) {
if (i == j) {
continue;
}
Persistent masterCandidate = persistent.get(j);
for (Object master : masters) {
if (masterCandidate == master) {
objectDependencyGraph.putArc(masterCandidate, current, Boolean.TRUE);
mastersFound++;
}
}
}
}
IndegreeTopologicalSort<Persistent> sorter = new IndegreeTopologicalSort<>(objectDependencyGraph);
while (sorter.hasNext()) {
Persistent o = sorter.next();
if (o == null) {
throw new CayenneRuntimeException("Sorting objects for %s failed. Cycles found.", objEntity.getClassName());
}
sorted.add(o);
}
// since API requires sorting within the same array,
// simply replace all objects with objects in the right order...
// may come up with something cleaner later
persistent.clear();
persistent.addAll(sorted);
if (deleteOrder) {
Collections.reverse(persistent);
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class AnnotationCommitLogEntityFactory method createDescriptor.
private CommitLogEntity createDescriptor(String entityName) {
EntityResolver entityResolver = getEntityResolver();
ClassDescriptor classDescriptor = entityResolver.getClassDescriptor(entityName);
CommitLog a = classDescriptor.getObjectClass().getAnnotation(CommitLog.class);
if (a == null) {
return BLOCKED_ENTITY;
}
ObjEntity entity = entityResolver.getObjEntity(entityName);
return new MutableCommitLogLogEntity(entity).setConfidential(a.confidential()).setIgnoreProperties(a.ignoredProperties()).setIgnoreAttributes(a.ignoreAttributes()).setIgnoreToOneRelationships(a.ignoreToOneRelationships()).setIgnoreToManyRelationships(a.ignoreToManyRelationships());
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class EJBQLPathAnaliserTranslator method visitMemberOf.
@Override
public boolean visitMemberOf(EJBQLExpression expression) {
if (expression.getChildrenCount() != 2) {
throw new EJBQLException("MEMBER OF must have exactly two children, got: " + expression.getChildrenCount());
}
if (!(expression.getChild(1) instanceof EJBQLPath)) {
throw new EJBQLException("Second child of the MEMBER OF must be a collection path, got: " + expression.getChild(1));
}
QuotingStrategy quoter = context.getQuotingStrategy();
EJBQLPath path = (EJBQLPath) expression.getChild(1);
// make sure the ID for the path does not overlap with other condition
// joins...
String id = path.getAbsolutePath();
String correlatedEntityId = path.getId();
ClassDescriptor correlatedEntityDescriptor = context.getEntityDescriptor(correlatedEntityId);
String correlatedTableName = quoter.quotedFullyQualifiedName(correlatedEntityDescriptor.getEntity().getDbEntity());
String correlatedTableAlias = context.getTableAlias(correlatedEntityId, correlatedTableName);
String subqueryId = context.createIdAlias(id);
ClassDescriptor targetDescriptor = context.getEntityDescriptor(subqueryId);
if (expression.isNegated()) {
context.append(" NOT");
}
context.append(" EXISTS (SELECT 1 FROM ");
String subqueryTableName = quoter.quotedFullyQualifiedName(targetDescriptor.getEntity().getDbEntity());
String subqueryRootAlias = context.getTableAlias(subqueryId, subqueryTableName);
ObjRelationship relationship = correlatedEntityDescriptor.getEntity().getRelationship(path.getRelativePath());
if (relationship.getDbRelationshipPath().contains(".")) {
// if the DbRelationshipPath contains '.', the relationship is
// flattened
subqueryRootAlias = processFlattenedRelationShip(subqueryRootAlias, relationship);
} else {
// not using "AS" to separate table name and alias name - OpenBase
// doesn't
// support "AS", and the rest of the databases do not care
context.append(subqueryTableName).append(' ').append(subqueryRootAlias);
}
context.append(" WHERE");
DbRelationship correlatedJoinRelationship = context.getIncomingRelationships(new EJBQLTableId(id)).get(0);
for (DbJoin join : correlatedJoinRelationship.getJoins()) {
context.append(' ').append(subqueryRootAlias).append('.').append(join.getTargetName()).append(" = ");
context.append(correlatedTableAlias).append('.').append(quoter.quotedSourceName(join));
context.append(" AND");
}
// translate subquery_root_id = LHS_of_memberof
EJBQLEquals equals = new EJBQLEquals(-1);
EJBQLIdentificationVariable identifier = new EJBQLIdentificationVariable(-1);
identifier.setText(subqueryId);
equals.jjtAddChild(identifier, 0);
equals.jjtAddChild((Node) expression.getChild(0), 1);
equals.visit(this);
context.append(")");
return false;
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class EJBQLPathAnaliserTranslator method visitIdentificationVariable.
@Override
public boolean visitIdentificationVariable(EJBQLExpression expression) {
// this is a match on a variable, like "x = :x"
ClassDescriptor descriptor = context.getEntityDescriptor(expression.getText());
if (descriptor == null) {
throw new EJBQLException("Invalid identification variable: " + expression.getText());
}
DbEntity table = descriptor.getEntity().getDbEntity();
String alias = context.getTableAlias(expression.getText(), context.getQuotingStrategy().quotedFullyQualifiedName(table));
Collection<DbAttribute> pks = table.getPrimaryKeys();
if (pks.size() == 1) {
DbAttribute pk = pks.iterator().next();
context.append(' ').append(alias).append('.').append(context.getQuotingStrategy().quotedName(pk));
} else {
throw new EJBQLException("Multi-column PK to-many matches are not yet supported.");
}
return false;
}
Aggregations