This project has retired. For details please refer to its Attic page.
FindBusinessByTModelKeyQuery 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.List;
21  import javax.persistence.EntityManager;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.juddi.query.util.DynamicQuery;
26  import org.apache.juddi.query.util.FindQualifiers;
27  import org.uddi.api_v3.TModelBag;
28  
29  /**
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   */
50  public class FindBusinessByTModelKeyQuery extends BusinessEntityQuery {
51  	
52  	@SuppressWarnings("unused")
53  	private final static Log log = LogFactory.getLog(FindBusinessByTModelKeyQuery.class);
54  
55  	public static final String ENTITY_NAME_CHILD = "TmodelInstanceInfo";
56  
57  	protected static final String entityAliasChild;
58  	
59  	static {
60  		entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
61  	}
62  
63  	public static 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.
65  		if ((keysIn != null) && (keysIn.isEmpty()))
66  			return keysIn;
67  		
68  		if (tModels == null)
69  			return keysIn;
70  		
71  		List<String> tmodelKeys = tModels.getTModelKey();
72  		if (tmodelKeys == null || tmodelKeys.isEmpty())
73  			return keysIn;
74  		
75  		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
76  		appendConditions(dynamicQry, fq, tmodelKeys);
77  		
78  		if (restrictions != null && restrictions.length > 0)
79  			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
80  		
81  		return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
82  	}
83  	
84  	
85  	/*
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 are
87  	 * all AND'd or OR'd respectively.  
88  	 *	 
89  	 */
90  	public static void appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
91  		
92  		// 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();
95  		
96  		int count = 0;
97  		int tblCount = -1;
98  		for(String tmodelKey : tmodelKeys) {
99  			
100 			tblCount++;
101 			String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
102 			qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
103 			
104 			if (count + 1 < tmodelKeys.size()) {
105 				if (fq.isOrAllKeys())
106 					qry.OR().pad();
107 				else
108 					qry.AND().pad();
109 			}
110 			
111 			count++;
112 		}
113 		qry.closeParen().pad();
114 		
115 	}
116 	
117 	/*
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 	 */
121 	public static void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
122 		
123 
124 		if (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();
127 			
128 			StringBuilder thetaJoins = new StringBuilder(200);
129 			int tblCount = 0;
130 			for(int count = 0; count < tmodelKeys.size(); count++) {
131 				if (count != 0) {
132 					if (!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 				}
139 				else {
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 			}
145 			
146 			qry.WHERE().pad().openParen().pad();
147 			
148 			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();
151 			
152 			String thetaJoinsStr = thetaJoins.toString();
153 			if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
154 				thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
155 			qry.append(thetaJoinsStr);
156 
157 			qry.closeParen().pad();
158 			if (fq!=null && fq.isSignaturePresent()) {
159 				qry.AND().pad().openParen().pad().append(BusinessEntityQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
160 			}
161 		}
162 	}
163 	
164 }