Search in sources :

Example 26 with ObjEntity

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

the class ObjRelationshipInfo method configureRelationship.

/**
 * Stores current state of the model in the internal ObjRelationship.
 */
public boolean configureRelationship() {
    boolean hasChanges = false;
    boolean oldToMany = relationship.isToMany();
    boolean oldPathNotEmpty = !relationship.getDbRelationships().isEmpty();
    String relationshipName = getRelationshipName();
    if (!Util.nullSafeEquals(relationship.getName(), relationshipName)) {
        ProjectUtil.setRelationshipName(relationship.getSourceEntity(), relationship, relationshipName);
        hasChanges = true;
    }
    if (savedDbRelationships.size() > 0) {
        DbEntity lastEntity = savedDbRelationships.get(savedDbRelationships.size() - 1).getTargetEntity();
        if (objectTarget == null || objectTarget.getDbEntity() != lastEntity) {
            /*
                 * Entities in combobox and path browser do not match. In this
                 * case, we rely on the browser and automatically select one of
                 * lastEntity's ObjEntities
                 */
            Collection<ObjEntity> objEntities = lastEntity.getDataMap().getMappedEntities(lastEntity);
            objectTarget = objEntities.size() == 0 ? null : objEntities.iterator().next();
        }
    }
    if (objectTarget == null || !Util.nullSafeEquals(objectTarget.getName(), relationship.getTargetEntityName())) {
        hasChanges = true;
        // note on events notification - this needs to be propagated
        // via old modeler events, but we leave this to the controller
        // since model knows nothing about Modeler mediator.
        relationship.setTargetEntityName(objectTarget);
    }
    // check for path modifications
    List<DbRelationship> oldPath = relationship.getDbRelationships();
    if (oldPath.size() != savedDbRelationships.size()) {
        hasChanges = true;
        updatePath();
    } else {
        for (int i = 0; i < oldPath.size(); i++) {
            DbRelationship next = savedDbRelationships.get(i);
            if (oldPath.get(i) != next) {
                hasChanges = true;
                updatePath();
                break;
            }
        }
    }
    String collectionType = ObjRelationship.DEFAULT_COLLECTION_TYPE.equals(targetCollection) || !relationship.isToMany() ? null : targetCollection;
    if (!Util.nullSafeEquals(collectionType, relationship.getCollectionType())) {
        hasChanges = true;
        relationship.setCollectionType(collectionType);
    }
    // map key only makes sense for Map relationships
    String mapKey = COLLECTION_TYPE_MAP.equals(collectionType) && !DEFAULT_MAP_KEY.equals(this.mapKey) ? this.mapKey : null;
    if (!Util.nullSafeEquals(mapKey, relationship.getMapKey())) {
        hasChanges = true;
        relationship.setMapKey(mapKey);
    }
    /*
         * As of CAY-436 here we check if to-many property has changed during
         * the editing, and if so, delete rule must be reset to default value
         */
    if (oldPathNotEmpty && hasChanges && relationship.isToMany() != oldToMany) {
        DeleteRuleUpdater.updateObjRelationship(relationship);
    }
    return hasChanges;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 27 with ObjEntity

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

the class ObjRelationshipInfo method updateTargetCombo.

/**
 * Places in objectTargets list all ObjEntities for specified DbEntity
 */
@SuppressWarnings("unchecked")
protected void updateTargetCombo(DbEntity dbTarget) {
    // copy those that have DbEntities mapped to dbTarget, and then sort
    this.objectTargets = new ArrayList<>();
    if (dbTarget != null) {
        objectTargets.addAll(dbTarget.getDataMap().getMappedEntities(dbTarget));
        objectTargets.sort(Comparators.getNamedObjectComparator());
    }
    view.getTargetCombo().removeAllItems();
    for (ObjEntity s : objectTargets) {
        view.getTargetCombo().addItem(s.getName());
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity)

Example 28 with ObjEntity

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

the class AttributeErrorMsg method displayField.

public void displayField(ProjectController mediator, JFrame frame) {
    AttributeDisplayEvent event = new AttributeDisplayEvent(frame, attribute, entity, map, domain);
    // twice
    if (entity instanceof ObjEntity) {
        mediator.fireObjEntityDisplayEvent(event);
        mediator.fireObjAttributeDisplayEvent(event);
    } else if (entity instanceof DbEntity) {
        mediator.fireDbEntityDisplayEvent(event);
        mediator.fireDbAttributeDisplayEvent(event);
    }
}
Also used : AttributeDisplayEvent(org.apache.cayenne.modeler.event.AttributeDisplayEvent) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity)

Example 29 with ObjEntity

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

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

the class EOModelProcessorTest method assertOneWayRelationships.

protected void assertOneWayRelationships(DataMap map) throws Exception {
    // assert that one way relationships are loaded properly
    // - Db loaded as two-way, obj - as one-way
    ObjEntity exhibitEntity = map.getObjEntity("Exhibit");
    ObjRelationship toTypeObject = exhibitEntity.getRelationship("toExhibitType");
    DbRelationship toTypeDB = exhibitEntity.getDbEntity().getRelationship("toExhibitType");
    assertNotNull(toTypeObject);
    assertNotNull(toTypeDB);
    assertNull(toTypeObject.getReverseRelationship());
    assertNotNull(toTypeDB.getReverseRelationship());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbRelationship(org.apache.cayenne.map.DbRelationship)

Aggregations

ObjEntity (org.apache.cayenne.map.ObjEntity)294 Test (org.junit.Test)110 DbEntity (org.apache.cayenne.map.DbEntity)72 ObjAttribute (org.apache.cayenne.map.ObjAttribute)68 ObjRelationship (org.apache.cayenne.map.ObjRelationship)62 DataMap (org.apache.cayenne.map.DataMap)57 DbAttribute (org.apache.cayenne.map.DbAttribute)37 DbRelationship (org.apache.cayenne.map.DbRelationship)29 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)27 ObjectId (org.apache.cayenne.ObjectId)26 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)22 ArrayList (java.util.ArrayList)19 Embeddable (org.apache.cayenne.map.Embeddable)18 EntityResolver (org.apache.cayenne.map.EntityResolver)17 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)16 Expression (org.apache.cayenne.exp.Expression)15 Persistent (org.apache.cayenne.Persistent)12 EntityEvent (org.apache.cayenne.map.event.EntityEvent)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 Entity (org.apache.cayenne.map.Entity)11