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.stream.Collectors; 022 023import org.apache.commons.lang.StringUtils; 024 025import org.ametys.core.observation.Event; 026import org.ametys.core.observation.Observer; 027import org.ametys.core.user.UserIdentity; 028import org.ametys.plugins.repository.AmetysObject; 029import org.ametys.plugins.workspaces.ObservationConstants; 030import org.ametys.plugins.workspaces.project.notification.preferences.NotificationPreferencesHelper.Frequency; 031import org.ametys.plugins.workspaces.project.objects.Project; 032import org.ametys.plugins.workspaces.tasks.Task; 033import org.ametys.runtime.i18n.I18nizableText; 034 035/** 036 * This {@link Observer} observes events on task to notify concerned users 037 * 038 */ 039public class TaskMailNotifierObserver extends AbstractSendNotificationObserver 040{ 041 @Override 042 public boolean supports(Event event) 043 { 044 return event.getId().equals(ObservationConstants.EVENT_TASK_CREATED) 045 || event.getId().equals(ObservationConstants.EVENT_TASK_ASSIGNED) 046 || event.getId().equals(ObservationConstants.EVENT_TASK_CLOSED_STATUS_CHANGED) 047 || event.getId().equals(ObservationConstants.EVENT_TASK_DELETING); 048 } 049 050 @Override 051 protected AmetysObject getEventAmetysObject(Event event) 052 { 053 return (Task) event.getArguments().get(ObservationConstants.ARGS_TASK); 054 } 055 056 @Override 057 protected String getMailBodyURI(Event event, Project project) 058 { 059 return "cocoon://_plugins/workspaces/notification-mail-task"; 060 } 061 062 @Override 063 protected I18nizableText getI18nSubject(Event event, Project project) 064 { 065 Map<String, Object> eventArgs = event.getArguments(); 066 String mailSubjecti18nKey = _getSubjectI18nKey(event.getId(), eventArgs); 067 List<String> mailSubjectParams = _getSubjectParams (project, eventArgs); 068 return new I18nizableText("plugin." + _pluginName, mailSubjecti18nKey, mailSubjectParams); 069 } 070 071 @Override 072 protected List<UserIdentity> getUsersToNotify(String eventId, AmetysObject object, Project project) 073 { 074 List<UserIdentity> users = new ArrayList<>(); 075 076 if (object instanceof Task) 077 { 078 // User in charge of the task 079 users.addAll(((Task) object).getAssignments()); 080 } 081 082 return users.stream().filter(userId -> _notificationPrefHelper.askedToBeNotified(userId, project.getName(), Frequency.EACH)).collect(Collectors.toList()); 083 } 084 085 /** 086 * Get the i18n subject key 087 * @param eventType The type of event 088 * @param eventParams The optional event parameters 089 * @return the i18 key 090 */ 091 protected String _getSubjectI18nKey (String eventType, Map<String, Object> eventParams) 092 { 093 return "PROJECT_MAIL_NOTIFICATION_SUBJECT_" + StringUtils.replaceChars(eventType.toUpperCase(), '.', '_'); 094 } 095 096 /** 097 * Get the i18n parameters for subject key 098 * @param project The parent project 099 * @param eventParams The optional event parameters 100 * @return the i18n parameters 101 */ 102 protected List<String> _getSubjectParams (Project project, Map<String, Object> eventParams) 103 { 104 List<String> i18nparams = new ArrayList<>(); 105 i18nparams.add(project.getTitle()); // {0} 106 107 Task task = (Task) eventParams.get(ObservationConstants.ARGS_TASK); 108 if (task != null) 109 { 110 i18nparams.add(task.getLabel()); // {1} 111 } 112 return i18nparams; 113 } 114}