1 19 20 package org.apache.cayenne.project.validator; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.access.DataDomain; 25 import org.apache.cayenne.map.DataMap; 26 import org.apache.cayenne.map.ObjEntity; 27 import org.apache.cayenne.project.ProjectPath; 28 import org.apache.cayenne.util.Util; 29 30 33 public class ObjEntityValidator extends TreeNodeValidator { 34 35 38 public ObjEntityValidator() { 39 super(); 40 } 41 42 public void validateObject(ProjectPath path, Validator validator) { 43 ObjEntity ent = (ObjEntity) path.getObject(); 44 validateName(ent, path, validator); 45 validateClassName(ent, path, validator); 46 validateSuperClassName(ent, path, validator); 47 48 if (ent.getDbEntity() == null) { 50 validator.registerWarning("ObjEntity has no DbEntity mapping.", path); 51 } 52 } 53 54 private void validateClassName(ObjEntity ent, ProjectPath path, Validator validator) { 55 String className = ent.getClassName(); 56 57 if (Util.isEmptyString(className)) { 59 return; 60 } 61 62 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 63 String invalidChars = helper.invalidCharsInJavaClassName(className); 64 65 if (invalidChars != null) { 66 validator.registerWarning( 67 "ObjEntity Java class contains invalid characters: " + invalidChars, 68 path); 69 } 70 else if (helper.invalidDataObjectClass(className)) { 71 validator.registerWarning( 72 "ObjEntity Java class is invalid: " + className, 73 path); 74 } 75 else if (className.indexOf('.') < 0) { 76 validator.registerWarning( 77 "Placing Java class in default package is discouraged: " + className, 78 path); 79 } 80 } 81 82 private void validateSuperClassName( 83 ObjEntity ent, 84 ProjectPath path, 85 Validator validator) { 86 String superClassName = ent.getSuperClassName(); 87 88 if (Util.isEmptyString(superClassName)) { 89 return; } 91 92 MappingNamesHelper helper = MappingNamesHelper.getInstance(); 93 String invalidChars = helper.invalidCharsInJavaClassName(superClassName); 94 95 if (invalidChars != null) { 96 validator.registerWarning( 97 "ObjEntity Java superclass contains invalid characters: " 98 + invalidChars, 99 path); 100 } 101 else if (helper.invalidDataObjectClass(superClassName)) { 102 validator.registerWarning("ObjEntity Java superclass is invalid: " 103 + superClassName, path); 104 } 105 106 DataMap map = (DataMap) path.getObjectParent(); 107 if (map == null) { 108 return; 109 } 110 } 111 112 protected void validateName(ObjEntity entity, ProjectPath path, Validator validator) { 113 String name = entity.getName(); 114 115 if (Util.isEmptyString(name)) { 117 validator.registerError("Unnamed ObjEntity.", path); 118 return; 119 } 120 121 DataMap map = (DataMap) path.getObjectParent(); 122 if (map == null) { 123 return; 124 } 125 126 Iterator it = map.getObjEntities().iterator(); 128 while (it.hasNext()) { 129 ObjEntity otherEnt = (ObjEntity) it.next(); 130 if (otherEnt == entity) { 131 continue; 132 } 133 134 if (name.equals(otherEnt.getName())) { 135 validator.registerError("Duplicate ObjEntity name: " + name + ".", path); 136 break; 137 } 138 } 139 140 DataDomain domain = (DataDomain) path.firstInstanceOf(DataDomain.class); 142 if (domain != null) { 143 Iterator maps = domain.getDataMaps().iterator(); 144 while (maps.hasNext()) { 145 DataMap nextMap = (DataMap) maps.next(); 146 if (nextMap == map) { 147 continue; 148 } 149 150 ObjEntity conflictingEntity = nextMap.getObjEntity(name); 151 if (conflictingEntity != null) { 152 153 if (!Util.nullSafeEquals(conflictingEntity.getClassName(), entity 154 .getClassName())) { 155 validator.registerWarning( 156 "Duplicate ObjEntity name in another DataMap: " 157 + name 158 + ".", 159 path); 160 break; 161 } 162 } 163 } 164 } 165 } 166 } 167 | Popular Tags |