KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20 import org.outerx.daisy.x10Publisher.ResolveDocumentIdsDocument;
21 import org.outerj.daisy.repository.variant.VariantManager;
22 import org.outerj.daisy.repository.variant.BranchNotFoundException;
23 import org.outerj.daisy.repository.*;
24 import org.outerj.daisy.publisher.serverimpl.PublisherImpl;
25
26 public class ResolveDocumentIdsRequest implements Request {
27     private ResolveDocumentIdsDocument.ResolveDocumentIds.Document documents[];
28     private long defaultBranchId;
29     private long defaultLanguageId;
30
31     public ResolveDocumentIdsRequest(ResolveDocumentIdsDocument.ResolveDocumentIds.Document documents[], long defaultBranchId, long defaultLanguageId) {
32         this.documents = documents;
33         this.defaultBranchId = defaultBranchId;
34         this.defaultLanguageId = defaultLanguageId;
35     }
36
37     public void process(ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
38         contentHandler.startElement(PublisherImpl.NAMESPACE, "resolvedDocumentIds", "p:resolvedDocumentIds", new AttributesImpl JavaDoc());
39
40         Repository repository = publisherContext.getRepository();
41         VariantManager variantManager = publisherContext.getRepository().getVariantManager();
42         for (int i = 0; i < documents.length; i++) {
43             String JavaDoc name = null;
44             ResolveDocumentIdsDocument.ResolveDocumentIds.Document document = documents[i];
45             long id = document.getId();
46
47             long branchId = defaultBranchId;
48             long languageId = defaultLanguageId;
49
50             if (document.isSetBranch()) {
51                 try {
52                     branchId = variantManager.getBranch(document.getBranch(), false).getId();
53                 } catch (BranchNotFoundException e) {
54                     name = "(branch does not exist: " + document.getBranch() + ")";
55                 }
56             }
57
58             if (name == null && document.isSetLanguage()) {
59                 try {
60                     languageId = variantManager.getLanguage(document.getLanguage(), false).getId();
61                 } catch (BranchNotFoundException e) {
62                     name = "(language does not exist: " + document.getLanguage() + ")";
63                 }
64             }
65
66             Document daisyDoc = null;
67             if (name == null) {
68                 try {
69                     daisyDoc = repository.getDocument(id, branchId, languageId, false);
70                 } catch (DocumentNotFoundException e) {
71                     name = "(document does not exist)";
72                 } catch (DocumentVariantNotFoundException e) {
73                     name = "(document variant does not exist)";
74                 }
75             }
76
77             if (name == null) {
78                 Version version = null;
79                 if (document.isSetVersion()) {
80                     String JavaDoc versionParam = document.getVersion();
81                     if (versionParam.equalsIgnoreCase("last")) {
82                         version = daisyDoc.getLastVersion();
83                     } else if (versionParam.equalsIgnoreCase("live")) {
84                         version = daisyDoc.getLiveVersion();
85                     } else {
86                         try {
87                             long versionId = Long.parseLong(versionParam);
88                             if (versionId >= 1 && versionId <= daisyDoc.getLastVersionId()) {
89                                 version = daisyDoc.getVersion(versionId);
90                             }
91                         } catch (NumberFormatException JavaDoc e) {
92                             // ignore
93
}
94                     }
95                 }
96                 if (version == null) {
97                     version = daisyDoc.getLiveVersion();
98                     if (version == null)
99                         version = daisyDoc.getLastVersion();
100                 }
101                 name = version.getDocumentName();
102             }
103
104             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
105             attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(document.getId()));
106             if (document.isSetBranch())
107                 attrs.addAttribute("", "branch", "branch", "CDATA", document.getBranch());
108             if (document.isSetLanguage())
109                 attrs.addAttribute("", "language", "language", "CDATA", document.getLanguage());
110             if (document.isSetVersion())
111                 attrs.addAttribute("", "version", "version", "CDATA", document.getVersion());
112             attrs.addAttribute("", "name", "name", "CDATA", name);
113             contentHandler.startElement(PublisherImpl.NAMESPACE, "document", "document", attrs);
114             contentHandler.endElement(PublisherImpl.NAMESPACE, "document", "document");
115         }
116
117         contentHandler.endElement(PublisherImpl.NAMESPACE, "resolvedDocumentIds", "p:resolvedDocumentIds");
118     }
119 }
120
Popular Tags