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.project.ProjectPath; 26 import org.apache.cayenne.query.Query; 27 import org.apache.cayenne.query.SQLTemplate; 28 import org.apache.cayenne.util.Util; 29 30 36 public class SQLTemplateValidator extends TreeNodeValidator { 37 38 public void validateObject(ProjectPath treeNodePath, Validator validator) { 39 SQLTemplate query = (SQLTemplate) treeNodePath.getObject(); 40 41 validateName(query, treeNodePath, validator); 42 validateRoot(query, treeNodePath, validator); 43 validateDefaultSQL(query, treeNodePath, validator); 44 } 45 46 protected void validateDefaultSQL( 47 SQLTemplate query, 48 ProjectPath path, 49 Validator validator) { 50 51 if (Util.isEmptyString(query.getDefaultTemplate())) { 52 54 Iterator it = query.getTemplateKeys().iterator(); 55 while (it.hasNext()) { 56 String key = (String ) it.next(); 57 if (!Util.isEmptyString(query.getCustomTemplate(key))) { 58 return; 59 } 60 } 61 62 validator.registerWarning("Query has no default SQL template", path); 63 } 64 } 65 66 protected void validateRoot(SQLTemplate query, ProjectPath path, Validator validator) { 67 DataMap map = (DataMap) path.firstInstanceOf(DataMap.class); 68 if (query.getRoot() == null && map != null) { 69 validator.registerWarning("Query has no root", path); 70 } 71 } 72 73 protected void validateName(Query query, ProjectPath path, Validator validator) { 74 String name = query.getName(); 75 76 if (Util.isEmptyString(name)) { 78 validator.registerError("Unnamed Query.", path); 79 return; 80 } 81 82 DataMap map = (DataMap) path.getObjectParent(); 83 if (map == null) { 84 return; 85 } 86 87 Iterator it = map.getQueries().iterator(); 89 while (it.hasNext()) { 90 Query otherQuery = (Query) it.next(); 91 if (otherQuery == query) { 92 continue; 93 } 94 95 if (name.equals(otherQuery.getName())) { 96 validator.registerError("Duplicate Query name: " + name + ".", path); 97 break; 98 } 99 } 100 } 101 } 102 | Popular Tags |