1/*2 * Copyright 2001-2008 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */1718package org.apache.juddi.query;
1920import java.util.ArrayList;
21import java.util.HashMap;
22import java.util.HashSet;
23import java.util.List;
24import java.util.Map;
25import java.util.Set;
2627import javax.persistence.EntityManager;
2829import org.apache.commons.logging.Log;
30import org.apache.commons.logging.LogFactory;
31import org.apache.juddi.query.util.DynamicQuery;
32import org.apache.juddi.query.util.FindQualifiers;
33import org.uddi.api_v3.CategoryBag;
34import org.uddi.api_v3.KeyedReference;
3536/**37 * Returns the list of "entity" keys possessing the keyedReferences passed in or the level below.38 * For findBusiness queries, this means keyedReferences on the businessEntity and service levels,39 * and for findService queries this means keyedReferences on the service and serviceBinding level.40 * 41 * Output is restricted by list of "entity" keys passed in. If null, all entities are searched.42 * Output is produced by building the appropriate JPA query based on input and find qualifiers.43 * 44 * From the spec : 45 * 46 * * From specification:47 * "combineCategoryBags: this may only be used in the find_business and find_service calls. In the case of 48 * find_business, this qualifier makes the categoryBag entries for the full businessEntity element behave as though all 49 * categoryBag elements found at the businessEntity level and in all contained or referenced businessService elements 50 * and bindingTemplate elements were combined. Searching for a category will yield a positive match on a registered 51 * business if any of the categoryBag elements contained within the full businessEntity element (including the 52 * categoryBag elements within contained or referenced businessService elements or bindingTemplate elements) 53 * contains the filter criteria. In the case of find_service, this qualifier makes the categoryBag entries 54 * for the full businessService element behave as though all categoryBag elements found at the businessService level 55 * and in all contained or referenced elements in the bindingTemplate elements were combined. Searching for a category 56 * will yield a positive match on a registered service if any of the categoryBag elements contained within the 57 * full businessService element (including the categoryBag elements within contained or referenced bindingTemplate 58 * elements) contains the filter criteria. This find qualifier does not cause the keyedReferences in categoryBags 59 * to be combined with the keyedReferences in keyedReferenceGroups in categoryBags when performing the comparison. 60 * The keyedReferences are combined with each other, and the keyedReferenceGroups are combined with each other."61 * 62 * 63 * NOTES:64 * 1) Categories are grouped with a logical AND by default.65 * 2) Concerning when the categories are AND'd together - the only way this can be done with a single query was to create a self-join for 66 * each category. If there are a lot of categories, the performance could suffer.67 * TODO: Test performance with multiple AND'd categories. If too slow, look to process this query in multiple steps.68 * 3) The "orLikeKeys" qualifier complicates matters. The "like" keys are OR'd together and these groups of "like" keys are AND'd together.69 * As with "andAllKeys", self-joins are created but only one for each group of "like" keys. If none of the keyedReferences passed are alike then this70 * will reduce to an "andAllKeys" query. If all are alike, then this will query will exhibit the behavior of OR'ing all keys.71 * 72 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>73 * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>74 * @author <a href="mailto:kstam@apache.org">Kurt Stam</a>75 */76publicclassFindEntityByCombinedCategoryQueryextendsFindEntityByCategoryQuery {
7778 @SuppressWarnings("unused")
79privatefinalstatic Log log = LogFactory.getLog(FindEntityByCombinedCategoryQuery.class);
8081protected String entityField2;
82protected String entityNameChild2;
83protected String entityAliasChild2;
8485protected String entityField3;
86protected String entityNameChild3;
87protected String entityAliasChild3;
888990publicFindEntityByCombinedCategoryQuery(String entityName, String entityAlias, String keyName,
91 String entityField, String entityNameChild, String signaturePresent) {
92super(entityName, entityAlias, keyName, entityField, entityNameChild, signaturePresent);
93 }
9495publicFindEntityByCombinedCategoryQuery(String entityName, String entityAlias, String keyName,
96 String entityField, String entityNameChild,
97 String entityField2, String entityNameChild2, String entityField3, String entityNameChild3,
98 String signaturePresent) {
99super(entityName, entityAlias, keyName, entityField, entityNameChild, signaturePresent);
100101this.entityNameChild2 = entityNameChild2;
102this.entityAliasChild2 = buildAlias(entityNameChild2);
103this.entityField2 = entityField2;
104if (entityNameChild3!=null) {
105this.entityField3 = entityField3;
106this.entityNameChild3 = entityNameChild3;
107this.entityAliasChild3 = buildAlias(entityNameChild3);
108 }
109this.signaturePresent = signaturePresent;
110 selectSQL = "";
111 }
112113public String getEntityNameChild2() {
114return entityNameChild2;
115 }
116117public String getEntityAliasChild2() {
118return entityAliasChild2;
119 }
120121public String getEntityNameChild3() {
122return entityNameChild3;
123 }
124125public String getEntityAliasChild3() {
126return entityAliasChild3;
127 }
128129public List<Object> select(EntityManager em, FindQualifiers fq, CategoryBag categoryBag,
130 List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
131132// If keysIn is not null and empty, then search is over.133if ((keysIn != null) && (keysIn.size() == 0))
134return keysIn;
135136if (categoryBag == null)
137return keysIn;
138139 List<KeyedReference> keyRefsInCategories = categoryBag.getKeyedReference();
140if (keyRefsInCategories == null || keyRefsInCategories.size() == 0)
141return keysIn;
142143 Map<KeyedReference,Set<String>> map = new HashMap<KeyedReference,Set<String>>();
144//1. First match at the top level (i.e. categoryBag on business)145 findEntityByCategoryQuery(map, em, fq, categoryBag, entityField, entityNameChild, keysIn, restrictions);
146//2. Now match at the second level (i.e. categoryBag on services for businesses)147 findEntityByCategoryQuery(map, em, fq, categoryBag, entityField2, entityNameChild2, keysIn, restrictions);
148//3. Now match at the third level (i.e. categoryBag on binding for businesses)149// This does only apply to businesses (not for services)150if (entityNameChild3!=null) {
151 findEntityByCategoryQuery(map, em, fq, categoryBag, entityField3, entityNameChild3, keysIn, restrictions);
152 }
153154//Now build the results taking into account AND/OR/LIKE155 Set<String> resultingEntityKeys = new HashSet<String>();
156if (fq.isOrAllKeys()) {
157//in this case get ALL businessKeys158for (KeyedReference keyRef: map.keySet()) {
159 resultingEntityKeys.addAll(map.get(keyRef));
160 }
161 } elseif (fq.isOrLikeKeys()) {
162// any keyedReference filters that come from the same namespace (e.g. have the same tModelKey value) 163// are OR’d together rather than AND’d164// 1. OR if we have keys with similar namespaces (keyValue)165 Map<String,Set<String>> likeMap = new HashMap<String,Set<String>>();
166for (KeyedReference keyRef: map.keySet()) {
167 String keyValue = keyRef.getKeyValue();
168if (likeMap.containsKey(keyValue)) {
169 likeMap.get(keyValue).addAll(map.get(keyRef));
170 } else {
171 likeMap.put(keyValue, map.get(keyRef));
172 }
173 }
174// 2. Now AND the likeMap175boolean firstTime = true;
176for (String keyValue: likeMap.keySet()) {
177if (firstTime) {
178 resultingEntityKeys = map.get(keyValue);
179 firstTime = false;
180 } else {
181//FIXME this is wrong182 resultingEntityKeys.retainAll(map.get(keyValue));
183 }
184 }
185 } else {
186// AND keys by default, in this case each entity (business or service)187// needs to have ALL keys188boolean firstTime = true;
189for (KeyedReference keyRef: map.keySet()) {
190if (firstTime) {
191 resultingEntityKeys = map.get(keyRef);
192 firstTime = false;
193 } else {
194 resultingEntityKeys.retainAll(map.get(keyRef));
195 }
196 }
197 }
198returnnew ArrayList<Object>(resultingEntityKeys);
199 }
200/**201 * Finding the entities (businesses or services) that have a matching keyedReference in their202 * categoryBag.203 * 204 * @param map - result map of keyedReference and matching businesses205 * @param em206 * @param fq207 * @param categoryBag208 * @param entityField209 * @param entityNameChild210 * @param keysIn211 * @param restrictions212 */213privatevoid 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 = newFindEntityByCategoryQuery(
218 entityName, entityAlias, keyName, entityField, entityNameChild, signaturePresent);
219for (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);
226if (map.containsKey(keyedReference)) {
227 map.get(keyedReference).addAll(keySet);
228 } else {
229 map.put(keyedReference, keySet);
230 }
231 }
232 }
233234 }