Search in sources :

Example 11 with ObjRelationship

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

the class DataMapUtils method getParameterNames.

private Map<String, String> getParameterNames(Expression expression, Object root) {
    if (expression != null) {
        Map<String, String> types = new HashMap<>();
        String typeName = "";
        List<String> names = new LinkedList<>();
        for (int i = 0; i < expression.getOperandCount(); i++) {
            Object operand = expression.getOperand(i);
            if (operand instanceof Expression) {
                types.putAll(getParameterNames((Expression) operand, root));
            }
            if (operand instanceof ASTObjPath) {
                PathComponent<ObjAttribute, ObjRelationship> component = ((Entity) root).lastPathComponent((ASTObjPath) operand, null);
                ObjAttribute attribute = component.getAttribute();
                if (attribute != null) {
                    typeName = attribute.getType();
                } else {
                    ObjRelationship relationship = component.getRelationship();
                    if (relationship != null) {
                        typeName = relationship.getTargetEntity().getClassName();
                    } else {
                        typeName = "Object";
                    }
                }
            }
            if (operand instanceof ASTList) {
                Object[] values = (Object[]) ((ASTList) operand).getOperand(0);
                for (Object value : values) {
                    if (value instanceof ExpressionParameter) {
                        names.add(((ExpressionParameter) value).getName());
                    }
                }
            }
            if (operand instanceof ExpressionParameter) {
                names.add(((ExpressionParameter) operand).getName());
            }
        }
        for (String name : names) {
            types.put(Util.underscoredToJava(name, false), typeName);
        }
        return types;
    }
    return Collections.emptyMap();
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Expression(org.apache.cayenne.exp.Expression) ExpressionParameter(org.apache.cayenne.exp.ExpressionParameter) ASTList(org.apache.cayenne.exp.parser.ASTList)

Example 12 with ObjRelationship

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

the class ClassGenerationActionTest method testExecuteArtifactPairsMapRelationships.

@Test
public void testExecuteArtifactPairsMapRelationships() throws Exception {
    ObjEntity testEntity1 = new ObjEntity("TE1");
    testEntity1.setClassName("org.example.TestClass1");
    final ObjEntity testEntity2 = new ObjEntity("TE1");
    testEntity2.setClassName("org.example.TestClass2");
    ObjRelationship relationship = new ObjRelationship("xMap") {

        private static final long serialVersionUID = 8042147877503405974L;

        @Override
        public boolean isToMany() {
            return true;
        }

        @Override
        public ObjEntity getTargetEntity() {
            return testEntity2;
        }
    };
    relationship.setCollectionType("java.util.Map");
    testEntity1.addRelationship(relationship);
    action.setMakePairs(true);
    List<String> generated = execute(new EntityArtifact(testEntity1));
    assertNotNull(generated);
    assertEquals(2, generated.size());
    String superclass = generated.get(0);
    assertTrue(superclass, superclass.contains("import java.util.Map;"));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) Test(org.junit.Test)

Example 13 with ObjRelationship

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

the class SingleClassGenerationTest method testContainsPropertyImportForRelationships.

@Test
public void testContainsPropertyImportForRelationships() throws Exception {
    ObjEntity objEntity = new ObjEntity("TEST1");
    ObjRelationship rel = new ObjRelationship("rel");
    objEntity.addRelationship(rel);
    VelocityContext context = new VelocityContext();
    context.put(Artifact.OBJECT_KEY, objEntity);
    String res = renderTemplate(ClassGenerationAction.SINGLE_CLASS_TEMPLATE, context);
    assertTrue(res.contains("org.apache.cayenne.exp.Property"));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) VelocityContext(org.apache.velocity.VelocityContext) Test(org.junit.Test)

Example 14 with ObjRelationship

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

the class SuperClassGenerationTest method testContainsPropertyImport.

@Test
public void testContainsPropertyImport() throws Exception {
    ObjEntity objEntity = new ObjEntity("TEST1");
    ObjAttribute attr = new ObjAttribute("attr");
    ObjRelationship rel = new ObjRelationship("rel");
    objEntity.addAttribute(attr);
    objEntity.addRelationship(rel);
    VelocityContext context = new VelocityContext();
    context.put(Artifact.OBJECT_KEY, objEntity);
    String res = renderTemplate(ClassGenerationAction.SUPERCLASS_TEMPLATE, context);
    assertTrue(res.contains("org.apache.cayenne.exp.Property"));
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) VelocityContext(org.apache.velocity.VelocityContext) Test(org.junit.Test)

Example 15 with ObjRelationship

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

the class ObjRelationshipValidator method checkForDuplicates.

/**
 * Per CAY-1813, make sure two (or more) ObjRelationships do not map to the
 * same database path.
 */
private void checkForDuplicates(ObjRelationship relationship, ValidationResult validationResult) {
    if (relationship != null && relationship.getName() != null && relationship.getTargetEntityName() != null) {
        String dbRelationshipPath = relationship.getTargetEntityName() + "." + relationship.getDbRelationshipPath();
        ObjEntity entity = relationship.getSourceEntity();
        for (ObjRelationship comparisonRelationship : entity.getRelationships()) {
            if (relationship != comparisonRelationship) {
                String comparisonDbRelationshipPath = comparisonRelationship.getTargetEntityName() + "." + comparisonRelationship.getDbRelationshipPath();
                if (dbRelationshipPath.equals(comparisonDbRelationshipPath)) {
                    addFailure(validationResult, relationship, "ObjEntity '%s' contains a duplicate ObjRelationship mapping ('%s' -> '%s')", entity.getName(), relationship.getName(), dbRelationshipPath);
                    // Duplicate found, stop.
                    return;
                }
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship)

Aggregations

ObjRelationship (org.apache.cayenne.map.ObjRelationship)84 ObjEntity (org.apache.cayenne.map.ObjEntity)48 ObjAttribute (org.apache.cayenne.map.ObjAttribute)27 DbRelationship (org.apache.cayenne.map.DbRelationship)26 Test (org.junit.Test)24 DbEntity (org.apache.cayenne.map.DbEntity)18 DbAttribute (org.apache.cayenne.map.DbAttribute)15 DbJoin (org.apache.cayenne.map.DbJoin)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)12 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ObjectId (org.apache.cayenne.ObjectId)10 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)9 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)9 ArrayList (java.util.ArrayList)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 List (java.util.List)7 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)7 DataMap (org.apache.cayenne.map.DataMap)7 HashMap (java.util.HashMap)6