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.relations;
029
030import org.opencms.file.CmsObject;
031import org.opencms.mail.CmsSimpleMail;
032import org.opencms.main.CmsException;
033import org.opencms.main.OpenCms;
034import org.opencms.report.CmsStringBufferReport;
035import org.opencms.report.I_CmsReport;
036import org.opencms.scheduler.I_CmsScheduledJob;
037import org.opencms.util.CmsStringUtil;
038
039import java.util.ArrayList;
040import java.util.Iterator;
041import java.util.List;
042import java.util.Map;
043
044import javax.mail.internet.InternetAddress;
045
046/**
047 * A schedulable OpenCms job to validate internal relations.<p>
048 *
049 * This job will validate all link parseable resources of the context project.<p>
050 *
051 * Parameters used by this job (all optional):
052 * <code>email</code> parameter, should be a comma separated list of email addresses,
053 *            if empty the email address of the context user will be user instead.<p>
054 * <code>from</code> parameter, should be the "from" field of the email to send,
055 *            a valid email address, if empty the default will be used.<p>
056 * <code>subject</code> parameter, should be the subject of the sent email,
057 *            if empty a default subject text will be used,
058 *            a good practice is to use the same job name as subject.<p>
059 *
060 * @since 6.5.0
061 *
062 * @see org.opencms.relations.I_CmsLinkParseable
063 */
064public class CmsInternalRelationsValidationJob implements I_CmsScheduledJob {
065
066    /** The email parameter name. */
067    public static final String PARAM_EMAIL = "email";
068
069    /** The subject parameter name. */
070    public static final String PARAM_SUBJECT = "subject";
071
072    /** The from parameter name. */
073    public static final String PARAM_FROM = "from";
074
075    /**
076     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(CmsObject, Map)
077     */
078    public String launch(CmsObject cms, Map<String, String> parameters) throws Exception {
079
080        I_CmsReport report = null;
081        String msg = null;
082        try {
083            report = new CmsStringBufferReport(cms.getRequestContext().getLocale());
084            report.println(
085                Messages.get().container(
086                    Messages.GUI_RELATIONS_VALIDATION_PROJECT_1,
087                    cms.getRequestContext().getCurrentProject().getName()),
088                I_CmsReport.FORMAT_HEADLINE);
089            // TODO: replace by CmsObject#getRelationsForResource
090            OpenCms.getPublishManager().validateRelations(cms, null, report);
091        } catch (Exception e) {
092            if (report != null) {
093                report.println(e);
094            } else {
095                msg = CmsException.getStackTraceAsString(e);
096            }
097        }
098
099        // parse the parameters
100        String from = parameters.get(PARAM_FROM);
101        String addresses = parameters.get(PARAM_EMAIL);
102        if (CmsStringUtil.isEmptyOrWhitespaceOnly(addresses)) {
103            addresses = cms.getRequestContext().getCurrentUser().getEmail();
104        }
105        List<InternetAddress> to = new ArrayList<InternetAddress>();
106        Iterator<String> it = CmsStringUtil.splitAsList(addresses, ',').iterator();
107        while (it.hasNext()) {
108            to.add(new InternetAddress(it.next()));
109        }
110        String subject = parameters.get(PARAM_SUBJECT);
111        if (CmsStringUtil.isEmptyOrWhitespaceOnly(subject)) {
112            subject = Messages.get().getBundle(cms.getRequestContext().getLocale()).key(
113                Messages.GUI_RELATIONS_VALIDATION_DEFAULT_SUBJECT_0);
114        }
115        if (report != null) {
116            msg = report.toString();
117        }
118
119        // compose the email
120        CmsSimpleMail mail = new CmsSimpleMail();
121        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(from)) {
122            mail.setFrom(from);
123        }
124        mail.setTo(to);
125        mail.setSubject(subject);
126        mail.setMsg(msg);
127        mail.send();
128
129        return null;
130    }
131}