001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.impl.stuck;
017
018import freemarker.cache.StringTemplateLoader;
019import freemarker.template.Configuration;
020import freemarker.template.Template;
021import freemarker.template.TemplateException;
022import org.apache.commons.lang.StringUtils;
023import org.kuali.rice.core.api.config.property.ConfigContext;
024import org.kuali.rice.core.api.config.property.RuntimeConfig;
025import org.kuali.rice.core.api.config.property.RuntimeConfigSet;
026import org.kuali.rice.core.api.mail.MailMessage;
027import org.kuali.rice.core.api.mail.Mailer;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.krad.util.KRADConstants;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.beans.factory.InitializingBean;
033import org.springframework.beans.factory.annotation.Required;
034
035import java.io.IOException;
036import java.io.StringWriter;
037import java.util.Collections;
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041
042public class StuckDocumentNotifierImpl implements StuckDocumentNotifier, InitializingBean {
043
044    private static final Logger LOG = LoggerFactory.getLogger(StuckDocumentNotifierImpl.class);
045
046    private static final String NOTIFICATION_SUBJECT_TEMPLATE_NAME = "notificationSubject";
047    private static final String NOTIFICATION_EMAIL_TEMPLATE_NAME = "notificationEmail";
048    private static final String AUTOFIX_SUBJECT_TEMPLATE_NAME = "autofixSubject";
049    private static final String AUTOFIX_EMAIL_TEMPLATE_NAME = "autofixEmail";
050
051    private static final String NOTIFICATION_EMAIL_TEMPLATE =
052            "${numStuckDocuments} stuck documents have been identified within the workflow system:\n\n" +
053                    "Document ID, Document Type, Create Date\n" +
054                    "---------------------------------------\n" +
055                    "<#list stuckDocuments as stuckDocument>${stuckDocument.documentId}, ${stuckDocument.documentTypeLabel}, ${stuckDocument.createDate}\n</#list>";
056    private static final String AUTOFIX_EMAIL_TEMPLATE =
057            "Failed to autofix document ${documentId}, ${documentTypeLabel}.\n\nIncident details:\n\tStarted: ${startDate}\n\tEnded: ${endDate}\n\n" +
058                    "Attempts occurred at the following times: <#list autofixAttempts as autofixAttempt>\n\t${autofixAttempt.timestamp}</#list>";
059    private static final String FAILURE_EMAIL_SUBJECT_TEMPLATE = "Failed to autofix stuck document with ID {0}";
060
061    private RuntimeConfig from;
062    private RuntimeConfig to;
063    private RuntimeConfig subject;
064
065    private RuntimeConfig autofixSubject;
066
067    private Configuration freemarkerConfig;
068    private StringTemplateLoader templateLoader;
069
070    private Mailer mailer;
071
072    public void afterPropertiesSet() {
073        this.freemarkerConfig = new Configuration(Configuration.VERSION_2_3_25);
074        this.templateLoader = new StringTemplateLoader();
075        this.freemarkerConfig.setTemplateLoader(templateLoader);
076        updateTemplates();
077        new RuntimeConfigSet(subject, autofixSubject).listen(runtimeConfigSet -> updateTemplates());
078    }
079
080    private void updateTemplates() {
081        this.templateLoader.putTemplate(NOTIFICATION_SUBJECT_TEMPLATE_NAME, subject.getValue());
082        this.templateLoader.putTemplate(NOTIFICATION_EMAIL_TEMPLATE_NAME, NOTIFICATION_EMAIL_TEMPLATE);
083        this.templateLoader.putTemplate(AUTOFIX_SUBJECT_TEMPLATE_NAME, autofixSubject.getValue());
084        this.templateLoader.putTemplate(AUTOFIX_EMAIL_TEMPLATE_NAME, AUTOFIX_EMAIL_TEMPLATE);
085        this.freemarkerConfig.clearTemplateCache();
086    }
087
088    @Override
089    public void notify(List<StuckDocument> stuckDocuments) {
090        if (!stuckDocuments.isEmpty()) {
091            Map<String, Object> dataModel = buildNotificationTemplateDataModel(stuckDocuments);
092            String subject = processTemplate(NOTIFICATION_SUBJECT_TEMPLATE_NAME, dataModel);
093            String body = processTemplate(NOTIFICATION_EMAIL_TEMPLATE_NAME, dataModel);
094            send(subject, body);
095        }
096    }
097
098    /**
099     * Supported values include:
100     *
101     * - numStuckDocuments
102     * - stuckDocuments (List of StuckDocument)
103     * - environment
104     * - applicationUrl
105     */
106    private Map<String, Object> buildNotificationTemplateDataModel(List<StuckDocument> stuckDocuments) {
107        Map<String, Object> dataModel = new HashMap<>();
108        dataModel.put("numStuckDocuments", stuckDocuments.size());
109        dataModel.put("stuckDocuments", stuckDocuments);
110        addGlobalDataModel(dataModel);
111        return dataModel;
112    }
113
114    @Override
115    public void notifyIncidentFailure(StuckDocumentIncident incident, List<StuckDocumentFixAttempt> attempts) {
116        Map<String, Object> dataModel = buildIncidentFailureTemplateDataModel(incident, attempts);
117        String subject = processTemplate(AUTOFIX_SUBJECT_TEMPLATE_NAME, dataModel);
118        String body = processTemplate(AUTOFIX_EMAIL_TEMPLATE_NAME, dataModel);
119        send(subject, body);
120    }
121
122    /**
123     * Supported values include:
124     *
125     * - documentId
126     * - documentTypeLabel
127     * - startDate
128     * - endDate
129     * - numberOfAutofixAttempts
130     * - attempts (List of StuckDocumentFixAttempt)
131     * - environment
132     * - applicationUrl
133     */
134    private Map<String, Object> buildIncidentFailureTemplateDataModel(StuckDocumentIncident incident, List<StuckDocumentFixAttempt> attempts) {
135        Map<String, Object> dataModel = new HashMap<>();
136        dataModel.put("documentId", incident.getDocumentId());
137        dataModel.put("documentTypeLabel", resolveDocumentTypeLabel(incident.getDocumentId()));
138        dataModel.put("startDate", incident.getStartDate());
139        dataModel.put("endDate", incident.getEndDate());
140        dataModel.put("numberOfAutofixAttempts", attempts.size());
141        dataModel.put("autofixAttempts", attempts);
142        addGlobalDataModel(dataModel);
143        return dataModel;
144    }
145
146    private void addGlobalDataModel(Map<String, Object> dataModel) {
147        dataModel.put("environment", ConfigContext.getCurrentContextConfig().getEnvironment());
148        dataModel.put("applicationUrl", ConfigContext.getCurrentContextConfig().getProperty(KRADConstants.APPLICATION_URL_KEY));
149    }
150
151    private String processTemplate(String templateName, Object dataModel) {
152        try {
153            StringWriter writer = new StringWriter();
154            Template template = freemarkerConfig.getTemplate(templateName);
155            template.process(dataModel, writer);
156            return writer.toString();
157        } catch (IOException | TemplateException e) {
158            throw new IllegalStateException("Failed to execute template " + templateName, e);
159        }
160    }
161
162    private String resolveDocumentTypeLabel(String documentId) {
163        return KEWServiceLocator.getDocumentTypeService().findByDocumentId(documentId).getLabel();
164    }
165
166    private void send(String messageBody) {
167        send(subject.getValue(), messageBody);
168    }
169
170    private void send(String subject, String messageBody) {
171        if (checkCanSend()) {
172            MailMessage message = new MailMessage();
173            message.setFromAddress(from.getValue());
174            message.setToAddresses(Collections.singleton(to.getValue()));
175            message.setSubject(subject);
176            message.setMessage(messageBody);
177            try {
178                mailer.sendEmail(message);
179            } catch (Exception e) {
180                // we don't want some email configuration issue to mess up our stuck document processing, just log the error
181                LOG.error("Failed to send stuck document notification email with the body:\n" + messageBody, e);
182            }
183        }
184    }
185
186    private boolean checkCanSend() {
187        boolean canSend = true;
188        if (StringUtils.isBlank(from.getValue())) {
189            LOG.error("Cannot send stuck documentation notification because no 'from' address is configured.");
190            canSend = false;
191        }
192        if (StringUtils.isBlank(to.getValue())) {
193            LOG.error("Cannot send stuck documentation notification because no 'to' address is configured.");
194            canSend = false;
195        }
196        return canSend;
197    }
198
199    @Required
200    public void setFrom(RuntimeConfig from) {
201        this.from = from;
202    }
203
204    @Required
205    public void setTo(RuntimeConfig to) {
206        this.to = to;
207    }
208
209    @Required
210    public void setSubject(RuntimeConfig subject) {
211        this.subject = subject;
212    }
213
214    @Required
215    public void setMailer(Mailer mailer) {
216        this.mailer = mailer;
217    }
218
219    @Required
220    public void setAutofixSubject(RuntimeConfig autofixSubject) {
221        this.autofixSubject = autofixSubject;
222    }
223}