KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerx.daisy.x10Publisher.*;
19 import org.outerj.daisy.repository.Repository;
20 import org.outerj.daisy.repository.LocaleHelper;
21 import org.outerj.daisy.repository.VariantKey;
22 import org.outerj.daisy.repository.ValueType;
23 import org.outerj.daisy.repository.schema.FieldType;
24 import org.outerj.daisy.repository.query.PredicateExpression;
25 import org.outerj.daisy.repository.variant.VariantManager;
26 import org.outerj.daisy.repository.variant.Branch;
27 import org.outerj.daisy.repository.variant.Language;
28 import org.outerj.daisy.navigation.NavigationVersionMode;
29 import org.apache.xmlbeans.XmlObject;
30 import org.apache.xmlbeans.XmlOptions;
31 import org.apache.xmlbeans.QNameSet;
32
33 import java.util.*;
34
35 public class PublisherRequestBuilder {
36     public static PublisherRequest build(PublisherRequestDocument.PublisherRequest publisherRequestXml,
37             Repository repository) throws Exception JavaDoc {
38
39         Locale locale = publisherRequestXml.isSetLocale() ? LocaleHelper.parseLocale(publisherRequestXml.getLocale()) : null;
40         String JavaDoc styleHint = publisherRequestXml.getStyleHint();
41         boolean inlineExceptions = publisherRequestXml.isSetExceptions() && "inline".equals(publisherRequestXml.getExceptions().toString());
42
43         PublisherVersionMode versionMode = null;
44         if (publisherRequestXml.isSetVersionMode())
45             versionMode = PublisherVersionMode.fromString(publisherRequestXml.getVersionMode());
46
47         PublisherRequest publisherRequest = new PublisherRequest(locale, styleHint, inlineExceptions, versionMode);
48         buildChildren(publisherRequestXml, publisherRequest, repository);
49
50         return publisherRequest;
51     }
52
53     private static void buildChildren(XmlObject container, ParentPublisherRequest request, Repository repository) throws Exception JavaDoc {
54         XmlObject[] requestsXml = container.selectChildren(QNameSet.ALL);
55         for (int i = 0; i < requestsXml.length; i++) {
56             RequestBuilder requestBuilder = (RequestBuilder)REQUEST_BUILDERS.get(requestsXml[i].schemaType());
57             if (requestBuilder == null) {
58                 XmlOptions xmlOptions = new XmlOptions();
59                 xmlOptions.setSaveOuter();
60                 throw new Exception JavaDoc("Unknown type of request: " + requestsXml[i].xmlText(xmlOptions));
61             } else {
62                 request.addRequest(requestBuilder.build(requestsXml[i], repository));
63             }
64         }
65     }
66
67     interface RequestBuilder {
68         Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc;
69     }
70
71     private static RequestBuilder ACL_INFO_BUILDER = new RequestBuilder() {
72         public Request build(XmlObject xmlObject, Repository repository) {
73             return new AclInfoRequest();
74         }
75     };
76
77     private static RequestBuilder SUBSCRIPTION_INFO_BUILDER = new RequestBuilder() {
78         public Request build(XmlObject xmlObject, Repository repository) {
79             return new SubscriptionInfoRequest();
80         }
81     };
82
83     private static RequestBuilder COMMENTS_BUILDER = new RequestBuilder() {
84         public Request build(XmlObject xmlObject, Repository repository) {
85             return new CommentsRequest();
86         }
87     };
88
89     private static RequestBuilder AVAILABLE_VARIANTS_BUILDER = new RequestBuilder() {
90         public Request build(XmlObject xmlObject, Repository repository) {
91             return new AvailableVariantsRequest();
92         }
93     };
94
95     private static RequestBuilder SHALLOW_ANN_VERSION_BUILDER = new RequestBuilder() {
96         public Request build(XmlObject xmlObject, Repository repository) {
97             return new ShallowAnnotatedVersionRequest();
98         }
99     };
100
101     private static RequestBuilder ANNOTATED_DOC_BUILDER = new RequestBuilder() {
102         public Request build(XmlObject xmlObject, Repository repository) {
103             return new AnnotatedDocumentRequest();
104         }
105     };
106
107     private static RequestBuilder ANNOTATED_VERSIONLIST_BUILDER = new RequestBuilder() {
108         public Request build(XmlObject xmlObject, Repository repository) {
109             return new AnnotatedVersionListRequest();
110         }
111     };
112
113     private static RequestBuilder PREPDOC_BUILDER = new RequestBuilder() {
114         public Request build(XmlObject xmlObject, Repository repository) {
115             PrepareDocumentDocument1.PrepareDocument preparedDoc = (PrepareDocumentDocument1.PrepareDocument)xmlObject;
116             Set inlineParts;
117             if (preparedDoc.isSetInlineParts()) {
118                 inlineParts = new HashSet();
119                 String JavaDoc inlinePartsParam = preparedDoc.getInlineParts();
120                 StringTokenizer tokenizer = new StringTokenizer(inlinePartsParam, ",");
121                 while (tokenizer.hasMoreTokens()) {
122                     String JavaDoc token = tokenizer.nextToken();
123                     token = token.trim();
124                     if (token.length() > 0) {
125                         inlineParts.add(token);
126                     }
127                 }
128                 inlineParts = Collections.unmodifiableSet(inlineParts);
129             } else {
130                 inlineParts = Collections.EMPTY_SET;
131             }
132             return new PrepareDocumentRequest(inlineParts);
133         }
134     };
135
136     private static RequestBuilder DIFF_BUILDER = new RequestBuilder() {
137         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
138             DiffDocument.Diff diff = (DiffDocument.Diff)xmlObject;
139             if (diff.isSetOtherDocument()) {
140                 DiffDocument.Diff.OtherDocument otherDocXml = diff.getOtherDocument();
141                 long documentId = otherDocXml.isSetId() ? otherDocXml.getId() : -1;
142                 VariantManager variantManager = repository.getVariantManager();
143                 long branchId = otherDocXml.isSetBranch() ? variantManager.getBranch(otherDocXml.getBranch(), false).getId() : -1;
144                 long languageId = otherDocXml.isSetLanguage() ? variantManager.getLanguage(otherDocXml.getLanguage(), false).getId() : -1;
145                 long versionId = otherDocXml.isSetVersion() ? otherDocXml.getVersion() : -1;
146                 return new DiffRequest(documentId, branchId, languageId, versionId);
147             } else {
148                 return new DiffRequest(-1, -1, -1, -1);
149             }
150         }
151     };
152
153     private static RequestBuilder PREPDOCS_BUILDER = new RequestBuilder() {
154         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
155             PreparedDocumentsDocument.PreparedDocuments preparedDocuments = (PreparedDocumentsDocument.PreparedDocuments)xmlObject;
156             VariantManager variantManager = repository.getVariantManager();
157             VariantKey navigationDoc = null;
158             if (preparedDocuments.isSetNavigationDocument()) {
159                 VariantKeyType navigationDocXml = preparedDocuments.getNavigationDocument();
160                 long branchId = variantManager.getBranch(navigationDocXml.getBranch(), false).getId();
161                 long languageId = variantManager.getLanguage(navigationDocXml.getLanguage(), false).getId();
162                 navigationDoc = new VariantKey(navigationDocXml.getId(), branchId, languageId);
163             }
164             boolean applyDocumentTypeStyling = preparedDocuments.getApplyDocumentTypeStyling();
165             String JavaDoc pubReqSet = "default";
166             if (preparedDocuments.isSetPublisherRequestSet() && preparedDocuments.getPublisherRequestSet().trim().length() != 0) {
167                 pubReqSet = preparedDocuments.getPublisherRequestSet();
168             }
169             return new PreparedDocumentsRequest(navigationDoc, applyDocumentTypeStyling, pubReqSet);
170         }
171     };
172
173     private static RequestBuilder NAVTREE_BUILDER = new RequestBuilder() {
174         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
175             NavigationTreeDocument.NavigationTree navigationRequestXml = (NavigationTreeDocument.NavigationTree)xmlObject;
176
177             VariantKeyType navigationDocXml = navigationRequestXml.getNavigationDocument();
178             VariantManager variantManager = repository.getVariantManager();
179             long navDocBranchId = variantManager.getBranch(navigationDocXml.getBranch(), false).getId();
180             long navDocLanguageId = variantManager.getLanguage(navigationDocXml.getLanguage(), false).getId();
181             VariantKey navigationDoc = new VariantKey(navigationDocXml.getId(), navDocBranchId, navDocLanguageId);
182
183             VariantKey activeDoc = null;
184             if (navigationRequestXml.isSetActiveDocument()) {
185                 VariantKeyType activeDocXml = navigationRequestXml.getActiveDocument();
186                 long activeDocBranchId = variantManager.getBranch(activeDocXml.getBranch(), false).getId();
187                 long activeDocLanguageId = variantManager.getLanguage(activeDocXml.getLanguage(), false).getId();
188                 activeDoc = new VariantKey(activeDocXml.getId(), activeDocBranchId, activeDocLanguageId);
189             }
190
191             String JavaDoc activePath = navigationRequestXml.isSetActivePath() ? navigationRequestXml.getActivePath() : null;
192
193             boolean contextualized = navigationRequestXml.getContextualized();
194             NavigationVersionMode versionMode = navigationRequestXml.isSetVersionMode() ?
195                     NavigationVersionMode.fromString(navigationRequestXml.getVersionMode()) : null;
196
197             return new NavigationTreeRequest(navigationDoc, activeDoc, activePath, contextualized, versionMode);
198         }
199     };
200
201     private static RequestBuilder MYCOMMENTS_BUILDER = new RequestBuilder() {
202         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
203             return new MyCommentsRequest();
204         }
205     };
206
207     private static RequestBuilder PERFORMQUERY_BUILDER = new RequestBuilder() {
208         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
209             PerformQueryDocument.PerformQuery performQueryXml = (PerformQueryDocument.PerformQuery)xmlObject;
210             String JavaDoc query = performQueryXml.getQuery();
211             String JavaDoc extraConditions = performQueryXml.isSetExtraConditions() ? performQueryXml.getExtraConditions() : null;
212             return new PerformQueryRequest(query, extraConditions);
213         }
214     };
215
216     private static RequestBuilder GROUP_BUILDER = new RequestBuilder() {
217         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
218             GroupDocument.Group groupXml = (GroupDocument.Group)xmlObject;
219             String JavaDoc id = groupXml.getId();
220             GroupRequest groupRequest = new GroupRequest(id);
221             buildChildren(groupXml, groupRequest, repository);
222             return groupRequest;
223         }
224     };
225
226     private static RequestBuilder FOREACH_BUILDER = new RequestBuilder() {
227         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
228             ForEachDocument.ForEach forEachXml = (ForEachDocument.ForEach)xmlObject;
229             DocumentRequest documentRequest = (DocumentRequest)DOCUMENT_BUILDER.build(forEachXml.getDocument(), repository);
230             String JavaDoc query = forEachXml.getQuery();
231             boolean useLastVersion = forEachXml.isSetUseLastVersion() && forEachXml.getUseLastVersion();
232             return new ForEachRequest(query, useLastVersion, documentRequest);
233         }
234     };
235
236     private static RequestBuilder RESOLVE_DOCIDS_BUILDER = new RequestBuilder() {
237         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
238             ResolveDocumentIdsDocument.ResolveDocumentIds resolveDocIdsXml = (ResolveDocumentIdsDocument.ResolveDocumentIds)xmlObject;
239             VariantManager variantManager = repository.getVariantManager();
240             long defaultBranchId = resolveDocIdsXml.isSetBranch() ? variantManager.getBranch(resolveDocIdsXml.getBranch(), false).getId() : Branch.MAIN_BRANCH_ID;
241             long defaultLangaugeId = resolveDocIdsXml.isSetLanguage() ? variantManager.getLanguage(resolveDocIdsXml.getLanguage(), false).getId() : Language.DEFAULT_LANGUAGE_ID;
242             return new ResolveDocumentIdsRequest(resolveDocIdsXml.getDocumentArray(), defaultBranchId, defaultLangaugeId);
243         }
244     };
245
246     private static RequestBuilder DOCUMENT_BUILDER = new RequestBuilder() {
247         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
248             DocumentDocument.Document documentRequestXml = (DocumentDocument.Document)xmlObject;
249             DocumentRequest documentRequest;
250             if (documentRequestXml.isSetId()) {
251                 long documentId = documentRequestXml.getId();
252                 VariantManager variantManager= repository.getVariantManager();
253                 long branchId = documentRequestXml.isSetBranch() ? variantManager.getBranch(documentRequestXml.getBranch(), false).getId() : Branch.MAIN_BRANCH_ID;
254                 long languageId = documentRequestXml.isSetLanguage() ? variantManager.getLanguage(documentRequestXml.getLanguage(), false).getId() : Language.DEFAULT_LANGUAGE_ID;
255                 long versionId = -1; // default to live version
256
if (documentRequestXml.isSetVersion()) {
257                     String JavaDoc versionSpec = documentRequestXml.getVersion();
258                     if (versionSpec.equals("default") || versionSpec.equalsIgnoreCase("")) {
259                         versionId = -3;
260                     } else if (versionSpec.equalsIgnoreCase("live")) {
261                         versionId = -1;
262                     } else if (versionSpec.equalsIgnoreCase("last")) {
263                         versionId = -2;
264                     } else {
265                         try {
266                             versionId = Long.parseLong(versionSpec);
267                         } catch (NumberFormatException JavaDoc e) {
268                             throw new Exception JavaDoc("Invalid version ID specified: " + versionSpec);
269                         }
270                     }
271                 }
272                 documentRequest = new DocumentRequest(documentId, branchId, languageId, versionId);
273             } else if (documentRequestXml.isSetField() && documentRequestXml.getField().length() > 0) {
274                 String JavaDoc fieldTypeParam = documentRequestXml.getField();
275                 FieldType fieldType;
276                 try {
277                     if (Character.isDigit(fieldTypeParam.charAt(0))) {
278                         fieldType = repository.getRepositorySchema().getFieldTypeById(Long.parseLong(fieldTypeParam), false);
279                     } else {
280                         fieldType = repository.getRepositorySchema().getFieldTypeByName(fieldTypeParam, false);
281                     }
282                 } catch (Exception JavaDoc e) {
283                     throw new Exception JavaDoc("Problem getting the field type specified in the field attribute on a document publisher request.", e);
284                 }
285                 if (fieldType.getValueType() != ValueType.LINK)
286                     throw new Exception JavaDoc("The field type specified in the field attribute on a document publisher request does not specify a field of type link.");
287                 documentRequest = new DocumentRequest(fieldType.getId());
288             } else {
289                 documentRequest = new DocumentRequest();
290             }
291             buildChildren(documentRequestXml, documentRequest, repository);
292             return documentRequest;
293         }
294     };
295
296     private static RequestBuilder IF_BUILDER = new RequestBuilder() {
297         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
298             IfDocument.If ifXml = (IfDocument.If)xmlObject;
299             String JavaDoc test = ifXml.getTest();
300             PredicateExpression expression = repository.getQueryManager().parsePredicateExpression(test);
301             IfRequest ifRequest = new IfRequest(expression);
302             buildChildren(ifXml, ifRequest, repository);
303             return ifRequest;
304         }
305     };
306
307     private static RequestBuilder CHOOSE_BUILDER = new RequestBuilder() {
308         public Request build(XmlObject xmlObject, Repository repository) throws Exception JavaDoc {
309             ChooseDocument.Choose chooseXml = (ChooseDocument.Choose)xmlObject;
310             ChooseDocument.Choose.When[] whensXml = chooseXml.getWhenArray();
311             ChooseRequest chooseRequest = new ChooseRequest();
312             for (int i = 0; i < whensXml.length; i++) {
313                 PredicateExpression expression = repository.getQueryManager().parsePredicateExpression(whensXml[i].getTest());
314                 ChooseWhen chooseWhen = new ChooseWhen(expression);
315                 buildChildren(whensXml[i], chooseWhen, repository);
316                 chooseRequest.addWhen(chooseWhen);
317             }
318             if (chooseXml.isSetOtherwise()) {
319                 ChooseOtherwise chooseOtherwise = new ChooseOtherwise();
320                 buildChildren(chooseXml.getOtherwise(), chooseOtherwise, repository);
321                 chooseRequest.setOtherwise(chooseOtherwise);
322             }
323             return chooseRequest;
324         }
325     };
326
327
328     private static Map REQUEST_BUILDERS = new HashMap();
329     static {
330         REQUEST_BUILDERS.put(AclInfoDocument.AclInfo.type, ACL_INFO_BUILDER);
331         REQUEST_BUILDERS.put(SubscriptionInfoDocument.SubscriptionInfo.type, SUBSCRIPTION_INFO_BUILDER);
332         REQUEST_BUILDERS.put(CommentsDocument.Comments.type, COMMENTS_BUILDER);
333         REQUEST_BUILDERS.put(AvailableVariantsDocument.AvailableVariants.type, AVAILABLE_VARIANTS_BUILDER);
334         REQUEST_BUILDERS.put(ShallowAnnotatedVersionDocument.ShallowAnnotatedVersion.type, SHALLOW_ANN_VERSION_BUILDER);
335         REQUEST_BUILDERS.put(AnnotatedDocumentDocument1.AnnotatedDocument.type, ANNOTATED_DOC_BUILDER);
336         REQUEST_BUILDERS.put(AnnotatedVersionListDocument.AnnotatedVersionList.type, ANNOTATED_VERSIONLIST_BUILDER);
337         REQUEST_BUILDERS.put(DiffDocument.Diff.type, DIFF_BUILDER);
338         REQUEST_BUILDERS.put(PreparedDocumentsDocument.PreparedDocuments.type, PREPDOCS_BUILDER);
339         REQUEST_BUILDERS.put(DocumentDocument.Document.type, DOCUMENT_BUILDER);
340         REQUEST_BUILDERS.put(NavigationTreeDocument.NavigationTree.type, NAVTREE_BUILDER);
341         REQUEST_BUILDERS.put(MyCommentsDocument.MyComments.type, MYCOMMENTS_BUILDER);
342         REQUEST_BUILDERS.put(PerformQueryDocument.PerformQuery.type, PERFORMQUERY_BUILDER);
343         REQUEST_BUILDERS.put(GroupDocument.Group.type, GROUP_BUILDER);
344         REQUEST_BUILDERS.put(ForEachDocument.ForEach.type, FOREACH_BUILDER);
345         REQUEST_BUILDERS.put(ResolveDocumentIdsDocument.ResolveDocumentIds.type, RESOLVE_DOCIDS_BUILDER);
346         REQUEST_BUILDERS.put(IfDocument.If.type, IF_BUILDER);
347         REQUEST_BUILDERS.put(ChooseDocument.Choose.type, CHOOSE_BUILDER);
348         REQUEST_BUILDERS.put(PrepareDocumentDocument1.PrepareDocument.type, PREPDOC_BUILDER);
349     }
350 }
351
Popular Tags