Search in sources :

Example 1 with ExpressionConvertor

use of org.apache.cayenne.modeler.util.ExpressionConvertor 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 2 with ExpressionConvertor

use of org.apache.cayenne.modeler.util.ExpressionConvertor in project cayenne by apache.

the class ObjEntityTab method setQualifier.

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

Example 3 with ExpressionConvertor

use of org.apache.cayenne.modeler.util.ExpressionConvertor in project cayenne by apache.

the class ObjEntityTab method initFromModel.

/**
 * Updates the view from the current model state. Invoked when a currently displayed
 * ObjEntity is changed.
 *
 * @param entity current entity
 */
private void initFromModel(final ObjEntity entity) {
    // TODO: this is a hack until we implement a real MVC
    qualifier.getComponent().setBackground(Color.WHITE);
    name.setText(entity.getName());
    superClassName.setText(entity.getSuperClassName());
    className.setText(entity.getClassName());
    readOnly.setSelected(entity.isReadOnly());
    isAbstract.setSelected(entity.isAbstract());
    comment.setText(getComment(entity));
    serverOnly.setSelected(entity.isServerOnly());
    clientClassName.setText(entity.getClientClassName());
    clientSuperClassName.setText(entity.getClientSuperClassName());
    qualifier.setText(new ExpressionConvertor().valueAsString(entity.getDeclaredQualifier()));
    // TODO: fix inheritance - we should allow to select optimistic
    // lock if superclass is not already locked,
    // otherwise we must keep this checked in but not editable.
    optimisticLocking.setSelected(entity.getDeclaredLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC);
    // init DbEntities
    EntityResolver resolver = mediator.getEntityResolver();
    DataMap map = mediator.getCurrentDataMap();
    DbEntity[] dbEntities = resolver.getDbEntities().toArray(new DbEntity[0]);
    Arrays.sort(dbEntities, Comparators.getDataMapChildrenComparator());
    DefaultComboBoxModel<DbEntity> dbModel = new DefaultComboBoxModel<>(dbEntities);
    dbModel.setSelectedItem(entity.getDbEntity());
    dbEntityCombo.setRenderer(CellRenderers.entityListRendererWithIcons(map));
    dbEntityCombo.setModel(dbModel);
    boolean isUsedInheritance = entity.getSuperEntity() != null;
    dbEntityCombo.setEnabled(!isUsedInheritance);
    // toggle visibility and editability rules
    toggleClientFieldsVisible(map.isClientSupported());
    toggleEnabled(entity.getSuperEntityName() == null, !entity.isServerOnly());
    // do not show this entity or any of the subentities
    List<ObjEntity> objEntities = map.getObjEntities().stream().filter(object -> entity != object && !object.isSubentityOf(entity)).sorted(Comparators.getDataMapChildrenComparator()).collect(Collectors.toList());
    objEntities.add(0, NO_INHERITANCE);
    DefaultComboBoxModel<ObjEntity> superEntityModel = new DefaultComboBoxModel<>(objEntities.toArray(new ObjEntity[0]));
    superEntityModel.setSelectedItem(entity.getSuperEntity());
    superEntityCombo.setRenderer(CellRenderers.entityListRendererWithIcons(map));
    superEntityCombo.setModel(superEntityModel);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) EntityResolver(org.apache.cayenne.map.EntityResolver) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) ExpressionConvertor(org.apache.cayenne.modeler.util.ExpressionConvertor) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with ExpressionConvertor

use of org.apache.cayenne.modeler.util.ExpressionConvertor in project cayenne by apache.

the class SelectQueryMainTab method createQualifier.

/**
 * Method to create and check an expression
 * @param text String to be converted as Expression
 * @return Expression if a new expression was created, null otherwise.
 * @throws ValidationException if <code>text</code> can't be converted
 */
Expression createQualifier(String text) throws ValidationException {
    SelectQueryDescriptor query = getQuery();
    if (query == null) {
        return null;
    }
    ExpressionConvertor convertor = new ExpressionConvertor();
    try {
        String oldQualifier = convertor.valueAsString(query.getQualifier());
        if (!Util.nullSafeEquals(oldQualifier, text)) {
            Expression exp = (Expression) convertor.stringAsValue(text);
            /*
                 * Advanced checking. See CAY-888 #1
                 */
            if (query.getRoot() instanceof Entity) {
                checkExpression((Entity) query.getRoot(), exp);
            }
            return exp;
        }
        return null;
    } catch (IllegalArgumentException ex) {
        // unparsable qualifier
        throw new ValidationException(ex.getMessage());
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) ValidationException(org.apache.cayenne.validation.ValidationException) Expression(org.apache.cayenne.exp.Expression) SelectQueryDescriptor(org.apache.cayenne.map.SelectQueryDescriptor) ExpressionConvertor(org.apache.cayenne.modeler.util.ExpressionConvertor)

Example 5 with ExpressionConvertor

use of org.apache.cayenne.modeler.util.ExpressionConvertor in project cayenne by apache.

the class DbEntityTab method currentDbEntityChanged.

public void currentDbEntityChanged(EntityDisplayEvent e) {
    DbEntity entity = (DbEntity) e.getEntity();
    if (entity == null) {
        return;
    }
    // if entity hasn't changed, still notify PK Generator panels, as entity PK may have changed...
    for (int i = 0; i < pkGeneratorDetail.getComponentCount(); i++) {
        ((PKGeneratorPanel) pkGeneratorDetail.getComponent(i)).setDbEntity(entity);
    }
    name.setText(entity.getName());
    catalog.setText(entity.getCatalog());
    schema.setText(entity.getSchema());
    qualifier.setText(new ExpressionConvertor().valueAsString(entity.getQualifier()));
    comment.setText(getComment(entity));
    String type = PK_DEFAULT_GENERATOR;
    if (entity.getPrimaryKeyGenerator() != null) {
        type = PK_CUSTOM_SEQUENCE_GENERATOR;
    } else {
        for (DbAttribute a : entity.getPrimaryKeys()) {
            if (a.isGenerated()) {
                type = PK_DB_GENERATOR;
                break;
            }
        }
    }
    catalogLabel.setEnabled(true);
    catalog.getComponent().setEnabled(true);
    schemaLabel.setEnabled(true);
    schema.getComponent().setEnabled(true);
    pkGeneratorDetail.setVisible(true);
    pkGeneratorType.setVisible(true);
    pkGeneratorType.setSelectedItem(type);
    pkGeneratorDetailLayout.show(pkGeneratorDetail, type);
    if (entity.getDataMap().getMappedEntities(entity).isEmpty()) {
        toolBar.getComponentAtIndex(4).setEnabled(false);
        toolBar.getComponentAtIndex(5).setEnabled(false);
    } else {
        toolBar.getComponentAtIndex(4).setEnabled(true);
        toolBar.getComponentAtIndex(5).setEnabled(true);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) ExpressionConvertor(org.apache.cayenne.modeler.util.ExpressionConvertor)

Aggregations

ExpressionConvertor (org.apache.cayenne.modeler.util.ExpressionConvertor)5 Expression (org.apache.cayenne.exp.Expression)3 DbEntity (org.apache.cayenne.map.DbEntity)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 ValidationException (org.apache.cayenne.validation.ValidationException)3 EntityEvent (org.apache.cayenne.map.event.EntityEvent)2 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)1 DataMap (org.apache.cayenne.map.DataMap)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 Entity (org.apache.cayenne.map.Entity)1 EntityResolver (org.apache.cayenne.map.EntityResolver)1 SelectQueryDescriptor (org.apache.cayenne.map.SelectQueryDescriptor)1