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;
2122import javax.persistence.EntityManager;
2324import org.apache.commons.logging.Log;
25import org.apache.commons.logging.LogFactory;
26import org.apache.juddi.query.util.DynamicQuery;
27import org.apache.juddi.query.util.FindQualifiers;
28import org.uddi.api_v3.Name;
2930/**31 * 32 * Returns the list of tmodel keys possessing the passed Name argument.33 * Output is restricted by list of tModel keys passed in. If null, all tModels are searched.34 * Output is produced by building the appropriate JPA query based on input and find qualifiers.35 * 36 * From specification:37 * "This string value represents the name of the tModel elements to be found. Since tModel data only has a single 38 * name, only a single name may be passed. The argument must match exactly since "exactMatch" is the default behavior, 39 * but if the "approximateMatch" findQualifier is used together with the appropriate wildcard character, then matching 40 * is done according to wildcard rules. See Section 5.1.6 About Wildcards for additional information. The name MAY be 41 * marked with an xml:lang adornment. If a language markup is specified, the search results report a match only on those 42 * entries that match both the name value and language criteria. The match on language is a leftmost case-insensitive 43 * comparison of the characters supplied. This allows one to find all tModels whose name begins with an "A" and are expressed 44 * in any dialect of French, for example. Values which can be passed in the language criteria adornment MUST obey the rules 45 * governing the xml:lang data type as defined in Section 3.3.2.3 name."46 * 47 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>48 */49publicclassFindTModelByNameQueryextendsTModelQuery {
5051 @SuppressWarnings("unused")
52privatestatic Log log = LogFactory.getLog(FindTModelByNameQuery.class);
5354publicstatic List<Object> select(EntityManager em, FindQualifiers fq, Name name, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
55// If keysIn is not null and empty, then search is over.56if ((keysIn != null) && (keysIn.isEmpty()))
57return keysIn;
5859if (name == null)
60return keysIn;
6162 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
63 appendConditions(dynamicQry, fq, name);
64// Since this is a tModel, don't need to search the lazily deleted ones.65 dynamicQry.AND().pad().appendGroupedAnd(new DynamicQuery.Parameter(ENTITY_ALIAS + ".deleted", false, DynamicQuery.PREDICATE_EQUALS));
66if (restrictions != null && restrictions.length > 0)
67 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
6869return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
70 }
7172publicstaticvoid appendConditions(DynamicQuery qry, FindQualifiers fq, Name name) {
73 String namePredicate = DynamicQuery.PREDICATE_EQUALS;
74if (fq!=null && fq.isApproximateMatch()) {
75 namePredicate = DynamicQuery.PREDICATE_LIKE;
76 }
7778 qry.WHERE().pad().openParen().pad();
7980 String nameTerm = ENTITY_ALIAS + ".name";
81 String nameValue = name.getValue();
82if (fq!=null && fq.isCaseInsensitiveMatch()) {
83 nameTerm = "upper(" + ENTITY_ALIAS + ".name)";
84 nameValue = name.getValue().toUpperCase();
85 }
86// JUDDI-235: wildcards are provided by user (only commenting in case a new interpretation arises)87//if (fq.isApproximateMatch())88// nameValue = nameValue.endsWith(DynamicQuery.WILDCARD)?nameValue:nameValue + DynamicQuery.WILDCARD;8990if (name.getLang() == null || name.getLang().length() == 0 ) {
91 qry.appendGroupedAnd(new DynamicQuery.Parameter(nameTerm, nameValue, namePredicate));
92 }
93else {
94 String langValue = name.getLang().endsWith(DynamicQuery.WILDCARD)?name.getLang().toUpperCase():name.getLang().toUpperCase() + DynamicQuery.WILDCARD;
95 qry.appendGroupedAnd(new DynamicQuery.Parameter(nameTerm, nameValue, namePredicate),
96new DynamicQuery.Parameter("upper(" + ENTITY_ALIAS + ".langCode)", langValue, DynamicQuery.PREDICATE_LIKE));
97 }
9899 qry.closeParen().pad();
100if (fq!=null && fq.isSignaturePresent()) {
101 qry.AND().pad().openParen().pad().append(TModelQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
102 }
103 }
104105 }