1 56 package org.objectstyle.cayenne.query; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 62 import org.apache.log4j.Level; 63 import org.objectstyle.cayenne.CayenneRuntimeException; 64 import org.objectstyle.cayenne.map.EntityResolver; 65 66 72 public class QueryChain implements Query { 73 74 protected String name; 75 protected Level loggingLevel; 76 protected Collection chain; 77 78 81 public QueryChain() { 82 } 83 84 87 public QueryChain(Collection queries) { 88 if (queries != null && !queries.isEmpty()) { 89 this.chain = new ArrayList (queries); 90 } 91 } 92 93 96 public void addQuery(Query query) { 97 if (chain == null) { 98 chain = new ArrayList (); 99 } 100 101 chain.add(query); 102 } 103 104 108 public boolean removeQuery(Query query) { 109 return (chain != null) ? chain.remove(query) : false; 110 } 111 112 public boolean isEmpty() { 113 return chain == null || chain.isEmpty(); 114 } 115 116 public String getName() { 117 return name; 118 } 119 120 public void setName(String name) { 121 this.name = name; 122 } 123 124 public Level getLoggingLevel() { 125 return loggingLevel; 126 } 127 128 132 public void setLoggingLevel(Level loggingLevel) { 133 this.loggingLevel = loggingLevel; 134 } 135 136 140 public Object getRoot() { 141 throw new CayenneRuntimeException( 142 "Chain doesn't support its own root. Root should be deprecated soon anyway"); 143 } 144 145 149 public void setRoot(Object root) { 150 throw new CayenneRuntimeException( 151 "Chain doesn't support its own root. Root should be deprecated soon anyway. An attempt to set it to " 152 + root); 153 } 154 155 159 public Query resolve(EntityResolver resolver) { 160 if (isEmpty()) { 161 return this; 162 } 163 164 Collection resolvedChain = new ArrayList (chain.size()); 165 166 Iterator it = chain.iterator(); 167 while (it.hasNext()) { 168 Query resolved = ((Query) it.next()).resolve(resolver); 169 if (resolved != null) { 170 resolvedChain.add(resolved); 171 } 172 } 173 174 return new QueryChain(resolvedChain); 175 } 176 177 181 public void route(QueryRouter router, EntityResolver resolver) { 182 if (chain != null && !chain.isEmpty()) { 183 Iterator it = chain.iterator(); 184 while (it.hasNext()) { 185 Query q = (Query) it.next(); 186 q.route(router, resolver); 187 } 188 } 189 } 190 191 195 public SQLAction createSQLAction(SQLActionVisitor visitor) { 196 throw new CayenneRuntimeException("Chain doesn't support its own execution " 197 + "and should've been split into separate queries during routing phase."); 198 } 199 } 200 | Popular Tags |