This project has retired. For details please refer to its
Attic page.
FindEntityByCategoryGroupQuery xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.juddi.query;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import javax.persistence.EntityManager;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.juddi.config.Constants;
31 import org.apache.juddi.query.util.DynamicQuery;
32 import org.apache.juddi.query.util.FindQualifiers;
33 import org.apache.juddi.query.util.KeyedRefGroupTModelComparator;
34 import org.uddi.api_v3.CategoryBag;
35 import org.uddi.api_v3.KeyedReference;
36 import org.uddi.api_v3.KeyedReferenceGroup;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class FindEntityByCategoryGroupQuery extends EntityQuery {
55
56 @SuppressWarnings("unused")
57 private final static Log log = LogFactory.getLog(FindEntityByCategoryGroupQuery.class);
58
59 private static final String ENTITY_KEYEDREFERENCEGROUP = "KeyedReferenceGroup";
60 private static final String ALIAS_KEYEDREFERENCEGROUP = "krg";
61 private static final String FIELD_CATEGORYBAG = "categoryBag";
62
63 private static final String ENTITY_KEYEDREFERENCE = "KeyedReference";
64 private static final String ALIAS_KEYEDREFERENCE = buildAlias(ENTITY_KEYEDREFERENCE);
65 private static final String FIELD_KEYEDREFERENCEGROUP = "keyedReferenceGroup";
66
67 private final String entityName;
68 private final String entityAlias;
69 private final String keyName;
70 private final String entityField;
71 private final String entityNameChild;
72 private final String entityAliasChild;
73 private final String selectSQL;
74 private String signaturePresent;
75
76 public FindEntityByCategoryGroupQuery(String entityName, String entityAlias, String keyName,
77 String entityField, String entityNameChild, String signaturePresent) {
78 this.entityName = entityName;
79 this.entityAlias = entityAlias;
80 this.keyName = keyName;
81 this.entityField = entityField;
82 this.entityNameChild = entityNameChild;
83 this.entityAliasChild = buildAlias(entityNameChild);
84 this.signaturePresent = signaturePresent;
85
86 StringBuffer sql = new StringBuffer(200);
87 sql.append("select distinct " + entityAlias + "." + keyName + " from "
88 + entityName + " " + entityAlias + " , "
89 + entityNameChild + " " + entityAliasChild + " , "
90 + ENTITY_KEYEDREFERENCEGROUP + " " + ALIAS_KEYEDREFERENCEGROUP + " ");
91 selectSQL = sql.toString();
92 }
93
94 public String getEntityName() {
95 return entityName;
96 }
97
98 public String getEntityAlias() {
99 return entityAlias;
100 }
101
102 public String getKeyName() {
103 return keyName;
104 }
105
106 public String getEntityField() {
107 return entityField;
108 }
109
110 public String getEntityNameChild() {
111 return entityNameChild;
112 }
113
114 public String getEntityAliasChild() {
115 return entityAliasChild;
116 }
117
118 public String getSelectSQL() {
119 return selectSQL;
120 }
121
122 public String getSignaturePresent() {
123 return signaturePresent;
124 }
125
126 public void setSignaturePresent(String signaturePresent) {
127 this.signaturePresent = signaturePresent;
128 }
129
130 @SuppressWarnings("unchecked")
131 public List<Object> select(EntityManager em, FindQualifiers fq, CategoryBag categoryBag, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
132
133 if ((keysIn != null) && (keysIn.size() == 0))
134 return keysIn;
135
136 if (categoryBag == null)
137 return keysIn;
138
139 List<KeyedReferenceGroup> categories = categoryBag.getKeyedReferenceGroup();
140 if (categories == null || categories.size() == 0)
141 return keysIn;
142
143 List<KeyedReferenceGroup> keyedRefGroups = new ArrayList<KeyedReferenceGroup>(0);
144 for (KeyedReferenceGroup elem : categories) {
145 if (elem instanceof KeyedReferenceGroup)
146 keyedRefGroups.add((KeyedReferenceGroup)elem);
147 }
148 if (keyedRefGroups.size() == 0)
149 return keysIn;
150
151
152 Collections.sort(keyedRefGroups, new KeyedRefGroupTModelComparator());
153 int count = 0;
154 String prevTModelKey = null;
155 Set<Object> orResults = new HashSet<Object>(0);
156 List<Object> restrictionList = keysIn;
157 List<Object> curResult = null;
158 for (KeyedReferenceGroup keyedRefGroup : keyedRefGroups) {
159 String curTModelKey = keyedRefGroup.getTModelKey();
160
161 DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
162 appendConditions(dynamicQry, fq, keyedRefGroup);
163 if (restrictions != null && restrictions.length > 0)
164 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
165
166
167 if (fq.isOrLikeKeys()) {
168 if (!curTModelKey.equals(prevTModelKey)) {
169 if (count != 0) {
170 restrictionList = new ArrayList<Object>(orResults);
171 orResults.clear();
172 }
173 }
174 }
175 else if (!fq.isOrAllKeys()) {
176 if (count != 0)
177 restrictionList = curResult;
178 }
179
180 if (restrictionList != null && restrictionList.size() == 0)
181 break;
182
183 curResult = getQueryResult(em, dynamicQry, restrictionList, entityAlias + "." + keyName);
184
185 if (fq.isOrAllKeys() || fq.isOrLikeKeys()) {
186 orResults.addAll((List<Object>)curResult);
187 }
188
189 prevTModelKey = curTModelKey;
190 count++;
191 }
192
193 List<Object> result = null;
194 if (fq.isOrAllKeys() || fq.isOrLikeKeys()) {
195 result = new ArrayList<Object>(0);
196 result.addAll(orResults);
197 }
198 else
199 result = (List<Object>)curResult;
200
201 return result;
202
203 }
204
205
206
207
208
209 public void appendConditions(DynamicQuery qry, FindQualifiers fq, KeyedReferenceGroup keyedRefGroup) {
210
211
212 appendJoinTables(qry, fq, keyedRefGroup);
213
214
215 qry.AND().pad().appendGroupedAnd(new DynamicQuery.Parameter(ALIAS_KEYEDREFERENCEGROUP + ".tmodelKey", keyedRefGroup.getTModelKey(), DynamicQuery.PREDICATE_EQUALS));
216
217 List<KeyedReference> keyedRefs = keyedRefGroup.getKeyedReference();
218 if (keyedRefs != null && keyedRefs.size() > 0) {
219
220 qry.AND().pad().openParen().pad();
221
222 String predicate = DynamicQuery.PREDICATE_EQUALS;
223 if (fq.isApproximateMatch()) {
224 predicate = DynamicQuery.PREDICATE_LIKE;
225 }
226
227 int count = 0;
228 int tblCount = -1;
229 for(KeyedReference keyedRef : keyedRefs) {
230 String tmodelKey = keyedRef.getTModelKey();
231 String keyValue = keyedRef.getKeyValue();
232 String keyName = keyedRef.getKeyName();
233
234 if (fq.isApproximateMatch()) {
235
236
237
238 }
239
240 tblCount++;
241 String keyValueTerm = ALIAS_KEYEDREFERENCE + tblCount + ".keyValue";
242 String keyNameTerm = ALIAS_KEYEDREFERENCE + tblCount + ".keyName";
243 String tmodelKeyTerm = ALIAS_KEYEDREFERENCE + tblCount + ".tmodelKeyRef";
244 if (fq.isCaseInsensitiveMatch()) {
245 keyValueTerm = "upper(" + keyValueTerm + ")";
246 keyValue = keyValue.toUpperCase();
247
248 keyNameTerm = "upper(" + keyNameTerm + ")";
249 keyName = keyName.toUpperCase();
250 }
251
252
253 if (Constants.GENERAL_KEYWORD_TMODEL.equalsIgnoreCase(tmodelKey)) {
254 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS),
255 new DynamicQuery.Parameter(keyValueTerm, keyValue, predicate),
256 new DynamicQuery.Parameter(keyNameTerm, keyName, predicate));
257 }
258 else {
259 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS),
260 new DynamicQuery.Parameter(keyValueTerm, keyValue, predicate));
261
262 }
263
264 if (count + 1 < keyedRefs.size())
265 qry.AND().pad();
266
267 count++;
268 }
269 qry.closeParen().pad();
270 }
271
272 }
273
274
275
276
277
278
279
280 public void appendJoinTables(DynamicQuery qry, FindQualifiers fq, KeyedReferenceGroup keyedRefGroup) {
281
282 if (keyedRefGroup != null) {
283
284 List<KeyedReference> keyedRefs = keyedRefGroup.getKeyedReference();
285 StringBuffer thetaJoins = new StringBuffer(200);
286 if (keyedRefs != null && keyedRefs.size() > 0) {
287 int tblCount = 0;
288 for(int count = 0; count<keyedRefs.size(); count++) {
289 if (count != 0) {
290 tblCount++;
291 qry.comma().pad().append(ENTITY_KEYEDREFERENCE + " " + ALIAS_KEYEDREFERENCE + tblCount).pad();
292 thetaJoins.append(ALIAS_KEYEDREFERENCE + (tblCount - 1) + "." + FIELD_KEYEDREFERENCEGROUP + ".id = " + ALIAS_KEYEDREFERENCE + tblCount + "." + FIELD_KEYEDREFERENCEGROUP + ".id ");
293 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
294 } else {
295 qry.comma().pad().append(ENTITY_KEYEDREFERENCE + " " + ALIAS_KEYEDREFERENCE + tblCount).pad();
296 thetaJoins.append(ALIAS_KEYEDREFERENCEGROUP + ".id = " + ALIAS_KEYEDREFERENCE + tblCount + "." + FIELD_KEYEDREFERENCEGROUP + ".id ");
297 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
298 }
299 }
300 }
301 qry.WHERE().pad().openParen().pad();
302
303
304 qry.append(entityAlias + "." + keyName + " = " + entityAliasChild + "." + entityField + "." + KEY_NAME).pad();
305 qry.AND().pad();
306
307
308 qry.append(entityAliasChild + ".id = " + ALIAS_KEYEDREFERENCEGROUP + "." + FIELD_CATEGORYBAG + ".id").pad();
309
310 String thetaJoinsStr = thetaJoins.toString();
311 if (thetaJoinsStr != null && thetaJoinsStr.length() > 0)
312 qry.AND().pad();
313
314 if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
315 thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
316 qry.append(thetaJoinsStr);
317
318 qry.closeParen().pad();
319 if (fq!=null && fq.isSignaturePresent()) {
320 qry.AND().pad().openParen().pad().append(getSignaturePresent()).pad().closeParen().pad();
321 }
322 }
323 }
324
325 }