Search in sources :

Example 41 with DbRelationship

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

the class DbRelationshipPathComboBoxEditor method enterPressed.

@Override
protected void enterPressed(JTable table) {
    String dbRelationshipPath = ((JTextComponent) (comboBoxPathChooser).getEditor().getEditorComponent()).getText();
    changeObjEntity(dbRelationshipPath);
    Object currentNode = getCurrentNode(dbRelationshipPath);
    String[] pathStrings = dbRelationshipPath.split(Pattern.quote("."));
    String lastStringInPath = pathStrings[pathStrings.length - 1];
    if (lastStringInPath.equals(ModelerUtil.getObjectName(currentNode)) && currentNode instanceof DbRelationship) {
        if (enterPressedCount == 1) {
            // it is second time enter pressed.. so we will save input data
            enterPressedCount = 0;
            if (table.getCellEditor() != null) {
                table.getCellEditor().stopCellEditing();
                if (dbRelationshipPath.equals(savePath)) {
                    return;
                }
                // we need object target to save it in model
                DbEntity lastEntity = ((DbRelationship) currentNode).getTargetEntity();
                Collection<ObjEntity> objEntities = ((DbRelationship) currentNode).getTargetEntity().getDataMap().getMappedEntities(lastEntity);
                ObjEntity objectTarget = objEntities.isEmpty() ? null : objEntities.iterator().next();
                model.getRelationship(row).setTargetEntityName(objectTarget);
                model.setUpdatedValueAt(dbRelationshipPath, row, REL_TARGET_PATH_COLUMN);
                model.getRelationship(row).setDbRelationshipPath(dbRelationshipPath);
                model.getRelationship(row).setMapKey(null);
            }
            table.repaint();
        }
        enterPressedCount = 1;
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) JTextComponent(javax.swing.text.JTextComponent)

Example 42 with DbRelationship

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

the class ProjectUtil method getRelationshipsUsingAttributeAsTarget.

/**
 * Returns a collection of DbRelationships that use this attribute as a source.
 */
public static Collection<DbRelationship> getRelationshipsUsingAttributeAsTarget(DbAttribute attribute) {
    Entity parent = attribute.getEntity();
    if (parent == null) {
        return Collections.EMPTY_LIST;
    }
    DataMap map = parent.getDataMap();
    if (map == null) {
        return Collections.EMPTY_LIST;
    }
    Collection<DbRelationship> relationships = new ArrayList<DbRelationship>();
    for (Entity entity : map.getDbEntities()) {
        if (entity == parent) {
            continue;
        }
        Collection<DbRelationship> entityRelationships = (Collection<DbRelationship>) entity.getRelationships();
        for (DbRelationship relationship : entityRelationships) {
            if (ProjectUtil.containsTargetAttribute(relationship, attribute)) {
                relationships.add(relationship);
            }
        }
    }
    return relationships;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DataMap(org.apache.cayenne.map.DataMap)

Example 43 with DbRelationship

use of org.apache.cayenne.map.DbRelationship 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 44 with DbRelationship

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

the class EOModelProcessor method makeReverseDbRelationships.

/**
 * Create reverse DbRelationships that were not created so far, since
 * Cayenne requires them.
 *
 * @since 1.0.5
 */
protected void makeReverseDbRelationships(DbEntity dbEntity) {
    if (dbEntity == null) {
        throw new NullPointerException("Attempt to create reverse relationships for the null DbEntity.");
    }
    for (DbRelationship relationship : new ArrayList<>(dbEntity.getRelationships())) {
        if (relationship.getReverseRelationship() == null) {
            DbRelationship reverse = relationship.createReverseRelationship();
            reverse.setName(NameBuilder.builder(reverse, reverse.getSourceEntity()).baseName(relationship.getName() + "Reverse").name());
            relationship.getTargetEntity().addRelationship(reverse);
        }
    }
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList)

Example 45 with DbRelationship

use of org.apache.cayenne.map.DbRelationship 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

DbRelationship (org.apache.cayenne.map.DbRelationship)106 DbEntity (org.apache.cayenne.map.DbEntity)59 DbAttribute (org.apache.cayenne.map.DbAttribute)35 DbJoin (org.apache.cayenne.map.DbJoin)35 ObjEntity (org.apache.cayenne.map.ObjEntity)30 ObjRelationship (org.apache.cayenne.map.ObjRelationship)28 ObjAttribute (org.apache.cayenne.map.ObjAttribute)20 Test (org.junit.Test)15 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)13 ArrayList (java.util.ArrayList)11 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DataMap (org.apache.cayenne.map.DataMap)9 Entity (org.apache.cayenne.map.Entity)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 HashMap (java.util.HashMap)6 ObjectId (org.apache.cayenne.ObjectId)6