1 19 20 package org.apache.cayenne.map; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.exp.Expression; 29 import org.apache.cayenne.query.Ordering; 30 import org.apache.cayenne.query.Query; 31 32 40 public abstract class QueryBuilder { 41 42 public static final String OBJ_ENTITY_ROOT = "obj-entity"; 43 public static final String DB_ENTITY_ROOT = "db-entity"; 44 public static final String PROCEDURE_ROOT = "procedure"; 45 public static final String DATA_MAP_ROOT = "data-map"; 46 public static final String JAVA_CLASS_ROOT = "java-class"; 47 48 protected String name; 49 protected Map properties; 50 protected List resultColumns; 51 protected String sql; 52 protected Map adapterSql; 53 protected Expression qualifier; 54 protected List orderings; 55 protected List prefetches; 56 protected DataMap dataMap; 57 protected String rootType; 58 protected String rootName; 59 protected String resultEntity; 60 61 64 public abstract Query getQuery(); 65 66 public void setName(String name) { 67 this.name = name; 68 } 69 70 76 protected Object getRoot() { 77 78 Object root = null; 79 80 if (rootType == null || DATA_MAP_ROOT.equals(rootType) || rootName == null) { 81 root = dataMap; 82 } 83 else if (OBJ_ENTITY_ROOT.equals(rootType)) { 84 root = dataMap.getObjEntity(rootName); 85 } 86 else if (DB_ENTITY_ROOT.equals(rootType)) { 87 root = dataMap.getDbEntity(rootName); 88 } 89 else if (PROCEDURE_ROOT.equals(rootType)) { 90 root = dataMap.getProcedure(rootName); 91 } 92 else if (JAVA_CLASS_ROOT.equals(rootType)) { 93 root = dataMap.getObjEntityForJavaClass(rootName); 96 } 97 98 return (root != null) ? root : dataMap; 99 } 100 101 public void setResultEntity(String resultEntity) { 102 this.resultEntity = resultEntity; 103 } 104 105 108 public void setRoot(DataMap dataMap, String rootType, String rootName) { 109 this.dataMap = dataMap; 110 this.rootType = rootType; 111 this.rootName = rootName; 112 } 113 114 118 public void addSql(String sql, String adapterClass) { 119 if (adapterClass == null) { 120 this.sql = sql; 121 } 122 else { 123 if (adapterSql == null) { 124 adapterSql = new HashMap (); 125 } 126 127 adapterSql.put(adapterClass, sql); 128 } 129 } 130 131 public void setQualifier(String qualifier) { 132 if (qualifier == null || qualifier.trim().length() == 0) { 133 this.qualifier = null; 134 } 135 else { 136 this.qualifier = Expression.fromString(qualifier.trim()); 137 } 138 } 139 140 public void addProperty(String name, String value) { 141 if (properties == null) { 142 properties = new HashMap (); 143 } 144 145 properties.put(name, value); 146 } 147 148 public void addOrdering(String path, String descending, String ignoreCase) { 149 if (orderings == null) { 150 orderings = new ArrayList (); 151 } 152 153 if (path != null && path.trim().length() == 0) { 154 path = null; 155 } 156 boolean isDescending = "true".equalsIgnoreCase(descending); 157 boolean isIgnoringCase = "true".equalsIgnoreCase(ignoreCase); 158 orderings.add(new Ordering(path, !isDescending, isIgnoringCase)); 159 } 160 161 public void addPrefetch(String path) { 162 if (path == null || path != null && path.trim().length() == 0) { 163 return; 165 } 166 167 if (prefetches == null) { 168 prefetches = new ArrayList (); 169 } 170 prefetches.add(path.trim()); 171 } 172 } 173 | Popular Tags |