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;
2122import javax.persistence.EntityManager;
2324import org.apache.commons.logging.Log;
25import org.apache.commons.logging.LogFactory;
26import org.apache.juddi.query.util.DynamicQuery;
27import org.apache.juddi.query.util.FindQualifiers;
28import org.uddi.api_v3.TModelBag;
2930/**31 * Returns the list of service keys with bindings that possess the tModels in the passed tModelBag.32 * Output is restricted by list of service keys passed in. If null, all services are searched.33 * Output is produced by building the appropriate JPA query based on input and find qualifiers.34 * 35 * From specification:36 * "Every Web service instance is represented in UDDI by a bindingTemplate contained within some businessService. A 37 * bindingTemplate contains a collection of tModel references called its "technical fingerprint" that specifies its type. 38 * The tModelBag argument is a collection of tModelKey values specifying that the search results are to be limited to 39 * businessServices containing bindingTemplates with technical fingerprints that match.40 *41 * If a find_tModel argument is specified (see below), it is treated as an embedded inquiry. The tModelKeys returned as a result 42 * of this embedded find_tModel argument are used as if they had been supplied in a tModelBag argument. Changing the order of 43 * the keys in the collection or specifying the same tModelKey more than once does not change the behavior of the find. 44 *45 * By default, only bindingTemplates that contain all of the tModelKeys in the technical fingerprint match (logical AND). Specifying 46 * appropriate findQualifiers can override this behavior so that bindingTemplates containing any of the specified tModelKeys 47 * match (logical OR)."48 * 49 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>50 * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>51 */52publicclassFindServiceByTModelKeyQueryextendsBusinessServiceQuery {
5354 @SuppressWarnings("unused")
55privatestatic Log log = LogFactory.getLog(FindServiceByTModelKeyQuery.class);
5657publicstaticfinal String ENTITY_NAME_CHILD = "TmodelInstanceInfo";
5859protectedstaticfinal String entityAliasChild;
6061static {
62 entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
63 }
6465publicstatic List<Object> select(EntityManager em, FindQualifiers fq, TModelBag tModels, String parentKey, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
66// If keysIn is not null and empty, then search is over.67if ((keysIn != null) && (keysIn.isEmpty()))
68return keysIn;
6970if (tModels == null)
71return keysIn;
7273 List<String> tmodelKeys = tModels.getTModelKey();
74if (tmodelKeys == null || tmodelKeys.isEmpty())
75return keysIn;
7677 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
78 appendConditions(dynamicQry, fq, tmodelKeys);
79if (parentKey != null && parentKey.length() > 0)
80 dynamicQry.AND().pad().appendGroupedAnd(new DynamicQuery.Parameter(BusinessServiceQuery.ENTITY_ALIAS + "." + BusinessServiceQuery.KEY_NAME_PARENT, parentKey, DynamicQuery.PREDICATE_EQUALS));
81//dynamicQry.AND().pad().appendGroupedAnd(new DynamicQuery.Parameter(BindingTemplateQuery.ENTITY_ALIAS + "." + BindingTemplateQuery.KEY_NAME_PARENT, parentKey, DynamicQuery.PREDICATE_EQUALS));8283if (restrictions != null && restrictions.length > 0)
84 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
8586return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
87 }
888990/*91 * Appends the conditions to the query based on the tModelKey list. With the default or when "orAllKeys" is passed, the keyedReferences are autonomous and are92 * all AND'd or OR'd respectively. 93 * 94 */95publicstaticvoid appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
9697// Append the necessary tables (one will always be added connecting the entity to its instanceinfo table).98 appendJoinTables(qry, fq, tmodelKeys);
99 qry.AND().pad().openParen().pad();
100101int count = 0;
102int tblCount = -1;
103for(String tmodelKey : tmodelKeys) {
104105 tblCount++;
106 String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
107 qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
108109if (count + 1 < tmodelKeys.size()) {
110if (fq.isOrAllKeys())
111 qry.OR().pad();
112else113 qry.AND().pad();
114 }
115116 count++;
117 }
118 qry.closeParen().pad();
119120 }
121122/*123 * Appends the necessary join table for the child entity and additional tables for when keys are AND'd. This is the default behavior 124 * so only need to add additional tables if "orAllKeys" has not been set.125 */126publicstaticvoid appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
127128129if (tmodelKeys != null && tmodelKeys.size() > 0) {
130 qry.comma().pad().append(BindingTemplateQuery.ENTITY_NAME + " " + BindingTemplateQuery.ENTITY_ALIAS).pad();
131132 StringBuilder thetaJoins = new StringBuilder(200);
133int tblCount = 0;
134for(int count = 0; count < tmodelKeys.size(); count++) {
135if (count != 0) {
136if (!fq.isOrAllKeys()) {
137 tblCount++;
138 qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild + tblCount).pad();
139 thetaJoins.append(entityAliasChild).append(tblCount - 1).append("." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " = ").append(entityAliasChild).append(tblCount).append("." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " ");
140 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
141 }
142 }
143else {
144 qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild + tblCount).pad();
145 thetaJoins.append(BindingTemplateQuery.ENTITY_ALIAS + "." + BindingTemplateQuery.KEY_NAME + " = ").append(entityAliasChild).append(tblCount).append("." + BindingTemplateQuery.ENTITY_FIELD + "." + BindingTemplateQuery.KEY_NAME + " ");
146 thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
147 }
148 }
149150 qry.WHERE().pad().openParen().pad();
151152 qry.append(ENTITY_ALIAS + "." + KEY_NAME + " = " + BindingTemplateQuery.ENTITY_ALIAS + "." + ENTITY_FIELD + "." + KEY_NAME).pad();
153 qry.AND().pad();
154155 String thetaJoinsStr = thetaJoins.toString();
156if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
157 thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
158 qry.append(thetaJoinsStr);
159160 qry.closeParen().pad();
161if (fq!=null && fq.isSignaturePresent()) {
162 qry.AND().pad().openParen().pad().append(BusinessServiceQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
163 }
164 }
165 }
166167 }