Search in sources :

Example 86 with DbRelationship

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

the class DbAttributeTableModel method setPrimaryKey.

public boolean setPrimaryKey(Boolean newVal, DbAttribute attr, int row) {
    boolean flag = newVal;
    // when PK is unset, we need to fix some derived flags
    if (!flag) {
        attr.setGenerated(false);
        Collection<DbRelationship> relationships = ProjectUtil.getRelationshipsUsingAttributeAsTarget(attr);
        relationships.addAll(ProjectUtil.getRelationshipsUsingAttributeAsSource(attr));
        if (relationships.size() > 0) {
            Iterator<DbRelationship> it = relationships.iterator();
            while (it.hasNext()) {
                DbRelationship relationship = it.next();
                if (!relationship.isToDependentPK()) {
                    it.remove();
                }
            }
            // filtered only those that are to dep PK
            if (relationships.size() > 0) {
                String message = (relationships.size() == 1) ? "Fix \"To Dep PK\" relationship using this attribute?" : "Fix " + relationships.size() + " \"To Dep PK\" relationships using this attribute?";
                int answer = JOptionPane.showConfirmDialog(Application.getFrame(), message);
                if (answer != JOptionPane.YES_OPTION) {
                    // no action needed
                    return false;
                }
                // fix target relationships
                for (DbRelationship relationship : relationships) {
                    relationship.setToDependentPK(false);
                }
            }
        }
    }
    attr.setPrimaryKey(flag);
    if (flag) {
        attr.setMandatory(true);
        fireTableCellUpdated(row, DB_ATTRIBUTE_MANDATORY);
    }
    return true;
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 87 with DbRelationship

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

the class InferRelationshipsControllerBase method createNames.

protected void createNames() {
    for (InferredRelationship myir : inferredRelationships) {
        DbRelationship localRelationship = new DbRelationship();
        localRelationship.setToMany(myir.isToMany());
        if (myir.getJoinSource().isPrimaryKey()) {
            localRelationship.addJoin(new DbJoin(localRelationship, myir.getJoinSource().getName(), myir.getJoinTarget().getName()));
            localRelationship.setSourceEntity(myir.getSource());
            localRelationship.setTargetEntityName(myir.getTarget().getName());
        } else {
            localRelationship.addJoin(new DbJoin(localRelationship, myir.getJoinTarget().getName(), myir.getJoinSource().getName()));
            localRelationship.setSourceEntity(myir.getTarget());
            localRelationship.setTargetEntityName(myir.getSource().getName());
        }
        myir.setName(strategy.relationshipName(localRelationship));
    }
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Example 88 with DbRelationship

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

the class TableSelectorController method updateTables.

/**
 * Performs validation of DbEntities in the current DataMap. Returns a collection of
 * ValidationInfo objects describing the problems.
 */
public void updateTables(Collection<DataMap> dataMaps) {
    this.tables = new ArrayList<DbEntity>();
    for (DataMap dataMap : dataMaps) {
        this.tables.addAll(dataMap.getDbEntities());
    }
    excludedTables.clear();
    validationMessages.clear();
    // if there were errors, filter out those related to
    // non-derived DbEntities...
    // TODO: this is inefficient.. we need targeted validation
    // instead of doing it on the whole project
    Project project = getApplication().getProject();
    ProjectValidator projectValidator = getApplication().getInjector().getInstance(ProjectValidator.class);
    ValidationResult validationResult = projectValidator.validate(project.getRootNode());
    if (validationResult.getFailures().size() > 0) {
        for (ValidationFailure nextProblem : validationResult.getFailures()) {
            DbEntity failedEntity = null;
            if (nextProblem.getSource() instanceof DbAttribute) {
                DbAttribute failedAttribute = (DbAttribute) nextProblem.getSource();
                failedEntity = (DbEntity) failedAttribute.getEntity();
            } else if (nextProblem.getSource() instanceof DbRelationship) {
                DbRelationship failedRelationship = (DbRelationship) nextProblem.getSource();
                failedEntity = (DbEntity) failedRelationship.getSourceEntity();
            } else if (nextProblem.getSource() instanceof DbEntity) {
                failedEntity = (DbEntity) nextProblem.getSource();
            }
            if (failedEntity == null) {
                continue;
            }
            excludedTables.put(failedEntity.getName(), failedEntity);
            validationMessages.put(failedEntity.getName(), nextProblem.getDescription());
        }
    }
    // Find selectable tables
    permanentlyExcludedCount = excludedTables.size();
    selectableTablesList.clear();
    for (DbEntity table : tables) {
        if (false == excludedTables.containsKey(table.getName())) {
            selectableTablesList.add(table);
        }
    }
    tableBinding.updateView();
    tableSelectedAction();
}
Also used : Project(org.apache.cayenne.project.Project) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) ProjectValidator(org.apache.cayenne.project.validation.ProjectValidator) ValidationResult(org.apache.cayenne.validation.ValidationResult) DataMap(org.apache.cayenne.map.DataMap) ValidationFailure(org.apache.cayenne.validation.ValidationFailure)

Example 89 with DbRelationship

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

the class ObjAttributeInfoDialog method getFirstEntity.

private Entity getFirstEntity() {
    Iterator<CayenneMapEntry> it = attribute.getDbPathIterator();
    Entity firstEnt = attribute.getDbAttribute().getEntity();
    boolean setEnt = false;
    while (it.hasNext()) {
        Object ob = it.next();
        if (ob instanceof DbRelationship) {
            if (!setEnt) {
                firstEnt = ((DbRelationship) ob).getSourceEntity();
                setEnt = true;
            }
        } else if (ob instanceof DbAttribute) {
            if (!setEnt) {
                firstEnt = ((DbAttribute) ob).getEntity();
            }
        }
    }
    return firstEnt;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 90 with DbRelationship

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

the class ObjAttributeInfoDialog method initController.

private void initController(ObjAttribute attr) {
    for (String embeddableName : embeddableNames) {
        ((DefaultComboBoxModel) view.getTypeComboBox().getModel()).addElement(embeddableName);
    }
    this.attribute = attr;
    if (attribute instanceof EmbeddedAttribute || embeddableNames.contains(attribute.getType())) {
        this.attributeSaved = new EmbeddedAttribute();
    } else {
        this.attributeSaved = new ObjAttribute();
    }
    copyObjAttribute(attributeSaved, attribute);
    relTargets = new ArrayList<DbEntity>(attribute.getEntity().getDataMap().getDbEntities());
    /*
		 * Register auto-selection of the target
		 */
    view.getPathBrowser().addTreeSelectionListener(this);
    view.getAttributeName().setText(attribute.getName());
    if (attribute.getDbAttributePath() != null) {
        if (attribute.getDbAttributePath().contains(".")) {
            String path = attribute.getDbAttributePath();
            view.getCurrentPathLabel().setText(path.replace(".", " -> "));
        } else {
            view.getCurrentPathLabel().setText(attribute.getDbAttributePath());
        }
    } else {
        view.getCurrentPathLabel().setText("");
    }
    view.getSourceEntityLabel().setText(attribute.getEntity().getName());
    view.getTypeComboBox().setSelectedItem(attribute.getType());
    BindingBuilder builder = new BindingBuilder(getApplication().getBindingFactory(), this);
    builder.bindToAction(view.getCancelButton(), "closeAction()");
    builder.bindToAction(view.getSelectPathButton(), "setPath(true)");
    builder.bindToAction(view.getSaveButton(), "saveMapping()");
    /*
		 * set filter for ObjAttributePathBrowser
		 */
    if (view.getPathBrowser().getModel() == null) {
        Entity firstEntity = null;
        if (attribute.getDbAttribute() == null) {
            if (attribute.getParent() instanceof ObjEntity) {
                DbEntity dbEnt = ((ObjEntity) attribute.getParent()).getDbEntity();
                if (dbEnt != null) {
                    Collection<DbAttribute> attrib = dbEnt.getAttributes();
                    Collection<DbRelationship> rel = dbEnt.getRelationships();
                    if (attrib.size() > 0) {
                        Iterator<DbAttribute> iter = attrib.iterator();
                        firstEntity = iter.next().getEntity();
                    } else if (rel.size() > 0) {
                        Iterator<DbRelationship> iter = rel.iterator();
                        firstEntity = iter.next().getSourceEntity();
                    }
                }
            }
        } else {
            firstEntity = getFirstEntity();
        }
        if (firstEntity != null) {
            EntityTreeModel treeModel = new EntityTreeModel(firstEntity);
            treeModel.setFilter(new EntityTreeAttributeRelationshipFilter());
            view.getPathBrowser().setModel(treeModel);
        }
    }
    if (attribute.getDbAttribute() != null) {
        setSelectionPath();
    }
    view.getTypeComboBox().addItemListener(e -> {
        if (lastObjectType != null) {
            if (!lastObjectType.equals(e.getItemSelectable())) {
                if (embeddableNames.contains(e.getItemSelectable().getSelectedObjects()[0].toString())) {
                    if (attributeSaved instanceof ObjAttribute) {
                        EmbeddedAttribute copyAttrSaved = new EmbeddedAttribute();
                        copyObjAttribute(copyAttrSaved, attributeSaved);
                        attributeSaved = copyAttrSaved;
                    }
                } else {
                    if (attributeSaved instanceof EmbeddedAttribute) {
                        ObjAttribute copyAttrSaved = new ObjAttribute();
                        copyObjAttribute(copyAttrSaved, attributeSaved);
                        attributeSaved = copyAttrSaved;
                    }
                }
                attributeSaved.setType(e.getItemSelectable().getSelectedObjects()[0].toString());
                rebuildTable();
                setEnabledSaveButton();
            }
        }
    });
    view.getAttributeName().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (!view.getAttributeName().getText().equals(attribute.getName())) {
                setEnabledSaveButton();
            }
        }

        public void keyReleased(KeyEvent e) {
            if (!view.getAttributeName().getText().equals(attribute.getName())) {
                setEnabledSaveButton();
            }
        }

        public void keyTyped(KeyEvent e) {
        }
    });
    rebuildTable();
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) BindingBuilder(org.apache.cayenne.swing.BindingBuilder) DbAttribute(org.apache.cayenne.map.DbAttribute) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) KeyEvent(java.awt.event.KeyEvent) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) EntityTreeModel(org.apache.cayenne.modeler.util.EntityTreeModel) DbRelationship(org.apache.cayenne.map.DbRelationship) EntityTreeAttributeRelationshipFilter(org.apache.cayenne.modeler.util.EntityTreeAttributeRelationshipFilter) Iterator(java.util.Iterator) KeyListener(java.awt.event.KeyListener)

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