1 17 18 19 20 package org.apache.lenya.cms.ant; 21 22 import org.apache.lenya.cms.publication.Label; 23 import org.apache.lenya.cms.publication.Publication; 24 import org.apache.lenya.cms.publication.SiteTree; 25 import org.apache.lenya.cms.publication.SiteTreeException; 26 import org.apache.lenya.cms.publication.SiteTreeNode; 27 import org.apache.lenya.cms.publishing.ParentNodeNotFoundException; 28 import org.apache.lenya.cms.publishing.PublishingException; 29 import org.apache.tools.ant.BuildException; 30 31 34 public class TreePublisher extends PublicationTask { 35 private String documentid; 36 private String language; 37 38 41 public TreePublisher() { 42 } 43 44 49 protected String getDocumentid() { 50 return documentid; 51 } 52 53 58 public void setDocumentid(String documentid) { 59 this.documentid = documentid; 60 } 61 62 67 public String getLanguage() { 68 return language; 69 } 70 71 76 public void setLanguage(String string) { 77 language = string; 78 } 79 80 89 public void publish(String documentId, String language) throws PublishingException { 90 SiteTree authoringTree = null; 91 SiteTree liveTree = null; 92 93 try { 94 authoringTree = getPublication().getTree(Publication.AUTHORING_AREA); 95 liveTree = getPublication().getTree(Publication.LIVE_AREA); 96 97 SiteTreeNode authoringNode = authoringTree.getNode(documentId); 98 SiteTreeNode[] siblings = authoringNode.getNextSiblings(); 99 String parentId = authoringNode.getAbsoluteParentId(); 100 SiteTreeNode sibling = null; 101 String siblingDocId = null; 102 for (int i = 0; i < siblings.length; i++) { 103 String docId = parentId + "/" + siblings[i].getId(); 104 sibling = liveTree.getNode(docId); 105 if (sibling != null) { 106 siblingDocId = docId; 107 break; 108 } 109 } 110 111 if (language == null) { 112 try { 115 liveTree.addNode(authoringNode, siblingDocId); 116 } catch (SiteTreeException e1) { 117 throw new ParentNodeNotFoundException("Couldn't add document: " + documentId 118 + " to live tree.", e1); 119 } 120 } else { 121 Label label = authoringNode.getLabel(language); 125 if (label != null) { 126 SiteTreeNode liveNode = liveTree.getNode(documentId); 129 if (liveNode != null) { 130 liveTree.setLabel(documentId, label); 134 } else { 135 Label[] labels = { label }; 138 try { 139 liveTree.addNode(documentId, labels, authoringNode.visibleInNav(), 140 authoringNode.getHref(),authoringNode.getSuffix(), 141 authoringNode.hasLink(),siblingDocId); 142 } catch (SiteTreeException e1) { 143 throw new ParentNodeNotFoundException("Couldn't add document: " 144 + documentId + " to live tree.", e1); 145 } 146 } 147 } else { 148 throw new PublishingException("The node " + documentId 151 + " doesn't contain a label for language " + language); 152 } 153 } 154 liveTree.save(); 155 } catch (PublishingException e) { 156 throw e; 157 } catch (Exception e) { 158 throw new PublishingException("Couldn't publish to live tree :", e); 159 } 160 } 161 162 167 public void execute() throws BuildException { 168 try { 169 log("document id: " + getDocumentid()); 170 log("language: " + getLanguage()); 171 172 publish(getDocumentid(), getLanguage()); 173 } catch (Exception e) { 174 throw new BuildException(e); 175 } 176 } 177 } 178 | Popular Tags |