This project has retired. For details please refer to its Attic page.
FindBusinessByDiscoveryURLQuery 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.DiscoveryURLs;
28  import org.uddi.api_v3.DiscoveryURL;
29  
30  /**
31   * 
32   * Returns the list of business keys possessing the DiscoveryUrls in the passed DiscoveryUrl list.
33   * Output is restricted by list of business keys passed in.  If null, all businesses are searched.
34   * Output is produced by building the appropriate JPA query based on input and find qualifiers.
35   * 
36   * From specification:
37   * "This is a list of discoveryURL structures to be matched against the discoveryURL data associated with registered businessEntity 
38   * information.  To search for URL without regard to useType attribute values, omit the useType attribute or pass it as an empty 
39   * attribute.  If useType values are included, the match occurs only on registered information that matches both the useType and 
40   * URL value.  The returned businessList contains businessInfo structures matching any of the URL's passed (logical OR)."
41   * 
42   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
43   */
44  public class FindBusinessByDiscoveryURLQuery extends BusinessEntityQuery {
45  
46  	@SuppressWarnings("unused")
47  	private static Log log = LogFactory.getLog(FindBusinessByDiscoveryURLQuery.class);
48  
49  	private static final String ENTITY_NAME_CHILD = "DiscoveryUrl";
50  
51  	private static final String entityAliasChild;;
52  	
53  	static {
54  		entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
55  	}
56  
57  	public static List<Object> select(EntityManager em, FindQualifiers fq, DiscoveryURLs discURLs, 
58  			List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
59  		// If keysIn is not null and empty, then search is over.
60  		if ((keysIn != null) && (keysIn.size() == 0))
61  			return keysIn;
62  		
63  		if (discURLs == null)
64  			return keysIn;
65  		
66  		List<DiscoveryURL> discURLlist = discURLs.getDiscoveryURL();
67  		if (discURLlist == null || discURLlist.size() == 0)
68  			return keysIn;
69  		
70  		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
71  		appendConditions(dynamicQry, fq, discURLlist);
72  		if (restrictions != null && restrictions.length > 0)
73  			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
74  
75  		return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
76  	}
77  	
78  	/*
79  	 * Appends the conditions to the query based on the discovery url list.  By default, the urls are OR'd and this cannot be changed. 
80  	 *	 
81  	 */
82  	public static void appendConditions(DynamicQuery qry, FindQualifiers fq, List<DiscoveryURL> discURLs) {
83  		
84  		// Append the necessary tables (one will always be added connecting the entity to its discovery url table).
85  		appendJoinTables(qry, fq, discURLs);
86  		qry.AND().pad().openParen().pad();
87  
88  		int count = 0;
89  		for(DiscoveryURL discURL : discURLs) {
90  			String urlTerm = entityAliasChild + ".url";
91  			String urlValue = discURL.getValue();
92  			
93  			if (discURL.getUseType() == null || discURL.getUseType().length() == 0 ) {
94  				qry.appendGroupedAnd(new DynamicQuery.Parameter(urlTerm, urlValue, DynamicQuery.PREDICATE_LIKE));
95  			}
96  			else {
97  				qry.appendGroupedAnd(new DynamicQuery.Parameter(urlTerm, urlValue, DynamicQuery.PREDICATE_LIKE), 
98  									 new DynamicQuery.Parameter(entityAliasChild + ".useType", discURL.getUseType(), DynamicQuery.PREDICATE_EQUALS));
99  			}
100 			
101 			if (count + 1 < discURLs.size())
102 				qry.OR().pad();
103 			
104 			count++;
105 		}
106 		qry.closeParen().pad();
107 		
108 	}
109 	
110 	/*
111 	 * Appends the necessary join table for the child entity 
112 	 */
113 	public static void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<DiscoveryURL> discURLs) {
114 		qry.comma().pad().append(ENTITY_NAME_CHILD + " " + entityAliasChild).pad();
115 		qry.WHERE().pad().openParen().pad();
116 		qry.append(ENTITY_ALIAS + "." + KEY_NAME + " = " + entityAliasChild + "." + ENTITY_FIELD + "." + KEY_NAME + " ");
117 		qry.closeParen().pad();
118 	}
119 	
120 }