1 19 20 package org.apache.cayenne.query; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.map.EntityResolver; 29 30 38 public class QueryChain implements Query { 39 40 protected Collection chain; 41 protected String name; 42 43 46 public QueryChain() { 47 } 48 49 52 public QueryChain(Query[] queries) { 53 if (queries != null && queries.length > 0) { 54 this.chain = new ArrayList (Arrays.asList(queries)); 55 } 56 } 57 58 61 public QueryChain(Collection queries) { 62 if (queries != null && !queries.isEmpty()) { 63 this.chain = new ArrayList (queries); 64 } 65 } 66 67 70 public void addQuery(Query query) { 71 if (chain == null) { 72 chain = new ArrayList (); 73 } 74 75 chain.add(query); 76 } 77 78 82 public boolean removeQuery(Query query) { 83 return (chain != null) ? chain.remove(query) : false; 84 } 85 86 public boolean isEmpty() { 87 return chain == null || chain.isEmpty(); 88 } 89 90 94 public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) { 95 if (chain != null && !chain.isEmpty()) { 96 Iterator it = chain.iterator(); 97 while (it.hasNext()) { 98 Query q = (Query) it.next(); 99 q.route(router, resolver, substitutedQuery); 100 } 101 } 102 } 103 104 108 public SQLAction createSQLAction(SQLActionVisitor visitor) { 109 throw new CayenneRuntimeException("Chain doesn't support its own execution " 110 + "and should've been split into separate queries during routing phase."); 111 } 112 113 public String getName() { 114 return name; 115 } 116 117 public void setName(String name) { 118 this.name = name; 119 } 120 121 124 public QueryMetadata getMetaData(EntityResolver resolver) { 125 QueryMetadataWrapper wrapper = new QueryMetadataWrapper( 126 DefaultQueryMetadata.defaultMetadata); 127 wrapper.override(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY, Boolean.TRUE); 128 return wrapper; 129 } 130 } 131 | Popular Tags |