1 17 18 19 20 package org.apache.lenya.defaultpub.cms.task; 21 22 import java.io.IOException ; 23 24 import org.apache.avalon.framework.parameters.ParameterException; 25 import org.apache.lenya.cms.publication.Document; 26 import org.apache.lenya.cms.publication.DocumentBuildException; 27 import org.apache.lenya.cms.publication.DocumentBuilder; 28 import org.apache.lenya.cms.publication.DocumentException; 29 import org.apache.lenya.cms.publication.Label; 30 import org.apache.lenya.cms.publication.Publication; 31 import org.apache.lenya.cms.publication.PublicationException; 32 import org.apache.lenya.cms.publication.ResourcesManager; 33 import org.apache.lenya.cms.publication.SiteTree; 34 import org.apache.lenya.cms.publication.SiteTreeException; 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 Deactivate extends PublicationTask { 45 46 private static final Category log = Category.getInstance(Deactivate.class); 47 48 public static final String PARAMETER_DOCUMENT_ID = "document-id"; 49 public static final String PARAMETER_DOCUMENT_LANGUAGE = "document-language"; 50 51 54 public void execute(String servletContextPath) throws ExecutionException { 55 if (log.isDebugEnabled()) { 56 log.debug("Starting deactivation"); 57 } 58 59 try { 60 Document liveDocument = getLiveDocument(); 61 62 if (!checkPreconditions(liveDocument)) { 63 setResult(FAILURE); 64 } else { 65 if (log.isDebugEnabled()) { 66 log.debug("Can execute task: last label, children are published."); 67 } 68 deactivate(liveDocument); 69 setResult(SUCCESS); 70 } 71 72 } catch (ExecutionException e) { 73 throw e; 74 } catch (Exception e) { 75 throw new ExecutionException(e); 76 } 77 78 } 79 80 89 protected boolean checkPreconditions(Document liveDocument) 90 throws 91 PublicationException, 92 DocumentException, 93 SiteTreeException, 94 ExecutionException { 95 boolean OK = true; 96 97 Document authoringDocument = getPublication().getAreaVersion(liveDocument, Publication.AUTHORING_AREA); 98 OK = OK && canWorkflowFire(authoringDocument); 99 100 SiteTree tree = getPublication().getTree(liveDocument.getArea()); 101 SiteTreeNode node = tree.getNode(liveDocument.getId()); 102 103 if (node == null) { 104 throw new ExecutionException( 105 "Sitetree node for document [" + liveDocument + "] does not exist!"); 106 } 107 108 Label label = node.getLabel(liveDocument.getLanguage()); 109 110 if (label == null) { 111 throw new ExecutionException( 112 "Sitetree label for document [" + liveDocument + "] does not exist!"); 113 } 114 115 if (node.getLabels().length == 1 && node.getChildren().length > 0) { 116 if (log.isDebugEnabled()) { 117 log.debug( 118 "Cannot delete last language version of document [" 119 + liveDocument 120 + "] because this node has children."); 121 } 122 OK = false; 123 } 124 125 return OK; 126 } 127 128 132 protected void deactivate(Document liveDocument) 133 throws 134 PublicationException, 135 ExecutionException, 136 IOException , 137 ParameterException, 138 WorkflowException, 139 DocumentException { 140 getPublication().deleteDocument(liveDocument); 141 142 if (!liveDocument.existsInAnyLanguage()) { 143 ResourcesManager resourcesManager = new ResourcesManager(liveDocument); 144 resourcesManager.deleteResources(); 145 } 146 147 Document authoringDocument = getPublication().getAreaVersion(liveDocument, Publication.AUTHORING_AREA); 148 triggerWorkflow(authoringDocument); 149 } 150 151 158 protected Document getLiveDocument() 159 throws ParameterException, DocumentBuildException, ExecutionException { 160 String id = getParameters().getParameter(PARAMETER_DOCUMENT_ID); 161 String language = getParameters().getParameter(PARAMETER_DOCUMENT_LANGUAGE); 162 DocumentBuilder builder = getPublication().getDocumentBuilder(); 163 String url = 164 builder.buildCanonicalUrl(getPublication(), Publication.LIVE_AREA, id, language); 165 Document document = builder.buildDocument(getPublication(), url); 166 return document; 167 } 168 169 } 170 | Popular Tags |