KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > requestmodel > DocumentRequest


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.publisher.serverimpl.requestmodel;
17
18 import org.outerj.daisy.repository.*;
19 import org.outerj.daisy.publisher.serverimpl.PublisherImpl;
20 import org.xml.sax.ContentHandler JavaDoc;
21 import org.xml.sax.helpers.AttributesImpl JavaDoc;
22
23 public class DocumentRequest extends AbstractParentPublisherRequest implements Request {
24     private boolean documentInformationSet = false;
25     private long documentId = -1;
26     private long branchId = -1;
27     private long languageId = -1;
28     private long versionId = -3;
29     private long fieldTypeId = -1;
30
31     public DocumentRequest() {
32
33     }
34
35     public DocumentRequest(long fieldTypeId) {
36         this.fieldTypeId = fieldTypeId;
37     }
38
39     public DocumentRequest(long documentId, long branchId, long languageId, long versionId) {
40         this.documentId = documentId;
41         this.branchId = branchId;
42         this.languageId = languageId;
43         this.documentInformationSet = true;
44         this.versionId = versionId;
45     }
46
47     public void process(ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
48         if (fieldTypeId != -1) {
49             Version version = publisherContext.getVersion();
50             if (version != null) {
51                 if (version.hasField(fieldTypeId)) {
52                     Field field = version.getField(fieldTypeId);
53                     Object JavaDoc value = field.getValue();
54                     if (field.isMultiValue()) {
55                         Object JavaDoc[] values = (Object JavaDoc[])value;
56                         for (int i = 0; i < values.length; i++)
57                             emitDocumentForVariantKey((VariantKey)values[i], contentHandler, publisherContext);
58                     } else {
59                         emitDocumentForVariantKey((VariantKey)value, contentHandler, publisherContext);
60                     }
61                 }
62             }
63         } else {
64             PublisherContextImpl childPublisherContext = new PublisherContextImpl(publisherContext);
65             if (documentInformationSet) {
66                 childPublisherContext.setDocumentVariant(documentId, branchId, languageId);
67                 childPublisherContext.setVersionId(versionId);
68             }
69             emitDocument(contentHandler, childPublisherContext);
70         }
71     }
72
73     private void emitDocumentForVariantKey(VariantKey variantKey, ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
74         PublisherContextImpl childPublisherContext = new PublisherContextImpl(publisherContext);
75         Document document = publisherContext.getDocument();
76         long branchId = variantKey.getBranchId() != -1 ? variantKey.getBranchId() : document.getBranchId();
77         long languageId = variantKey.getLanguageId() != -1 ? variantKey.getLanguageId() : document.getLanguageId();
78         childPublisherContext.setDocumentVariant(variantKey.getDocumentId(), branchId, languageId);
79
80         // Make sure the version ID is set, so that it is not taken from the parent
81
// publisher context, which would make no sense because that's about another document.
82
// The version preference (live, last, default) is inherited from the parent though.
83
long parentVersionId = publisherContext.getVersionId();
84         childPublisherContext.setVersionId(parentVersionId < 0 ? parentVersionId : -3);
85
86         emitDocument(contentHandler, childPublisherContext);
87     }
88
89     private void emitDocument(ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
90         Repository repository = publisherContext.getRepository();
91
92         long documentId = publisherContext.getDocumentId();
93         long branchId = publisherContext.getBranchId();
94         long languageId = publisherContext.getLanguageId();
95
96         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
97         attrs.addAttribute("", "documentId", "documentId", "CDATA", String.valueOf(documentId));
98         attrs.addAttribute("", "branchId", "branchId", "CDATA", String.valueOf(branchId));
99         attrs.addAttribute("", "branch", "branch", "CDATA", repository.getVariantManager().getBranch(branchId, false).getName());
100         attrs.addAttribute("", "languageId", "languageId", "CDATA", String.valueOf(languageId));
101         attrs.addAttribute("", "language", "language", "CDATA", repository.getVariantManager().getLanguage(languageId, false).getName());
102         contentHandler.startElement(PublisherImpl.NAMESPACE, "document", "p:document", attrs);
103
104         super.process(contentHandler, publisherContext);
105
106         contentHandler.endElement(PublisherImpl.NAMESPACE, "document", "p:document");
107
108     }
109 }
110
Popular Tags