001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.scheduler.jobs;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.notification.CmsPublishNotification;
037import org.opencms.report.CmsLogReport;
038import org.opencms.scheduler.I_CmsScheduledJob;
039
040import java.text.DateFormat;
041import java.util.Date;
042import java.util.Map;
043
044import org.apache.commons.logging.Log;
045
046/**
047 * Scheduled job for time based publishing.<p>
048 *
049 * This class is called via the scheduled job backoffice to publish a project at a given time.<p>
050 *
051 * Per default, it publishes all new, edited and deleted resources in the project which are not locked.
052 * To unlock all resources in the project before publishing, add the parameter <code>unlock=true</code>
053 * in the scheduled job configuration. In addition you are able to perform a link validation before
054 * publishing the project by adding the parameter <code>linkcheck=true</code>. It is possible to send
055 * an email to a user in OpenCms in case somthing went wrong during this process. To do so specifiy
056 * a parameter<code>mail-to-user=username_in_opencms</code>.<p>
057 *
058 * @since 6.0.0
059 */
060public class CmsPublishJob implements I_CmsScheduledJob {
061
062    /** Linkcheck parameter. */
063    public static final String PARAM_LINKCHECK = "linkcheck";
064
065    /** Unlock parameter. */
066    public static final String PARAM_UNLOCK = "unlock";
067
068    /** Mail to user parameter. */
069    public static final String PARAM_USER = "mail-to-user";
070
071    /** The log object for this class. */
072    private static final Log LOG = CmsLog.getLog(CmsPublishJob.class);
073
074    /**
075     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(org.opencms.file.CmsObject, java.util.Map)
076     */
077    public synchronized String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
078
079        Date jobStart = new Date();
080        String finishMessage;
081        String unlock = parameters.get(PARAM_UNLOCK);
082        String linkcheck = parameters.get(PARAM_LINKCHECK);
083        CmsProject project = cms.getRequestContext().getCurrentProject();
084
085        CmsLogReport report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsPublishJob.class);
086
087        try {
088
089            // check if the unlock parameter was given
090            if (Boolean.valueOf(unlock).booleanValue()) {
091                cms.unlockProject(project.getUuid());
092            }
093
094            // validate links if linkcheck parameter was given
095            if (Boolean.valueOf(linkcheck).booleanValue()) {
096                OpenCms.getPublishManager().validateRelations(
097                    cms,
098                    OpenCms.getPublishManager().getPublishList(cms),
099                    report);
100            }
101
102            // publish the project, the publish output will be put in the logfile
103            OpenCms.getPublishManager().publishProject(cms, report);
104            OpenCms.getPublishManager().waitWhileRunning();
105            finishMessage = Messages.get().getBundle().key(Messages.LOG_PUBLISH_FINISHED_1, project.getName());
106        } catch (CmsException e) {
107            // there was an error, so create an output for the logfile
108            finishMessage = Messages.get().getBundle().key(
109                Messages.LOG_PUBLISH_FAILED_2,
110                project.getName(),
111                e.getMessageContainer().key());
112
113            // add error to report
114            report.addError(finishMessage);
115        } finally {
116            //wait for other processes that may add entries to the report
117            long lastTime = report.getLastEntryTime();
118            long beforeLastTime = 0;
119            while (lastTime != beforeLastTime) {
120                wait(300000);
121                beforeLastTime = lastTime;
122                lastTime = report.getLastEntryTime();
123            }
124
125            // send publish notification
126            if (report.hasWarning() || report.hasError()) {
127                try {
128                    String userName = parameters.get(PARAM_USER);
129                    CmsUser user = cms.readUser(userName);
130
131                    CmsPublishNotification notification = new CmsPublishNotification(cms, user, report);
132
133                    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
134                    notification.addMacro("jobStart", df.format(jobStart));
135
136                    notification.send();
137                } catch (Exception e) {
138                    LOG.error(Messages.get().getBundle().key(Messages.LOG_PUBLISH_SEND_NOTIFICATION_FAILED_0), e);
139                }
140            }
141        }
142
143        return finishMessage;
144    }
145
146}