Search in sources :

Example 6 with EntityEvent

use of org.apache.cayenne.map.event.EntityEvent in project cayenne by apache.

the class DbEntitySyncAction method syncDbEntity.

protected void syncDbEntity() {
    ProjectController mediator = getProjectController();
    DbEntity dbEntity = mediator.getCurrentDbEntity();
    if (dbEntity != null) {
        Collection<ObjEntity> entities = dbEntity.getDataMap().getMappedEntities(dbEntity);
        if (entities.isEmpty()) {
            return;
        }
        EntityMergeSupport merger = new EntitySyncController(Application.getInstance().getFrameController(), dbEntity).createMerger();
        if (merger == null) {
            return;
        }
        merger.setNameGenerator(new PreserveRelationshipNameGenerator());
        DbEntitySyncUndoableEdit undoableEdit = new DbEntitySyncUndoableEdit((DataChannelDescriptor) mediator.getProject().getRootNode(), mediator.getCurrentDataMap());
        // filter out inherited entities, as we need to add attributes only to the roots
        filterInheritedEntities(entities);
        for (ObjEntity entity : entities) {
            DbEntitySyncUndoableEdit.EntitySyncUndoableListener listener = undoableEdit.new EntitySyncUndoableListener(entity);
            merger.addEntityMergeListener(listener);
            // we should not be trying to introspect the merger
            if (merger.isRemovingMeaningfulFKs()) {
                undoableEdit.addEdit(undoableEdit.new MeaningfulFKsUndoableEdit(entity, merger.getMeaningfulFKs(entity)));
            }
            if (merger.synchronizeWithDbEntity(entity)) {
                mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
            }
            merger.removeEntityMergeListener(listener);
        }
        application.getUndoManager().addEdit(undoableEdit);
    }
}
Also used : EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) EntityEvent(org.apache.cayenne.map.event.EntityEvent) DbEntitySyncUndoableEdit(org.apache.cayenne.modeler.undo.DbEntitySyncUndoableEdit) ProjectController(org.apache.cayenne.modeler.ProjectController) EntitySyncController(org.apache.cayenne.modeler.dialog.objentity.EntitySyncController)

Example 7 with EntityEvent

use of org.apache.cayenne.map.event.EntityEvent in project cayenne by apache.

the class DbEntityTab method setQualifier.

void setQualifier(String qualifier) {
    if (qualifier != null && qualifier.trim().length() == 0) {
        qualifier = null;
    }
    DbEntity ent = mediator.getCurrentDbEntity();
    if (ent != null && !Util.nullSafeEquals(ent.getQualifier(), qualifier)) {
        ExpressionConvertor convertor = new ExpressionConvertor();
        try {
            String oldQualifier = convertor.valueAsString(ent.getQualifier());
            if (!Util.nullSafeEquals(oldQualifier, qualifier)) {
                Expression exp = (Expression) convertor.stringAsValue(qualifier);
                ent.setQualifier(exp);
                mediator.fireDbEntityEvent(new EntityEvent(this, ent));
            }
        } catch (IllegalArgumentException ex) {
            // unparsable qualifier
            throw new ValidationException(ex.getMessage());
        }
    }
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) DbEntity(org.apache.cayenne.map.DbEntity) Expression(org.apache.cayenne.exp.Expression) EntityEvent(org.apache.cayenne.map.event.EntityEvent) ExpressionConvertor(org.apache.cayenne.modeler.util.ExpressionConvertor)

Example 8 with EntityEvent

use of org.apache.cayenne.map.event.EntityEvent in project cayenne by apache.

the class DbEntityTab method setCatalog.

void setCatalog(String text) {
    if (text != null && text.trim().length() == 0) {
        text = null;
    }
    DbEntity ent = mediator.getCurrentDbEntity();
    if (ent != null && !Util.nullSafeEquals(ent.getCatalog(), text)) {
        ent.setCatalog(text);
        mediator.fireDbEntityEvent(new EntityEvent(this, ent));
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) EntityEvent(org.apache.cayenne.map.event.EntityEvent)

Example 9 with EntityEvent

use of org.apache.cayenne.map.event.EntityEvent in project cayenne by apache.

the class PKDBGeneratorPanel method updateView.

void updateView(final DbEntity entity) {
    for (ItemListener listener : attributes.getItemListeners()) {
        attributes.removeItemListener(listener);
    }
    Collection<DbAttribute> pkAttributes = entity.getPrimaryKeys();
    if (pkAttributes.isEmpty()) {
        attributes.removeAllItems();
        attributes.addItem("<Entity has no PK columns>");
        attributes.setSelectedIndex(0);
        attributes.setEnabled(false);
    } else {
        attributes.setEnabled(true);
        MutableComboBoxModel model = new DefaultComboBoxModel(pkAttributes.toArray());
        String noSelection = "<Select Generated Column>";
        model.insertElementAt(noSelection, 0);
        model.setSelectedItem(noSelection);
        attributes.setModel(model);
        for (DbAttribute a : pkAttributes) {
            if (a.isGenerated()) {
                model.setSelectedItem(a);
                break;
            }
        }
        // listen for selection changes of the new entity
        attributes.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                Object item = e.getItem();
                if (item instanceof DbAttribute) {
                    boolean generated = e.getStateChange() == ItemEvent.SELECTED;
                    DbAttribute a = (DbAttribute) item;
                    if (a.isGenerated() != generated) {
                        a.setGenerated(generated);
                        mediator.fireDbEntityEvent(new EntityEvent(this, entity));
                    }
                }
            }
        });
    }
    // revalidate as children layout has changed...
    revalidate();
}
Also used : ItemEvent(java.awt.event.ItemEvent) EntityEvent(org.apache.cayenne.map.event.EntityEvent) DbAttribute(org.apache.cayenne.map.DbAttribute) MutableComboBoxModel(javax.swing.MutableComboBoxModel) ItemListener(java.awt.event.ItemListener) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel)

Example 10 with EntityEvent

use of org.apache.cayenne.map.event.EntityEvent in project cayenne by apache.

the class PKDBGeneratorPanel method onInitInternal.

protected void onInitInternal(DbEntity entity) {
    resetStrategy(entity, true, false);
    Collection pkAttributes = entity.getPrimaryKeys();
    // by default check the only numeric PK
    if (pkAttributes.size() == 1) {
        DbAttribute pk = (DbAttribute) pkAttributes.iterator().next();
        if (TypesMapping.isNumeric(pk.getType()) && !pk.isGenerated()) {
            pk.setGenerated(true);
            mediator.fireDbEntityEvent(new EntityEvent(this, entity));
        }
    }
    updateView(entity);
}
Also used : EntityEvent(org.apache.cayenne.map.event.EntityEvent) DbAttribute(org.apache.cayenne.map.DbAttribute) Collection(java.util.Collection)

Aggregations

EntityEvent (org.apache.cayenne.map.event.EntityEvent)36 DbEntity (org.apache.cayenne.map.DbEntity)14 ObjEntity (org.apache.cayenne.map.ObjEntity)13 EntityDisplayEvent (org.apache.cayenne.modeler.event.EntityDisplayEvent)7 ProjectController (org.apache.cayenne.modeler.ProjectController)6 Entity (org.apache.cayenne.map.Entity)5 Test (org.junit.Test)5 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)4 ArrayList (java.util.ArrayList)3 ProcedureEvent (org.apache.cayenne.configuration.event.ProcedureEvent)3 DbAttribute (org.apache.cayenne.map.DbAttribute)3 ObjAttribute (org.apache.cayenne.map.ObjAttribute)3 AttributeEvent (org.apache.cayenne.map.event.AttributeEvent)3 ValidationException (org.apache.cayenne.validation.ValidationException)3 ItemEvent (java.awt.event.ItemEvent)2 ItemListener (java.awt.event.ItemListener)2 QueryEvent (org.apache.cayenne.configuration.event.QueryEvent)2 EntityMergeSupport (org.apache.cayenne.dbsync.merge.context.EntityMergeSupport)2 Expression (org.apache.cayenne.exp.Expression)2 DbKeyGenerator (org.apache.cayenne.map.DbKeyGenerator)2