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.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  import java.util.Optional;
42  
43  import javax.inject.Inject;
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.lang3.ArrayUtils;
47  import org.apache.commons.lang3.StringUtils;
48  
49  import fr.paris.lutece.plugins.workflow.modules.assignment.business.AssignmentHistory;
50  import fr.paris.lutece.plugins.workflow.modules.assignment.business.TaskAssignmentConfig;
51  import fr.paris.lutece.plugins.workflow.modules.assignment.business.WorkgroupConfig;
52  import fr.paris.lutece.plugins.workflow.modules.assignment.service.IAssignmentHistoryService;
53  import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
54  import fr.paris.lutece.plugins.workflow.web.task.AbstractTaskComponent;
55  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
56  import fr.paris.lutece.portal.service.admin.AdminUserService;
57  import fr.paris.lutece.portal.service.i18n.I18nService;
58  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
59  import fr.paris.lutece.portal.service.message.AdminMessage;
60  import fr.paris.lutece.portal.service.message.AdminMessageService;
61  import fr.paris.lutece.portal.service.template.AppTemplateService;
62  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
63  import fr.paris.lutece.util.ReferenceItem;
64  import fr.paris.lutece.util.ReferenceList;
65  import fr.paris.lutece.util.html.HtmlTemplate;
66  
67  /**
68   *
69   * AssignmentTaskComponent
70   *
71   */
72  public class AssignmentTaskComponent extends AbstractTaskComponent
73  {
74      // TEMPLATES
75      private static final String TEMPLATE_TASK_ASSIGNMENT_CONFIG = "admin/plugins/workflow/modules/assignment/task_assignment_config.html";
76      private static final String TEMPLATE_TASK_ASSIGNMENT_FORM = "admin/plugins/workflow/modules/assignment/task_assignment_form.html";
77      private static final String TEMPLATE_TASK_ASSIGNMENT_INFORMATION = "admin/plugins/workflow/modules/assignment/task_assignment_information.html";
78  
79      // MARKS
80      private static final String MARK_CONFIG = "config";
81      private static final String MARK_WORKGROUP_LIST = "workgroup_list";
82      private static final String MARK_ITEM = "item";
83      private static final String MARK_MAILING_LIST = "mailing_list";
84  
85      // PARAMETERS
86      private static final String PARAMETER_TITLE = "title";
87      private static final String PARAMETER_IS_MULTIPLE_OWNER = "is_multiple_owner";
88      private static final String PARAMETER_WORKGROUPS = "workgroups";
89      private static final String PARAMETER_ID_MAILING_LIST = "id_mailing_list";
90      private static final String PARAMETER_MESSAGE = "message";
91      private static final String PARAMETER_IS_NOTIFICATION = "is_notify";
92      private static final String PARAMETER_SUBJECT = "subject";
93      private static final String PARAMETER_IS_USE_USER_NAME = "is_use_user_name";
94  
95      // PROPERTIES
96      private static final String FIELD_TITLE = "module.workflow.assignment.task_assignment_config.label_title";
97      private static final String FIELD_WORKGROUPS = "module.workflow.assignment.task_assignment_config.label_workgroups";
98      private static final String FIELD_MAILINGLIST_SUBJECT = "module.workflow.assignment.task_assignment_config.label_mailinglist_subject";
99      private static final String FIELD_MAILINGLIST_MESSAGE = "module.workflow.assignment.task_assignment_config.label_mailinglist_message";
100     private static final String PROPERTY_SELECT_EMPTY_CHOICE = "module.workflow.assignment.task_assignment_config.label_empty_choice";
101 
102     // MESSAGES
103     private static final String MESSAGE_MANDATORY_FIELD = "module.workflow.assignment.task_assignment_config.message.mandatory.field";
104     private static final String MESSAGE_NO_CONFIGURATION_FOR_TASK_ASSIGNMENT = "module.workflow.assignment.task_assignment_config.message.no_configuration_for_task_comment";
105     private static final String MESSAGE_NO_MAILINGLIST_FOR_WORKGROUP = "module.workflow.assignment.task_assignment_config.message.no_mailinglist_for_workgroup";
106 
107     // SERVICES
108     @Inject
109     private IAssignmentHistoryService _assignmentHistoryService;
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
116     {
117         String strError = null;
118         String strTitle = request.getParameter( PARAMETER_TITLE );
119         String strIsMultipleOwner = request.getParameter( PARAMETER_IS_MULTIPLE_OWNER );
120         String strIsNotification = request.getParameter( PARAMETER_IS_NOTIFICATION );
121         String strIsUseUserName = request.getParameter( PARAMETER_IS_USE_USER_NAME );
122         String strMessage = request.getParameter( PARAMETER_MESSAGE );
123         String strSubject = request.getParameter( PARAMETER_SUBJECT );
124         String [ ] tabWorkgroups = request.getParameterValues( PARAMETER_WORKGROUPS );
125 
126         if ( StringUtils.isBlank( strTitle ) )
127         {
128             strError = FIELD_TITLE;
129         }
130         if ( ArrayUtils.isEmpty( tabWorkgroups ) )
131         {
132             strError = FIELD_WORKGROUPS;
133         }
134         if ( strIsNotification != null && StringUtils.isEmpty( strSubject ) )
135         {
136             strError = FIELD_MAILINGLIST_SUBJECT;
137         }
138 
139         if ( strIsNotification != null && StringUtils.isEmpty( strMessage ) )
140         {
141             strError = FIELD_MAILINGLIST_MESSAGE;
142         }
143 
144         if ( strError != null )
145         {
146             Object [ ] tabRequiredFields = {
147                     I18nService.getLocalizedString( strError, locale )
148             };
149 
150             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
151         }
152 
153         TaskAssignmentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
154         boolean bCreate = false;
155 
156         if ( config == null )
157         {
158             config = new TaskAssignmentConfig( );
159             config.setIdTask( task.getId( ) );
160             bCreate = true;
161         }
162         config.setTitle( strTitle );
163         config.setNotify( strIsNotification != null );
164         config.setMultipleOwner( strIsMultipleOwner != null );
165         config.setMessage( strMessage );
166         config.setSubject( strSubject );
167         config.setUseUserName( strIsUseUserName != null );
168 
169         // add workgroups
170         List<WorkgroupConfig> listWorkgroupConfig = new ArrayList<>( );
171         // Ignore potential null pointer : tabWorkgroups can not be null here
172         for ( int i = 0; i < tabWorkgroups.length; i++ )
173         {
174             WorkgroupConfigodules/assignment/business/WorkgroupConfig.html#WorkgroupConfig">WorkgroupConfig workgroupConfig = new WorkgroupConfig( );
175             workgroupConfig.setIdTask( task.getId( ) );
176             workgroupConfig.setWorkgroupKey( tabWorkgroups [i] );
177 
178             if ( strIsNotification != null )
179             {
180                 if ( WorkflowUtils.convertStringToInt( request.getParameter( PARAMETER_ID_MAILING_LIST + "_" + tabWorkgroups [i] ) ) != -1 )
181                 {
182                     workgroupConfig.setIdMailingList(
183                             WorkflowUtils.convertStringToInt( request.getParameter( PARAMETER_ID_MAILING_LIST + "_" + tabWorkgroups [i] ) ) );
184                 }
185                 else
186                 {
187                     return AdminMessageService.getMessageUrl( request, MESSAGE_NO_MAILINGLIST_FOR_WORKGROUP, AdminMessage.TYPE_STOP );
188                 }
189             }
190 
191             listWorkgroupConfig.add( workgroupConfig );
192         }
193         config.setWorkgroups( listWorkgroupConfig );
194 
195         if ( bCreate )
196         {
197             this.getTaskConfigService( ).create( config );
198         }
199         else
200         {
201             this.getTaskConfigService( ).update( config );
202         }
203 
204         return null;
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
212     {
213         String strError = StringUtils.EMPTY;
214         String [ ] tabWorkgroups = request.getParameterValues( PARAMETER_WORKGROUPS + "_" + task.getId( ) );
215         TaskAssignmentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
216 
217         if ( config == null )
218         {
219             return AdminMessageService.getMessageUrl( request, MESSAGE_NO_CONFIGURATION_FOR_TASK_ASSIGNMENT, AdminMessage.TYPE_STOP );
220         }
221 
222         if ( ( tabWorkgroups == null ) || ( tabWorkgroups.length == 0 ) )
223         {
224             strError = config.getTitle( );
225         }
226 
227         if ( StringUtils.isNotBlank( strError ) )
228         {
229             Object [ ] tabRequiredFields = {
230                     strError
231             };
232 
233             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
234         }
235 
236         return null;
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
244     {
245         String strNothing = I18nService.getLocalizedString( PROPERTY_SELECT_EMPTY_CHOICE, locale );
246 
247         TaskAssignmentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
248 
249         ReferenceList refWorkgroups = AdminWorkgroupService.getUserWorkgroups( AdminUserService.getAdminUser( request ), locale );
250 
251         List<HashMap<String, Object>> listWorkgroups = new ArrayList<>( );
252 
253         for ( ReferenceItem referenceItem : refWorkgroups )
254         {
255             if ( !referenceItem.getCode( ).equals( AdminWorkgroupService.ALL_GROUPS ) )
256             {
257                 HashMap<String, Object> workgroupsItem = new HashMap<>( );
258                 workgroupsItem.put( MARK_ITEM, referenceItem );
259 
260                 List<WorkgroupConfig> workgroupConfigList = Optional.ofNullable( config ).map( TaskAssignmentConfig::getWorkgroups )
261                         .orElse( new ArrayList<>( ) );
262                 for ( WorkgroupConfig workgroupSelected : workgroupConfigList )
263                 {
264                     if ( referenceItem.getCode( ).equals( workgroupSelected.getWorkgroupKey( ) ) )
265                     {
266                         workgroupsItem.put( MARK_CONFIG, workgroupSelected );
267 
268                         break;
269                     }
270                 }
271                 listWorkgroups.add( workgroupsItem );
272             }
273         }
274 
275         ReferenceList refMailingList = new ReferenceList( );
276         refMailingList.addItem( WorkflowUtils.CONSTANT_ID_NULL, strNothing );
277         refMailingList.addAll( AdminMailingListService.getMailingLists( AdminUserService.getAdminUser( request ) ) );
278 
279         Map<String, Object> model = new HashMap<>( );
280         model.put( MARK_WORKGROUP_LIST, listWorkgroups );
281         model.put( MARK_CONFIG, config );
282         model.put( MARK_MAILING_LIST, refMailingList );
283 
284         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_ASSIGNMENT_CONFIG, locale, model );
285 
286         return template.getHtml( );
287     }
288 
289     /**
290      * {@inheritDoc}
291      */
292     @Override
293     public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
294     {
295         Map<String, Object> model = new HashMap<>( );
296         TaskAssignmentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
297 
298         ReferenceList refWorkgroups = new ReferenceList( );
299 
300         for ( ReferenceItem referenceItem : AdminWorkgroupService.getUserWorkgroups( AdminUserService.getAdminUser( request ), locale ) )
301         {
302             if ( !referenceItem.getCode( ).equals( AdminWorkgroupService.ALL_GROUPS ) && config != null && config.getWorkgroups( ) != null )
303             {
304                 for ( WorkgroupConfig workgroupSelected : config.getWorkgroups( ) )
305                 {
306                     if ( referenceItem.getCode( ).equals( workgroupSelected.getWorkgroupKey( ) ) )
307                     {
308                         refWorkgroups.add( referenceItem );
309                     }
310                 }
311             }
312         }
313 
314         model.put( MARK_CONFIG, config );
315         model.put( MARK_WORKGROUP_LIST, refWorkgroups );
316 
317         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_ASSIGNMENT_FORM, locale, model );
318 
319         return template.getHtml( );
320     }
321 
322     /**
323      * {@inheritDoc}
324      */
325     @Override
326     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
327     {
328         List<AssignmentHistory> listAssignmentHistory = _assignmentHistoryService.getListByHistory( nIdHistory, task.getId( ), WorkflowUtils.getPlugin( ) );
329         Map<String, Object> model = new HashMap<>( );
330 
331         TaskAssignmentConfig config = this.getTaskConfigService( ).findByPrimaryKey( task.getId( ) );
332 
333         ReferenceList refWorkgroups = new ReferenceList( );
334 
335         for ( ReferenceItem referenceItem : AdminWorkgroupService.getUserWorkgroups( AdminUserService.getAdminUser( request ), locale ) )
336         {
337             if ( !referenceItem.getCode( ).equals( AdminWorkgroupService.ALL_GROUPS ) && listAssignmentHistory != null )
338             {
339                 for ( AssignmentHistory assignmentHistory : listAssignmentHistory )
340                 {
341                     if ( referenceItem.getCode( ).equals( assignmentHistory.getWorkgroup( ) ) )
342                     {
343                         refWorkgroups.add( referenceItem );
344                     }
345                 }
346             }
347         }
348 
349         model.put( MARK_CONFIG, config );
350         model.put( MARK_WORKGROUP_LIST, refWorkgroups );
351 
352         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_ASSIGNMENT_INFORMATION, locale, model );
353 
354         return template.getHtml( );
355     }
356 }