KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > defaultpub > cms > task > Deactivate


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: Deactivate.java 160154 2005-04-05 10:04:51Z michi $ */
19
20 package org.apache.lenya.defaultpub.cms.task;
21
22 import java.io.IOException JavaDoc;
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 /**
42  * Deactivate a document.
43  */

44 public class Deactivate extends PublicationTask {
45
46     private static final Category log = Category.getInstance(Deactivate.class);
47
48     public static final String JavaDoc PARAMETER_DOCUMENT_ID = "document-id";
49     public static final String JavaDoc PARAMETER_DOCUMENT_LANGUAGE = "document-language";
50
51     /**
52      * @see org.apache.lenya.cms.task.Task#execute(java.lang.String)
53      */

54     public void execute(String JavaDoc 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 JavaDoc e) {
75             throw new ExecutionException(e);
76         }
77
78     }
79
80     /**
81      * Checks if the preconditions are complied.
82      * @param liveDocument The document to publish.
83      * @return A boolean value.
84      * @throws PublicationException when something went wrong.
85      * @throws ExecutionException when something went wrong.
86      * @throws DocumentException when something went wrong.
87      * FIXME: Remove references to sitetree
88      */

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     /**
129      * Deactivates a document.
130      * @param liveDocument The live document.
131      */

132     protected void deactivate(Document liveDocument)
133         throws
134             PublicationException,
135             ExecutionException,
136             IOException JavaDoc,
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     /**
152      * Returns the live document to apply this task on.
153      * @return A document.
154      * @throws ParameterException when something went wrong.
155      * @throws DocumentBuildException when something went wrong.
156      * @throws ExecutionException when something went wrong.
157      */

158     protected Document getLiveDocument()
159         throws ParameterException, DocumentBuildException, ExecutionException {
160         String JavaDoc id = getParameters().getParameter(PARAMETER_DOCUMENT_ID);
161         String JavaDoc language = getParameters().getParameter(PARAMETER_DOCUMENT_LANGUAGE);
162         DocumentBuilder builder = getPublication().getDocumentBuilder();
163         String JavaDoc 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