01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.outerj.daisy.publisher.serverimpl.requestmodel;
17:
18: import org.xml.sax.ContentHandler;
19: import org.outerj.daisy.repository.query.QueryManager;
20: import org.outerj.daisy.repository.query.EvaluationContext;
21: import org.outerj.daisy.repository.*;
22: import org.outerj.daisy.publisher.PublisherException;
23:
24: import java.util.Map;
25: import java.util.HashMap;
26:
27: public class QueryForEachRequest extends AbstractRequest implements
28: Request {
29: private final String query;
30: private final boolean useLastVersion;
31: private final DocumentRequest documentRequest;
32:
33: public QueryForEachRequest(String query, boolean useLastVersion,
34: DocumentRequest documentRequest, LocationInfo locationInfo) {
35: super (locationInfo);
36: this .query = query;
37: this .useLastVersion = useLastVersion;
38: this .documentRequest = documentRequest;
39: }
40:
41: public void processInt(ContentHandler contentHandler,
42: PublisherContext publisherContext) throws Exception {
43: EvaluationContext evaluationContext = new EvaluationContext();
44: publisherContext.pushContextDocuments(evaluationContext);
45: QueryManager queryManager = publisherContext.getRepository()
46: .getQueryManager();
47:
48: Map<String, String> queryOptions = null;
49: if (publisherContext.getVersionMode() == PublisherVersionMode.LAST) {
50: queryOptions = new HashMap<String, String>(3);
51: queryOptions.put("search_last_version", "true");
52: }
53:
54: VariantKey[] keys;
55: try {
56: keys = queryManager.performQueryReturnKeys(query, null,
57: queryOptions, publisherContext.getLocale(),
58: evaluationContext);
59: } catch (RepositoryException e) {
60: throw new PublisherException(
61: "Error performing query in publisher request at "
62: + getLocationInfo().getFormattedLocation(),
63: e);
64: }
65:
66: for (VariantKey key : keys) {
67: PublisherContextImpl childPublisherContext = new PublisherContextImpl(
68: publisherContext);
69: childPublisherContext.setDocumentVariant(key
70: .getDocumentId(), key.getBranchId(), key
71: .getLanguageId());
72: childPublisherContext
73: .setVersionId(useLastVersion ? -2 : -3);
74: documentRequest.process(contentHandler,
75: childPublisherContext);
76: }
77: }
78: }
|