1 19 20 package org.apache.cayenne.project.validator; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.map.DbJoin; 25 import org.apache.cayenne.map.DbRelationship; 26 import org.apache.cayenne.project.ProjectPath; 27 import org.apache.cayenne.util.Util; 28 29 32 public class DbRelationshipValidator extends TreeNodeValidator { 33 34 37 public DbRelationshipValidator() { 38 super(); 39 } 40 41 public void validateObject(ProjectPath path, Validator validator) { 42 DbRelationship rel = (DbRelationship) path.getObject(); 43 if (rel.getTargetEntity() == null) { 44 validator.registerWarning("DbRelationship " + dbRelationshipIdentifier(rel) + " has no target entity.", path); 45 } 46 else if (rel.getJoins().size() == 0) { 47 validator.registerWarning("DbRelationship " + dbRelationshipIdentifier(rel) + " has no joins.", path); 48 } 49 else { 50 Iterator joins = rel.getJoins().iterator(); 52 while (joins.hasNext()) { 53 DbJoin join = (DbJoin) joins.next(); 54 if (join.getSource() == null && join.getTarget() == null) { 55 validator 56 .registerWarning( 57 "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no source and target attributes selected.", 58 path); 59 } 60 else if (join.getSource() == null) { 61 validator.registerWarning( 62 "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no source attribute selected.", 63 path); 64 } 65 else if (join.getTarget() == null) { 66 validator.registerWarning( 67 "DbRelationship " + dbRelationshipIdentifier(rel) + " join has no target attribute selected.", 68 path); 69 } 70 } 71 72 if (rel.getReverseRelationship() == null) { 74 validator 75 .registerWarning( 76 "Missing reverse DbRelationship " + dbRelationshipIdentifier(rel) + " (currently required by Cayenne).", 77 path); 78 } 79 } 80 81 if (Util.isEmptyString(rel.getName())) { 82 validator.registerError("Unnamed DbRelationship.", path); 83 } 84 else if (rel.getSourceEntity().getAttribute(rel.getName()) != null) { 86 validator.registerError( 87 "DbRelationship " + dbRelationshipIdentifier(rel) + " has the same name as one of DbAttributes", 88 path); 89 } 90 else { 91 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 92 String invalidChars = helper.invalidCharsInDbPathComponent(rel.getName()); 93 94 if (invalidChars != null) { 95 validator 96 .registerWarning( 97 "DbRelationship " + dbRelationshipIdentifier(rel) + " name contains invalid characters: " 98 + invalidChars, 99 path); 100 } 101 } 102 } 103 104 public String dbRelationshipIdentifier(DbRelationship rel) 105 { 106 if (null == rel.getSourceEntity()) 107 { 108 return "<[null source entity]." + rel.getName() + ">"; 109 } 110 return "<" + rel.getSourceEntity().getName() + "." + rel.getName() + ">"; 111 } 112 } 113 | Popular Tags |