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;
21import javax.persistence.EntityManager;
2223import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.juddi.query.util.DynamicQuery;
26import org.apache.juddi.query.util.FindQualifiers;
27import org.uddi.api_v3.DiscoveryURLs;
28import org.uddi.api_v3.DiscoveryURL;
2930/**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 */44publicclassFindBusinessByDiscoveryURLQueryextendsBusinessEntityQuery {
4546 @SuppressWarnings("unused")
47privatestatic Log log = LogFactory.getLog(FindBusinessByDiscoveryURLQuery.class);
4849privatestaticfinal String ENTITY_NAME_CHILD = "DiscoveryUrl";
5051privatestaticfinal String entityAliasChild;;
5253static {
54 entityAliasChild = buildAlias(ENTITY_NAME_CHILD);
55 }
5657publicstatic 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.60if ((keysIn != null) && (keysIn.size() == 0))
61return keysIn;
6263if (discURLs == null)
64return keysIn;
6566 List<DiscoveryURL> discURLlist = discURLs.getDiscoveryURL();
67if (discURLlist == null || discURLlist.size() == 0)
68return keysIn;
6970 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
71 appendConditions(dynamicQry, fq, discURLlist);
72if (restrictions != null && restrictions.length > 0)
73 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
7475return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
76 }
7778/*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 */82publicstaticvoid appendConditions(DynamicQuery qry, FindQualifiers fq, List<DiscoveryURL> discURLs) {
8384// 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();
8788int count = 0;
89for(DiscoveryURL discURL : discURLs) {
90 String urlTerm = entityAliasChild + ".url";
91 String urlValue = discURL.getValue();
9293if (discURL.getUseType() == null || discURL.getUseType().length() == 0 ) {
94 qry.appendGroupedAnd(new DynamicQuery.Parameter(urlTerm, urlValue, DynamicQuery.PREDICATE_LIKE));
95 }
96else {
97 qry.appendGroupedAnd(new DynamicQuery.Parameter(urlTerm, urlValue, DynamicQuery.PREDICATE_LIKE),
98new DynamicQuery.Parameter(entityAliasChild + ".useType", discURL.getUseType(), DynamicQuery.PREDICATE_EQUALS));
99 }
100101if (count + 1 < discURLs.size())
102 qry.OR().pad();
103104 count++;
105 }
106 qry.closeParen().pad();
107108 }
109110/*111 * Appends the necessary join table for the child entity 112 */113publicstaticvoid 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 }
119120 }