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.Name;
29  
30  /**
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   */
49  public class FindTModelByNameQuery extends TModelQuery {
50  
51  	@SuppressWarnings("unused")
52  	private static Log log = LogFactory.getLog(FindTModelByNameQuery.class);
53  
54  	public static 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.
56  		if ((keysIn != null) && (keysIn.isEmpty()))
57  			return keysIn;
58  		
59  		if (name == null)
60  			return keysIn;
61  		
62  		DynamicQuery dynamicQry = new DynamicQuery(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));
66  		if (restrictions != null && restrictions.length > 0)
67  			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
68  		
69  		return getQueryResult(em, dynamicQry, keysIn, ENTITY_ALIAS + "." + KEY_NAME);
70  	}
71  	
72  	public static void appendConditions(DynamicQuery qry, FindQualifiers fq, Name name) {
73  		String namePredicate = DynamicQuery.PREDICATE_EQUALS;
74  		if (fq!=null && fq.isApproximateMatch()) {
75  			namePredicate = DynamicQuery.PREDICATE_LIKE;
76  		}
77  
78  		qry.WHERE().pad().openParen().pad();
79  
80  		String nameTerm = ENTITY_ALIAS + ".name";
81  		String nameValue = name.getValue();
82  		if (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;
89  		
90  		if (name.getLang() == null || name.getLang().length() == 0 ) {
91  			qry.appendGroupedAnd(new DynamicQuery.Parameter(nameTerm, nameValue, namePredicate));
92  		}
93  		else {
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), 
96  								 new DynamicQuery.Parameter("upper(" + ENTITY_ALIAS + ".langCode)", langValue, DynamicQuery.PREDICATE_LIKE));
97  		}
98  
99  		qry.closeParen().pad();
100 		if (fq!=null && fq.isSignaturePresent()) {
101 			qry.AND().pad().openParen().pad().append(TModelQuery.SIGNATURE_PRESENT).pad().closeParen().pad();
102 		}
103 	}
104 	
105 }