Search in sources :

Example 31 with ObjAttribute

use of org.apache.cayenne.map.ObjAttribute 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)

Example 32 with ObjAttribute

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

the class PersistentDescriptorResultMetadata method addObjectField.

/**
 * Adds a result set column mapping for a single object property of a
 * specified entity that may differ from the root entity if inheritance is
 * involved.
 */
void addObjectField(String entityName, String attributeName, String column) {
    ObjEntity entity = classDescriptor.getEntity().getDataMap().getObjEntity(entityName);
    ObjAttribute attribute = entity.getAttribute(attributeName);
    fields.put(attribute.getDbAttributePath(), column);
    reverseFields.put(column, attribute.getDbAttributePath());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute)

Example 33 with ObjAttribute

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

the class DbAttributePathComboBoxEditor method createTreeModelForComboBox.

@Override
protected EntityTreeModel createTreeModelForComboBox(int attributeIndexInTable) {
    ObjAttribute attribute = model.getAttribute(attributeIndexInTable).getValue();
    Entity firstEntity = null;
    if (attribute.getDbAttribute() == null) {
        if (attribute.getParent() instanceof ObjEntity) {
            DbEntity dbEnt = ((ObjEntity) attribute.getParent()).getDbEntity();
            if (dbEnt != null) {
                Collection<DbAttribute> attributes = dbEnt.getAttributes();
                Collection<DbRelationship> rel = dbEnt.getRelationships();
                if (!attributes.isEmpty()) {
                    Iterator<DbAttribute> iterator = attributes.iterator();
                    firstEntity = iterator.next().getEntity();
                } else if (!rel.isEmpty()) {
                    Iterator<DbRelationship> iterator = rel.iterator();
                    firstEntity = iterator.next().getSourceEntity();
                }
            }
        }
    } else {
        firstEntity = getFirstEntity(attribute);
    }
    if (firstEntity != null) {
        EntityTreeModel treeModel = new EntityTreeModel(firstEntity);
        treeModel.setFilter(new EntityTreeAttributeRelationshipFilter());
        return treeModel;
    }
    return null;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Entity(org.apache.cayenne.map.Entity) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) Iterator(java.util.Iterator)

Example 34 with ObjAttribute

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

the class ProjectUtil method findObjAttributesForDbRelationship.

public static Collection<ObjAttribute> findObjAttributesForDbRelationship(ProjectController mediator, DbRelationship relationship) {
    DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
    List<ObjAttribute> attributes = new ArrayList<>();
    String[] dbAttrPathByDot;
    if (domain != null) {
        for (DataMap map : domain.getDataMaps()) {
            for (ObjEntity entity : map.getObjEntities()) {
                for (ObjAttribute objAttribute : entity.getAttributes()) {
                    if (objAttribute.getDbAttributePath() != null) {
                        dbAttrPathByDot = objAttribute.getDbAttributePath().split(Pattern.quote("."));
                        for (String partOfPath : dbAttrPathByDot) {
                            if (partOfPath.equals(relationship.getName())) {
                                attributes.add(objAttribute);
                            }
                        }
                    }
                }
            }
        }
    }
    return attributes;
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ArrayList(java.util.ArrayList) DataMap(org.apache.cayenne.map.DataMap)

Example 35 with ObjAttribute

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

the class EOModelProcessorTest method testLoadModelWithDependencies.

@Test
public void testLoadModelWithDependencies() throws Exception {
    URL url = getClass().getClassLoader().getResource("wotests/cross-model-relationships.eomodeld/");
    assertNotNull(url);
    DataMap map = processor.loadEOModel(url);
    ObjEntity entity = map.getObjEntity("CrossModelRelTest");
    assertNotNull(entity);
    ObjAttribute a1 = entity.getAttribute("testAttribute");
    assertNotNull(a1);
    DbAttribute da1 = a1.getDbAttribute();
    assertNotNull(da1);
    assertSame(da1, entity.getDbEntity().getAttribute("TEST_ATTRIBUTE"));
    // for now cross model relationships are simply ignored
    // eventually we must handle those...
    assertEquals(0, entity.getRelationships().size());
    assertEquals(0, entity.getDbEntity().getRelationships().size());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

ObjAttribute (org.apache.cayenne.map.ObjAttribute)81 ObjEntity (org.apache.cayenne.map.ObjEntity)57 DbAttribute (org.apache.cayenne.map.DbAttribute)31 ObjRelationship (org.apache.cayenne.map.ObjRelationship)27 DbEntity (org.apache.cayenne.map.DbEntity)26 Test (org.junit.Test)26 DbRelationship (org.apache.cayenne.map.DbRelationship)19 DbJoin (org.apache.cayenne.map.DbJoin)12 DataMap (org.apache.cayenne.map.DataMap)9 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)9 HashMap (java.util.HashMap)8 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)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 ArrayList (java.util.ArrayList)7 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)7 HashSet (java.util.HashSet)6 Expression (org.apache.cayenne.exp.Expression)6