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.Collections;
21import java.util.List;
2223import javax.persistence.EntityManager;
2425import org.apache.commons.logging.Log;
26import org.apache.commons.logging.LogFactory;
27import org.apache.juddi.config.Constants;
28import org.apache.juddi.query.util.DynamicQuery;
29import org.apache.juddi.query.util.FindQualifiers;
30import org.apache.juddi.query.util.KeyedRefTModelComparator;
31import org.uddi.api_v3.IdentifierBag;
32import org.uddi.api_v3.KeyedReference;
3334/**35 * Returns the list of "entity" keys possessing the keyedReferences in the passed identifier bag.36 * Output is restricted by list of "entity" keys passed in. If null, all entities are searched.37 * Output is produced by building the appropriate JPA query based on input and find qualifiers.38 * 39 * NOTES:40 * 1) Identifiers are grouped with a logical OR by default.41 * 2) In the case that the "andAllKeys" find qualifier is used the identifiers are AND'd together. The only way this can be done42 * with a single query was to create a self-join for each identifier. If there are a lot of identifiers, the performance could suffer.43 * TODO: Test performance with multiple AND'd identifiers. If too slow, look to process this query in multiple steps.44 * 3) The "orLikeKeys" qualifier complicates matters. The "like" keys are OR'd together and these groups of "like" keys are AND'd together.45 * 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 this46 * will reduce to an "andAllKeys" query. If all are alike, then this will query will exhibit the default behavior of OR'ing all keys.47 * 48 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>49 */50publicclassFindEntityByIdentifierQueryextendsEntityQuery {
5152 @SuppressWarnings("unused")
53privatefinalstatic Log log = LogFactory.getLog(FindEntityByIdentifierQuery.class);
5455privatefinal String entityName;
56privatefinal String entityAlias;
57privatefinal String keyName;
58privatefinal String entityField;
59privatefinal String entityNameChild;
60privatefinal String entityAliasChild;
61privatefinal String selectSQL;
62private String signaturePresent;
6364publicFindEntityByIdentifierQuery(String entityName, String entityAlias, String keyName,
65 String entityField, String entityNameChild, String signaturePresent) {
66this.entityName = entityName;
67this.entityAlias = entityAlias;
68this.keyName = keyName;
69this.entityField = entityField;
70this.entityNameChild = entityNameChild;
71this.entityAliasChild = buildAlias(entityNameChild);
72this.signaturePresent = signaturePresent;
7374 StringBuilder sql = new StringBuilder(200);
75 sql.append("select distinct ").append(entityAlias).append(".").append(keyName).append(" from ").append(entityName).append(" ").append(entityAlias).append(" ");
76 selectSQL = sql.toString();
77 }
7879public String getEntityName() {
80return entityName;
81 }
8283public String getEntityAlias() {
84return entityAlias;
85 }
8687public String getKeyName() {
88return keyName;
89 }
9091public String getEntityField() {
92return entityField;
93 }
9495public String getEntityNameChild() {
96return entityNameChild;
97 }
9899public String getEntityAliasChild() {
100return entityAliasChild;
101 }
102103public String getSelectSQL() {
104return selectSQL;
105 }
106107public String getSignaturePresent() {
108return signaturePresent;
109 }
110111publicvoid setSignaturePresent(String signaturePresent) {
112this.signaturePresent = signaturePresent;
113 }
114115116public List<Object> select(EntityManager em, FindQualifiers fq, IdentifierBag identifiers, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
117// If keysIn is not null and empty, then search is over.118if ((keysIn != null) && (keysIn.size() == 0))
119return keysIn;
120121if (identifiers == null)
122return keysIn;
123124 List<KeyedReference> keyedRefs = identifiers.getKeyedReference();
125if (keyedRefs == null || keyedRefs.size() == 0)
126return keysIn;
127128 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
129 appendConditions(dynamicQry, fq, keyedRefs);
130if (restrictions != null && restrictions.length > 0)
131 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
132133return getQueryResult(em, dynamicQry, keysIn, entityAlias + "." + keyName);
134 }
135136137/*138 * Appends the conditions to the query based on the keyedReference list. With the default or when "andAllKeys" is passed, the keyedReferences are autonomous and are139 * all OR'd or AND'd respectively. However, "orLikeKeys" requires special treatment. The goal is to create the conditions in this format:140 * 141 * (likeKey1 = X or likeKey1 = Y) and (likeKey2 = A or likeKey2 = B or likeKey2 = C) 142 * 143 * ie. the "like" KeyedReferences are OR'd and the groups of "like" KeyedReferences are AND'd with each other.144 */145publicvoid appendConditions(DynamicQuery qry, FindQualifiers fq, List<KeyedReference> keyedRefs) {
146147// Append the necessary tables (one will always be added connecting the entity to its identifier table).148 appendJoinTables(qry, fq, keyedRefs);
149 qry.AND().pad().openParen().pad();
150151 String predicate = DynamicQuery.PREDICATE_EQUALS;
152if (fq.isApproximateMatch()) {
153 predicate = DynamicQuery.PREDICATE_LIKE;
154 }
155156// Sorting the collection by tModel Key157 Collections.sort(keyedRefs, newKeyedRefTModelComparator());
158159 String prevTModelKey = null;
160int count = 0;
161int tblCount = -1;
162for(KeyedReference keyedRef : keyedRefs) {
163 String tmodelKey = keyedRef.getTModelKey();
164 String keyValue = keyedRef.getKeyValue();
165 String keyName = keyedRef.getKeyName();
166167if (fq.isApproximateMatch()) {
168// JUDDI-235: wildcards are provided by user (only commenting in case a new interpretation arises)169//keyValue = keyValue.endsWith(DynamicQuery.WILDCARD)?keyValue:keyValue + DynamicQuery.WILDCARD;170//keyName = keyName.endsWith(DynamicQuery.WILDCARD)?keyName:keyName + DynamicQuery.WILDCARD;171 }
172173// Either opening up (and AND'ing) a new "group" of like keys or simply appending an "or". If this is not "orLikeKeys", then just need to increment174// the table count.175if (fq.isOrLikeKeys()) {
176if (count == 0) {
177 qry.openParen().pad();
178 tblCount++;
179 }
180else {
181if (!tmodelKey.equals(prevTModelKey)) {
182 qry.closeParen().pad().AND().pad().openParen().pad();
183 tblCount++;
184 }
185else186 qry.OR().pad();
187 }
188 }
189else190 tblCount++;
191192 String keyValueTerm = (fq.isAndAllKeys()||fq.isOrLikeKeys()?entityAliasChild + tblCount:entityAliasChild + "0") + ".keyValue";
193 String keyNameTerm = (fq.isAndAllKeys()||fq.isOrLikeKeys()?entityAliasChild + tblCount:entityAliasChild + "0") + ".keyName";
194 String tmodelKeyTerm = (fq.isAndAllKeys()||fq.isOrLikeKeys()?entityAliasChild + tblCount:entityAliasChild + "0") + ".tmodelKeyRef";
195if (fq.isCaseInsensitiveMatch()) {
196 keyValueTerm = "upper(" + keyValueTerm + ")";
197 keyValue = keyValue.toUpperCase();
198199 keyNameTerm = "upper(" + keyNameTerm + ")";
200 keyName = keyName.toUpperCase();
201 }
202203204// According to specification, if the "general keyword" tmodel is used, then the keyName must be part of the query.205if (Constants.GENERAL_KEYWORD_TMODEL.equalsIgnoreCase(tmodelKey)) {
206 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS),
207new DynamicQuery.Parameter(keyValueTerm, keyValue, predicate),
208new DynamicQuery.Parameter(keyNameTerm, keyName, predicate));
209 }
210else {
211 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS),
212new DynamicQuery.Parameter(keyValueTerm, keyValue, predicate));
213214 }
215216if (count + 1 < keyedRefs.size()) {
217if (fq.isAndAllKeys())
218 qry.AND().pad();
219elseif (fq.isOrLikeKeys()) {
220 }
221else222 qry.OR().pad();
223 }
224225// The "orLikeKeys" will always leave an unclosed parenthesis. This will close it.226if (fq.isOrLikeKeys() && (count + 1 == keyedRefs.size()))
227 qry.closeParen().pad();
228229 prevTModelKey = tmodelKey;
230 count++;
231 }
232 qry.closeParen().pad();
233234 }
235236/*237 * Appends the necessary join table for the child entity and additional tables for when keys are AND'd. When "orLikeKeys" is used, 238 * we only need an extra table for each distinct tmodelKey.239 */240publicvoid appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<KeyedReference> keyedRefs) {
241242if (keyedRefs != null && keyedRefs.size() > 0) {
243// Sorting the collection by tModel Key244 Collections.sort(keyedRefs, newKeyedRefTModelComparator());
245246 StringBuffer thetaJoins = new StringBuffer(200);
247int tblCount = 0;
248int count = 0;
249 String curTModelKey = null;
250 String prevTModelKey = null;
251for(KeyedReference kr : keyedRefs) {
252 curTModelKey = kr.getTModelKey();
253if (count != 0) {
254if (fq.isOrLikeKeys() && curTModelKey.equals(prevTModelKey)) {
255// Do nothing256 }
257else {
258 tblCount++;
259 qry.comma().pad().append(entityNameChild + " " + entityAliasChild + tblCount).pad();
260 thetaJoins.append(entityAliasChild + (tblCount - 1) + "." + entityField + "." + keyName + " = " + entityAliasChild + tblCount + "." + entityField + "." + keyName + " ");
261 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
262 }
263264 }
265else {
266 qry.comma().pad().append(entityNameChild + " " + entityAliasChild + tblCount).pad();
267 thetaJoins.append(entityAlias + "." + keyName + " = " + entityAliasChild + tblCount + "." + entityField + "." + keyName + " ");
268 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
269 }
270 prevTModelKey = curTModelKey;
271 count++;
272 }
273274 qry.WHERE().pad().openParen().pad();
275276 String thetaJoinsStr = thetaJoins.toString();
277if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
278 thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
279 qry.append(thetaJoinsStr);
280281 qry.closeParen().pad();
282if (fq!=null && fq.isSignaturePresent()) {
283 qry.AND().pad().openParen().pad().append(getSignaturePresent()).pad().closeParen().pad();
284 }
285 }
286 }
287288 }