Search in sources :

Example 91 with DbRelationship

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

the class ObjRelationshipInfo method connectEnds.

// Connects last selected DbRelationship in the path to the
// last DbEntity, creating a dummy relationship if needed.
private void connectEnds() {
    Relationship last = null;
    int size = dbRelationships.size();
    if (size > 0) {
        last = dbRelationships.get(size - 1);
    }
    Entity target = getEndEntity();
    if (target != null && (last == null || last.getTargetEntity() != target)) {
        // try to connect automatically, if we can't use dummy connector
        Entity source = (last == null) ? getStartEntity() : last.getTargetEntity();
        if (source != null) {
            Relationship anyConnector = source != null ? source.getAnyRelationship(target) : null;
            if (anyConnector != null) {
                dbRelationships.add((DbRelationship) anyConnector);
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) Relationship(org.apache.cayenne.map.Relationship) DbRelationship(org.apache.cayenne.map.DbRelationship) ObjRelationship(org.apache.cayenne.map.ObjRelationship)

Example 92 with DbRelationship

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

the class ObjRelationshipInfo method savePath.

/**
 * Stores current state of the model in the internal ObjRelationship.
 */
public boolean savePath() {
    boolean hasChanges = false;
    boolean oldToMany = relationship.isToMany();
    String relationshipName = getRelationshipName();
    if (!Util.nullSafeEquals(relationship.getName(), relationshipName)) {
        hasChanges = true;
        relationship.setName(relationshipName);
    }
    if (savedDbRelationships.size() > 0) {
        DbEntity lastEntity = (DbEntity) 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 (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 93 with DbRelationship

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

the class DbEntityRelationshipPanel method tableChanged.

public void tableChanged(TableModelEvent e) {
    if (table.getSelectedRow() >= 0) {
        DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
        DbRelationship rel = model.getRelationship(table.getSelectedRow());
        enabledResolve = (rel.getTargetEntity() != null);
        resolveMenu.setEnabled(enabledResolve);
    }
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 94 with DbRelationship

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

the class DbEntityRelationshipPanel method initController.

private void initController() {
    this.mediator.addDbEntityDisplayListener(this);
    this.mediator.addDbEntityListener(this);
    this.mediator.addDbRelationshipListener(this);
    resolver = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            if (row < 0) {
                return;
            }
            // Get DbRelationship
            DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
            DbRelationship rel = model.getRelationship(row);
            ResolveDbRelationshipDialog dialog = new ResolveDbRelationshipDialog(rel);
            dialog.setVisible(true);
            dialog.dispose();
        }
    };
    resolveMenu.addActionListener(resolver);
    table.getSelectionModel().addListSelectionListener(new DbRelationshipListSelectionListener());
    mediator.getApplication().getActionManager().setupCutCopyPaste(table, CutAttributeRelationshipAction.class, CopyAttributeRelationshipAction.class);
}
Also used : ResolveDbRelationshipDialog(org.apache.cayenne.modeler.dialog.ResolveDbRelationshipDialog) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 95 with DbRelationship

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

the class DbEntityRelationshipPanel method dbRelationshipRemoved.

public void dbRelationshipRemoved(RelationshipEvent e) {
    DbRelationshipTableModel model = (DbRelationshipTableModel) table.getModel();
    DbRelationship relationship = (DbRelationship) e.getRelationship();
    int ind = model.getObjectList().indexOf(relationship);
    model.removeRelationship(relationship);
    table.select(ind);
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship)

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