001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.outerj.daisy.publisher.serverimpl.requestmodel;
017:
018: import java.util.Map;
019:
020: import org.outerj.daisy.publisher.PublisherException;
021: import org.outerj.daisy.publisher.serverimpl.DummyLexicalHandler;
022: import org.outerj.daisy.repository.Repository;
023: import org.outerj.daisy.repository.RepositoryException;
024: import org.outerj.daisy.repository.query.EvaluationContext;
025: import org.outerj.daisy.repository.query.FacetConf;
026: import org.outerj.daisy.xmlutil.StripDocumentHandler;
027: import org.outerx.daisy.x10.FacetedQueryResultDocument;
028: import org.xml.sax.ContentHandler;
029:
030: public class PerformFacetedQueryRequest extends AbstractRequest
031: implements Request {
032: private String query;
033: private Map<String, String> queryOptions;
034: private FacetConf[] facetConfs;
035:
036: public PerformFacetedQueryRequest(
037: FacetDefinition[] facetDefinitions,
038: String defaultConditions, String defaultOrder,
039: Map<String, String> queryOptions, LocationInfo locationInfo) {
040: super (locationInfo);
041:
042: facetConfs = new FacetConf[facetDefinitions.length];
043:
044: // build query
045: StringBuffer queryBuffer = new StringBuffer();
046: queryBuffer.append("select");
047: for (int i = 0; i < facetDefinitions.length; i++) {
048: FacetDefinition facetDefinition = facetDefinitions[i];
049: queryBuffer.append(" ").append(
050: facetDefinition.getExpression()).append(",");
051: facetConfs[i] = facetDefinition.getFacetConf();
052: }
053: queryBuffer.deleteCharAt(queryBuffer.length() - 1);
054: queryBuffer.append(" where ").append(defaultConditions);
055:
056: if (defaultOrder != null)
057: queryBuffer.append(" order by ").append(defaultOrder);
058:
059: this .query = queryBuffer.toString();
060: this .queryOptions = queryOptions;
061: }
062:
063: @Override
064: public void processInt(ContentHandler contentHandler,
065: PublisherContext publisherContext) throws Exception {
066: Repository repository = publisherContext.getRepository();
067: FacetedQueryResultDocument facetResultDocument;
068:
069: EvaluationContext evaluationContext = new EvaluationContext();
070: publisherContext.pushContextDocuments(evaluationContext);
071:
072: try {
073: facetResultDocument = repository.getQueryManager()
074: .performFacetedQuery(query, facetConfs,
075: queryOptions, publisherContext.getLocale(),
076: evaluationContext);
077: } catch (RepositoryException e) {
078: throw new PublisherException(
079: "Error performing faceted query in publisher request at "
080: + getLocationInfo().getFormattedLocation(),
081: e);
082: }
083:
084: facetResultDocument.save(new StripDocumentHandler(
085: contentHandler), new DummyLexicalHandler());
086: }
087:
088: public static class FacetDefinition {
089: private FacetConf delegate;
090: private String expression;
091:
092: public FacetDefinition(String expression, boolean isFacet,
093: int maxValues, boolean sortOnValue,
094: boolean sortAscending, String type) {
095: delegate = new FacetConf(isFacet, maxValues, sortOnValue,
096: sortAscending, type.toUpperCase());
097: this .expression = expression;
098: }
099:
100: public String getExpression() {
101: return expression;
102: }
103:
104: public FacetConf getFacetConf() {
105: return delegate;
106: }
107:
108: public Map<String, String> getProperties() {
109: return delegate.getProperties();
110: }
111:
112: public void setProperties(Map<String, String> properties) {
113: this.delegate.setProperties(properties);
114: }
115: }
116: }
|