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();
}
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;"));
}
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"));
}
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"));
}
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;
}
}
}
}
}
Aggregations