KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > scheduler > jobs > CmsPublishJob


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/scheduler/jobs/CmsPublishJob.java,v $
3  * Date : $Date: 2006/10/04 07:35:21 $
4  * Version: $Revision: 1.11 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.scheduler.jobs;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsUser;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.notification.CmsPublishNotification;
40 import org.opencms.report.CmsLogReport;
41 import org.opencms.scheduler.I_CmsScheduledJob;
42
43 import java.text.DateFormat JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 /**
50  * Scheduled job for time based publishing.<p>
51  *
52  * This class is called via the scheduled job backoffice to publish a project at a given time.<p>
53  *
54  * Per default, it publishes all new, edited and deleted resources in the project which are not locked.
55  * To unlock all resources in the project before publishing, add the parameter <code>unlock=true</code>
56  * in the scheduled job configuration. In addition you are able to perform a link validation before
57  * publishing the project by adding the parameter <code>linkcheck=true</code>. It is possible to send
58  * an email to a user in OpenCms in case somthing went wrong during this process. To do so specifiy
59  * a parameter<code>mail-to-user=username_in_opencms</code>.<p>
60  *
61  * @author Michael Emmerich
62  * @author Peter Bonrad
63  *
64  * @version $Revision: 1.11 $
65  *
66  * @since 6.0.0
67  */

68 public class CmsPublishJob implements I_CmsScheduledJob {
69
70     /** Linkcheck parameter. */
71     public static final String JavaDoc PARAM_LINKCHECK = "linkcheck";
72
73     /** Unlock parameter. */
74     public static final String JavaDoc PARAM_UNLOCK = "unlock";
75
76     /** Mail to user parameter. */
77     public static final String JavaDoc PARAM_USER = "mail-to-user";
78
79     /** The log object for this class. */
80     private static final Log LOG = CmsLog.getLog(CmsPublishJob.class);
81
82     /**
83      * @see org.opencms.scheduler.I_CmsScheduledJob#launch(org.opencms.file.CmsObject, java.util.Map)
84      */

85     public String JavaDoc launch(CmsObject cms, Map JavaDoc parameters) throws Exception JavaDoc {
86
87         Date JavaDoc jobStart = new Date JavaDoc();
88         String JavaDoc finishMessage;
89         String JavaDoc unlock = (String JavaDoc)parameters.get(PARAM_UNLOCK);
90         String JavaDoc linkcheck = (String JavaDoc)parameters.get(PARAM_LINKCHECK);
91         CmsProject project = cms.getRequestContext().currentProject();
92
93         CmsLogReport report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsPublishJob.class);
94
95         try {
96
97             // check if the unlock parameter was given
98
if (Boolean.valueOf(unlock).booleanValue()) {
99                 cms.unlockProject(project.getId());
100             }
101
102             // validate links if linkcheck parameter was given
103
if (Boolean.valueOf(linkcheck).booleanValue()) {
104                 cms.validateHtmlLinks(cms.getPublishList(), report);
105             }
106
107             // publish the project, the publish output will be put in the logfile
108
cms.publishProject(report);
109             finishMessage = Messages.get().getBundle().key(Messages.LOG_PUBLISH_FINISHED_1, project.getName());
110         } catch (CmsException e) {
111             // there was an error, so create an output for the logfile
112
finishMessage = Messages.get().getBundle().key(
113                 Messages.LOG_PUBLISH_FAILED_2,
114                 project.getName(),
115                 e.getMessageContainer().key());
116
117             // add error to report
118
report.addError(finishMessage);
119         } finally {
120
121             // send publish notification
122
if (report.hasWarning() || report.hasError()) {
123                 try {
124                     String JavaDoc userName = (String JavaDoc)parameters.get(PARAM_USER);
125                     CmsUser user = cms.readUser(userName);
126
127                     CmsPublishNotification notification = new CmsPublishNotification(cms, user, report);
128
129                     DateFormat JavaDoc df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
130                     notification.addMacro("jobStart", df.format(jobStart));
131
132                     notification.send();
133                 } catch (Exception JavaDoc e) {
134                     LOG.error(Messages.get().getBundle().key(Messages.LOG_PUBLISH_SEND_NOTIFICATION_FAILED_0), e);
135                 }
136             }
137         }
138
139         return finishMessage;
140     }
141
142 }
Popular Tags