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.appointment.web;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.inject.Inject;
42  import javax.inject.Named;
43  import javax.servlet.http.HttpServletRequest;
44  
45  import org.apache.commons.lang3.StringUtils;
46  
47  import fr.paris.lutece.plugins.workflow.modules.appointment.business.TaskUpdateAppointmentCancelActionConfig;
48  import fr.paris.lutece.plugins.workflow.modules.appointment.service.TaskUpdateAppointmentCancelReportAction;
49  import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
50  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
51  import fr.paris.lutece.plugins.workflowcore.business.action.ActionFilter;
52  import fr.paris.lutece.plugins.workflowcore.service.action.ActionService;
53  import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
54  import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
55  import fr.paris.lutece.portal.service.i18n.I18nService;
56  import fr.paris.lutece.portal.service.message.AdminMessage;
57  import fr.paris.lutece.portal.service.message.AdminMessageService;
58  import fr.paris.lutece.portal.service.template.AppTemplateService;
59  import fr.paris.lutece.util.ReferenceList;
60  import fr.paris.lutece.util.html.HtmlTemplate;
61  
62  /**
63   *
64   * NotifyAppointmentTaskComponent
65   *
66   */
67  public class UpdateAppointmentCancelActionTaskComponent extends NoFormTaskComponent
68  {
69      // TEMPLATES
70      private static final String TEMPLATE_TASK_UPDATE_APPOINTMENT_CANCEL_ACTION_CONFIG = "admin/plugins/workflow/modules/appointment/task_update_appointment_cancel_action_config.html";
71  
72      // MESSAGES
73      private static final String MESSAGE_CANCEL_ACTION_UPDATED = "module.workflow.appointment.message.cancelActionUpdated";
74      private static final String MESSAGE_MANDATORY_FIELD = "module.workflow.appointment.message.mandatory.field";
75      private static final String FIELD_CANCEL_ACTION = "task_notify_appointment_config.label_action";
76      private static final String FIELD_REPORT_ACTION = "task_notify_appointment_config.label_action_report";
77  
78      // MARKS
79      private static final String MARK_LIST_ACTIONS = "list_actions";
80      private static final String MARK_CONFIG = "config";
81  
82      // PARAMETERS
83      private static final String PARAMETER_ID_ACTION_CANCEL = "id_action_cancel";
84      private static final String PARAMETER_ID_ACTION_REPORT = "id_action_report";
85  
86      // SERVICES
87      @Inject
88      @Named( TaskUpdateAppointmentCancelReportAction.CONFIG_SERVICE_BEAN_NAME )
89      private ITaskConfigService _taskUpdateAppointmentCancelActionConfigService;
90      @Inject
91      @Named( ActionService.BEAN_SERVICE )
92      private ActionService _actionService;
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
99      {
100         TaskUpdateAppointmentCancelActionConfig config = _taskUpdateAppointmentCancelActionConfigService.findByPrimaryKey( task.getId( ) );
101 
102         ActionFilter filter = new ActionFilter( );
103         filter.setAutomaticReflexiveAction( false );
104         Action action = _actionService.findByPrimaryKey( task.getAction( ).getId( ) );
105         filter.setIdStateBefore( action.getStateAfter( ).getId( ) );
106 
107         List<Action> listActions = _actionService.getListActionByFilter( filter );
108         List<Integer> listIdStateBefore = action.getListIdStateBefore();
109         int stateAfterId = action.getStateAfter().getId();
110         int alternativeStateAfterId = action.getStateAfter().getId();
111 
112         if (listIdStateBefore.contains(stateAfterId) || listIdStateBefore.contains(alternativeStateAfterId))
113         {
114             for ( Action actionFound : listActions )
115             {
116                 if ( actionFound.getId( ) == action.getId( ) )
117                 {
118                     listActions.remove( actionFound );
119 
120                     break;
121                 }
122             }
123         }
124 
125         ReferenceList refListActions = new ReferenceList( listActions.size( ) + 1 );
126         refListActions.addItem( 0, StringUtils.EMPTY );
127 
128         for ( Action actionFound : listActions )
129         {
130             refListActions.addItem( actionFound.getId( ), actionFound.getName( ) );
131         }
132 
133         Map<String, Object> model = new HashMap<>( );
134         model.put( MARK_LIST_ACTIONS, refListActions );
135         model.put( MARK_CONFIG, config );
136 
137         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_UPDATE_APPOINTMENT_CANCEL_ACTION_CONFIG, locale, model );
138 
139         return template.getHtml( );
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
147     {
148         TaskUpdateAppointmentCancelActionConfig config = _taskUpdateAppointmentCancelActionConfigService.findByPrimaryKey( task.getId( ) );
149         boolean bCreate = false;
150 
151         if ( config == null )
152         {
153             bCreate = true;
154             config = new TaskUpdateAppointmentCancelActionConfig( );
155             config.setIdTask( task.getId( ) );
156         }
157 
158         String strIdActionCancel = request.getParameter( PARAMETER_ID_ACTION_CANCEL );
159         String strIdActionReport = request.getParameter( PARAMETER_ID_ACTION_REPORT );
160 
161         int nIdActionCancel = 0;
162         int nIdActionReport = 0;
163 
164         if ( StringUtils.isEmpty( strIdActionCancel ) || !StringUtils.isNumeric( strIdActionCancel ) )
165         {
166             Object [ ] tabRequiredFields = {
167                     I18nService.getLocalizedString( FIELD_CANCEL_ACTION, locale )
168             };
169 
170             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
171         }
172         if ( StringUtils.isEmpty( strIdActionReport ) || !StringUtils.isNumeric( strIdActionReport ) )
173         {
174             Object [ ] tabRequiredFields = {
175                     I18nService.getLocalizedString( FIELD_REPORT_ACTION, locale )
176             };
177 
178             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
179         }
180 
181         nIdActionCancel = Integer.parseInt( strIdActionCancel );
182         nIdActionReport = Integer.parseInt( strIdActionReport );
183         config.setIdActionCancel( nIdActionCancel );
184         config.setIdActionReport( nIdActionReport );
185 
186         if ( bCreate )
187         {
188             _taskUpdateAppointmentCancelActionConfigService.create( config );
189         }
190         else
191         {
192             _taskUpdateAppointmentCancelActionConfigService.update( config );
193         }
194 
195         return null;
196     }
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
203     {
204         return I18nService.getLocalizedString( MESSAGE_CANCEL_ACTION_UPDATED, locale );
205     }
206 }