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  import javax.persistence.EntityManager;
22  
23  import org.apache.juddi.query.util.DynamicQuery;
24  import org.apache.juddi.query.util.FindQualifiers;
25  import org.apache.juddi.model.UddiEntityPublisher;
26  
27  /**
28   * Returns the list of "entity" keys possessing the publisher Id in the passed in UddiEntityPublisher
29   * Output is restricted by list of "entity" keys passed in.  If null, all entities are searched.
30   * Output is produced by building the appropriate JPA query based on input and find qualifiers.
31   * 
32   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
33   */
34  public class FindEntityByPublisherQuery extends EntityQuery {
35  
36  	public static final String AUTHORIZED_NAME_FIELD = "authorizedName";
37  	
38  	private final String entityName;
39  	private final String entityAlias;
40  	private final String keyName;
41  	private final String selectSQL;
42  	private String signaturePresent;
43  	
44  	public FindEntityByPublisherQuery(String entityName, String entityAlias, String keyName, String signaturePresent) {
45  		this.entityName = entityName;
46  		this.entityAlias = entityAlias;
47  		this.keyName = keyName;
48  		this.signaturePresent = signaturePresent;
49  		
50  		StringBuilder sql = new StringBuilder(200);
51  		sql.append("select distinct ").append(entityAlias).append(".").append(keyName).append(" from ").append(entityName).append(" ").append(entityAlias).append(" ");
52  		selectSQL = sql.toString();
53  	}
54  
55  	public String getEntityName() {
56  		return entityName;
57  	}
58  
59  	public String getEntityAlias() {
60  		return entityAlias;
61  	}
62  
63  	public String getKeyName() {
64  		return keyName;
65  	}
66  
67  	public String getSelectSQL() {
68  		return selectSQL;
69  	}
70  	
71  	public String getSignaturePresent() {
72  		return signaturePresent;
73  	}
74  
75  	public void setSignaturePresent(String signaturePresent) {
76  		this.signaturePresent = signaturePresent;
77  	}
78  		
79  	
80  	public List<?> select(EntityManager em, FindQualifiers fq, UddiEntityPublisher publisher, List<?> keysIn, DynamicQuery.Parameter... restrictions) {
81  		// If keysIn is not null and empty, then search is over.
82  		if ((keysIn != null) && (keysIn.size() == 0))
83  			return keysIn;
84  		
85  		if (publisher == null)
86  			return keysIn;
87  		
88  		DynamicQuery dynamicQry = new DynamicQuery(selectSQL);
89  		appendConditions(dynamicQry, fq, publisher);
90  		if (restrictions != null && restrictions.length > 0)
91  			dynamicQry.AND().pad().appendGroupedAnd(restrictions);
92  
93  		return getQueryResult(em, dynamicQry, keysIn, entityAlias + "." + keyName);
94  	}
95  	
96  	/*
97  	 * Appends the conditions to the query based on the publisher id
98  	 */
99  	public void appendConditions(DynamicQuery qry, FindQualifiers fq, UddiEntityPublisher publisher) {
100 		qry.WHERE().pad();
101 		if (fq!=null && fq.isSignaturePresent()) {
102 			qry.pad().openParen().pad().append(getSignaturePresent()).pad().closeParen().pad().AND();
103 		}
104 		qry.appendGroupedAnd(new DynamicQuery.Parameter(entityAlias + "." + AUTHORIZED_NAME_FIELD, publisher.getAuthorizedName(), DynamicQuery.PREDICATE_EQUALS));
105 	}
106 
107 
108 }