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 business keys with services that have bindings that possess the tModels in the passed tModelBag.31 * Output is restricted by list of service keys passed in. If null, all services are searched.32 * Output is produced by building the appropriate JPA query based on input and find qualifiers.33 * 34 * From specification:35 * "Every Web service instance exposed by a registered businessEntity is represented in UDDI by a bindingTemplate contained 36 * within the businessEntity. Each bindingTemplate contains a collection of tModel references called its "technical fingerprint" 37 * that specifies its type. The tModelBag argument is a collection of tModelKey elements specifying that the search results are 38 * to be limited to businesses that expose Web services with technical fingerprints that match.39 *40 * If a find_tModel argument is specified (see above), it is treated as an embedded inquiry. The tModelKeys returned as a result 41 * of this embedded find_tModel argument are used as if they had been supplied in a tModelBag argument. Changing the order of the 42 * keys in the collection or specifying the same tModelKey more than once does not change the behavior of the find. 43 *44 * By default, only bindingTemplates that contain all of the tModelKeys in the technical fingerprint match (logical AND). Specifying 45 * appropriate findQualifiers can override this behavior so that bindingTemplates containing any of the specified tModelKeys match 46 * (logical OR)."47 * 48 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>49 */50publicclassFindBusinessByTModelKeyQueryextendsBusinessEntityQuery {
5152 @SuppressWarnings("unused")
53privatefinalstatic Log log = LogFactory.getLog(FindBusinessByTModelKeyQuery.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, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
64// If keysIn is not null and empty, then search is over.65if ((keysIn != null) && (keysIn.isEmpty()))
66return keysIn;
6768if (tModels == null)
69return keysIn;
7071 List<String> tmodelKeys = tModels.getTModelKey();
72if (tmodelKeys == null || tmodelKeys.isEmpty())
73return keysIn;
7475 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
76 appendConditions(dynamicQry, fq, tmodelKeys);
7778if (restrictions != null && restrictions.length > 0)
79 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
8081return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
82 }
838485/*86 * Appends the conditions to the query based on the tModelKey list. With the default or when "orAllKeys" is passed, the keyedReferences are autonomous and are87 * all AND'd or OR'd respectively. 88 * 89 */90publicstaticvoid appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
9192// Append the necessary tables (one will always be added connecting the entity to its instanceinfo table).93 appendJoinTables(qry, fq, tmodelKeys);
94 qry.AND().pad().openParen().pad();
9596int count = 0;
97int tblCount = -1;
98for(String tmodelKey : tmodelKeys) {
99100 tblCount++;
101 String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
102 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
103104if (count + 1 < tmodelKeys.size()) {
105if (fq.isOrAllKeys())
106 qry.OR().pad();
107else108 qry.AND().pad();
109 }
110111 count++;
112 }
113 qry.closeParen().pad();
114115 }
116117/*118 * Appends the necessary join table for the child entity and additional tables for when keys are AND'd. This is the default behavior 119 * so only need to add additional tables if "orAllKeys" has not been set.120 */121publicstaticvoid appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
122123124if (tmodelKeys != null && tmodelKeys.size() > 0) {
125 qry.comma().pad().append(BusinessServiceQuery.ENTITY_NAME + " " + BusinessServiceQuery.ENTITY_ALIAS).pad();
126 qry.comma().pad().append(BindingTemplateQuery.ENTITY_NAME + " " + BindingTemplateQuery.ENTITY_ALIAS).pad();
127128 StringBuilder thetaJoins = new StringBuilder(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) + "." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " = " + entityAliasChild + tblCount + "." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.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(BindingTemplateQuery.ENTITY_ALIAS + "." + BindingTemplateQuery.KEY_NAME + " = " + entityAliasChild + tblCount + "." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " ");
142 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
143 }
144 }
145146 qry.WHERE().pad().openParen().pad();
147148 qry.append(ENTITY_ALIAS + "." + KEY_NAME + " = " + BusinessServiceQuery.ENTITY_ALIAS + "." + ENTITY_FIELD + "." + KEY_NAME).pad();
149 qry.AND().pad().append(BusinessServiceQuery.ENTITY_ALIAS + "." + BusinessServiceQuery.KEY_NAME + " = " + BindingTemplateQuery.ENTITY_ALIAS + "." + BusinessServiceQuery.ENTITY_FIELD + "." + BusinessServiceQuery.KEY_NAME).pad();
150 qry.AND().pad();
151152 String thetaJoinsStr = thetaJoins.toString();
153if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
154 thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
155 qry.append(thetaJoinsStr);
156157 qry.closeParen().pad();
158if (fq!=null && fq.isSignaturePresent()) {
159 qry.AND().pad().openParen().pad().append(BusinessEntityQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
160 }
161 }
162 }
163164 }