use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.
the class DbRelationshipPathComboBoxEditor method enterPressed.
@Override
protected void enterPressed(JTable table) {
String dbRelationshipPath = ((JTextComponent) (comboBoxPathChooser).getEditor().getEditorComponent()).getText();
changeObjEntity(dbRelationshipPath);
Object currentNode = getCurrentNode(dbRelationshipPath);
String[] pathStrings = dbRelationshipPath.split(Pattern.quote("."));
String lastStringInPath = pathStrings[pathStrings.length - 1];
if (lastStringInPath.equals(ModelerUtil.getObjectName(currentNode)) && currentNode instanceof DbRelationship) {
if (enterPressedCount == 1) {
// it is second time enter pressed.. so we will save input data
enterPressedCount = 0;
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
if (dbRelationshipPath.equals(savePath)) {
return;
}
// we need object target to save it in model
DbEntity lastEntity = ((DbRelationship) currentNode).getTargetEntity();
Collection<ObjEntity> objEntities = ((DbRelationship) currentNode).getTargetEntity().getDataMap().getMappedEntities(lastEntity);
ObjEntity objectTarget = objEntities.isEmpty() ? null : objEntities.iterator().next();
model.getRelationship(row).setTargetEntityName(objectTarget);
model.setUpdatedValueAt(dbRelationshipPath, row, REL_TARGET_PATH_COLUMN);
model.getRelationship(row).setDbRelationshipPath(dbRelationshipPath);
model.getRelationship(row).setMapKey(null);
}
table.repaint();
}
enterPressedCount = 1;
}
}
use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.
the class ProjectUtil method getRelationshipsUsingAttributeAsTarget.
/**
* Returns a collection of DbRelationships that use this attribute as a source.
*/
public static Collection<DbRelationship> getRelationshipsUsingAttributeAsTarget(DbAttribute attribute) {
Entity parent = attribute.getEntity();
if (parent == null) {
return Collections.EMPTY_LIST;
}
DataMap map = parent.getDataMap();
if (map == null) {
return Collections.EMPTY_LIST;
}
Collection<DbRelationship> relationships = new ArrayList<DbRelationship>();
for (Entity entity : map.getDbEntities()) {
if (entity == parent) {
continue;
}
Collection<DbRelationship> entityRelationships = (Collection<DbRelationship>) entity.getRelationships();
for (DbRelationship relationship : entityRelationships) {
if (ProjectUtil.containsTargetAttribute(relationship, attribute)) {
relationships.add(relationship);
}
}
}
return relationships;
}
use of org.apache.cayenne.map.DbRelationship 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.DbRelationship in project cayenne by apache.
the class EOModelProcessor method makeReverseDbRelationships.
/**
* Create reverse DbRelationships that were not created so far, since
* Cayenne requires them.
*
* @since 1.0.5
*/
protected void makeReverseDbRelationships(DbEntity dbEntity) {
if (dbEntity == null) {
throw new NullPointerException("Attempt to create reverse relationships for the null DbEntity.");
}
for (DbRelationship relationship : new ArrayList<>(dbEntity.getRelationships())) {
if (relationship.getReverseRelationship() == null) {
DbRelationship reverse = relationship.createReverseRelationship();
reverse.setName(NameBuilder.builder(reverse, reverse.getSourceEntity()).baseName(relationship.getName() + "Reverse").name());
relationship.getTargetEntity().addRelationship(reverse);
}
}
}
use of org.apache.cayenne.map.DbRelationship 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