/** * Returns a target of this relationship. If relationship is not attached to a * DbEntity, and DbEntity doesn't have a namcespace, and exception is thrown. */ public Entity getTargetEntity() { String targetName = getTargetEntityName(); if (targetName == null) { return null; } return getNonNullNamespace().getDbEntity(targetName); }
protected String toOneRelationshipName(DbRelationship... relationshipChain) { DbRelationship first = relationshipChain[0]; DbRelationship last = relationshipChain[relationshipChain.length - 1]; List<DbJoin> joins = first.getJoins(); if (joins.isEmpty()) { // In case, when uses EditRelationship button, relationship doesn't exist => it doesn't have joins // and just return targetName return stemmed(last.getTargetEntityName()); } DbJoin join1 = joins.get(0); // TODO: multi-join relationships // return the name of the FK column sans ID String fkColName = join1.getSourceName(); if (fkColName == null) { return stemmed(last.getTargetEntityName()); } else if (fkColName.toUpperCase().endsWith("_ID") && fkColName.length() > 3) { return fkColName.substring(0, fkColName.length() - 3); } else if (fkColName.toUpperCase().endsWith("ID") && fkColName.length() > 2) { return fkColName.substring(0, fkColName.length() - 2); } else { return stemmed(last.getTargetEntityName()); } }
/** * Returns a target of this relationship. If relationship is not attached to * a DbEntity, and DbEntity doesn't have a namespace, and exception is * thrown. */ @Override public DbEntity getTargetEntity() { String targetName = getTargetEntityName(); if (targetName == null) { return null; } return getNonNullNamespace().getDbEntity(targetName); }
private DbEntity getOriginalTargetDbEntity(DbRelationship relationship) { return getOriginalDictionary().getByName(relationship.getTargetEntityName().toUpperCase()); }
protected String toManyRelationshipName(DbRelationship... relationshipChain) { DbRelationship last = relationshipChain[relationshipChain.length - 1]; String baseName = stemmed(last.getTargetEntityName()); try { // by default we use English rules here... return Noun.pluralOf(baseName.toLowerCase(), Locale.ENGLISH); } catch (Exception inflectorError) { // seems that Inflector cannot be trusted. For instance, it // throws an exception when invoked for word "ADDRESS" (although // lower case works fine). To feel safe, we use superclass' // behavior if something's gone wrong return baseName; } }
public static String getTokenValue(DbRelationship rel) { String attributes = ""; if (rel.getJoins().size() == 1) { attributes = rel.getJoins().get(0).getTargetName(); } else { for (DbJoin dbJoin : rel.getJoins()) { attributes += dbJoin.getTargetName() + COMMA_SEPARATOR; } if(attributes.isEmpty()) { attributes = "{}"; } else { attributes = "{" + attributes.substring(0, attributes.length() - COMMA_SEPARATOR_LENGTH) + "}"; } } return rel.getName() + " " + rel.getSourceEntity().getName() + "->" + rel.getTargetEntityName() + "." + attributes; }
@Override public String getTokenValue() { if (!this.isEmpty()) { return relationship.getSourceEntity().getName() + "->" + relationship.getTargetEntityName(); } else { return "Skip. No sql representation."; } }
@Override public String getTokenValue() { if(relationship.isToMany()) { return "Skip. No sql representation."; } return relationship.getSourceEntity().getName() + "->" + relationship.getTargetEntityName(); } }
private void addMissingRelationship(ObjEntity entity, DbRelationship dbRelationship) { // getting DataMap from DbRelationship's source entity. This is the only object in our arguments that // is guaranteed to be a part of the map.... DataMap dataMap = dbRelationship.getSourceEntity().getDataMap(); DbEntity targetEntity = dbRelationship.getTargetEntity(); Collection<ObjEntity> mappedObjEntities = dataMap.getMappedEntities(targetEntity); if (mappedObjEntities.isEmpty()) { if (targetEntity == null) { targetEntity = new DbEntity(dbRelationship.getTargetEntityName()); } if (dbRelationship.getTargetEntityName() != null) { boolean needGeneratedEntity = createObjRelationship(entity, dbRelationship, nameGenerator.objEntityName(targetEntity)); if (needGeneratedEntity) { LOGGER.warn("Can't find ObjEntity for " + dbRelationship.getTargetEntityName()); LOGGER.warn("Db Relationship (" + dbRelationship + ") will have GUESSED Obj Relationship reflection. "); } } } else { for (Entity mappedTarget : mappedObjEntities) { createObjRelationship(entity, dbRelationship, mappedTarget.getName()); } } }
/** * @return true if objRelationship includes given dbRelationship */ private boolean objRelationshipHasDbRelationship(ObjRelationship objRelationship, DbRelationship dbRelationship) { for(DbRelationship relationship : objRelationship.getDbRelationships()) { if(relationship.getSourceEntityName().equals(dbRelationship.getSourceEntityName()) && relationship.getTargetEntityName().equals(dbRelationship.getTargetEntityName()) && isSameAttributes(relationship.getSourceAttributes(), dbRelationship.getSourceAttributes()) && isSameAttributes(relationship.getTargetAttributes(), dbRelationship.getTargetAttributes())) { return true; } } return false; }
if (relationship != null && relationship.getName() != null && relationship.getTargetEntityName() != null) { relationship.getTargetEntityName() + "." + getJoins(relationship); if (relationship != comparisonRelationship) { String comparisonDbRelationshipPath = comparisonRelationship.getTargetEntityName() + "." + getJoins(comparisonRelationship);
while (rels.hasNext()) { DbRelationship rel = (DbRelationship) rels.next(); if (dbEntityName.equals(rel.getTargetEntityName())) { dbEnt.removeRelationship(rel.getName());
/** * Appends a DbRelationship to the existing list of DbRelationships. */ public void addDbRelationship(DbRelationship dbRel) { refreshFromDeferredPath(); if (dbRel.getName() == null) { throw new IllegalArgumentException("DbRelationship has no name"); } // Adding a second is creating a flattened relationship. // Ensure that the new relationship properly continues // on the flattened path int numDbRelationships = dbRelationships.size(); if (numDbRelationships > 0) { DbRelationship lastRel = dbRelationships.get(numDbRelationships - 1); if (!lastRel.getTargetEntityName().equals(dbRel.getSourceEntity().getName())) { throw new CayenneRuntimeException("Error adding db relationship %s to ObjRelationship %s" + " because the source of the newly added relationship" + " is not the target of the previous relationship in the chain.", dbRel, this); } } dbRelationships.add(dbRel); this.recalculateReadOnlyValue(); this.recalculateToManyValue(); }
/** Appends a DbRelationship to the existing list of DbRelationships. */ public void addDbRelationship(DbRelationship dbRel) { refreshFromPath(true); // Adding a second is creating a flattened relationship. // Ensure that the new relationship properly continues // on the flattened path int numDbRelationships = dbRelationships.size(); if (numDbRelationships > 0) { DbRelationship lastRel = (DbRelationship) dbRelationships .get(numDbRelationships - 1); if (!lastRel.getTargetEntityName().equals(dbRel.getSourceEntity().getName())) { throw new CayenneRuntimeException("Error adding db relationship " + dbRel + " to ObjRelationship " + this + " because the source of the newly added relationship " + "is not the target of the previous relationship " + "in the chain"); } } EventManager.getDefaultManager().addListener( this, "dbRelationshipDidChange", RelationshipEvent.class, DbRelationship.PROPERTY_DID_CHANGE, dbRel); dbRelationships.add(dbRel); this.calculateReadOnlyValue(); this.calculateToManyValue(); }
source.removeRelationship(relationship.getName()); relationship.setName(DbLoader.uniqueRelName(source, relationship .getTargetEntityName(), false)); source.addRelationship(relationship);
/** * Updates missing mapping artifacts that can be guessed from other mapping * information. This implementation creates missing reverse relationships, * marking newly created relationships as "runtime". * * @since 3.0 */ public void applyDBLayerDefaults() { // connect DB layer for (DataMap map : getDataMaps()) { for (DbEntity entity : map.getDbEntities()) { // iterate by copy to avoid concurrency modification errors on // reflexive relationships DbRelationship[] relationships = entity.getRelationships().toArray( new DbRelationship[entity.getRelationships().size()]); for (DbRelationship relationship : relationships) { if (relationship.getReverseRelationship() == null) { DbRelationship reverse = relationship.createReverseRelationship(); Entity targetEntity = reverse.getSourceEntity(); reverse.setName(getUniqueRelationshipName(targetEntity)); reverse.setRuntime(true); targetEntity.addRelationship(reverse); logger.info("added runtime complimentary DbRelationship from " + targetEntity.getName() + " to " + reverse.getTargetEntityName()); } } } } }
private void initWithModel() { // init UI components sourceName.setText(relationship.getSourceEntityName()); targetName.setText(relationship.getTargetEntityName()); name.setText(relationship.getName()); if (reverseRelationship != null) { reverseName.setText(reverseRelationship.getName()); } table.setModel(new DbJoinTableModel(relationship, getMediator(), this, true)); TableColumn sourceColumn = table.getColumnModel().getColumn(DbJoinTableModel.SOURCE); JComboBox comboBox = Application.getWidgetFactory().createComboBox( ModelerUtil.getDbAttributeNames(getMediator(), relationship.getSourceEntity()), true); AutoCompletion.enable(comboBox); sourceColumn.setCellEditor(Application.getWidgetFactory().createCellEditor(comboBox)); TableColumn targetColumn = table.getColumnModel().getColumn(DbJoinTableModel.TARGET); comboBox = Application.getWidgetFactory().createComboBox( ModelerUtil.getDbAttributeNames(getMediator(), relationship.getTargetEntity()), true); AutoCompletion.enable(comboBox); targetColumn.setCellEditor(Application.getWidgetFactory().createCellEditor(comboBox)); tablePreferences.bind(table, null, null, null, DbJoinTableModel.SOURCE, true); }
/** * Prints itself as XML to the provided XMLEncoder. * * @since 1.1 */ public void encodeAsXML(XMLEncoder encoder) { encoder.print("<db-relationship name=\""); encoder.print(Util.encodeXmlAttribute(getName())); encoder.print("\" source=\""); encoder.print(getSourceEntity().getName()); if (getTargetEntityName() != null && getTargetEntity() != null) { encoder.print("\" target=\""); encoder.print(getTargetEntityName()); } if (isToDependentPK() && isValidForDepPk()) { encoder.print("\" toDependentPK=\"true"); } encoder.print("\" toMany=\""); encoder.print(isToMany()); encoder.println("\">"); encoder.indent(1); encoder.print(getJoins()); encoder.indent(-1); encoder.println("</db-relationship>"); }
/** * Prints itself as XML to the provided XMLEncoder. * * @since 1.1 */ public void encodeAsXML(XMLEncoder encoder) { encoder.print("<db-relationship name=\""); encoder.print(Util.encodeXmlAttribute(getName())); encoder.print("\" source=\""); encoder.print(Util.encodeXmlAttribute(getSourceEntity().getName())); if (getTargetEntityName() != null && getTargetEntity() != null) { encoder.print("\" target=\""); encoder.print(Util.encodeXmlAttribute(getTargetEntityName())); } if (isToDependentPK() && isValidForDepPk()) { encoder.print("\" toDependentPK=\"true"); } encoder.print("\" toMany=\""); encoder.print(isToMany()); encoder.println("\">"); encoder.indent(1); encoder.print(getJoins()); encoder.indent(-1); encoder.println("</db-relationship>"); }
if (renamedDbEntities.containsKey(rel.getTargetEntityName())) { rel.setTargetEntityName(renamedDbEntities.get(rel .getTargetEntityName()));