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   * Returns the list of "entity" keys possessing the Names in the passed Name list.
32   * Output is restricted by list of "entity" keys passed in.  If null, all entities are searched.
33   * Output is produced by building the appropriate JPA query based on input and find qualifiers.
34   * 
35   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
36   */
37  public class FindEntityByNamesQuery extends EntityQuery {
38  
39  	@SuppressWarnings("unused")
40  	private static Log log = LogFactory.getLog(FindEntityByNamesQuery.class);
41  
42  	private final String entityName;
43  	private final String entityAlias;
44  	private final String keyName;
45  	private final String entityField;
46  	private final String entityNameChild;
47  	private final String entityAliasChild;
48  	private final String selectSQL;
49  	private String signaturePresent;
50  	
51  	public FindEntityByNamesQuery(String entityName, String entityAlias, String keyName, String entityField, 
52  			String entityNameChild, String signaturePresent) {
53  		this.entityName = entityName;
54  		this.entityAlias = entityAlias;
55  		this.keyName = keyName;
56  		this.entityField = entityField;
57  		this.entityNameChild = entityNameChild;
58  		this.entityAliasChild = buildAlias(entityNameChild);
59  		this.signaturePresent = signaturePresent;
60  		
61  		StringBuffer sql = new StringBuffer(200);
62  		sql.append("select distinct " + entityAlias + "." + keyName + " from " + entityName + " " + entityAlias + " ");
63  		selectSQL = sql.toString();
64  	}
65  
66  	public String getEntityName() {
67  		return entityName;
68  	}
69  
70  	public String getEntityAlias() {
71  		return entityAlias;
72  	}
73  
74  	public String getKeyName() {
75  		return keyName;
76  	}
77  	
78  	public String getEntityField() {
79  		return entityField;
80  	}
81  
82  	public String getEntityNameChild() {
83  		return entityNameChild;
84  	}
85  	
86  	public String getEntityAliasChild() {
87  		return entityAliasChild;
88  	}
89  
90  	public String getSelectSQL() {
91  		return selectSQL;
92  	}
93  	
94  	public String getSignaturePresent() {
95  		return signaturePresent;
96  	}
97  
98  	public void setSignaturePresent(String signaturePresent) {
99  		this.signaturePresent = signaturePresent;
100 	}
101 	
102 	public List<Object> select(EntityManager em, FindQualifiers fq, List<Name> names, List<Object> keysIn, DynamicQuery.Parameter... restrictions) {
103 		// If keysIn is not null and empty, then search is over.
104 		if ((keysIn != null) && (keysIn.size() == 0))
105 			return keysIn;
106 		
107 		if (names == null || names.size() == 0)
108 			return keysIn;
109 		
110 		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
111 		appendConditions(dynamicQry, fq, names);
112 		if (restrictions != null && restrictions.length > 0)
113 			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
114 
115 		return getQueryResult(em, dynamicQry, keysIn, entityAlias + "." + keyName);
116 	}
117 	
118 	/*
119 	 * Appends the conditions to the query based on the name list.  	 
120 	 */
121 	public void appendConditions(DynamicQuery qry, FindQualifiers fq, List<Name> names) {
122 
123 		// Append the necessary tables (one will always be added connecting the entity to its name table).
124 		appendJoinTables(qry, fq, names);
125 		qry.AND().pad().openParen().pad();
126 		
127 		String namePredicate = DynamicQuery.PREDICATE_EQUALS;
128 		if (fq.isApproximateMatch()) {
129 			namePredicate = DynamicQuery.PREDICATE_LIKE;
130 		}
131 		
132 		int count = 0;
133 		for(Name n : names) {
134 			String nameTerm = entityAliasChild + ".name";
135 			String nameValue = n.getValue();
136 			if (fq.isCaseInsensitiveMatch()) {
137 				nameTerm = "upper(" + entityAliasChild + ".name)";
138 				nameValue = n.getValue().toUpperCase();
139 			}
140 			// JUDDI-235: wildcards are provided by user (only commenting in case a new interpretation arises)
141 			//if (fq.isApproximateMatch())
142 			//	nameValue = nameValue.endsWith(DynamicQuery.WILDCARD)?nameValue:nameValue + DynamicQuery.WILDCARD;
143 			
144 			if (n.getLang() == null || n.getLang().length() == 0 ) {
145 				qry.appendGroupedAnd(new DynamicQuery.Parameter(nameTerm, nameValue, namePredicate));
146 			}
147 			else {
148 				// Per spec, the language argument is always wildcarded and case insensitive
149 				String langValue = n.getLang().endsWith(DynamicQuery.WILDCARD)?n.getLang().toUpperCase():n.getLang().toUpperCase() + DynamicQuery.WILDCARD;
150 				qry.appendGroupedAnd(new DynamicQuery.Parameter(nameTerm, nameValue, namePredicate), 
151 									 new DynamicQuery.Parameter("upper(" + entityAliasChild + ".langCode)", langValue, DynamicQuery.PREDICATE_LIKE));
152 			}
153 			
154 			if (count + 1 < names.size())
155 				qry.OR().pad();
156 			
157 			count++;
158 		}
159 		qry.closeParen().pad();
160 		
161 	}
162 	
163 	/*
164 	 * Appends the necessary join table for the child entity 
165 	 */
166 	public void appendJoinTables(DynamicQuery qry, FindQualifiers fq, List<Name> names) {
167 		qry.comma().pad().append(entityNameChild + " " + entityAliasChild).pad();
168 		qry.WHERE().pad().openParen().pad();
169 		qry.append(entityAlias + "." + keyName + " = " + entityAliasChild + "." + entityField + "." + keyName + " ");
170 		qry.closeParen().pad();
171 		if (fq!=null && fq.isSignaturePresent()) {
172 			qry.AND().pad().openParen().pad().append(getSignaturePresent()).pad().closeParen().pad();
173 		}
174 	}
175 
176 
177 	
178 }