1 19 20 package org.apache.cayenne.project.validator; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.apache.cayenne.map.DbEntity; 26 import org.apache.cayenne.map.DbJoin; 27 import org.apache.cayenne.map.DbRelationship; 28 import org.apache.cayenne.map.DeleteRule; 29 import org.apache.cayenne.map.ObjEntity; 30 import org.apache.cayenne.map.ObjRelationship; 31 import org.apache.cayenne.project.ProjectPath; 32 import org.apache.cayenne.util.Util; 33 34 37 public class ObjRelationshipValidator extends TreeNodeValidator { 38 39 42 public ObjRelationshipValidator() { 43 super(); 44 } 45 46 public void validateObject(ProjectPath path, Validator validator) { 47 ObjRelationship rel = (ObjRelationship) path.getObject(); 48 49 if (path.getObjectParent() != null 51 && path.getObjectParent() != rel.getSourceEntity()) { 52 return; 53 } 54 55 if (Util.isEmptyString(rel.getName())) { 56 validator.registerError("Unnamed ObjRelationship.", path); 57 } 58 else if (rel.getSourceEntity().getAttribute(rel.getName()) != null) { 60 validator.registerWarning("ObjRelationship " 61 + objRelationshipIdentifier(rel) 62 + " has the same name as one of ObjAttributes", path); 63 } 64 else { 65 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 66 String invalidChars = helper.invalidCharsInObjPathComponent(rel.getName()); 67 68 if (invalidChars != null) { 69 validator.registerWarning("ObjRelationship " 70 + objRelationshipIdentifier(rel) 71 + " name contains invalid characters: " 72 + invalidChars, path); 73 } 74 else if (helper.invalidDataObjectProperty(rel.getName())) { 75 validator.registerWarning("ObjRelationship " 76 + objRelationshipIdentifier(rel) 77 + " name is invalid.", path); 78 } 79 } 80 81 if (rel.getTargetEntity() == null) { 82 validator.registerWarning("ObjRelationship " 83 + objRelationshipIdentifier(rel) 84 + " has no target entity.", path); 85 } 86 else { 87 List dbRels = rel.getDbRelationships(); 89 if (dbRels.size() == 0) { 90 validator.registerWarning("ObjRelationship " 91 + objRelationshipIdentifier(rel) 92 + " has no DbRelationship mapping.", path); 93 } 94 else { 95 DbEntity expectedSrc = ((ObjEntity) rel.getSourceEntity()).getDbEntity(); 96 DbEntity expectedTarget = ((ObjEntity) rel.getTargetEntity()) 97 .getDbEntity(); 98 99 if (((DbRelationship) dbRels.get(0)).getSourceEntity() != expectedSrc 100 || ((DbRelationship) dbRels.get(dbRels.size() - 1)) 101 .getTargetEntity() != expectedTarget) { 102 validator.registerWarning("ObjRelationship " 103 + objRelationshipIdentifier(rel) 104 + " has incomplete DbRelationship mapping.", path); 105 } 106 } 107 } 108 109 if (rel.isToMany() 112 && !rel.isFlattened() 113 && (rel.getDeleteRule() == DeleteRule.NULLIFY)) { 114 ObjRelationship inverse = rel.getReverseRelationship(); 115 if (inverse != null) { 116 DbRelationship firstRel = (DbRelationship) inverse 117 .getDbRelationships() 118 .get(0); 119 Iterator attributePairIterator = firstRel.getJoins().iterator(); 120 boolean check = true; 122 while (attributePairIterator.hasNext()) { 123 DbJoin pair = (DbJoin) attributePairIterator.next(); 124 if (!pair.getSource().isMandatory()) { 125 check = false; 127 break; 128 } 129 } 130 131 if (check) { 132 validator 133 .registerWarning( 134 "ObjRelationship " 135 + objRelationshipIdentifier(rel) 136 + " has a Nullify delete rule and a mandatory reverse relationship ", 137 path); 138 } 139 } 140 } 141 } 142 143 public String objRelationshipIdentifier(ObjRelationship rel) { 144 if (null == rel.getSourceEntity()) { 145 return "<[null source entity]." + rel.getName() + ">"; 146 } 147 return "<" + rel.getSourceEntity().getName() + "." + rel.getName() + ">"; 148 } 149 } 150 | Popular Tags |