1 56 package org.objectstyle.cayenne.project.validator; 57 58 import java.util.Iterator ; 59 60 import org.objectstyle.cayenne.map.DataMap; 61 import org.objectstyle.cayenne.map.DbEntity; 62 import org.objectstyle.cayenne.map.DerivedDbEntity; 63 import org.objectstyle.cayenne.project.ProjectPath; 64 import org.objectstyle.cayenne.util.Util; 65 66 69 public class DbEntityValidator extends TreeNodeValidator { 70 71 74 public DbEntityValidator() { 75 super(); 76 } 77 78 public void validateObject(ProjectPath path, Validator validator) { 79 DbEntity ent = (DbEntity) path.getObject(); 80 validateName(ent, path, validator); 81 validateAttributes(ent, path, validator); 82 validatePK(ent, path, validator); 83 84 if ((ent instanceof DerivedDbEntity) 85 && ((DerivedDbEntity) ent).getParentEntity() == null) { 86 validator.registerError( 87 "No parent selected for derived entity \"" 88 + ent.getName() 89 + "\".", 90 path); 91 } 92 } 93 94 99 protected void validatePK( 100 DbEntity ent, 101 ProjectPath path, 102 Validator validator) { 103 if (ent.getAttributes().size() > 0 104 && ent.getPrimaryKey().size() == 0) { 105 DataMap map = ent.getDataMap(); 106 if (map != null && map.getMappedEntities(ent).size() > 0) { 107 validator.registerWarning( 109 "DbEntity \"" 110 + ent.getName() 111 + "\" has no primary key attributes defined.", 112 path); 113 } 114 } 115 } 116 117 120 protected void validateAttributes( 121 DbEntity ent, 122 ProjectPath path, 123 Validator validator) { 124 if (ent.getAttributes().size() == 0) { 125 validator.registerWarning( 127 "DbEntity \"" + ent.getName() + "\" has no attributes defined.", 128 path); 129 } 130 } 131 132 protected void validateName( 133 DbEntity ent, 134 ProjectPath path, 135 Validator validator) { 136 String name = ent.getName(); 137 138 if (Util.isEmptyString(name)) { 140 validator.registerError("Unnamed DbEntity.", path); 141 return; 142 } 143 144 DataMap map = (DataMap) path.getObjectParent(); 145 if (map == null) { 146 return; 147 } 148 149 Iterator it = map.getDbEntities().iterator(); 151 while (it.hasNext()) { 152 DbEntity otherEnt = (DbEntity) it.next(); 153 if (otherEnt == ent) { 154 continue; 155 } 156 157 if (name.equals(otherEnt.getName())) { 158 validator.registerError( 159 "Duplicate DbEntity name: " + name + ".", 160 path); 161 break; 162 } 163 } 164 } 165 } 166 | Popular Tags |