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.web;
35  
36  import fr.paris.lutece.plugins.forms.business.Question;
37  import fr.paris.lutece.plugins.forms.business.QuestionHome;
38  import fr.paris.lutece.plugins.genericattributes.business.Field;
39  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
40  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.business.AutomaticAssignment;
41  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.business.TaskAutomaticAssignmentConfig;
42  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.service.AutomaticAssignmentPlugin;
43  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.service.AutomaticAssignmentService;
44  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.service.IAutomaticAssignmentService;
45  import fr.paris.lutece.plugins.workflow.modules.formsautomaticassignment.service.TaskAutomaticAssignmentConfigService;
46  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
47  import fr.paris.lutece.portal.business.workgroup.AdminWorkgroup;
48  import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
49  import fr.paris.lutece.portal.service.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.portal.service.plugin.Plugin;
52  import fr.paris.lutece.portal.service.plugin.PluginService;
53  import fr.paris.lutece.portal.service.spring.SpringContextService;
54  import fr.paris.lutece.portal.service.template.AppTemplateService;
55  import fr.paris.lutece.portal.service.util.AppPathService;
56  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
57  import fr.paris.lutece.util.html.HtmlTemplate;
58  import fr.paris.lutece.util.url.UrlItem;
59  
60  import java.util.HashMap;
61  import java.util.List;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  /**
66   *
67   * AutomaticAssignmentJspBean
68   *
69   */
70  public class AutomaticAssignmentJspBean extends PluginAdminPageJspBean
71  {
72      private static final long serialVersionUID = -8429549377694294789L;
73      private static final String TEMPLATE_MODIFY_QUESTION_ASSIGNMENT = "admin/plugins/workflow/modules/formsautomaticassignment/modify_question_assignment.html";
74      private static final String JSP_MODIFY_TASK = "jsp/admin/plugins/workflow/ModifyTask.jsp";
75      private static final String JSP_MODIFY_QUESTION_ASSIGNMENT = "jsp/admin/plugins/workflow/modules/formsautomaticassignment/ModifyQuestionAssignment.jsp";
76      private static final String PROPERTY_MODIFY_TASK_PAGE_TITLE = "workflow.modify_workflow.page_title";
77      private static final String PARAMETER_ID_TASK = "id_task";
78      private static final String PARAMETER_ID_QUESTION = "id_question";
79      private static final String PARAMETER_QUESTION = "question";
80      private static final String PARAMETER_URL = "url";
81      private static final String PARAMETER_WORKGROUP_LIST = "workgroup_list";
82      private static final String PARAMETER_WORKGROUP = "workgroup";
83      private static final String PARAMETER_VALUE = "value";
84      private static final String PARAMETER_ASSIGNMENT_LIST = "assignment_list";
85      private static final String PARAMETER_ID_FORM = "id_form";
86      private static final String MESSAGE_ALREADY_EXIST = "module.workflow.formsautomaticassignment.message.modify_entry_assignment.already_exist";
87      private static final String MESSAGE_ERROR_MISSING_FIELD = "module.workflow.formsautomaticassignment.message.missing_field";
88  
89      // SERVICES
90      private IAutomaticAssignmentService _automaticAssignmentService = SpringContextService.getBean( AutomaticAssignmentService.BEAN_SERVICE );
91      private ITaskConfigService _taskAutomaticAssignmentConfigService = SpringContextService.getBean( TaskAutomaticAssignmentConfigService.BEAN_SERVICE );
92  
93      /**
94       * Get the modify entry assignment page which allow the user to assign a workgroup to a field
95       * 
96       * @param request
97       *            the request
98       * @return the page
99       */
100     public String getModifyQuestionAssignments( HttpServletRequest request )
101     {
102         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
103         String strIdQuestion = request.getParameter( PARAMETER_ID_QUESTION );
104         List<AutomaticAssignment> assignmentList;
105         Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
106 
107         HashMap<String, Object> model = new HashMap<>( );
108         int nIdTask = -1;
109         int nIdQuestion = -1;
110 
111         if ( strIdTask != null )
112         {
113             nIdTask = Integer.parseInt( strIdTask );
114         }
115 
116         if ( strIdQuestion != null )
117         {
118             nIdQuestion = Integer.parseInt( strIdQuestion );
119         }
120 
121         assignmentList = _automaticAssignmentService.findByTaskByQuestion( nIdTask, nIdQuestion, autoAssignPlugin );
122         setAssignmentValues( assignmentList );
123 
124         Question question = QuestionHome.findByPrimaryKey( nIdQuestion );
125 
126         model.put( PARAMETER_QUESTION, question );
127 
128         UrlItem url = new UrlItem( JSP_MODIFY_TASK );
129         url.addParameter( PARAMETER_ID_TASK, nIdTask );
130         setPageTitleProperty( PROPERTY_MODIFY_TASK_PAGE_TITLE );
131         model.put( PARAMETER_URL, url.getUrl( ) );
132         model.put( PARAMETER_ID_TASK, nIdTask );
133         model.put( PARAMETER_WORKGROUP_LIST, AdminWorkgroupHome.findAll( ) );
134         model.put( PARAMETER_ASSIGNMENT_LIST, assignmentList );
135 
136         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_QUESTION_ASSIGNMENT, getLocale( ), model );
137 
138         return getAdminPage( template.getHtml( ) );
139     }
140 
141     /**
142      * Fill a list of assignment with workgroupdescription and field value for displaying purpose
143      * 
144      * @param assignmentList
145      *            a list of AutomaticAssignment
146      * @param directoryPlugin
147      *            the directory plugin
148      */
149     private void setAssignmentValues( List<AutomaticAssignment> assignmentList )
150     {
151         for ( AutomaticAssignment assignment : assignmentList )
152         {
153             AdminWorkgroup workgroup = AdminWorkgroupHome.findByPrimaryKey( assignment.getWorkgroupKey( ) );
154 
155             if ( workgroup != null )
156             {
157                 assignment.setWorkgroupDescription( workgroup.getDescription( ) );
158             }
159 
160             Field field = FieldHome.findByPrimaryKey( assignment.getIdField( ) );
161             assignment.setFieldValue( field.getTitle( ) );
162         }
163     }
164 
165     /**
166      * Assign the selected workgroup to the field
167      * 
168      * @param request
169      *            the request
170      * @return the modify assignment page url
171      */
172     public String doAddAssignment( HttpServletRequest request )
173     {
174         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
175         String strIdQuestion = request.getParameter( PARAMETER_ID_QUESTION );
176         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
177         String strValue = request.getParameter( PARAMETER_VALUE );
178         Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
179 
180         int nIdTask = -1;
181         int nIdQuestion = -1;
182         int nIdField = -1;
183 
184         if ( strIdTask != null )
185         {
186             nIdTask = Integer.parseInt( strIdTask );
187         }
188 
189         if ( strIdQuestion != null )
190         {
191             nIdQuestion = Integer.parseInt( strIdQuestion );
192         }
193 
194         if ( strValue != null )
195         {
196             nIdField = Integer.parseInt( strValue );
197         }
198 
199         if ( ( strValue == null ) || ( strWorkgroup == null ) )
200         {
201             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MISSING_FIELD, AdminMessage.TYPE_STOP );
202         }
203 
204         AutomaticAssignmentlow/modules/formsautomaticassignment/business/AutomaticAssignment.html#AutomaticAssignment">AutomaticAssignment assign = new AutomaticAssignment( );
205         assign.setIdQuestion( nIdQuestion );
206         assign.setIdTask( nIdTask );
207         assign.setIdField( nIdField );
208         assign.setWorkgroupKey( strWorkgroup );
209 
210         if ( !_automaticAssignmentService.checkExist( assign, autoAssignPlugin ) )
211         {
212             _automaticAssignmentService.create( assign, autoAssignPlugin );
213         }
214         else
215         {
216             return AdminMessageService.getMessageUrl( request, MESSAGE_ALREADY_EXIST, AdminMessage.TYPE_STOP );
217         }
218 
219         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MODIFY_QUESTION_ASSIGNMENT );
220 
221         url.addParameter( PARAMETER_ID_TASK, nIdTask );
222         url.addParameter( PARAMETER_ID_QUESTION, nIdQuestion );
223 
224         return url.getUrl( );
225     }
226 
227     /**
228      * Delete an assignment
229      * 
230      * @param request
231      *            the request
232      * @return the modify assignment page url
233      */
234     public String doDeleteAssignment( HttpServletRequest request )
235     {
236         Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
237         AutomaticAssignment assignment = getAutomaticAssignment( request );
238         _automaticAssignmentService.remove( assignment, autoAssignPlugin );
239 
240         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MODIFY_QUESTION_ASSIGNMENT );
241 
242         url.addParameter( PARAMETER_ID_TASK, assignment.getIdTask( ) );
243         url.addParameter( PARAMETER_ID_QUESTION, assignment.getIdQuestion( ) );
244 
245         return url.getUrl( );
246     }
247 
248     /**
249      * Use request parameters to create a AutomaticAssignment object
250      * 
251      * @param request
252      *            the request
253      * @return the AutomaticAssignment created
254      */
255     private AutomaticAssignment getAutomaticAssignment( HttpServletRequest request )
256     {
257         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
258         String strIdQuestion = request.getParameter( PARAMETER_ID_QUESTION );
259         String strWorkgroup = request.getParameter( PARAMETER_WORKGROUP );
260         String strValue = request.getParameter( PARAMETER_VALUE );
261 
262         int nIdTask = -1;
263         int nIdQuestion = -1;
264         int nIdField = -1;
265 
266         if ( strIdTask != null )
267         {
268             nIdTask = Integer.parseInt( strIdTask );
269         }
270 
271         if ( strIdQuestion != null )
272         {
273             nIdQuestion = Integer.parseInt( strIdQuestion );
274         }
275 
276         if ( strValue != null )
277         {
278             nIdField = Integer.parseInt( strValue );
279         }
280 
281         AutomaticAssignmentlow/modules/formsautomaticassignment/business/AutomaticAssignment.html#AutomaticAssignment">AutomaticAssignment assign = new AutomaticAssignment( );
282 
283         assign.setIdQuestion( nIdQuestion );
284         assign.setIdTask( nIdTask );
285         assign.setIdField( nIdField );
286         assign.setWorkgroupKey( strWorkgroup );
287 
288         return assign;
289     }
290 
291     /**
292      * Update the directory selected and reload the page
293      * 
294      * @param request
295      *            the request
296      * @return the modify autoassignment config page
297      */
298     public String doUpdateForm( HttpServletRequest request )
299     {
300         String strIdForm = request.getParameter( PARAMETER_ID_FORM );
301         String strIdTask = request.getParameter( PARAMETER_ID_TASK );
302         int nIdForm = -1;
303         int nIdTask = -1;
304         Plugin autoAssignPlugin = PluginService.getPlugin( AutomaticAssignmentPlugin.PLUGIN_NAME );
305 
306         if ( strIdForm != null )
307         {
308             nIdForm = Integer.parseInt( strIdForm );
309         }
310 
311         if ( strIdTask != null )
312         {
313             nIdTask = Integer.parseInt( strIdTask );
314         }
315 
316         TaskAutomaticAssignmentConfig config = _taskAutomaticAssignmentConfigService.findByPrimaryKey( nIdTask );
317         config.setIdForm( nIdForm );
318 
319         if ( nIdForm != -1 )
320         {
321             _taskAutomaticAssignmentConfigService.update( config );
322             _automaticAssignmentService.removeByTask( nIdTask, autoAssignPlugin );
323         }
324 
325         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MODIFY_TASK );
326         url.addParameter( PARAMETER_ID_TASK, nIdTask );
327 
328         return url.getUrl( );
329     }
330 }