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.assignment.service;
35  
36  import fr.paris.lutece.plugins.workflow.modules.assignment.business.AssignmentHistory;
37  import fr.paris.lutece.plugins.workflow.modules.assignment.business.TaskAssignmentConfig;
38  import fr.paris.lutece.plugins.workflow.modules.assignment.business.WorkgroupConfig;
39  import fr.paris.lutece.plugins.workflow.modules.comment.business.TaskCommentConfig;
40  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
41  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
42  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
43  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
44  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
45  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
46  import fr.paris.lutece.plugins.workflowcore.service.task.Task;
47  import fr.paris.lutece.portal.business.mailinglist.Recipient;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.admin.AdminUserService;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.mail.MailService;
52  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
53  import fr.paris.lutece.portal.service.spring.SpringContextService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  
57  import java.util.ArrayList;
58  import java.util.Collection;
59  import java.util.HashMap;
60  import java.util.List;
61  import java.util.Locale;
62  import java.util.Map;
63  
64  import javax.inject.Inject;
65  import javax.inject.Named;
66  
67  import javax.servlet.http.HttpServletRequest;
68  
69  /**
70   *
71   * TaskAssignment
72   *
73   */
74  public class TaskAssignment extends Task
75  {
76      // TEMPLATE
77      private static final String TEMPLATE_TASK_NOTIFICATION_MAIL = "admin/plugins/workflow/modules/notification/task_notification_mail.html";
78  
79      // PARAMETER
80      private static final String PARAMETER_WORKGROUPS = "workgroups";
81  
82      // MARK
83      private static final String MARK_MESSAGE = "message";
84  
85      // PROPERTIES
86      private static final String PROPERTY_MAIL_SENDER_NAME = "module.workflow.assignment.task_assignment_config.mailSenderName";
87  
88      // BEANS
89      private static final String BEAN_COMMENT_CONFIG_SERVICE = "workflow.taskCommentConfigService";
90      @Inject
91      @Named( BEAN_COMMENT_CONFIG_SERVICE )
92      private ITaskConfigService _taskCommentConfigService;
93      @Inject
94      private IAssignmentHistoryService _assignmentHistoryService;
95      @Inject
96      @Named( TaskAssignmentConfigService.BEAN_SERVICE )
97      private ITaskConfigService _taskAssignmentConfigService;
98      @Inject
99      private IWorkgroupConfigService _workgroupConfigService;
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void init( )
106     {
107         // Do nothing
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
115     {
116         String [ ] tabWorkgroups = request.getParameterValues( PARAMETER_WORKGROUPS + "_" + this.getId( ) );
117         AdminUser admin = AdminUserService.getAdminUser( request );
118         List<String> listWorkgroup = new ArrayList<>( );
119         TaskAssignmentConfig config = _taskAssignmentConfigService.findByPrimaryKey( this.getId( ) );
120 
121         for ( int i = 0; i < tabWorkgroups.length; i++ )
122         {
123             listWorkgroup.add( tabWorkgroups [i] );
124 
125             // add history
126             AssignmentHistoryflow/modules/assignment/business/AssignmentHistory.html#AssignmentHistory">AssignmentHistory history = new AssignmentHistory( );
127             history.setIdResourceHistory( nIdResourceHistory );
128             history.setIdTask( this.getId( ) );
129             history.setWorkgroup( tabWorkgroups [i] );
130             _assignmentHistoryService.create( history, WorkflowUtils.getPlugin( ) );
131 
132             if ( config.isNotify( ) )
133             {
134                 notifyUser( config, tabWorkgroups [i], admin, locale );
135             }
136         }
137 
138         IResourceHistoryService resourceHistoryService = SpringContextService.getBean( "workflow.resourceHistoryService" );
139         IResourceWorkflowService resourceWorkflowService = SpringContextService.getBean( "workflow.resourceWorkflowService" );
140 
141         // update resource workflow
142         ResourceHistory resourceHistory = resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
143         ResourceWorkflow resourceWorkflow = resourceWorkflowService.findByPrimaryKey( resourceHistory.getIdResource( ), resourceHistory.getResourceType( ),
144                 resourceHistory.getWorkflow( ).getId( ) );
145         resourceWorkflow.setWorkgroups( listWorkgroup );
146         resourceWorkflowService.update( resourceWorkflow );
147     }
148 
149     private void notifyUser( TaskAssignmentConfig config, String workgroup, AdminUser admin, Locale locale )
150     {
151         WorkgroupConfig workgroupConfig = _workgroupConfigService.findByPrimaryKey( this.getId( ), workgroup, WorkflowUtils.getPlugin( ) );
152 
153         if ( ( workgroupConfig != null ) && ( workgroupConfig.getIdMailingList( ) != WorkflowUtils.CONSTANT_ID_NULL ) )
154         {
155             Collection<Recipient> listRecipients = AdminMailingListService.getRecipients( workgroupConfig.getIdMailingList( ) );
156 
157             String strSenderEmail = MailService.getNoReplyEmail( );
158 
159             Map<String, Object> model = new HashMap<>( );
160             model.put( MARK_MESSAGE, config.getMessage( ) );
161 
162             HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_TASK_NOTIFICATION_MAIL, locale, model );
163 
164             // Send Mail
165             for ( Recipient recipient : listRecipients )
166             {
167                 String strSenderName = I18nService.getLocalizedString( PROPERTY_MAIL_SENDER_NAME, locale );
168                 if ( config.isUseUserName( ) )
169                 {
170                     // Build the mail message
171                     MailService.sendMailHtml( recipient.getEmail( ), strSenderName, strSenderEmail, config.getSubject( ), t.getHtml( ) );
172                 }
173                 else
174                 {
175                     MailService.sendMailHtml( recipient.getEmail( ), admin.getFirstName( ) + " " + admin.getLastName( ), admin.getEmail( ),
176                             config.getSubject( ), t.getHtml( ) );
177                 }
178             }
179         }
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public void doRemoveConfig( )
187     {
188         // remove config
189         _taskAssignmentConfigService.remove( this.getId( ) );
190         // remove task information
191         _assignmentHistoryService.removeByTask( this.getId( ), WorkflowUtils.getPlugin( ) );
192         _workgroupConfigService.removeByTask( this.getId( ), WorkflowUtils.getPlugin( ) );
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public void doRemoveTaskInformation( int nIdHistory )
200     {
201         _assignmentHistoryService.removeByHistory( nIdHistory, this.getId( ), WorkflowUtils.getPlugin( ) );
202     }
203 
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public String getTitle( Locale locale )
209     {
210         TaskAssignmentConfig config = _taskAssignmentConfigService.findByPrimaryKey( this.getId( ) );
211 
212         if ( config != null )
213         {
214             return config.getTitle( );
215         }
216 
217         return WorkflowUtils.EMPTY_STRING;
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     @Override
224     public Map<String, String> getTaskFormEntries( Locale locale )
225     {
226         Map<String, String> mapEntriesForm = null;
227         TaskCommentConfig config = _taskCommentConfigService.findByPrimaryKey( this.getId( ) );
228 
229         if ( config != null )
230         {
231             mapEntriesForm = new HashMap<>( );
232             mapEntriesForm.put( PARAMETER_WORKGROUPS + "_" + this.getId( ), config.getTitle( ) );
233         }
234 
235         return mapEntriesForm;
236     }
237 }