Search in sources :

Example 36 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class ProjectUtil method findObjRelationshipsForDbRelationship.

public static Collection<ObjRelationship> findObjRelationshipsForDbRelationship(ProjectController mediator, DbRelationship relationship) {
    DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
    List<ObjRelationship> objRelationships = new ArrayList<>();
    if (domain != null) {
        for (DataMap map : domain.getDataMaps()) {
            for (ObjEntity entity : map.getObjEntities()) {
                for (ObjRelationship objRelationship : entity.getRelationships()) {
                    if (objRelationship.getDbRelationships().contains(relationship)) {
                        objRelationships.add(objRelationship);
                    }
                }
            }
        }
    }
    return objRelationships;
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjEntity(org.apache.cayenne.map.ObjEntity) ArrayList(java.util.ArrayList) DataMap(org.apache.cayenne.map.DataMap)

Example 37 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class EOModelProcessor method makeRelationships.

/**
 * Create ObjRelationships of the specified entity, as well as
 * DbRelationships of the corresponding DbEntity.
 */
protected void makeRelationships(EOModelHelper helper, ObjEntity objEntity) {
    Map entityPlistMap = helper.entityPListMap(objEntity.getName());
    List classProps = (List) entityPlistMap.get("classProperties");
    List rinfo = (List) entityPlistMap.get("relationships");
    Collection attributes = (Collection) entityPlistMap.get("attributes");
    if (rinfo == null) {
        return;
    }
    if (classProps == null) {
        classProps = Collections.EMPTY_LIST;
    }
    if (attributes == null) {
        attributes = Collections.EMPTY_LIST;
    }
    DbEntity dbSrc = objEntity.getDbEntity();
    Iterator it = rinfo.iterator();
    while (it.hasNext()) {
        Map relMap = (Map) it.next();
        String targetName = (String) relMap.get("destination");
        // ignore flattened relationships for now
        if (targetName == null) {
            continue;
        }
        String relName = (String) relMap.get("name");
        boolean toMany = "Y".equals(relMap.get("isToMany"));
        boolean toDependentPK = "Y".equals(relMap.get("propagatesPrimaryKey"));
        ObjEntity target = helper.getDataMap().getObjEntity(targetName);
        // ignoring those now.
        if (target == null) {
            continue;
        }
        DbEntity dbTarget = target.getDbEntity();
        Map targetPlistMap = helper.entityPListMap(targetName);
        Collection targetAttributes = (Collection) targetPlistMap.get("attributes");
        DbRelationship dbRel = null;
        // Note: source maybe null, e.g. an abstract entity.
        if (dbSrc != null && dbTarget != null) {
            // in case of inheritance EOF stores duplicates of all inherited
            // relationships, so we must skip this relationship in DB entity
            // if it is
            // already there...
            dbRel = dbSrc.getRelationship(relName);
            if (dbRel == null) {
                dbRel = new DbRelationship();
                dbRel.setSourceEntity(dbSrc);
                dbRel.setTargetEntityName(dbTarget);
                dbRel.setToMany(toMany);
                dbRel.setName(relName);
                dbRel.setToDependentPK(toDependentPK);
                dbSrc.addRelationship(dbRel);
                List joins = (List) relMap.get("joins");
                Iterator jIt = joins.iterator();
                while (jIt.hasNext()) {
                    Map joinMap = (Map) jIt.next();
                    DbJoin join = new DbJoin(dbRel);
                    // find source attribute dictionary and extract the
                    // column name
                    String sourceAttributeName = (String) joinMap.get("sourceAttribute");
                    join.setSourceName(columnName(attributes, sourceAttributeName));
                    String targetAttributeName = (String) joinMap.get("destinationAttribute");
                    join.setTargetName(columnName(targetAttributes, targetAttributeName));
                    dbRel.addJoin(join);
                }
            }
        }
        // only create obj relationship if it is a class property
        if (classProps.contains(relName)) {
            ObjRelationship rel = new ObjRelationship();
            rel.setName(relName);
            rel.setSourceEntity(objEntity);
            rel.setTargetEntityName(target);
            objEntity.addRelationship(rel);
            if (dbRel != null) {
                rel.addDbRelationship(dbRel);
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) Iterator(java.util.Iterator) Collection(java.util.Collection) DbJoin(org.apache.cayenne.map.DbJoin) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DataMap(org.apache.cayenne.map.DataMap)

Example 38 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class EOModelProcessor method makeFlatRelationships.

/**
 * Create Flattened ObjRelationships of the specified entity.
 */
protected void makeFlatRelationships(EOModelHelper helper, ObjEntity e) {
    Map info = helper.entityPListMap(e.getName());
    List rinfo = (List) info.get("relationships");
    if (rinfo == null) {
        return;
    }
    Iterator it = rinfo.iterator();
    while (it.hasNext()) {
        Map relMap = (Map) it.next();
        String targetPath = (String) relMap.get("definition");
        // ignore normal relationships
        if (targetPath == null) {
            continue;
        }
        ObjRelationship flatRel = new ObjRelationship();
        flatRel.setName((String) relMap.get("name"));
        flatRel.setSourceEntity(e);
        try {
            flatRel.setDbRelationshipPath(targetPath);
        } catch (ExpressionException ex) {
            logger.warn("Invalid relationship: " + targetPath);
            continue;
        }
        // find target entity
        Map entityInfo = info;
        StringTokenizer toks = new StringTokenizer(targetPath, ".");
        while (toks.hasMoreTokens() && entityInfo != null) {
            String pathComponent = toks.nextToken();
            // get relationship info and reset entityInfo, so that we could
            // use
            // entityInfo state as an indicator of valid flat relationship
            // enpoint
            // outside the loop
            Collection relationshipInfo = (Collection) entityInfo.get("relationships");
            entityInfo = null;
            if (relationshipInfo == null) {
                break;
            }
            Iterator rit = relationshipInfo.iterator();
            while (rit.hasNext()) {
                Map pathRelationship = (Map) rit.next();
                if (pathComponent.equals(pathRelationship.get("name"))) {
                    String targetName = (String) pathRelationship.get("destination");
                    entityInfo = helper.entityPListMap(targetName);
                    break;
                }
            }
        }
        if (entityInfo != null) {
            flatRel.setTargetEntityName((String) entityInfo.get("name"));
        }
        e.addRelationship(flatRel);
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) StringTokenizer(java.util.StringTokenizer) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) DataMap(org.apache.cayenne.map.DataMap) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 39 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class EOModelProcessorInheritanceTest method testLoadFlattenedRelationship.

@Test
public void testLoadFlattenedRelationship() throws Exception {
    DataMap map = processor.loadEOModel(url);
    ObjEntity e1 = map.getObjEntity("HelperFlatEntity");
    assertNotNull(e1);
    ObjRelationship fr = (ObjRelationship) e1.getRelationship("singleTables");
    assertNotNull(fr);
    assertEquals("singleTableJoins.toSingleTable", fr.getDbRelationshipPath());
    assertEquals("SingleTableConcreteEntityOne", fr.getTargetEntityName());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 40 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class EOModelProcessorTest method assertLoaded.

protected void assertLoaded(String mapName, DataMap map) throws Exception {
    assertNotNull(map);
    assertEquals(mapName, map.getName());
    // check obj entities
    ObjEntity artistE = map.getObjEntity("Artist");
    assertNotNull(artistE);
    assertEquals("Artist", artistE.getName());
    // check Db entities
    DbEntity artistDE = map.getDbEntity("ARTIST");
    DbEntity artistDE1 = artistE.getDbEntity();
    assertSame(artistDE, artistDE1);
    // check attributes
    ObjAttribute a1 = artistE.getAttribute("artistName");
    assertNotNull(a1);
    DbAttribute da1 = a1.getDbAttribute();
    assertNotNull(da1);
    assertSame(da1, artistDE.getAttribute("ARTIST_NAME"));
    // check ObjRelationships
    ObjRelationship rel = artistE.getRelationship("artistExhibitArray");
    assertNotNull(rel);
    assertEquals(1, rel.getDbRelationships().size());
    // check DbRelationships
    DbRelationship drel = artistDE.getRelationship("artistExhibitArray");
    assertNotNull(drel);
    assertSame(drel, rel.getDbRelationships().get(0));
    // flattened relationships
    ObjRelationship frel = artistE.getRelationship("exhibitArray");
    assertNotNull(frel);
    assertEquals(2, frel.getDbRelationships().size());
    // storing data map may uncover some inconsistencies
    PrintWriter mockupWriter = new NullPrintWriter();
    map.encodeAsXML(new XMLEncoder(mockupWriter), new EmptyConfigurationNodeVisitor());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) XMLEncoder(org.apache.cayenne.util.XMLEncoder) EmptyConfigurationNodeVisitor(org.apache.cayenne.configuration.EmptyConfigurationNodeVisitor) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) PrintWriter(java.io.PrintWriter)

Aggregations

ObjRelationship (org.apache.cayenne.map.ObjRelationship)84 ObjEntity (org.apache.cayenne.map.ObjEntity)48 ObjAttribute (org.apache.cayenne.map.ObjAttribute)27 DbRelationship (org.apache.cayenne.map.DbRelationship)26 Test (org.junit.Test)24 DbEntity (org.apache.cayenne.map.DbEntity)18 DbAttribute (org.apache.cayenne.map.DbAttribute)15 DbJoin (org.apache.cayenne.map.DbJoin)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)12 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ObjectId (org.apache.cayenne.ObjectId)10 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)9 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)9 ArrayList (java.util.ArrayList)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 List (java.util.List)7 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)7 DataMap (org.apache.cayenne.map.DataMap)7 HashMap (java.util.HashMap)6