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  import javax.persistence.Query;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.juddi.model.PublisherAssertion;
28  import org.apache.juddi.query.util.DynamicQuery;
29  import org.uddi.api_v3.CompletionStatus;
30  
31  /**
32   * 
33   * Returns the list of PublisherAssertions that contain the input businessKeys as their from or to key
34   *  * 
35   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
36   */
37  public class FindPublisherAssertionByBusinessQuery extends PublisherAssertionQuery {
38  
39  	private static Log log = LogFactory.getLog(FindPublisherAssertionByBusinessQuery.class);
40  
41  	@SuppressWarnings("unchecked")
42  	public static List<PublisherAssertion> select(EntityManager em, List<?> businessKeys, CompletionStatus completionStatus) {
43  		if ((businessKeys == null) || (businessKeys.isEmpty()))
44  			return null;
45  		
46  		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
47  		appendConditions(dynamicQry, businessKeys, completionStatus);
48  		
49  		log.debug(dynamicQry);
50  		
51  		Query qry = dynamicQry.buildJPAQuery(em);
52  		List<PublisherAssertion> result = qry.getResultList();
53  		
54  		return result;
55  	}
56  	
57  	/*
58  	 * Appends the conditions to the query based on the businessKey list.  The keys can either be in the fromKey or toKey of the publisher assertion.
59  	 */
60  	public static void appendConditions(DynamicQuery qry, List<?> businessKeys, CompletionStatus completionStatus) {
61  		
62  		qry.WHERE().pad().openParen().pad();
63  		
64  		qry.appendInList(ENTITY_ALIAS + "." + FROM_KEY_NAME, businessKeys);
65  		qry.pad().OR().pad();
66  		qry.appendInList(ENTITY_ALIAS + "." + TO_KEY_NAME, businessKeys);
67  		qry.closeParen().pad();
68  		
69  		if (completionStatus != null) {
70  			qry.AND().pad().openParen().pad();
71  			
72  			String fromCheckTerm = "UPPER(" + ENTITY_ALIAS + ".fromCheck)";
73  			String toCheckTerm = "UPPER(" + ENTITY_ALIAS + ".toCheck)";
74  			if (completionStatus == CompletionStatus.STATUS_BOTH_INCOMPLETE) {
75  				qry.appendGroupedAnd(new DynamicQuery.Parameter(fromCheckTerm, "TRUE", DynamicQuery.PREDICATE_NOTEQUALS));
76  				qry.AND().pad();
77  				qry.appendGroupedAnd(new DynamicQuery.Parameter(toCheckTerm, "TRUE", DynamicQuery.PREDICATE_NOTEQUALS));
78  			}
79  			else if (completionStatus == CompletionStatus.STATUS_COMPLETE) {
80  				qry.appendGroupedAnd(new DynamicQuery.Parameter(fromCheckTerm, "TRUE", DynamicQuery.PREDICATE_EQUALS));
81  				qry.AND().pad();
82  				qry.appendGroupedAnd(new DynamicQuery.Parameter(toCheckTerm, "TRUE", DynamicQuery.PREDICATE_EQUALS));
83  			}
84  			else if (completionStatus == CompletionStatus.STATUS_FROM_KEY_INCOMPLETE) {
85  				qry.appendGroupedAnd(new DynamicQuery.Parameter(fromCheckTerm, "TRUE", DynamicQuery.PREDICATE_NOTEQUALS));
86  				qry.AND().pad();
87  				qry.appendGroupedAnd(new DynamicQuery.Parameter(toCheckTerm, "TRUE", DynamicQuery.PREDICATE_EQUALS));
88  			}
89  			else if (completionStatus == CompletionStatus.STATUS_TO_KEY_INCOMPLETE) {
90  				qry.appendGroupedAnd(new DynamicQuery.Parameter(fromCheckTerm, "TRUE", DynamicQuery.PREDICATE_EQUALS));
91  				qry.AND().pad();
92  				qry.appendGroupedAnd(new DynamicQuery.Parameter(toCheckTerm, "TRUE", DynamicQuery.PREDICATE_NOTEQUALS));
93  			}
94  
95  			qry.closeParen().pad();
96  		}
97  
98  		
99  	}
100 	
101 	
102 }