1 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 ; 21 import org.xml.sax.helpers.AttributesImpl ; 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 contentHandler, PublisherContext publisherContext) throws Exception { 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 value = field.getValue(); 54 if (field.isMultiValue()) { 55 Object [] values = (Object [])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 contentHandler, PublisherContext publisherContext) throws Exception { 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 long parentVersionId = publisherContext.getVersionId(); 84 childPublisherContext.setVersionId(parentVersionId < 0 ? parentVersionId : -3); 85 86 emitDocument(contentHandler, childPublisherContext); 87 } 88 89 private void emitDocument(ContentHandler contentHandler, PublisherContext publisherContext) throws Exception { 90 Repository repository = publisherContext.getRepository(); 91 92 long documentId = publisherContext.getDocumentId(); 93 long branchId = publisherContext.getBranchId(); 94 long languageId = publisherContext.getLanguageId(); 95 96 AttributesImpl attrs = new AttributesImpl (); 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 |