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;
21import javax.persistence.EntityManager;
2223import org.apache.juddi.query.util.DynamicQuery;
24import org.apache.juddi.query.util.FindQualifiers;
25import org.apache.juddi.model.UddiEntityPublisher;
2627/**28 * Returns the list of "entity" keys possessing the publisher Id in the passed in UddiEntityPublisher29 * 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 */34publicclassFindEntityByPublisherQueryextendsEntityQuery {
3536publicstaticfinal String AUTHORIZED_NAME_FIELD = "authorizedName";
3738privatefinal String entityName;
39privatefinal String entityAlias;
40privatefinal String keyName;
41privatefinal String selectSQL;
42private String signaturePresent;
4344publicFindEntityByPublisherQuery(String entityName, String entityAlias, String keyName, String signaturePresent) {
45this.entityName = entityName;
46this.entityAlias = entityAlias;
47this.keyName = keyName;
48this.signaturePresent = signaturePresent;
4950 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 }
5455public String getEntityName() {
56return entityName;
57 }
5859public String getEntityAlias() {
60return entityAlias;
61 }
6263public String getKeyName() {
64return keyName;
65 }
6667public String getSelectSQL() {
68return selectSQL;
69 }
7071public String getSignaturePresent() {
72return signaturePresent;
73 }
7475publicvoid setSignaturePresent(String signaturePresent) {
76this.signaturePresent = signaturePresent;
77 }
787980public 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.82if ((keysIn != null) && (keysIn.size() == 0))
83return keysIn;
8485if (publisher == null)
86return keysIn;
8788 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
89 appendConditions(dynamicQry, fq, publisher);
90if (restrictions != null && restrictions.length > 0)
91 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
9293return getQueryResult(em, dynamicQry, keysIn, entityAlias + "." + keyName);
94 }
9596/*97 * Appends the conditions to the query based on the publisher id98 */99publicvoid appendConditions(DynamicQuery qry, FindQualifiers fq, UddiEntityPublisher publisher) {
100 qry.WHERE().pad();
101if (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 }
106107108 }