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.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.persistence.EntityManager;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.juddi.query.util.DynamicQuery;
32 import org.apache.juddi.query.util.FindQualifiers;
33 import org.uddi.api_v3.CategoryBag;
34 import org.uddi.api_v3.KeyedReference;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public class FindEntityByCombinedCategoryQuery extends FindEntityByCategoryQuery {
77
78 @SuppressWarnings("unused")
79 private final static Log log = LogFactory.getLog(FindEntityByCombinedCategoryQuery.class);
80
81 protected String entityField2;
82 protected String entityNameChild2;
83 protected String entityAliasChild2;
84
85 protected String entityField3;
86 protected String entityNameChild3;
87 protected String entityAliasChild3;
88
89
90 public FindEntityByCombinedCategoryQuery(String entityName, String entityAlias, String keyName,
91 String entityField, String entityNameChild, String signaturePresent) {
92 super(entityName, entityAlias, keyName, entityField, entityNameChild, signaturePresent);
93 }
94
95 public FindEntityByCombinedCategoryQuery(String entityName, String entityAlias, String keyName,
96 String entityField, String entityNameChild,
97 String entityField2, String entityNameChild2, String entityField3, String entityNameChild3,
98 String signaturePresent) {
99 super(entityName, entityAlias, keyName, entityField, entityNameChild, signaturePresent);
100
101 this.entityNameChild2 = entityNameChild2;
102 this.entityAliasChild2 = buildAlias(entityNameChild2);
103 this.entityField2 = entityField2;
104 if (entityNameChild3!=null) {
105 this.entityField3 = entityField3;
106 this.entityNameChild3 = entityNameChild3;
107 this.entityAliasChild3 = buildAlias(entityNameChild3);
108 }
109 this.signaturePresent = signaturePresent;
110 selectSQL = "";
111 }
112
113 public String getEntityNameChild2() {
114 return entityNameChild2;
115 }
116
117 public String getEntityAliasChild2() {
118 return entityAliasChild2;
119 }
120
121 public String getEntityNameChild3() {
122 return entityNameChild3;
123 }
124
125 public String getEntityAliasChild3() {
126 return entityAliasChild3;
127 }
128
129 public List<Object> select(EntityManager em, FindQualifiers fq, CategoryBag categoryBag,
130 List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
131
132
133 if ((keysIn != null) && (keysIn.size() == 0))
134 return keysIn;
135
136 if (categoryBag == null)
137 return keysIn;
138
139 List<KeyedReference> keyRefsInCategories = categoryBag.getKeyedReference();
140 if (keyRefsInCategories == null || keyRefsInCategories.size() == 0)
141 return keysIn;
142
143 Map<KeyedReference,Set<String>> map = new HashMap<KeyedReference,Set<String>>();
144
145 findEntityByCategoryQuery(map, em, fq, categoryBag, entityField, entityNameChild, keysIn, restrictions);
146
147 findEntityByCategoryQuery(map, em, fq, categoryBag, entityField2, entityNameChild2, keysIn, restrictions);
148
149
150 if (entityNameChild3!=null) {
151 findEntityByCategoryQuery(map, em, fq, categoryBag, entityField3, entityNameChild3, keysIn, restrictions);
152 }
153
154
155 Set<String> resultingEntityKeys = new HashSet<String>();
156 if (fq.isOrAllKeys()) {
157
158 for (KeyedReference keyRef: map.keySet()) {
159 resultingEntityKeys.addAll(map.get(keyRef));
160 }
161 } else if (fq.isOrLikeKeys()) {
162
163
164
165 Map<String,Set<String>> likeMap = new HashMap<String,Set<String>>();
166 for (KeyedReference keyRef: map.keySet()) {
167 String keyValue = keyRef.getKeyValue();
168 if (likeMap.containsKey(keyValue)) {
169 likeMap.get(keyValue).addAll(map.get(keyRef));
170 } else {
171 likeMap.put(keyValue, map.get(keyRef));
172 }
173 }
174
175 boolean firstTime = true;
176 for (String keyValue: likeMap.keySet()) {
177 if (firstTime) {
178 resultingEntityKeys = map.get(keyValue);
179 firstTime = false;
180 } else {
181
182 resultingEntityKeys.retainAll(map.get(keyValue));
183 }
184 }
185 } else {
186
187
188 boolean firstTime = true;
189 for (KeyedReference keyRef: map.keySet()) {
190 if (firstTime) {
191 resultingEntityKeys = map.get(keyRef);
192 firstTime = false;
193 } else {
194 resultingEntityKeys.retainAll(map.get(keyRef));
195 }
196 }
197 }
198 return new ArrayList<Object>(resultingEntityKeys);
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213 private void findEntityByCategoryQuery(Map<KeyedReference,Set<String>> map, EntityManager em,
214 FindQualifiers fq, CategoryBag categoryBag, String entityField, String entityNameChild,
215 List<Object> keysIn, DynamicQuery.Parameter... restrictions)
216 {
217 FindEntityByCategoryQuery findEntityByCategoryQuery = new FindEntityByCategoryQuery(
218 entityName, entityAlias, keyName, entityField, entityNameChild, signaturePresent);
219 for (KeyedReference keyedReference : categoryBag.getKeyedReference()) {
220 CategoryBag categoryBagWithOneKey = new CategoryBag();
221 categoryBagWithOneKey.getKeyedReference().add(keyedReference);
222 List<?> entityKeys = findEntityByCategoryQuery.select(
223 em, fq, categoryBagWithOneKey, keysIn, restrictions);
224 @SuppressWarnings({ "unchecked", "rawtypes" })
225 Set<String> keySet = new HashSet(entityKeys);
226 if (map.containsKey(keyedReference)) {
227 map.get(keyedReference).addAll(keySet);
228 } else {
229 map.put(keyedReference, keySet);
230 }
231 }
232 }
233
234 }