1 17 18 19 20 package org.apache.lenya.defaultpub.cms.task; 21 22 import java.io.IOException ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 25 26 import org.apache.avalon.framework.parameters.ParameterException; 27 import org.apache.lenya.cms.publication.Document; 28 import org.apache.lenya.cms.publication.DocumentBuildException; 29 import org.apache.lenya.cms.publication.DocumentBuilder; 30 import org.apache.lenya.cms.publication.DocumentHelper; 31 import org.apache.lenya.cms.publication.DublinCore; 32 import org.apache.lenya.cms.publication.Publication; 33 import org.apache.lenya.cms.publication.PublicationException; 34 import org.apache.lenya.cms.publication.SiteTree; 35 import org.apache.lenya.cms.publication.SiteTreeNode; 36 import org.apache.lenya.cms.publication.task.PublicationTask; 37 import org.apache.lenya.cms.task.ExecutionException; 38 import org.apache.lenya.workflow.WorkflowException; 39 import org.apache.log4j.Category; 40 41 44 public class Publish extends PublicationTask { 45 46 private static final Category log = Category.getInstance(Publish.class); 47 48 public static final String PARAMETER_DOCUMENT_ID = "document-id"; 49 public static final String PARAMETER_DOCUMENT_LANGUAGE = "document-language"; 50 public static final String PARAMETER_USER_NAME = "user-name"; 51 public static final String PARAMETER_USER_EMAIL = "user-email"; 52 53 private static final String format = "yyyy-MM-dd HH:mm:ss"; 54 55 58 public void execute(String servletContextPath) throws ExecutionException { 59 60 if (log.isDebugEnabled()) { 61 log.debug("Starting publishing"); 62 } 63 64 try { 65 Document authoringDocument = getAuthoringDocument(); 66 67 if (!checkPreconditions(authoringDocument)) { 68 setResult(FAILURE); 69 } else { 70 if (log.isDebugEnabled()) { 71 log.debug("Can execute task: parent is published."); 72 } 73 String date = new SimpleDateFormat (format).format(new Date ()); 74 reservedCheckOut(authoringDocument); 75 setPublicationDate(authoringDocument, date); 76 setModificationDate(authoringDocument, date); 77 setPublisher(authoringDocument); 78 authoringDocument.getDublinCore().save(); 79 reservedCheckIn(authoringDocument, true); 80 81 publish(authoringDocument); 82 83 setResult(SUCCESS); 84 } 85 86 } catch (ExecutionException e) { 87 throw e; 88 } catch (Exception e) { 89 throw new ExecutionException(e); 90 } 91 92 } 93 94 100 public void setPublisher(Document document) throws PublicationException, ParameterException { 101 String userId = getParameters().getParameter(PARAMETER_USER_ID); 102 String userName = getParameters().getParameter(PARAMETER_USER_NAME); 103 String userEmail = getParameters().getParameter(PARAMETER_USER_EMAIL); 104 String publisher = document.getDublinCore().getFirstValue(DublinCore.ELEMENT_PUBLISHER); 105 document.getDublinCore().setValue(DublinCore.ELEMENT_PUBLISHER, userId + "|" + userName + "|" + userEmail); 106 } 107 108 113 public void setModificationDate(Document document,String date) throws PublicationException { 114 String lastModDate = document.getDublinCore().getFirstValue(DublinCore.TERM_MODIFIED); 115 document.getDublinCore().setValue(DublinCore.TERM_MODIFIED, date); 116 } 117 118 123 public void setPublicationDate(Document document,String date) throws PublicationException { 124 String publicationDate = document.getDublinCore().getFirstValue(DublinCore.TERM_ISSUED); 125 if (publicationDate != null && !publicationDate.equals("")){ 126 return; 127 } 128 document.getDublinCore().setValue(DublinCore.TERM_ISSUED, date); 129 } 130 131 139 protected boolean checkPreconditions(Document document) 140 throws 141 ExecutionException, IOException , Exception { 142 boolean OK = true; 143 144 if (!canWorkflowFire(document)) { 145 OK = false; 146 log.error("Cannot execute task: workflow event not supported."); 147 } 148 149 Document liveDocument = getPublication().getAreaVersion(document, Publication.LIVE_AREA); 150 Document liveParent = DocumentHelper.getParentDocument(liveDocument); 151 if (liveParent != null) { 152 SiteTree liveTree = getPublication().getTree(Publication.LIVE_AREA); 153 SiteTreeNode liveParentNode = liveTree.getNode(liveParent.getId()); 154 if (liveParentNode == null) { 155 log.error("Cannot execute task: live parent node does not exist."); 156 OK = false; 157 } 158 } 159 160 if (!canCheckOut(document)){ 161 log.error("Cannot execute task: the document is checked out by another user."); 162 OK = false; 163 } 164 165 return OK; 166 } 167 168 172 protected void publish(Document authoringDocument) 173 throws 174 PublicationException, 175 ExecutionException, 176 IOException , 177 ParameterException, 178 WorkflowException { 179 180 getPublication().copyDocumentToArea(authoringDocument, Publication.LIVE_AREA); 181 182 Document liveDocument = 183 getPublication().getAreaVersion(authoringDocument, Publication.LIVE_AREA); 184 185 copyResources(authoringDocument, liveDocument); 186 187 triggerWorkflow(authoringDocument); 188 } 189 190 197 protected Document getAuthoringDocument() 198 throws ParameterException, DocumentBuildException, ExecutionException { 199 String id = getParameters().getParameter(PARAMETER_DOCUMENT_ID); 200 String language = getParameters().getParameter(PARAMETER_DOCUMENT_LANGUAGE); 201 DocumentBuilder builder = getPublication().getDocumentBuilder(); 202 String url = 203 builder.buildCanonicalUrl(getPublication(), Publication.AUTHORING_AREA, id, language); 204 Document document = builder.buildDocument(getPublication(), url); 205 return document; 206 } 207 208 } 209 | Popular Tags |