1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
65
66
67 public class UpdateAppointmentCancelActionTaskComponent extends NoFormTaskComponent
68 {
69
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
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
79 private static final String MARK_LIST_ACTIONS = "list_actions";
80 private static final String MARK_CONFIG = "config";
81
82
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
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
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
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
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 }