001/* 002 * Copyright 2021 Anyware Services 003 * 004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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.ametys.plugins.joboffer.workflow; 017 018import java.io.IOException; 019import java.io.InputStreamReader; 020import java.io.Reader; 021import java.util.ArrayList; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025import java.util.Set; 026 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.cocoon.environment.Request; 030import org.apache.commons.io.IOUtils; 031import org.apache.excalibur.source.Source; 032 033import org.ametys.cms.data.ContentValue; 034import org.ametys.cms.repository.Content; 035import org.ametys.cms.repository.WorkflowAwareContent; 036import org.ametys.core.user.User; 037import org.ametys.core.user.UserIdentity; 038import org.ametys.core.util.mail.SendMailHelper; 039import org.ametys.plugins.joboffer.JobOfferConstants; 040import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector; 041import org.ametys.runtime.config.Config; 042import org.ametys.web.renderingcontext.RenderingContext; 043import org.ametys.web.renderingcontext.RenderingContextHandler; 044import org.ametys.web.repository.content.WebContent; 045import org.ametys.web.repository.site.Site; 046import org.ametys.web.workflow.SendMailFunction; 047 048import com.opensymphony.workflow.WorkflowException; 049 050import jakarta.mail.MessagingException; 051 052/** 053 * OS workflow function to send mail to person(s) in charge when a new application was submitted. 054 */ 055public class SendMailToPersonInChargeFunction extends SendMailFunction 056{ 057 private RenderingContextHandler _renderingContextHandler; 058 059 @Override 060 public void service(ServiceManager smanager) throws ServiceException 061 { 062 super.service(smanager); 063 _renderingContextHandler = (RenderingContextHandler) smanager.lookup(RenderingContextHandler.ROLE); 064 } 065 066 067 @Override 068 protected Set<UserIdentity> _getUsers(WorkflowAwareContent content, Set<String> rights) throws WorkflowException 069 { 070 if (content.hasDefinition(JobOfferConstants.JOB_APPLICATION_ATTRIBUTE_PATH_PERSON_IN_CHARGE)) 071 { 072 UserIdentity[] personIncharge = content.getValue(JobOfferConstants.JOB_APPLICATION_ATTRIBUTE_PATH_PERSON_IN_CHARGE); 073 return personIncharge != null ? Set.of(personIncharge) : Set.of(); 074 } 075 076 return Set.of(); 077 } 078 079 080 081 @Override 082 protected String getSender(Map transientVars, WorkflowAwareContent content) throws WorkflowException 083 { 084 if (content instanceof WebContent) 085 { 086 Site site = ((WebContent) content).getSite(); 087 return site.getValue("site-mail-from", false, Config.getInstance().getValue("smtp.mail.from")); 088 } 089 else 090 { 091 return Config.getInstance().getValue("smtp.mail.from"); 092 } 093 } 094 095 @Override 096 protected List<String> getSubjectI18nParams(User user, WorkflowAwareContent content) 097 { 098 ContentValue value = content.getValue(JobOfferConstants.JOB_APPLICATION_ATTRIBUTE_PATH_JOB_OFFER); 099 Content jobOffer = value.getContent(); 100 101 List<String> i18nParams = new ArrayList<>(); 102 i18nParams.add(jobOffer.getTitle()); // {0} 103 i18nParams.add(jobOffer.getValue(JobOfferConstants.JOB_OFFER_ATTRIBUTE_PATH_REF_ID, false, "")); // {1} 104 105 return i18nParams; 106 } 107 108 @Override 109 protected String getMailBody(String bodyI18nKey, User user, WorkflowAwareContent content, Map transientVars) 110 { 111 Source src = null; 112 113 Request request = _getRequest(); 114 String currentWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 115 RenderingContext currentContext = _renderingContextHandler.getRenderingContext(); 116 117 try 118 { 119 // Force default workspace and BACK rendering context to resolve uri (application contents are never published in live) 120 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, "default"); 121 _renderingContextHandler.setRenderingContext(RenderingContext.PREVIEW); 122 123 request.setAttribute("_baseServerPath", _getRequestUri()); 124 125 String uri = "cocoon:/apply/mail.html"; 126 Map<String, Object> parameters = new HashMap<>(); 127 parameters.put("content", content); 128 parameters.put("uriPrefix", _getRequestUri()); 129 parameters.put("siteName", _getSite(content).getName()); 130 131 src = _sourceResolver.resolveURI(uri, null, parameters); 132 Reader reader = new InputStreamReader(src.getInputStream(), "UTF-8"); 133 return IOUtils.toString(reader); 134 } 135 catch (IOException e) 136 { 137 _logger.warn("Failed to get HTML body for mail notification for new application " + content.getId(), e); 138 return null; 139 } 140 finally 141 { 142 _sourceResolver.release(src); 143 144 _renderingContextHandler.setRenderingContext(currentContext); 145 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWorkspace); 146 } 147 } 148 149 @Override 150 protected void _sendMails(String subject, String body, Set<String> recipients, String from) 151 { 152 if (body != null) 153 { 154 for (String recipient : recipients) 155 { 156 try 157 { 158 SendMailHelper.newMail() 159 .withSubject(subject) 160 .withHTMLBody(body) 161 .withSender(from) 162 .withRecipient(recipient) 163 .withAsync(true) 164 .sendMail(); 165 } 166 catch (MessagingException | IOException e) 167 { 168 _logger.warn("Could not send a workflow notification mail to " + recipient, e); 169 } 170 } 171 } 172 } 173 174}