001/* 002 * Copyright 2010 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.workspaces.project.notification; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Map; 021import java.util.Observer; 022 023import org.apache.commons.lang.StringUtils; 024 025import org.ametys.core.observation.Event; 026import org.ametys.core.user.User; 027import org.ametys.core.user.UserIdentity; 028import org.ametys.plugins.explorer.ObservationConstants; 029import org.ametys.plugins.explorer.tasks.Task; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.plugins.workspaces.project.objects.Project; 032import org.ametys.plugins.workspaces.tasks.TasksWorkspaceModule; 033 034/** 035 * This {@link Observer} observes events on task to notify concerned users 036 * 037 */ 038public class TaskMailNotifierObserver extends AbstractSendNotificationObserver 039{ 040 @Override 041 public boolean supports(Event event) 042 { 043 return event.getId().equals(ObservationConstants.EVENT_TASK_CREATED) 044 || event.getId().equals(ObservationConstants.EVENT_TASK_ASSIGNED) 045 || event.getId().equals(ObservationConstants.EVENT_TASK_UPDATED) 046 || event.getId().equals(ObservationConstants.EVENT_TASK_STATUS_CHANGED) 047 || event.getId().equals(ObservationConstants.EVENT_TASK_DELETING); 048 } 049 050 @Override 051 protected void notifyEvent(Project project, String eventId, Map<String, Object> eventParams, User issuer) 052 { 053 Task task = (Task) eventParams.get(ObservationConstants.ARGS_TASK); 054 055 // Subject 056 String mailSubjecti18nKey = _getSubjectI18nKey(eventId, eventParams); 057 List<String> mailSubjectParams = _getSubjectParams (eventId, project, issuer, eventParams); 058 059 // Body 060 String mailBodyi18nKey = _getBodyI18nKey(eventId, eventParams); 061 List<String> mailBodyParams = _getBodyParams(eventId, project, issuer, eventParams); 062 063 List<UserIdentity> recipients = getUsersToNotify(eventId, task); 064 065 sendMail(eventId, recipients, mailBodyi18nKey, mailSubjecti18nKey, mailBodyParams, mailSubjectParams); 066 067 } 068 069 @Override 070 protected String getUrl(Project project, String objectId) 071 { 072 return getModuleUrl(project, TasksWorkspaceModule.TASK_MODULE_ID, null); 073 } 074 075 @Override 076 protected String getRightIdForNotify() 077 { 078 // getUsersToNotify is overriden and do not check rights 079 return null; 080 } 081 082 @Override 083 protected List<UserIdentity> getUsersToNotify(String eventId, AmetysObject object) 084 { 085 List<UserIdentity> users = new ArrayList<>(); 086 087 if (object instanceof Task) 088 { 089 // User in charge of the task 090 users.addAll(((Task) object).getAssignment()); 091 092 if (eventId.equals(ObservationConstants.EVENT_TASK_STATUS_CHANGED) || eventId.equals(ObservationConstants.EVENT_TASK_UPDATED) || eventId.equals(ObservationConstants.EVENT_TASK_DELETED)) 093 { 094 // Add subscribers 095 users.addAll(((Task) object).getSubscribers()); 096 } 097 } 098 099 return users; 100 } 101 102 /** 103 * Get the i18n subject key 104 * @param eventType The type of event 105 * @param eventParams The optional event parameters 106 * @return the i18 key 107 */ 108 protected String _getSubjectI18nKey (String eventType, Map<String, Object> eventParams) 109 { 110 return "PROJECT_MAIL_NOTIFICATION_SUBJECT_" + StringUtils.replaceChars(eventType.toUpperCase(), '.', '_'); 111 } 112 113 /** 114 * Get the i18n parameters for subject key 115 * @param eventType The event type 116 * @param project The parent project 117 * @param issuer The user responsible of the action 118 * @param eventParams The optional event parameters 119 * @return the i18n parameters 120 */ 121 protected List<String> _getSubjectParams (String eventType, Project project, User issuer, Map<String, Object> eventParams) 122 { 123 List<String> i18nparams = new ArrayList<>(); 124 i18nparams.add(project.getTitle()); // {0} 125 126 Task task = (Task) eventParams.get(ObservationConstants.ARGS_TASK); 127 if (task != null) 128 { 129 i18nparams.add(task.getLabel()); // {1} 130 } 131 return i18nparams; 132 } 133 134 /** 135 * Get the i18n subject key 136 * @param eventType The type of event 137 * @param eventParams The optional event parameters 138 * @return the i18n key 139 */ 140 protected String _getBodyI18nKey (String eventType, Map<String, Object> eventParams) 141 { 142 return "PROJECT_MAIL_NOTIFICATION_BODY_" + StringUtils.replaceChars(eventType.toUpperCase(), '.', '_'); 143 } 144 145 /** 146 * Get the i18n parameters for body key 147 * @param eventType The event type 148 * @param project The parent project 149 * @param issuer The user responsible of the action 150 * @param eventParams The optional event parameters 151 * @return he i18n parameters 152 */ 153 protected List<String> _getBodyParams (String eventType, Project project, User issuer, Map<String, Object> eventParams) 154 { 155 Task task = (Task) eventParams.get(ObservationConstants.ARGS_TASK); 156 List<String> i18nparams = getMailCommonParams(project, issuer, task.getId()); 157 158 // {4} task label 159 i18nparams.add(task.getLabel()); 160 return i18nparams; 161 } 162}