View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.formsautomaticassignment.service;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  
40  import javax.inject.Inject;
41  import javax.inject.Named;
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.plugins.forms.business.FormQuestionResponse;
47  import fr.paris.lutece.plugins.forms.business.FormQuestionResponseHome;
48  import fr.paris.lutece.plugins.forms.business.FormResponse;
49  import fr.paris.lutece.plugins.forms.business.FormResponseHome;
50  import fr.paris.lutece.plugins.genericattributes.business.Response;
51  import fr.paris.lutece.plugins.workflow.modules.assignment.service.IAssignmentHistoryService;
52  import fr.paris.lutece.plugins.workflow.modules.assignment.service.IWorkgroupConfigService;
53  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.business.AutomaticAssignment;
54  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.business.TaskAutomaticAssignmentConfig;
55  import fr.paris.lutece.plugins.workflow.service.WorkflowPlugin;
56  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
57  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceWorkflow;
58  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
59  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
60  import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceWorkflowService;
61  import fr.paris.lutece.plugins.workflowcore.service.task.SimpleTask;
62  import fr.paris.lutece.portal.service.plugin.Plugin;
63  import fr.paris.lutece.portal.service.plugin.PluginService;
64  
65  /**
66   *
67   * TaskAutomaticAssignment
68   *
69   */
70  public class TaskAutomaticAssignment extends SimpleTask
71  {
72      // SERVICES
73      @Inject
74      @Named( TaskAutomaticAssignmentConfigService.BEAN_SERVICE )
75      private ITaskConfigService _taskAutomaticAssignmentConfigService;
76      @Inject
77      private IAutomaticAssignmentService _automaticAssignmentService;
78      @Inject
79      private IResourceHistoryService _resourceHistoryService;
80      @Inject
81      private IResourceWorkflowService _resourceWorkflowService;
82      @Inject
83      private IAssignmentHistoryService _assignmentHistoryService;
84      @Inject
85      private IWorkgroupConfigService _workgroupConfigService;
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
92      {
93          Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
94  
95          ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
96  
97          FormResponse formResponse = FormResponseHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
98          if ( formResponse == null )
99          {
100             return;
101         }
102 
103         List<String> listWorkgroup = new ArrayList<>( );
104 
105         List<AutomaticAssignment> automaticAssignementList = _automaticAssignmentService.findByTask( this.getId( ), autoAssignPlugin );
106         List<FormQuestionResponse> listFormQuestionResponse = FormQuestionResponseHome.getFormQuestionResponseListByFormResponse( formResponse.getId( ) );
107 
108         for ( FormQuestionResponse formQuestionResponse : listFormQuestionResponse )
109         {
110             for ( AutomaticAssignment automaticAssignment : automaticAssignementList )
111             {
112                 if ( formQuestionResponse.getQuestion( ).getId( ) == automaticAssignment.getIdQuestion( ) )
113                 {
114                     for ( Response response : formQuestionResponse.getEntryResponse( ) )
115                     {
116                         if ( response.getField( ) != null && response.getField( ).getIdField( ) == automaticAssignment.getIdField( ) )
117                         {
118                             listWorkgroup.add( automaticAssignment.getWorkgroupKey( ) );
119                         }
120                     }
121                 }
122             }
123 
124         }
125 
126         TaskAutomaticAssignmentConfig config = _taskAutomaticAssignmentConfigService.findByPrimaryKey( this.getId( ) );
127 
128         if ( ( config != null ) && config.isNotify( ) )
129         {
130             _automaticAssignmentService.notify( listFormQuestionResponse, config, listWorkgroup, resourceHistory, request, locale, this );
131         }
132 
133         // update resource workflow
134         ResourceWorkflow resourceWorkflow = _resourceWorkflowService.findByPrimaryKey( resourceHistory.getIdResource( ), resourceHistory.getResourceType( ),
135                 resourceHistory.getWorkflow( ).getId( ) );
136         resourceWorkflow.setWorkgroups( listWorkgroup );
137         _resourceWorkflowService.update( resourceWorkflow );
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public void doRemoveConfig( )
145     {
146         Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
147         Plugin workflowPlugin = PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
148         // Remove config
149         _taskAutomaticAssignmentConfigService.remove( this.getId( ) );
150         _automaticAssignmentService.removeByTask( this.getId( ), autoAssignPlugin );
151         // Remove task information
152         _assignmentHistoryService.removeByTask( this.getId( ), workflowPlugin );
153         _workgroupConfigService.removeByTask( this.getId( ), workflowPlugin );
154     }
155 
156     /**
157      * {@inheritDoc}
158      */
159     @Override
160     public void doRemoveTaskInformation( int nIdHistory )
161     {
162         Plugin workflowPlugin = PluginService.getPlugin( WorkflowPlugin.PLUGIN_NAME );
163         _assignmentHistoryService.removeByHistory( nIdHistory, this.getId( ), workflowPlugin );
164     }
165 
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public String getTitle( Locale locale )
171     {
172         TaskAutomaticAssignmentConfig config = _taskAutomaticAssignmentConfigService.findByPrimaryKey( this.getId( ) );
173 
174         if ( config != null )
175         {
176             return config.getTitle( );
177         }
178 
179         return StringUtils.EMPTY;
180     }
181 }