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.List;
21import javax.persistence.EntityManager;
2223import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.juddi.query.util.DynamicQuery;
26import org.apache.juddi.query.util.FindQualifiers;
27import org.uddi.api_v3.TModelBag;
2829/**30 * Returns the list of binding keys possessing the tModels in the passed tModelBag.31 * Output is restricted by list of binding keys passed in. If null, all bindings are searched.32 * Output is produced by building the appropriate JPA query based on input and find qualifiers.33 * 34 * From specification:35 * "This collection of tModelKey elements represent in part or in whole the technical fingerprint of the bindingTemplate 36 * structures for which the search is being performed. At least one of either a tModelBag or a find_tModel argument SHOULD be 37 * supplied, unless a categoryBag based search is being used. 3839 * If a find_tModel argument is specified (see above), it is treated as an embedded inquiry. The tModelKeys returned as a 40 * result of this embedded find_tModel argument are used as if they had been 41 * supplied in a tModelBag argument. Changing the order of the keys in the collection or specifying the same tModelKey more than 42 * once does not change the behavior of the find. 43 * 44 * By default, only bindingTemplates that have a technical fingerprint containing 45 * all of the supplied tModelKeys match (logical AND). Specifying appropriate findQualifiers can override this behavior so that 46 * bindingTemplates with a technical fingerprint containing any of the specified tModelKeys are returned (logical OR)." 47 * 48 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>49 */50publicclassFindBindingByTModelKeyQueryextendsBindingTemplateQuery {
5152 @SuppressWarnings("unused")
53privatestatic Log log = LogFactory.getLog(FindBindingByTModelKeyQuery.class);
5455publicstaticfinal String ENTITY_NAME_CHILD = "TmodelInstanceInfo";
5657protectedstaticfinal String entityAliasChild;
5859static {
60 entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
61 }
6263publicstatic List<Object> select(EntityManager em, FindQualifiers fq, TModelBag tModels, String parentKey,
64 List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
65// If keysIn is not null and empty, then search is over.66//if ((keysIn != null) && (keysIn.size() == 0))67// return keysIn;6869if (tModels == null)
70return keysIn;
7172 List<String> tmodelKeys = tModels.getTModelKey();
73if (tmodelKeys == null || tmodelKeys.size() == 0)
74return keysIn;
7576 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
77 appendConditions(dynamicQry, fq, tmodelKeys);
78if (parentKey != null && parentKey.length() > 0)
79 dynamicQry.AND().pad().appendGroupedAnd(new DynamicQuery.Parameter(BindingTemplateQuery.ENTITY_ALIAS + "." + BindingTemplateQuery.KEY_NAME_PARENT, parentKey, DynamicQuery.PREDICATE_EQUALS));
8081if (restrictions != null && restrictions.length > 0)
82 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
8384return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
85 }
868788/*89 * Appends the conditions to the query based on the tModelKey list. With the default or when "orAllKeys" is passed, the keyedReferences are autonomous and are90 * all AND'd or OR'd respectively. 91 * 92 */93publicstaticvoid appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
9495// Append the necessary tables (one will always be added connecting the entity to its instanceinfo table).96 appendJoinTables(qry, fq, tmodelKeys);
97 qry.AND().pad().openParen().pad();
9899int count = 0;
100int tblCount = -1;
101for(String tmodelKey : tmodelKeys) {
102103 tblCount++;
104 String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
105 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
106107if (count + 1 < tmodelKeys.size()) {
108if (fq.isOrAllKeys())
109 qry.OR().pad();
110else111 qry.AND().pad();
112 }
113114 count++;
115 }
116 qry.closeParen().pad();
117118 }
119120/*121 * Appends the necessary join table for the child entity and additional tables for when keys are AND'd. This is the default behavior 122 * so only need to add additional tables if "orAllKeys" has not been set.123 */124publicstaticvoid appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
125126if (tmodelKeys != null && tmodelKeys.size() > 0) {
127128 StringBuffer thetaJoins = new StringBuffer(200);
129int tblCount = 0;
130for(int count = 0; count < tmodelKeys.size(); count++) {
131if (count != 0) {
132if (!fq.isOrAllKeys()) {
133 tblCount++;
134 qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild + tblCount).pad();
135 thetaJoins.append(entityAliasChild + (tblCount - 1) + "." + ENTITY_FIELD + "." + KEY_NAME + " = " + entityAliasChild + tblCount + "." + ENTITY_FIELD + "." + KEY_NAME + " ");
136 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
137 }
138 }
139else {
140 qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild + tblCount).pad();
141 thetaJoins.append(ENTITY_ALIAS + "." + KEY_NAME + " = " + entityAliasChild + tblCount + "." + ENTITY_FIELD + "." + KEY_NAME + " ");
142 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
143 }
144 }
145146 qry.WHERE().pad().openParen().pad();
147148 String thetaJoinsStr = thetaJoins.toString();
149if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
150 thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
151 qry.append(thetaJoinsStr);
152153 qry.closeParen().pad();
154if (fq!=null && fq.isSignaturePresent()) {
155 qry.AND().pad().openParen().pad().append(BindingTemplateQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
156 }
157 }
158 }
159160 }