KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.daisy.publisher.serverimpl.docpreparation.PreparedDocuments;
21 import org.outerj.daisy.publisher.serverimpl.docpreparation.ContentProcessor;
22 import org.apache.avalon.framework.logger.Logger;
23
24 import java.util.Locale JavaDoc;
25 import java.text.DateFormat JavaDoc;
26
27 public class PublisherContextImpl implements PublisherContext {
28     private PublisherContext parentPublisherContext;
29
30     private Locale JavaDoc locale = null;
31     private boolean documentInformationSet = false;
32     private long documentId = -1;
33     private long branchId = -1;
34     private long languageId = -1;
35     private VariantKey variantKey;
36     private boolean versionIdSet = false;
37     private long versionId = -3;
38     private Document document = null;
39     private PreparedDocuments preparedDocuments;
40     private ContentProcessor contentProcessor;
41     private PublisherVersionMode versionMode;
42
43
44     public PublisherContextImpl(PublisherContext parentPublisherContext) {
45         if (parentPublisherContext == null)
46             throw new IllegalArgumentException JavaDoc("parentPublisherContext argument cannot be null");
47         this.parentPublisherContext = parentPublisherContext;
48     }
49
50     public void setLocale(Locale JavaDoc locale) {
51         this.locale = locale;
52     }
53
54     public Locale JavaDoc getLocale() {
55         if (this.locale != null)
56             return locale;
57         else
58             return parentPublisherContext.getLocale();
59     }
60
61     public Repository getRepository() {
62         return parentPublisherContext.getRepository();
63     }
64
65     public Logger getLogger() {
66         return parentPublisherContext.getLogger();
67     }
68
69     public void setDocumentVariant(long documentId, long branchId, long languageId) {
70         this.documentId = documentId;
71         this.branchId = branchId;
72         this.languageId = languageId;
73         this.variantKey = new VariantKey(documentId, branchId, languageId);
74         this.documentInformationSet = true;
75     }
76
77     public void setVersionId(long versionId) {
78         this.versionId = versionId;
79         this.versionIdSet = true;
80     }
81
82     public void setPreparedDocuments(PreparedDocuments preparedDocuments) {
83         this.preparedDocuments = preparedDocuments;
84     }
85
86     public void setContentProcessor(ContentProcessor contentProcessor) {
87         this.contentProcessor = contentProcessor;
88     }
89
90     public long getDocumentId() {
91         if (documentInformationSet)
92             return documentId;
93         else
94             return parentPublisherContext.getDocumentId();
95     }
96
97     public long getBranchId() {
98         if (documentInformationSet)
99             return branchId;
100         else
101             return parentPublisherContext.getBranchId();
102     }
103
104     public long getLanguageId() {
105         if (documentInformationSet)
106             return languageId;
107         else
108             return parentPublisherContext.getLanguageId();
109     }
110
111     public long getVersionId() throws RepositoryException {
112         if (versionIdSet) {
113             if (versionId == -1) {
114                 Version version = getDocument().getLiveVersion();
115                 if (version != null)
116                     return version.getId();
117                 else
118                     return NO_VERSION;
119             } else if (versionId == -2) {
120                 return getDocument().getLastVersionId();
121             } else {
122                 return versionId;
123             }
124         } else {
125             return parentPublisherContext.getVersionId();
126         }
127     }
128
129     public VariantKey getVariantKey() {
130         if (documentInformationSet)
131             return variantKey;
132         else
133             return parentPublisherContext.getVariantKey();
134     }
135
136     public DateFormat JavaDoc getTimestampFormat() {
137         return parentPublisherContext.getTimestampFormat();
138     }
139
140     public Document getDocument() throws RepositoryException {
141         if (documentInformationSet) {
142             if (document == null) {
143                 document = getRepository().getDocument(getDocumentId(), getBranchId(), getLanguageId(), false);
144             }
145             return document;
146         } else {
147             return parentPublisherContext.getDocument();
148         }
149     }
150
151     public Version getVersion() throws RepositoryException {
152         if (versionIdSet) {
153             // Special values for versionId:
154
// -1: live version
155
// -2: last version
156
// -3: live or last according to PublisherVersionMode
157

158             Version version;
159             if (versionId == -1 || (versionId == -3 && getVersionMode() == PublisherVersionMode.LIVE)) {
160                 version = getDocument().getLiveVersion();
161             } else if (versionId == -2 || (versionId == -3 && getVersionMode() == PublisherVersionMode.LAST)) {
162                 version = getDocument().getLastVersion();
163             } else {
164                 version = getDocument().getVersion(versionId);
165             }
166             return version;
167         } else {
168             return parentPublisherContext.getVersion();
169         }
170     }
171
172     public boolean hasDocument() {
173         if (documentInformationSet)
174             return true;
175         else
176             return parentPublisherContext.hasDocument();
177     }
178
179     public PublisherImpl getPublisher() {
180         return parentPublisherContext.getPublisher();
181     }
182
183     public PreparedDocuments getPreparedDocuments() {
184         if (this.preparedDocuments != null)
185             return this.preparedDocuments;
186         else
187             return parentPublisherContext.getPreparedDocuments();
188     }
189
190     public ContentProcessor getContentProcessor() {
191         if (this.contentProcessor != null)
192             return this.contentProcessor;
193         else
194             return parentPublisherContext.getContentProcessor();
195     }
196
197     public boolean searchRecursivePrepDocs(long documentId, long branchId, long languageId, String JavaDoc pubReqSetName) {
198         if (this.preparedDocuments != null && this.preparedDocuments.getPubReqSet().equals(pubReqSetName) &&
199                 getDocumentId() == documentId && getBranchId() == branchId && getLanguageId() == languageId)
200             return true;
201         else
202             return parentPublisherContext.searchRecursivePrepDocs(documentId, branchId, languageId, pubReqSetName);
203     }
204
205     public void setVersionMode(PublisherVersionMode versionMode) {
206         this.versionMode = versionMode;
207     }
208
209     public PublisherVersionMode getVersionMode() {
210         if (this.versionMode != null)
211             return versionMode;
212         else
213             return parentPublisherContext.getVersionMode();
214     }
215 }
216
Popular Tags