View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.workflow.modules.notification.service;
35  
36  import fr.paris.lutece.plugins.workflow.modules.notification.business.TaskNotificationConfig;
37  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
38  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
39  import fr.paris.lutece.plugins.workflowcore.service.task.Task;
40  import fr.paris.lutece.portal.business.mailinglist.Recipient;
41  import fr.paris.lutece.portal.service.mail.MailService;
42  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import java.util.Collection;
47  import java.util.HashMap;
48  import java.util.Locale;
49  import java.util.Map;
50  
51  import javax.inject.Inject;
52  import javax.inject.Named;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   *
58   * TaskNotification
59   *
60   */
61  public class TaskNotification extends Task
62  {
63      // TEMPLATES
64      private static final String TEMPLATE_TASK_NOTIFICATION_MAIL = "admin/plugins/workflow/modules/notification/task_notification_mail.html";
65  
66      // MARKS
67      private static final String MARK_MESSAGE = "message";
68  
69      // BEANS
70      private static final String BEAN_NOTIFICATION_CONFIG_SERVICE = "workflow.taskNotificationConfigService";
71      @Inject
72      @Named( BEAN_NOTIFICATION_CONFIG_SERVICE )
73      private ITaskConfigService _taskNotificationConfigService;
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public void init( )
80      {
81          // Do nothing
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
89      {
90          TaskNotificationConfig config = _taskNotificationConfigService.findByPrimaryKey( this.getId( ) );
91  
92          if ( config != null )
93          {
94              String strSenderEmail = MailService.getNoReplyEmail( );
95              Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( config.getIdMailingList( ) );
96              Map<String, Object> model = new HashMap<>( );
97              model.put( MARK_MESSAGE, config.getMessage( ) );
98  
99              HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFICATION_MAIL, locale, model );
100 
101             // Send Mail
102             for ( Recipient recipient : listRecipients )
103             {
104                 // Build the mail message
105                 MailService.sendMailHtml( recipient.getEmail( ), config.getSenderName( ), strSenderEmail, config.getSubject( ), t.getHtml( ) );
106             }
107         }
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void doRemoveConfig( )
115     {
116         _taskNotificationConfigService.remove( this.getId( ) );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void doRemoveTaskInformation( int nIdHistory )
124     {
125         // Do nothing
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public String getTitle( Locale locale )
133     {
134         TaskNotificationConfig config = _taskNotificationConfigService.findByPrimaryKey( this.getId( ) );
135 
136         if ( config != null )
137         {
138             return config.getSubject( );
139         }
140 
141         return WorkflowUtils.EMPTY_STRING;
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public Map<String, String> getTaskFormEntries( Locale locale )
149     {
150         return null;
151     }
152 }