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.Collections;
21import java.util.List;
22import java.util.UUID;
2324import javax.persistence.EntityManager;
2526import org.apache.commons.configuration.ConfigurationException;
27import org.apache.commons.logging.Log;
28import org.apache.commons.logging.LogFactory;
29import org.apache.juddi.config.AppConfig;
30import org.apache.juddi.config.Property;
31import org.apache.juddi.query.util.DynamicQuery;
32import org.apache.juddi.query.util.FindQualifiers;
33import org.uddi.v3_service.DispositionReportFaultMessage;
34import org.uddi.api_v3.ListDescription;
3536/**37 * The "select" method retrieves all the entities for the input key list and sorts according to the user settings. Paging is taken into account when retrieving 38 * the results. The result is a list of the entity objects containing all top level elements (restricted to the given page). 39 * 40 * NOTE: Results usually need multiple one-to-many collections to be fetched. Although this could be done in one query with eager fetching, this strategy is not41 * recommended as it will lead to a potentially large Cartesian product. Therefore, the collections are initialized in separate queries during the mapping 42 * stage. If the returned results are small (maxRows parameters is set appropriately), a single query is likely faster, but probably not by enough to consider 43 * the optimization under these conditions.44 * 45 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>46 */47publicclassFetchBindingTemplatesQueryextendsBindingTemplateQuery {
4849privatestaticfinal Log log = LogFactory.getLog(FetchBindingTemplatesQuery.class);
5051protectedstaticfinal String selectSQL;
5253static {
54 StringBuilder sql = new StringBuilder(200);
55 sql.append("select " + ENTITY_ALIAS + " from " + ENTITY_NAME + " " + ENTITY_ALIAS + " ");
56 selectSQL = sql.toString();
57 }
5859publicstatic List<?> select(EntityManager em, FindQualifiers fq, List<?> keysIn, Integer maxRowsUser, Integer listHead, ListDescription listDesc, DynamicQuery.Parameter... restrictions) throws DispositionReportFaultMessage {
6061// If keysIn is null or empty, then nothing to fetch.62if ((keysIn == null) || (keysIn.size() == 0))
63return Collections.emptyList();
64int maxRows = DEFAULT_MAXROWS;
65try {
66 maxRows = AppConfig.getConfiguration().getInteger(Property.JUDDI_MAX_ROWS, DEFAULT_MAXROWS);
67 }
68catch(ConfigurationException ce) {
69 log.error("Configuration exception occurred retrieving: " + Property.JUDDI_MAX_ROWS);
70 }
71 DynamicQuery dynamicQry = newDynamicQuery(selectSQL);
72if (keysIn.size() > maxRows) {
73 UUID uuid = UUID.randomUUID();
74 storeIntermediateKeySetResults(em, uuid.toString(), keysIn);
75 appendTempTable(dynamicQry);
76 appendSortTables(dynamicQry);
77 appendTempJoin(dynamicQry, uuid.toString());
78 }
79else {
80 appendSortTables(dynamicQry);
81 dynamicQry.appendInListWithAnd(ENTITY_ALIAS + "." + KEY_NAME, keysIn);
82 }
83if (restrictions != null && restrictions.length > 0)
84 dynamicQry.AND().pad().appendGroupedAnd(restrictions);
8586 appendSortCriteria(dynamicQry, fq);
8788 log.debug(dynamicQry);
8990return getPagedResult(em, dynamicQry, maxRowsUser, listHead, listDesc);
91 }
9293privatestaticvoid appendTempTable(DynamicQuery qry) {
94 qry.comma().append(TEMP_ENTITY_NAME + " " + TEMP_ENTITY_ALIAS );
95 }
9697privatestaticvoid appendTempJoin(DynamicQuery qry, String uuid) {
98 qry.pad().AND().pad().append(TEMP_ENTITY_PK_KEY_NAME).append(DynamicQuery.PREDICATE_EQUALS);
99 qry.pad().append(ENTITY_ALIAS + "." + KEY_NAME);
100 qry.pad().AND().pad().append(TEMP_ENTITY_PK_TXID_NAME).append(DynamicQuery.PREDICATE_EQUALS);
101 qry.append("'" + uuid + "'").pad();
102 }
103104privatestaticvoid appendSortTables(DynamicQuery qry) {
105// BindingTemplates can only be sorted by date.106 qry.WHERE().pad().append("1=1").pad();
107 }
108109/*110 * BindingTemplates can only be sorted by date.111 */112privatestaticvoid appendSortCriteria(DynamicQuery qry, FindQualifiers fq) {
113114 qry.ORDERBY().pad();
115116if (fq.isSortByDateAsc())
117 qry.append(ENTITY_ALIAS + ".modified").pad().append(DynamicQuery.SORT_ASC);
118else119 qry.append(ENTITY_ALIAS + ".modified").pad().append(DynamicQuery.SORT_DESC);
120121 qry.pad();
122123 }
124125 }