1 19 20 package org.apache.cayenne.project.validator; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.map.DataMap; 25 import org.apache.cayenne.map.DbEntity; 26 import org.apache.cayenne.map.DerivedDbEntity; 27 import org.apache.cayenne.project.ProjectPath; 28 import org.apache.cayenne.util.Util; 29 30 33 public class DbEntityValidator extends TreeNodeValidator { 34 35 38 public DbEntityValidator() { 39 super(); 40 } 41 42 public void validateObject(ProjectPath path, Validator validator) { 43 DbEntity ent = (DbEntity) path.getObject(); 44 validateName(ent, path, validator); 45 validateAttributes(ent, path, validator); 46 validatePK(ent, path, validator); 47 48 if ((ent instanceof DerivedDbEntity) 49 && ((DerivedDbEntity) ent).getParentEntity() == null) { 50 validator.registerError( 51 "No parent selected for derived entity \"" 52 + ent.getName() 53 + "\".", 54 path); 55 } 56 } 57 58 63 protected void validatePK( 64 DbEntity ent, 65 ProjectPath path, 66 Validator validator) { 67 if (ent.getAttributes().size() > 0 68 && ent.getPrimaryKey().size() == 0) { 69 DataMap map = ent.getDataMap(); 70 if (map != null && map.getMappedEntities(ent).size() > 0) { 71 validator.registerWarning( 73 "DbEntity \"" 74 + ent.getName() 75 + "\" has no primary key attributes defined.", 76 path); 77 } 78 } 79 } 80 81 84 protected void validateAttributes( 85 DbEntity ent, 86 ProjectPath path, 87 Validator validator) { 88 if (ent.getAttributes().size() == 0) { 89 validator.registerWarning( 91 "DbEntity \"" + ent.getName() + "\" has no attributes defined.", 92 path); 93 } 94 } 95 96 protected void validateName( 97 DbEntity ent, 98 ProjectPath path, 99 Validator validator) { 100 String name = ent.getName(); 101 102 if (Util.isEmptyString(name)) { 104 validator.registerError("Unnamed DbEntity.", path); 105 return; 106 } 107 108 DataMap map = (DataMap) path.getObjectParent(); 109 if (map == null) { 110 return; 111 } 112 113 Iterator it = map.getDbEntities().iterator(); 115 while (it.hasNext()) { 116 DbEntity otherEnt = (DbEntity) it.next(); 117 if (otherEnt == ent) { 118 continue; 119 } 120 121 if (name.equals(otherEnt.getName())) { 122 validator.registerError( 123 "Duplicate DbEntity name: " + name + ".", 124 path); 125 break; 126 } 127 } 128 } 129 } 130 | Popular Tags |