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  
22  import javax.persistence.EntityManager;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.query.util.DynamicQuery;
27  import org.apache.juddi.query.util.FindQualifiers;
28  import org.uddi.api_v3.TModelBag;
29  
30  /**
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   */
52  public class FindServiceByTModelKeyQuery extends BusinessServiceQuery {
53  	
54  	@SuppressWarnings("unused")
55  	private static Log log = LogFactory.getLog(FindServiceByTModelKeyQuery.class);
56  
57  	public static final String ENTITY_NAME_CHILD = "TmodelInstanceInfo";
58  
59  	protected static final String entityAliasChild;
60  	
61  	static {
62  		entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
63  	}
64  
65  	public static 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.
67  		if ((keysIn != null) && (keysIn.isEmpty()))
68  			return keysIn;
69  		
70  		if (tModels == null)
71  			return keysIn;
72  		
73  		List<String> tmodelKeys = tModels.getTModelKey();
74  		if (tmodelKeys == null || tmodelKeys.isEmpty())
75  			return keysIn;
76  		
77  		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
78  		appendConditions(dynamicQry, fq, tmodelKeys);
79  		if (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));
82  		
83  		if (restrictions != null && restrictions.length > 0)
84  			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
85  		
86  		return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
87  	}
88  	
89  	
90  	/*
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 are
92  	 * all AND'd or OR'd respectively.  
93  	 *	 
94  	 */
95  	public static void appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
96  		
97  		// 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();
100 		
101 		int count = 0;
102 		int tblCount = -1;
103 		for(String tmodelKey : tmodelKeys) {
104 			
105 			tblCount++;
106 			String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
107 			qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
108 			
109 			if (count + 1 < tmodelKeys.size()) {
110 				if (fq.isOrAllKeys())
111 					qry.OR().pad();
112 				else
113 					qry.AND().pad();
114 			}
115 			
116 			count++;
117 		}
118 		qry.closeParen().pad();
119 		
120 	}
121 	
122 	/*
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 	 */
126 	public static void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
127 		
128 
129 		if (tmodelKeys != null && tmodelKeys.size() > 0) {
130 			qry.comma().pad().append(BindingTemplateQuery.ENTITY_NAME + " " + BindingTemplateQuery.ENTITY_ALIAS).pad();
131 			
132 			StringBuilder thetaJoins = new StringBuilder(200);
133 			int tblCount = 0;
134 			for(int count = 0; count < tmodelKeys.size(); count++) {
135 				if (count != 0) {
136 					if (!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 				}
143 				else {
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 			}
149 			
150 			qry.WHERE().pad().openParen().pad();
151 			
152 			qry.append(ENTITY_ALIAS + "." + KEY_NAME + " = " + BindingTemplateQuery.ENTITY_ALIAS + "." + ENTITY_FIELD + "." + KEY_NAME).pad();
153 			qry.AND().pad();
154 		
155 			String thetaJoinsStr = thetaJoins.toString();
156 			if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
157 				thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
158 			qry.append(thetaJoinsStr);
159 
160 			qry.closeParen().pad();
161 			if (fq!=null && fq.isSignaturePresent()) {
162 				qry.AND().pad().openParen().pad().append(BusinessServiceQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
163 			}
164 		}
165 	}
166 	
167 }