Search in sources :

Example 1 with EmbeddedAttribute

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

the class BaseDataObject method validateForSave.

/**
 * Performs property validation of the object, appending any validation
 * failures to the provided validationResult object. This method is invoked
 * from "validateFor.." before committing a NEW or MODIFIED object to the
 * database. Validation includes checking for null values and value sizes.
 * CayenneDataObject subclasses may override this method, calling super.
 *
 * @since 1.1
 */
protected void validateForSave(ValidationResult validationResult) {
    ObjEntity objEntity = getObjectContext().getEntityResolver().getObjEntity(this);
    if (objEntity == null) {
        throw new CayenneRuntimeException("No ObjEntity mapping found for DataObject %s", getClass().getName());
    }
    // validate mandatory attributes
    Map<String, ValidationFailure> failedDbAttributes = null;
    for (ObjAttribute next : objEntity.getAttributes()) {
        // TODO: andrus, 2/20/2007 - handle embedded attribute
        if (next instanceof EmbeddedAttribute) {
            continue;
        }
        DbAttribute dbAttribute = next.getDbAttribute();
        if (dbAttribute == null) {
            throw new CayenneRuntimeException("ObjAttribute '%s" + "' does not have a corresponding DbAttribute", next.getName());
        }
        // pk may still be generated
        if (dbAttribute.isPrimaryKey()) {
            continue;
        }
        Object value = this.readPropertyDirectly(next.getName());
        if (dbAttribute.isMandatory()) {
            ValidationFailure failure = BeanValidationFailure.validateNotNull(this, next.getName(), value);
            if (failure != null) {
                if (failedDbAttributes == null) {
                    failedDbAttributes = new HashMap<>();
                }
                failedDbAttributes.put(dbAttribute.getName(), failure);
                continue;
            }
        }
        // validate length
        if (value != null && dbAttribute.getMaxLength() > 0) {
            if (value.getClass().isArray()) {
                int len = Array.getLength(value);
                if (len > dbAttribute.getMaxLength()) {
                    String message = "\"" + next.getName() + "\" exceeds maximum allowed length (" + dbAttribute.getMaxLength() + " bytes): " + len;
                    validationResult.addFailure(new BeanValidationFailure(this, next.getName(), message));
                }
            } else if (value instanceof CharSequence) {
                int len = ((CharSequence) value).length();
                if (len > dbAttribute.getMaxLength()) {
                    String message = "\"" + next.getName() + "\" exceeds maximum allowed length (" + dbAttribute.getMaxLength() + " chars): " + len;
                    validationResult.addFailure(new BeanValidationFailure(this, next.getName(), message));
                }
            }
        }
    }
    // validate mandatory relationships
    for (final ObjRelationship relationship : objEntity.getRelationships()) {
        List<DbRelationship> dbRels = relationship.getDbRelationships();
        if (dbRels.isEmpty()) {
            continue;
        }
        // see ObjRelationship.recalculateReadOnlyValue() for more info
        if (relationship.isSourceIndependentFromTargetChange()) {
            continue;
        }
        // if db relationship is not based on a PK and is based on mandatory
        // attributes, see if we have a target object set
        // relationship will be validated only if all db path has mandatory
        // db relationships
        boolean validate = true;
        for (DbRelationship dbRelationship : dbRels) {
            for (DbJoin join : dbRelationship.getJoins()) {
                DbAttribute source = join.getSource();
                if (source.isMandatory()) {
                    // clear attribute failures...
                    if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
                        failedDbAttributes.remove(source.getName());
                    }
                } else {
                    // do not validate if the relation is based on
                    // multiple keys with some that can be nullable.
                    validate = false;
                }
            }
        }
        if (validate) {
            Object value = this.readPropertyDirectly(relationship.getName());
            ValidationFailure failure = BeanValidationFailure.validateNotNull(this, relationship.getName(), value);
            if (failure != null) {
                validationResult.addFailure(failure);
            }
        }
    }
    // deal with previously found attribute failures...
    if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
        for (ValidationFailure failure : failedDbAttributes.values()) {
            validationResult.addFailure(failure);
        }
    }
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) DbAttribute(org.apache.cayenne.map.DbAttribute) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Example 2 with EmbeddedAttribute

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

the class ObjAttributeTableModel method setObjAttributeType.

private void setObjAttributeType(ObjAttributeWrapper attribute, Object value) {
    String oldType = attribute.getType();
    String newType = value != null ? value.toString() : null;
    attribute.setType(newType);
    if (oldType == null || newType == null) {
        return;
    }
    String[] registeredTypes = ModelerUtil.getRegisteredTypeNames();
    Collection<String> registeredTypesList = Arrays.asList(registeredTypes);
    if (registeredTypesList.contains(oldType) == registeredTypesList.contains(newType)) {
        return;
    }
    ObjEntity entity = attribute.getEntity();
    ObjAttribute attributeNew;
    if (registeredTypesList.contains(newType) || !mediator.getEmbeddableNamesInCurrentDataDomain().contains(newType)) {
        attributeNew = new ObjAttribute();
        attributeNew.setDbAttributePath(attribute.getDbAttributePath());
    } else {
        attributeNew = new EmbeddedAttribute();
        attributeNew.setDbAttributePath(null);
    }
    attributeNew.setName(attribute.getName());
    attributeNew.setEntity(entity);
    attributeNew.setParent(attribute.getParent());
    attributeNew.setType(attribute.getType());
    attributeNew.setUsedForLocking(attribute.isUsedForLocking());
    entity.updateAttribute(attributeNew);
    mediator.fireObjEntityEvent(new EntityEvent(this, entity, MapEvent.CHANGE));
    mediator.fireObjEntityDisplayEvent(new EntityDisplayEvent(this, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
    mediator.fireObjAttributeEvent(new AttributeEvent(this, attributeNew, entity, MapEvent.CHANGE));
    mediator.fireObjAttributeDisplayEvent(new AttributeDisplayEvent(this, attributeNew, mediator.getCurrentObjEntity(), mediator.getCurrentDataMap(), (DataChannelDescriptor) mediator.getProject().getRootNode()));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) AttributeDisplayEvent(org.apache.cayenne.modeler.event.AttributeDisplayEvent) EntityDisplayEvent(org.apache.cayenne.modeler.event.EntityDisplayEvent) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EntityEvent(org.apache.cayenne.map.event.EntityEvent) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute) AttributeEvent(org.apache.cayenne.map.event.AttributeEvent)

Example 3 with EmbeddedAttribute

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

the class ObjAttributeInfoDialog method setPath.

public boolean setPath(boolean isChange) {
    if (isChange()) {
        attributeSaved.setType(view.getTypeComboBox().getSelectedItem().toString());
        attributeSaved.setName(view.getAttributeName().getText());
    }
    if (!(attributeSaved instanceof EmbeddedAttribute) || isRegistredType(attributeSaved.getType())) {
        StringBuilder attributePath = new StringBuilder();
        StringBuilder pathStr = new StringBuilder();
        if (attribute.getEntity().getDbEntity() != null) {
            TreePath path = view.getPathBrowser().getSelectionPath();
            if (path.getLastPathComponent() instanceof DbAttribute) {
                Object[] pathComponents = path.getPath();
                for (int i = 0; i < pathComponents.length; i++) {
                    boolean attrOrRel = true;
                    if (pathComponents[i] instanceof DbAttribute) {
                        pathStr.append(((DbAttribute) pathComponents[i]).getName());
                        attributePath.append(((DbAttribute) pathComponents[i]).getName());
                    } else if (pathComponents[i] instanceof DbRelationship) {
                        pathStr.append(((DbRelationship) pathComponents[i]).getName());
                        attributePath.append(((DbRelationship) pathComponents[i]).getName());
                    } else {
                        attrOrRel = false;
                    }
                    if (i != pathComponents.length - 1 && attrOrRel) {
                        pathStr.append(" -> ");
                        attributePath.append(".");
                    }
                }
            }
        } else {
            view.getCurrentPathLabel().setText("");
        }
        view.getCurrentPathLabel().setText(pathStr.toString());
        if (attribute.getDbAttributePath() != null && !embeddableNames.contains(view.getTypeComboBox().getSelectedItem().toString())) {
            if (!attribute.getDbAttributePath().equals(attributePath.toString())) {
                attributeSaved.setDbAttributePath(attributePath.toString());
                if (!attribute.getDbAttributePath().equals(attributePath.toString()) && isChange) {
                    model.setUpdatedValueAt(attributeSaved.getDbAttributePath(), row, 3);
                }
                return true;
            }
        } else {
            if (attributePath.length() > 0 || (attribute instanceof EmbeddedAttribute && !(attributeSaved instanceof EmbeddedAttribute))) {
                attributeSaved.setDbAttributePath(attributePath.toString());
                if (attributePath.length() == 0) {
                    model.setUpdatedValueAt(attributeSaved.getDbAttributePath(), row, 3);
                    return false;
                }
                return true;
            }
        }
    }
    return false;
}
Also used : TreePath(javax.swing.tree.TreePath) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute)

Example 4 with EmbeddedAttribute

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

the class EmbeddableAttributeHandler method createEmbeddableAttribute.

private void createEmbeddableAttribute(Attributes attributes) {
    embeddedAttribute = new EmbeddedAttribute(attributes.getValue("name"));
    embeddedAttribute.setType(attributes.getValue("type"));
    entity.addAttribute(embeddedAttribute);
}
Also used : EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute)

Example 5 with EmbeddedAttribute

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

the class PersistentDescriptorFactory method getDescriptor.

protected ClassDescriptor getDescriptor(ObjEntity entity, Class<?> entityClass) {
    String superEntityName = entity.getSuperEntityName();
    ClassDescriptor superDescriptor = (superEntityName != null) ? descriptorMap.getDescriptor(superEntityName) : null;
    PersistentDescriptor descriptor = createDescriptor();
    descriptor.setEntity(entity);
    descriptor.setSuperclassDescriptor(superDescriptor);
    descriptor.setObjectClass(entityClass);
    descriptor.setPersistenceStateAccessor(new BeanAccessor(entityClass, "persistenceState", Integer.TYPE));
    // only include this entity attributes and skip superclasses...
    for (ObjAttribute attribute : descriptor.getEntity().getDeclaredAttributes()) {
        if (attribute instanceof EmbeddedAttribute) {
            EmbeddedAttribute embedded = (EmbeddedAttribute) attribute;
            for (ObjAttribute objAttribute : embedded.getAttributes()) {
                createEmbeddedAttributeProperty(descriptor, embedded, objAttribute);
            }
        } else {
            createAttributeProperty(descriptor, attribute);
        }
    }
    // only include this entity relationships and skip superclasses...
    for (ObjRelationship relationship : descriptor.getEntity().getDeclaredRelationships()) {
        if (relationship.isToMany()) {
            String collectionType = relationship.getCollectionType();
            if (collectionType == null || ObjRelationship.DEFAULT_COLLECTION_TYPE.equals(collectionType)) {
                createToManyListProperty(descriptor, relationship);
            } else if ("java.util.Map".equals(collectionType)) {
                createToManyMapProperty(descriptor, relationship);
            } else if ("java.util.Set".equals(collectionType)) {
                createToManySetProperty(descriptor, relationship);
            } else if ("java.util.Collection".equals(collectionType)) {
                createToManyCollectionProperty(descriptor, relationship);
            } else {
                throw new IllegalArgumentException("Unsupported to-many collection type: " + collectionType);
            }
        } else {
            createToOneProperty(descriptor, relationship);
        }
    }
    EntityInheritanceTree inheritanceTree = descriptorMap.getResolver().getInheritanceTree(descriptor.getEntity().getName());
    descriptor.setEntityInheritanceTree(inheritanceTree);
    indexSubclassDescriptors(descriptor, inheritanceTree);
    indexQualifiers(descriptor, inheritanceTree);
    appendDeclaredRootDbEntity(descriptor, descriptor.getEntity());
    indexRootDbEntities(descriptor, inheritanceTree);
    indexSuperclassProperties(descriptor);
    descriptor.sortProperties();
    return descriptor;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) EntityInheritanceTree(org.apache.cayenne.map.EntityInheritanceTree) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EmbeddedAttribute(org.apache.cayenne.map.EmbeddedAttribute)

Aggregations

EmbeddedAttribute (org.apache.cayenne.map.EmbeddedAttribute)8 ObjAttribute (org.apache.cayenne.map.ObjAttribute)5 ObjEntity (org.apache.cayenne.map.ObjEntity)4 DbAttribute (org.apache.cayenne.map.DbAttribute)3 DbRelationship (org.apache.cayenne.map.DbRelationship)3 AttributeEvent (org.apache.cayenne.map.event.AttributeEvent)3 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 EntityEvent (org.apache.cayenne.map.event.EntityEvent)2 AttributeDisplayEvent (org.apache.cayenne.modeler.event.AttributeDisplayEvent)2 EntityDisplayEvent (org.apache.cayenne.modeler.event.EntityDisplayEvent)2 KeyEvent (java.awt.event.KeyEvent)1 KeyListener (java.awt.event.KeyListener)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Pattern (java.util.regex.Pattern)1 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)1 TreePath (javax.swing.tree.TreePath)1 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)1 DbEntity (org.apache.cayenne.map.DbEntity)1