1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.krad.data.jpa;
17  
18  import org.kuali.rice.core.api.criteria.CountFlag;
19  import org.kuali.rice.core.api.criteria.GenericQueryResults;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  
22  import javax.persistence.Query;
23  import java.util.List;
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  abstract class DataObjectCriteriaQueryBase<C, Q> implements CriteriaQuery {
36  
37      
38  
39  
40  
41  
42      protected abstract QueryTranslator<C, Q> getQueryTranslator();
43  
44      
45  
46  
47  
48  
49  
50      protected abstract int getRowCount(Q query);
51  
52      
53  
54  
55  
56  
57  
58  
59  
60      protected abstract int getIncludedRowCount(Q query, List rows);
61  
62      
63  
64  
65  
66  
67  
68  
69      protected abstract <T> List<T> getResults(Q query);
70  
71      
72  
73  
74  
75  
76  
77      protected abstract int executeUpdate(Query query);
78  
79      
80  
81  
82      public <T> void deleteMatching(Class<T> type, QueryByCriteria criteria) {
83  
84          if (type == null) {
85              throw new IllegalArgumentException("class type is null");
86          }
87  
88          
89          if (criteria == null || criteria.getPredicate() == null) {
90              throw new IllegalArgumentException("criteria is null");
91          }
92  
93          final C parent = getQueryTranslator().translateCriteria(type, criteria);
94          final Query query = getQueryTranslator().createDeletionQuery(type, parent);
95          executeUpdate(query);
96      }
97  
98      
99  
100 
101     public <T> void deleteAll(Class<T> type) {
102         if (type == null) {
103             throw new IllegalArgumentException("class type is null");
104         }
105 
106         final C parent = getQueryTranslator().translateCriteria(type,
107                 QueryByCriteria.Builder.create().build());
108         final Query query = getQueryTranslator().createDeletionQuery(type, parent);
109         executeUpdate(query);
110     }
111 
112     
113 
114 
115     @Override
116     public <T> GenericQueryResults<T> lookup(Class<T> queryClass, QueryByCriteria criteria) {
117         if (queryClass == null) {
118             throw new IllegalArgumentException("queryClass is null");
119         }
120 
121         if (criteria == null) {
122             throw new IllegalArgumentException("criteria is null");
123         }
124 
125         final C parent = getQueryTranslator().translateCriteria(queryClass, criteria);
126 
127         switch (criteria.getCountFlag()) {
128             case ONLY:
129                 return forCountOnly(queryClass, criteria, parent);
130             case NONE:
131                 return forRowResults(queryClass, criteria, parent, criteria.getCountFlag());
132             case INCLUDE:
133                 return forRowResults(queryClass, criteria, parent, criteria.getCountFlag());
134             default: throw new UnsupportedCountFlagException(criteria.getCountFlag());
135         }
136     }
137 
138     
139 
140 
141 
142 
143 
144 
145 
146 
147 
148     protected <T> GenericQueryResults<T> forRowResults(final Class<T> queryClass, final QueryByCriteria criteria, final C ojbCriteria, CountFlag flag) {
149         final Q query = getQueryTranslator().createQuery(queryClass, ojbCriteria);
150         final GenericQueryResults.Builder<T> results = GenericQueryResults.Builder.<T>create();
151 
152         getQueryTranslator().convertQueryFlags(criteria, query);
153 
154         final List<T> rows = getResults(query);
155         if (flag == CountFlag.INCLUDE) {
156             results.setTotalRowCount(getIncludedRowCount(query, rows));
157         }
158 
159         if (criteria.getMaxResults() != null && rows.size() > criteria.getMaxResults()) {
160             results.setMoreResultsAvailable(true);
161             
162             rows.remove(criteria.getMaxResults().intValue());
163         }
164 
165         results.setResults(rows);
166         return results.build();
167     }
168 
169     
170 
171 
172 
173 
174 
175 
176 
177 
178     protected <T> GenericQueryResults<T> forCountOnly(final Class<T> queryClass, final QueryByCriteria criteria, final C platformCriteria) {
179         final Q query = getQueryTranslator().createQuery(queryClass, platformCriteria);
180         final GenericQueryResults.Builder<T> results = GenericQueryResults.Builder.<T>create();
181         results.setTotalRowCount(getRowCount(query));
182         return results.build();
183     }
184 
185     
186 
187 
188 
189 
190     protected static class UnsupportedCountFlagException extends RuntimeException {
191 
192         
193 
194 
195 
196         protected UnsupportedCountFlagException(CountFlag flag) {
197             super("Unsupported predicate [" + String.valueOf(flag) + "]");
198         }
199     }
200 }