001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.publisher.serverimpl.requestmodel;
017:
018: import org.xml.sax.ContentHandler;
019: import org.xml.sax.helpers.AttributesImpl;
020: import org.outerx.daisy.x10Publisher.ResolveDocumentIdsDocument;
021: import org.outerj.daisy.repository.variant.VariantManager;
022: import org.outerj.daisy.repository.variant.BranchNotFoundException;
023: import org.outerj.daisy.repository.*;
024: import org.outerj.daisy.publisher.serverimpl.PublisherImpl;
025:
026: import java.util.List;
027:
028: public class ResolveDocumentIdsRequest extends AbstractRequest
029: implements Request {
030: private List<ResolveDocumentIdsDocument.ResolveDocumentIds.Document> documents;
031: private long defaultBranchId;
032: private long defaultLanguageId;
033:
034: public ResolveDocumentIdsRequest(
035: List<ResolveDocumentIdsDocument.ResolveDocumentIds.Document> documents,
036: long defaultBranchId, long defaultLanguageId,
037: LocationInfo locationInfo) {
038: super (locationInfo);
039: this .documents = documents;
040: this .defaultBranchId = defaultBranchId;
041: this .defaultLanguageId = defaultLanguageId;
042: }
043:
044: public void processInt(ContentHandler contentHandler,
045: PublisherContext publisherContext) throws Exception {
046: contentHandler.startElement(PublisherImpl.NAMESPACE,
047: "resolvedDocumentIds", "p:resolvedDocumentIds",
048: new AttributesImpl());
049:
050: Repository repository = publisherContext.getRepository();
051: VariantManager variantManager = publisherContext
052: .getRepository().getVariantManager();
053: for (ResolveDocumentIdsDocument.ResolveDocumentIds.Document document : documents) {
054: String name = null;
055: String id = document.getId();
056:
057: long branchId = defaultBranchId;
058: long languageId = defaultLanguageId;
059:
060: if (document.isSetBranch()) {
061: try {
062: branchId = variantManager.getBranch(
063: document.getBranch(), false).getId();
064: } catch (BranchNotFoundException e) {
065: name = "(branch does not exist: "
066: + document.getBranch() + ")";
067: }
068: }
069:
070: if (name == null && document.isSetLanguage()) {
071: try {
072: languageId = variantManager.getLanguage(
073: document.getLanguage(), false).getId();
074: } catch (BranchNotFoundException e) {
075: name = "(language does not exist: "
076: + document.getLanguage() + ")";
077: }
078: }
079:
080: Document daisyDoc = null;
081: if (name == null) {
082: try {
083: daisyDoc = repository.getDocument(id, branchId,
084: languageId, false);
085: } catch (DocumentNotFoundException e) {
086: name = "(document does not exist)";
087: } catch (DocumentVariantNotFoundException e) {
088: name = "(document variant does not exist)";
089: }
090: }
091:
092: if (name == null) {
093: Version version = null;
094: if (document.isSetVersion()) {
095: String versionParam = document.getVersion();
096: if (versionParam.equalsIgnoreCase("last")) {
097: version = daisyDoc.getLastVersion();
098: } else if (versionParam.equalsIgnoreCase("live")) {
099: version = daisyDoc.getLiveVersion();
100: } else {
101: try {
102: long versionId = Long
103: .parseLong(versionParam);
104: if (versionId >= 1
105: && versionId <= daisyDoc
106: .getLastVersionId()) {
107: version = daisyDoc
108: .getVersion(versionId);
109: }
110: } catch (NumberFormatException e) {
111: // ignore
112: }
113: }
114: }
115: if (version == null) {
116: version = daisyDoc.getLiveVersion();
117: if (version == null)
118: version = daisyDoc.getLastVersion();
119: }
120: name = version.getDocumentName();
121: }
122:
123: AttributesImpl attrs = new AttributesImpl();
124: attrs.addAttribute("", "id", "id", "CDATA", document
125: .getId());
126: if (document.isSetBranch())
127: attrs.addAttribute("", "branch", "branch", "CDATA",
128: document.getBranch());
129: if (document.isSetLanguage())
130: attrs.addAttribute("", "language", "language", "CDATA",
131: document.getLanguage());
132: if (document.isSetVersion())
133: attrs.addAttribute("", "version", "version", "CDATA",
134: document.getVersion());
135: attrs.addAttribute("", "name", "name", "CDATA", name);
136: contentHandler.startElement(PublisherImpl.NAMESPACE,
137: "document", "document", attrs);
138: contentHandler.endElement(PublisherImpl.NAMESPACE,
139: "document", "document");
140: }
141:
142: contentHandler.endElement(PublisherImpl.NAMESPACE,
143: "resolvedDocumentIds", "p:resolvedDocumentIds");
144: }
145: }
|