Search in sources :

Example 76 with ClassDescriptor

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;
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) SelectQuery(org.apache.cayenne.query.SelectQuery) Query(org.apache.cayenne.query.Query) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Expression(org.apache.cayenne.exp.Expression)

Example 77 with ClassDescriptor

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);
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) EntityResolver(org.apache.cayenne.map.EntityResolver) IndegreeTopologicalSort(org.apache.cayenne.ashwood.graph.IndegreeTopologicalSort) MapDigraph(org.apache.cayenne.ashwood.graph.MapDigraph) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) List(java.util.List)

Example 78 with ClassDescriptor

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());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) EntityResolver(org.apache.cayenne.map.EntityResolver) CommitLog(org.apache.cayenne.commitlog.CommitLog)

Example 79 with ClassDescriptor

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;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) EJBQLIdentificationVariable(org.apache.cayenne.ejbql.parser.EJBQLIdentificationVariable) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy) EJBQLPath(org.apache.cayenne.ejbql.parser.EJBQLPath) EJBQLEquals(org.apache.cayenne.ejbql.parser.EJBQLEquals)

Example 80 with ClassDescriptor

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;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbEntity(org.apache.cayenne.map.DbEntity) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute)

Aggregations

ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)81 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)23 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)21 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)21 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)21 ObjEntity (org.apache.cayenne.map.ObjEntity)18 ArcProperty (org.apache.cayenne.reflect.ArcProperty)18 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 Persistent (org.apache.cayenne.Persistent)17 ObjectId (org.apache.cayenne.ObjectId)14 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)14 DbRelationship (org.apache.cayenne.map.DbRelationship)13 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 DataRow (org.apache.cayenne.DataRow)10 DbAttribute (org.apache.cayenne.map.DbAttribute)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DbEntity (org.apache.cayenne.map.DbEntity)9 DbJoin (org.apache.cayenne.map.DbJoin)9