1 56 package org.objectstyle.cayenne.project.validator; 57 58 import java.util.Iterator ; 59 60 import org.objectstyle.cayenne.map.DbJoin; 61 import org.objectstyle.cayenne.map.DbRelationship; 62 import org.objectstyle.cayenne.project.ProjectPath; 63 import org.objectstyle.cayenne.util.Util; 64 65 68 public class DbRelationshipValidator extends TreeNodeValidator { 69 70 73 public DbRelationshipValidator() { 74 super(); 75 } 76 77 public void validateObject(ProjectPath path, Validator validator) { 78 DbRelationship rel = (DbRelationship) path.getObject(); 79 if (rel.getTargetEntity() == null) { 80 validator.registerWarning("DbRelationship " + dbRelationshipIdentifier(rel) + " has no target entity.", path); 81 } 82 else if (rel.getJoins().size() == 0) { 83 validator.registerWarning("DbRelationship " + dbRelationshipIdentifier(rel) + " has no joins.", path); 84 } 85 else { 86 Iterator joins = rel.getJoins().iterator(); 88 while (joins.hasNext()) { 89 DbJoin join = (DbJoin) joins.next(); 90 if (join.getSource() == null && join.getTarget() == null) { 91 validator 92 .registerWarning( 93 "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no source and target attributes selected.", 94 path); 95 } 96 else if (join.getSource() == null) { 97 validator.registerWarning( 98 "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no source attribute selected.", 99 path); 100 } 101 else if (join.getTarget() == null) { 102 validator.registerWarning( 103 "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no target attribute selected.", 104 path); 105 } 106 } 107 108 if (rel.getReverseRelationship() == null) { 110 validator 111 .registerWarning( 112 "Missing reverse DbRelationship " + dbRelationshipIdentifier(rel) + " (currently required by Cayenne).", 113 path); 114 } 115 } 116 117 if (Util.isEmptyString(rel.getName())) { 118 validator.registerError("Unnamed DbRelationship.", path); 119 } 120 else if (rel.getSourceEntity().getAttribute(rel.getName()) != null) { 122 validator.registerWarning( 123 "DbRelationship " + dbRelationshipIdentifier(rel) + " has the same name as one of DbAttributes", 124 path); 125 } 126 else { 127 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 128 String invalidChars = helper.invalidCharsInDbPathComponent(rel.getName()); 129 130 if (invalidChars != null) { 131 validator 132 .registerWarning( 133 "DbRelationship " + dbRelationshipIdentifier(rel) + " name contains invalid characters: " 134 + invalidChars, 135 path); 136 } 137 } 138 } 139 140 public String dbRelationshipIdentifier(DbRelationship rel) 141 { 142 if (null == rel.getSourceEntity()) 143 { 144 return "<[null source entity]." + rel.getName() + ">"; 145 } 146 return "<" + rel.getSourceEntity().getName() + "." + rel.getName() + ">"; 147 } 148 } | Popular Tags |