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 String getRightIdForNotify() 073 { 074 // getUsersToNotify is overriden and do not check rights 075 return null; 076 } 077 078 @Override 079 protected List<UserIdentity> getUsersToNotify(String eventId, AmetysObject object, Project project) 080 { 081 List<UserIdentity> users = new ArrayList<>(); 082 083 if (object instanceof Task) 084 { 085 // User in charge of the task 086 users.addAll(((Task) object).getAssignments()); 087 } 088 089 return users.stream().filter(userId -> _notificationPrefHelper.askedToBeNotified(userId, project.getName(), Frequency.EACH)).collect(Collectors.toList()); 090 } 091 092 /** 093 * Get the i18n subject key 094 * @param eventType The type of event 095 * @param eventParams The optional event parameters 096 * @return the i18 key 097 */ 098 protected String _getSubjectI18nKey (String eventType, Map<String, Object> eventParams) 099 { 100 return "PROJECT_MAIL_NOTIFICATION_SUBJECT_" + StringUtils.replaceChars(eventType.toUpperCase(), '.', '_'); 101 } 102 103 /** 104 * Get the i18n parameters for subject key 105 * @param project The parent project 106 * @param eventParams The optional event parameters 107 * @return the i18n parameters 108 */ 109 protected List<String> _getSubjectParams (Project project, Map<String, Object> eventParams) 110 { 111 List<String> i18nparams = new ArrayList<>(); 112 i18nparams.add(project.getTitle()); // {0} 113 114 Task task = (Task) eventParams.get(ObservationConstants.ARGS_TASK); 115 if (task != null) 116 { 117 i18nparams.add(task.getLabel()); // {1} 118 } 119 return i18nparams; 120 } 121}