KickJava   Java API By Example, From Geeks To Geeks.

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


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: Publish.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 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
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 /**
42  * Publish a document.
43  */

44 public class Publish extends PublicationTask {
45
46     private static final Category log = Category.getInstance(Publish.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     public static final String JavaDoc PARAMETER_USER_NAME = "user-name";
51     public static final String JavaDoc PARAMETER_USER_EMAIL = "user-email";
52
53     private static final String JavaDoc format = "yyyy-MM-dd HH:mm:ss";
54     
55     /**
56      * @see org.apache.lenya.cms.task.Task#execute(java.lang.String)
57      */

58     public void execute(String JavaDoc 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 JavaDoc date = new SimpleDateFormat JavaDoc(format).format(new Date JavaDoc());
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 JavaDoc e) {
89             throw new ExecutionException(e);
90         }
91
92     }
93
94     /**
95      * set the publisher (the user-id, the user-name and the user-email seperate with a |)
96      * @param document The document.
97      * @throws PublicationException When something went wrong.
98      * @throws ParameterException When something went wrong.
99      */

100     public void setPublisher(Document document) throws PublicationException, ParameterException {
101         String JavaDoc userId = getParameters().getParameter(PARAMETER_USER_ID);
102         String JavaDoc userName = getParameters().getParameter(PARAMETER_USER_NAME);
103         String JavaDoc userEmail = getParameters().getParameter(PARAMETER_USER_EMAIL);
104         String JavaDoc publisher = document.getDublinCore().getFirstValue(DublinCore.ELEMENT_PUBLISHER);
105         document.getDublinCore().setValue(DublinCore.ELEMENT_PUBLISHER, userId + "|" + userName + "|" + userEmail);
106     }
107
108     /**set the date of the publication, the date of the last change
109      * @param document The document.
110      * @param date The date in the format yyyy-MM-dd HH:mm:ss.
111      * @throws PublicationException When something went wrong
112      */

113     public void setModificationDate(Document document,String JavaDoc date) throws PublicationException {
114         String JavaDoc lastModDate = document.getDublinCore().getFirstValue(DublinCore.TERM_MODIFIED);
115         document.getDublinCore().setValue(DublinCore.TERM_MODIFIED, date);
116     }
117     
118     /** set the date of the first publication
119      * @param document The document.
120      * @param date The date in the format yyyy-MM-dd HH:mm:ss.
121      * @throws PublicationException When something went wrong
122      */

123     public void setPublicationDate(Document document,String JavaDoc date) throws PublicationException {
124         String JavaDoc 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     /**
132      * Checks if the preconditions are complied.
133      * @param document The document to publish.
134      * @return
135      * @throws ExecutionException
136      * @throws Exception
137      * @throws IOException
138      */

139     protected boolean checkPreconditions(Document document)
140         throws
141             ExecutionException, IOException JavaDoc, Exception JavaDoc {
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     /**
169      * Publishes a document.
170      * @param authoringDocument The authoring document.
171      */

172     protected void publish(Document authoringDocument)
173         throws
174             PublicationException,
175             ExecutionException,
176             IOException JavaDoc,
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     /**
191      * Returns the authoring document to apply this task on.
192      * @return A document.
193      * @throws ParameterException when something went wrong.
194      * @throws DocumentBuildException when something went wrong.
195      * @throws ExecutionException when something went wrong.
196      */

197     protected Document getAuthoringDocument()
198         throws ParameterException, DocumentBuildException, ExecutionException {
199         String JavaDoc id = getParameters().getParameter(PARAMETER_DOCUMENT_ID);
200         String JavaDoc language = getParameters().getParameter(PARAMETER_DOCUMENT_LANGUAGE);
201         DocumentBuilder builder = getPublication().getDocumentBuilder();
202         String JavaDoc 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