public boolean isToMany() { refreshFromPath(true); return super.isToMany(); }
private Entity entityForNonLeafNode(Object node) { if (node instanceof Entity) { return (Entity) node; } else if (node instanceof Relationship) { return ((Relationship) node).getTargetEntity(); } String className = (node != null) ? node.getClass().getName() : "null"; throw new IllegalArgumentException("Unexpected non-leaf node: " + className); }
/** * Returns label for relationship on the graph, considering its "mandatory" * and "to-many" properties */ private static String getRelationshipLabel(Relationship rel) { if (rel == null) { return null; } if (rel.isToMany()) { return "0..*"; } return rel.isMandatory() ? "1" : "0..1"; }
/** * Returns qualified name (entity name + relationship name) for a * relationship */ static String getQualifiedName(Relationship rel) { return rel.getSourceEntity().getName() + "." + rel.getName(); }
/** * Updates relationship labels for specified relationship edge. */ protected void updateRelationshipLabels(DefaultEdge edge, Relationship rel, Relationship reverse) { DefaultGraphCell sourceCell = entityCells.get(rel.getSourceEntity().getName()); DefaultGraphCell targetCell = entityCells.get(rel.getTargetEntity().getName()); edge.setSource(sourceCell != null ? sourceCell.getChildAt(0) : null); edge.setTarget(targetCell != null ? targetCell.getChildAt(0) : null); Object[] labels = { rel.getName() + " " + getRelationshipLabel(rel), reverse == null ? "" : reverse.getName() + " " + getRelationshipLabel(reverse) }; GraphConstants.setExtraLabels(edge.getAttributes(), labels); Point2D[] labelPositions = { new Point2D.Double(GraphConstants.PERMILLE * (0.1 + 0.2 * Math.random()), 10), new Point2D.Double(GraphConstants.PERMILLE * (0.9 - 0.2 * Math.random()), -10) }; GraphConstants.setExtraLabelPositions(edge.getAttributes(), labelPositions); }
protected void updateRelationshipCell(Relationship rel) { if (rel.getSourceEntity() != null && rel.getTargetEntity() != null) { DefaultEdge edge = relCells.get(getQualifiedName(rel)); if (edge != null) { updateRelationshipLabels(edge, rel, rel.getReverseRelationship()); Map nested = new HashMap(); nested.put(edge, edge.getAttributes()); edit(nested); } else { insertRelationshipCell(rel); } } }
/** * Post (i.e. after creation on entity cell) process of the entity */ protected void postProcessEntity(Entity entity, DefaultGraphCell cell) { for (Relationship rel : entity.getRelationships()) { if (rel.getSourceEntity() != null && rel.getTargetEntity() != null) { DefaultEdge edge = createRelationshipCell(rel); if (edge != null) { createdObjects.add(edge); } } } }
@Override public ObjEntity getSourceEntity() { return (ObjEntity) super.getSourceEntity(); }
/** Changes the name of the attribute in all places in DataMap. */ public static void setRelationshipName(Entity entity, Relationship rel, String newName) { if (rel == null || rel != entity.getRelationship(rel.getName())) { return; } entity.removeRelationship(rel.getName()); rel.setName(newName); entity.addRelationship(rel); }
/** * Removes ObjEntity from the DataMap. If <code>clearDependencies</code> is * true, all ObjRelationships that reference this entity are also removed. * * @since 1.1 */ public void removeObjEntity(String objEntityName, boolean clearDependencies) { ObjEntity entity = objEntityMap.remove(objEntityName); if (entity != null && clearDependencies) { // remove relationships that point to this entity for (ObjEntity ent : getObjEntities()) { // take a copy since we're going to modify the entity for (Relationship relationship : new ArrayList<>(ent.getRelationships())) { if (objEntityName.equals(relationship.getTargetEntityName()) || objEntityName.equals(relationship.getTargetEntityName())) { ent.removeRelationship(relationship.getName()); } } } MappingNamespace ns = getNamespace(); if (ns instanceof EntityResolver) { ((EntityResolver) ns).refreshMappingCache(); } } }
/** * Adds new relationship to the entity. */ public void addRelationship(Relationship relationship) { if (relationship.getName() == null) { throw new IllegalArgumentException("Attempt to insert unnamed relationship."); } // block overrides // TODO: change method signature to return replaced attribute and make sure the // Modeler handles it... Object existingRelationship = relationships.get(relationship.getName()); if (existingRelationship != null) { if (existingRelationship == relationship) { return; } else { throw new IllegalArgumentException( "An attempt to override relationship '" + relationship.getName() + "'"); } } // Check that there aren't any attributes with the same name as the given // relationship. Object existingAttribute = attributes.get(relationship.getName()); if (existingAttribute != null) { throw new IllegalArgumentException( "Relationship name conflict with existing attribute '" + relationship.getName() + "'"); } relationships.put(relationship.getName(), relationship); relationship.setSourceEntity(this); }
@Override public String getNewName() { return (relationship != null) ? relationship.getName() : null; } }
/** * Creates a named relationship. */ public Relationship(String name) { setName(name); }
/** * Sets relationship target entity. Internally calls <code>setTargetEntityName</code>. */ public void setTargetEntityName(Entity targetEntity) { if (targetEntity != null) { setTargetEntityName(targetEntity.getName()); } else { setTargetEntityName((String) null); } }
public void setParent(Object parent) { if (parent != null && !(parent instanceof Entity)) { throw new IllegalArgumentException("Expected null or Entity, got: " + parent); } setSourceEntity((Entity) parent); }
public RemoveUndoableEdit(DataMap map, ObjEntity objEntity) { this.map = map; this.objEntity = objEntity; this.mode = REMOVE_MODE.OBJECT_ENTITY; for (ObjEntity ent : map.getObjEntities()) { // take a copy since we're going to modify the entity for (Relationship relationship : new ArrayList<Relationship>(ent .getRelationships())) { if (this.objEntity.getName().equals(relationship.getTargetEntityName())) { ObjRelationship rel = (ObjRelationship) relationship; if (objRelationshipMap.get(rel.getSourceEntity()) == null) { objRelationshipMap.put( (ObjEntity) rel.getSourceEntity(), new LinkedList<ObjRelationship>()); } objRelationshipMap.get(rel.getSourceEntity()).add(rel); } } } }
/** * Returns guaranteed non-null MappingNamespace of this relationship. If it happens to * be null, and exception is thrown. This method is intended for internal use by * Relationship class. */ final MappingNamespace getNonNullNamespace() { Entity entity = getSourceEntity(); if (entity == null) { throw new CayenneRuntimeException("Relationship '%s' has no parent Entity.", getName()); } return entity.getNonNullNamespace(); }
@Override public DbEntity getSourceEntity() { return (DbEntity) super.getSourceEntity(); }
if (dbEntityName.equals(rel.getTargetEntityName())) { dbEnt.removeRelationship(rel.getName());