This project has retired. For details please refer to its Attic page.
FindEntityByCombinedCategoryQuery xref
View Javadoc
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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
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   * 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 this
70   *    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   */
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         // If keysIn is not null and empty, then search is over.
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 		//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)
150 		if (entityNameChild3!=null) {
151 			findEntityByCategoryQuery(map, em, fq, categoryBag, entityField3, entityNameChild3, keysIn, restrictions);
152 		}
153 		
154 		//Now build the results taking into account AND/OR/LIKE
155 		Set<String> resultingEntityKeys = new HashSet<String>();
156 		if (fq.isOrAllKeys()) {
157 			//in this case get ALL businessKeys
158 			for (KeyedReference keyRef: map.keySet()) {
159 				resultingEntityKeys.addAll(map.get(keyRef));
160 			}
161 		} else if (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’d
164 			// 1. OR if we have keys with similar namespaces (keyValue)
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 			// 2. Now AND the likeMap
175 			boolean firstTime = true;
176 			for (String keyValue: likeMap.keySet()) {
177 				if (firstTime) {
178 					resultingEntityKeys = map.get(keyValue);
179 					firstTime = false;
180 				} else {
181                                         //FIXME this is wrong
182 					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 keys
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 	 * Finding the entities (businesses or services) that have a matching keyedReference in their
202 	 * categoryBag.
203 	 * 
204 	 * @param map - result map of keyedReference and matching businesses
205 	 * @param em
206 	 * @param fq
207 	 * @param categoryBag
208 	 * @param entityField
209 	 * @param entityNameChild
210 	 * @param keysIn
211 	 * @param restrictions
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 }