This project has retired. For details please refer to its Attic page.
FindBindingByTModelKeyQuery 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 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.  
38  
39   * 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   */
50  public class FindBindingByTModelKeyQuery extends BindingTemplateQuery {
51  	
52  	@SuppressWarnings("unused")
53  	private static Log log = LogFactory.getLog(FindBindingByTModelKeyQuery.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, 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;
68  		
69  		if (tModels == null)
70  			return keysIn;
71  		
72  		List<String> tmodelKeys = tModels.getTModelKey();
73  		if (tmodelKeys == null || tmodelKeys.size() == 0)
74  			return keysIn;
75  		
76  		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
77  		appendConditions(dynamicQry, fq, tmodelKeys);
78  		if (parentKey != null && parentKey.length() > 0)
79  			dynamicQry.AND().pad().appendGroupedAnd(new DynamicQuery.Parameter(BindingTemplateQuery.ENTITY_ALIAS + "." + BindingTemplateQuery.KEY_NAME_PARENT, parentKey, DynamicQuery.PREDICATE_EQUALS));
80  		
81  		if (restrictions != null && restrictions.length > 0)
82  			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
83  		
84  		return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
85  	}
86  	
87  	
88  	/*
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 are
90  	 * all AND'd or OR'd respectively.  
91  	 *	 
92  	 */
93  	public static void appendConditions(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
94  		
95  		// 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();
98  		
99  		int count = 0;
100 		int tblCount = -1;
101 		for(String tmodelKey : tmodelKeys) {
102 			
103 			tblCount++;
104 			String tmodelKeyTerm = (fq.isOrAllKeys()?entityAliasChild + "0":entityAliasChild + tblCount) + ".tmodelKey";
105 			qry.appendGroupedAnd(new DynamicQuery.Parameter(tmodelKeyTerm, tmodelKey, DynamicQuery.PREDICATE_EQUALS));
106 			
107 			if (count + 1 < tmodelKeys.size()) {
108 				if (fq.isOrAllKeys())
109 					qry.OR().pad();
110 				else
111 					qry.AND().pad();
112 			}
113 			
114 			count++;
115 		}
116 		qry.closeParen().pad();
117 		
118 	}
119 	
120 	/*
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 	 */
124 	public static void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<String> tmodelKeys) {
125 		
126 		if (tmodelKeys != null && tmodelKeys.size() > 0) {
127 
128 			StringBuffer thetaJoins = new StringBuffer(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) + "." + ENTITY_FIELD + "." + KEY_NAME + " = " + entityAliasChild + tblCount + "." + ENTITY_FIELD + "." + 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(ENTITY_ALIAS + "." + KEY_NAME + " = " + entityAliasChild + tblCount + "." + ENTITY_FIELD + "." + KEY_NAME + " ");
142 					thetaJoins.append(DynamicQuery.OPERATOR_AND + " ");
143 				}
144 			}
145 			
146 			qry.WHERE().pad().openParen().pad();
147 
148 			String thetaJoinsStr = thetaJoins.toString();
149 			if (thetaJoinsStr.endsWith(DynamicQuery.OPERATOR_AND + " "))
150 				thetaJoinsStr = thetaJoinsStr.substring(0, thetaJoinsStr.length() - (DynamicQuery.OPERATOR_AND + " ").length());
151 			qry.append(thetaJoinsStr);
152 
153 			qry.closeParen().pad();
154 			if (fq!=null && fq.isSignaturePresent()) {
155 				qry.AND().pad().openParen().pad().append(BindingTemplateQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
156 			}
157 		}
158 	}
159 	
160 }