use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class ProjectUtil method findObjRelationshipsForDbRelationship.
public static Collection<ObjRelationship> findObjRelationshipsForDbRelationship(ProjectController mediator, DbRelationship relationship) {
DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();
List<ObjRelationship> objRelationships = new ArrayList<>();
if (domain != null) {
for (DataMap map : domain.getDataMaps()) {
for (ObjEntity entity : map.getObjEntities()) {
for (ObjRelationship objRelationship : entity.getRelationships()) {
if (objRelationship.getDbRelationships().contains(relationship)) {
objRelationships.add(objRelationship);
}
}
}
}
}
return objRelationships;
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EOModelProcessor method makeRelationships.
/**
* Create ObjRelationships of the specified entity, as well as
* DbRelationships of the corresponding DbEntity.
*/
protected void makeRelationships(EOModelHelper helper, ObjEntity objEntity) {
Map entityPlistMap = helper.entityPListMap(objEntity.getName());
List classProps = (List) entityPlistMap.get("classProperties");
List rinfo = (List) entityPlistMap.get("relationships");
Collection attributes = (Collection) entityPlistMap.get("attributes");
if (rinfo == null) {
return;
}
if (classProps == null) {
classProps = Collections.EMPTY_LIST;
}
if (attributes == null) {
attributes = Collections.EMPTY_LIST;
}
DbEntity dbSrc = objEntity.getDbEntity();
Iterator it = rinfo.iterator();
while (it.hasNext()) {
Map relMap = (Map) it.next();
String targetName = (String) relMap.get("destination");
// ignore flattened relationships for now
if (targetName == null) {
continue;
}
String relName = (String) relMap.get("name");
boolean toMany = "Y".equals(relMap.get("isToMany"));
boolean toDependentPK = "Y".equals(relMap.get("propagatesPrimaryKey"));
ObjEntity target = helper.getDataMap().getObjEntity(targetName);
// ignoring those now.
if (target == null) {
continue;
}
DbEntity dbTarget = target.getDbEntity();
Map targetPlistMap = helper.entityPListMap(targetName);
Collection targetAttributes = (Collection) targetPlistMap.get("attributes");
DbRelationship dbRel = null;
// Note: source maybe null, e.g. an abstract entity.
if (dbSrc != null && dbTarget != null) {
// in case of inheritance EOF stores duplicates of all inherited
// relationships, so we must skip this relationship in DB entity
// if it is
// already there...
dbRel = dbSrc.getRelationship(relName);
if (dbRel == null) {
dbRel = new DbRelationship();
dbRel.setSourceEntity(dbSrc);
dbRel.setTargetEntityName(dbTarget);
dbRel.setToMany(toMany);
dbRel.setName(relName);
dbRel.setToDependentPK(toDependentPK);
dbSrc.addRelationship(dbRel);
List joins = (List) relMap.get("joins");
Iterator jIt = joins.iterator();
while (jIt.hasNext()) {
Map joinMap = (Map) jIt.next();
DbJoin join = new DbJoin(dbRel);
// find source attribute dictionary and extract the
// column name
String sourceAttributeName = (String) joinMap.get("sourceAttribute");
join.setSourceName(columnName(attributes, sourceAttributeName));
String targetAttributeName = (String) joinMap.get("destinationAttribute");
join.setTargetName(columnName(targetAttributes, targetAttributeName));
dbRel.addJoin(join);
}
}
}
// only create obj relationship if it is a class property
if (classProps.contains(relName)) {
ObjRelationship rel = new ObjRelationship();
rel.setName(relName);
rel.setSourceEntity(objEntity);
rel.setTargetEntityName(target);
objEntity.addRelationship(rel);
if (dbRel != null) {
rel.addDbRelationship(dbRel);
}
}
}
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EOModelProcessor method makeFlatRelationships.
/**
* Create Flattened ObjRelationships of the specified entity.
*/
protected void makeFlatRelationships(EOModelHelper helper, ObjEntity e) {
Map info = helper.entityPListMap(e.getName());
List rinfo = (List) info.get("relationships");
if (rinfo == null) {
return;
}
Iterator it = rinfo.iterator();
while (it.hasNext()) {
Map relMap = (Map) it.next();
String targetPath = (String) relMap.get("definition");
// ignore normal relationships
if (targetPath == null) {
continue;
}
ObjRelationship flatRel = new ObjRelationship();
flatRel.setName((String) relMap.get("name"));
flatRel.setSourceEntity(e);
try {
flatRel.setDbRelationshipPath(targetPath);
} catch (ExpressionException ex) {
logger.warn("Invalid relationship: " + targetPath);
continue;
}
// find target entity
Map entityInfo = info;
StringTokenizer toks = new StringTokenizer(targetPath, ".");
while (toks.hasMoreTokens() && entityInfo != null) {
String pathComponent = toks.nextToken();
// get relationship info and reset entityInfo, so that we could
// use
// entityInfo state as an indicator of valid flat relationship
// enpoint
// outside the loop
Collection relationshipInfo = (Collection) entityInfo.get("relationships");
entityInfo = null;
if (relationshipInfo == null) {
break;
}
Iterator rit = relationshipInfo.iterator();
while (rit.hasNext()) {
Map pathRelationship = (Map) rit.next();
if (pathComponent.equals(pathRelationship.get("name"))) {
String targetName = (String) pathRelationship.get("destination");
entityInfo = helper.entityPListMap(targetName);
break;
}
}
}
if (entityInfo != null) {
flatRel.setTargetEntityName((String) entityInfo.get("name"));
}
e.addRelationship(flatRel);
}
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EOModelProcessorInheritanceTest method testLoadFlattenedRelationship.
@Test
public void testLoadFlattenedRelationship() throws Exception {
DataMap map = processor.loadEOModel(url);
ObjEntity e1 = map.getObjEntity("HelperFlatEntity");
assertNotNull(e1);
ObjRelationship fr = (ObjRelationship) e1.getRelationship("singleTables");
assertNotNull(fr);
assertEquals("singleTableJoins.toSingleTable", fr.getDbRelationshipPath());
assertEquals("SingleTableConcreteEntityOne", fr.getTargetEntityName());
}
use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.
the class EOModelProcessorTest method assertLoaded.
protected void assertLoaded(String mapName, DataMap map) throws Exception {
assertNotNull(map);
assertEquals(mapName, map.getName());
// check obj entities
ObjEntity artistE = map.getObjEntity("Artist");
assertNotNull(artistE);
assertEquals("Artist", artistE.getName());
// check Db entities
DbEntity artistDE = map.getDbEntity("ARTIST");
DbEntity artistDE1 = artistE.getDbEntity();
assertSame(artistDE, artistDE1);
// check attributes
ObjAttribute a1 = artistE.getAttribute("artistName");
assertNotNull(a1);
DbAttribute da1 = a1.getDbAttribute();
assertNotNull(da1);
assertSame(da1, artistDE.getAttribute("ARTIST_NAME"));
// check ObjRelationships
ObjRelationship rel = artistE.getRelationship("artistExhibitArray");
assertNotNull(rel);
assertEquals(1, rel.getDbRelationships().size());
// check DbRelationships
DbRelationship drel = artistDE.getRelationship("artistExhibitArray");
assertNotNull(drel);
assertSame(drel, rel.getDbRelationships().get(0));
// flattened relationships
ObjRelationship frel = artistE.getRelationship("exhibitArray");
assertNotNull(frel);
assertEquals(2, frel.getDbRelationships().size());
// storing data map may uncover some inconsistencies
PrintWriter mockupWriter = new NullPrintWriter();
map.encodeAsXML(new XMLEncoder(mockupWriter), new EmptyConfigurationNodeVisitor());
}
Aggregations