1 56 package org.objectstyle.cayenne.project.validator; 57 58 import java.util.Iterator ; 59 import java.util.List ; 60 61 import org.objectstyle.cayenne.map.DbEntity; 62 import org.objectstyle.cayenne.map.DbJoin; 63 import org.objectstyle.cayenne.map.DbRelationship; 64 import org.objectstyle.cayenne.map.DeleteRule; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 import org.objectstyle.cayenne.map.ObjRelationship; 67 import org.objectstyle.cayenne.project.ProjectPath; 68 import org.objectstyle.cayenne.util.Util; 69 70 73 public class ObjRelationshipValidator extends TreeNodeValidator { 74 75 78 public ObjRelationshipValidator() { 79 super(); 80 } 81 82 public void validateObject(ProjectPath path, Validator validator) { 83 ObjRelationship rel = (ObjRelationship) path.getObject(); 84 85 if (path.getObjectParent() != null 87 && path.getObjectParent() != rel.getSourceEntity()) { 88 return; 89 } 90 91 if (Util.isEmptyString(rel.getName())) { 92 validator.registerError("Unnamed ObjRelationship.", path); 93 } 94 else if (rel.getSourceEntity().getAttribute(rel.getName()) != null) { 96 validator.registerWarning( 97 "ObjRelationship " + objRelationshipIdentifier(rel) + " has the same name as one of ObjAttributes", 98 path); 99 } 100 else { 101 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 102 String invalidChars = helper.invalidCharsInObjPathComponent(rel.getName()); 103 104 if (invalidChars != null) { 105 validator.registerWarning( 106 "ObjRelationship " + objRelationshipIdentifier(rel) + " name contains invalid characters: " 107 + invalidChars, 108 path); 109 } 110 else if (helper.invalidDataObjectProperty(rel.getName())) { 111 validator.registerWarning("ObjRelationship " + objRelationshipIdentifier(rel) + " name is invalid.", path); 112 } 113 } 114 115 if (rel.getTargetEntity() == null) { 116 validator.registerWarning("ObjRelationship " + objRelationshipIdentifier(rel) + " has no target entity.", path); 117 } 118 else { 119 List dbRels = rel.getDbRelationships(); 121 if (dbRels.size() == 0) { 122 validator.registerWarning( 123 "ObjRelationship " + objRelationshipIdentifier(rel) + " has no DbRelationship mapping.", 124 path); 125 } 126 else { 127 DbEntity expectedSrc = ((ObjEntity) rel.getSourceEntity()).getDbEntity(); 128 DbEntity expectedTarget = ((ObjEntity) rel.getTargetEntity()) 129 .getDbEntity(); 130 131 if (((DbRelationship) dbRels.get(0)).getSourceEntity() != expectedSrc 132 || ((DbRelationship) dbRels.get(dbRels.size() - 1)) 133 .getTargetEntity() != expectedTarget) { 134 validator.registerWarning( 135 "ObjRelationship " + objRelationshipIdentifier(rel) + " has incomplete DbRelationship mapping.", 136 path); 137 } 138 } 139 } 140 141 if (rel.isToMany() 144 && !rel.isFlattened() 145 && (rel.getDeleteRule() == DeleteRule.NULLIFY)) { 146 ObjRelationship inverse = rel.getReverseRelationship(); 147 if (inverse != null) { 148 DbRelationship firstRel = (DbRelationship) inverse 149 .getDbRelationships() 150 .get(0); 151 Iterator attributePairIterator = firstRel.getJoins().iterator(); 152 while (attributePairIterator.hasNext()) { 153 DbJoin pair = (DbJoin) attributePairIterator.next(); 154 if (pair.getSource().isMandatory()) { 155 validator 156 .registerWarning( 157 "ObjRelationship " + objRelationshipIdentifier(rel) + " has a Nullify delete rule and a mandatory reverse relationship ", 158 path); 159 } 160 } 161 } 162 } 163 } 164 165 public String objRelationshipIdentifier(ObjRelationship rel) 166 { 167 if (null == rel.getSourceEntity()) 168 { 169 return "<[null source entity]." + rel.getName() + ">"; 170 } 171 return "<" + rel.getSourceEntity().getName() + "." + rel.getName() + ">"; 172 } 173 } | Popular Tags |