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.appointment.modules.resource.web.workflow;
35
36 import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
37 import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentFormResourceType;
38 import fr.paris.lutece.plugins.appointment.modules.resource.business.AppointmentFormResourceTypeHome;
39 import fr.paris.lutece.plugins.appointment.modules.resource.business.workflow.SetAppointmentResourceHistory;
40 import fr.paris.lutece.plugins.appointment.modules.resource.business.workflow.SetAppointmentResourceHistoryHome;
41 import fr.paris.lutece.plugins.appointment.modules.resource.business.workflow.TaskSetAppointmentResourceConfig;
42 import fr.paris.lutece.plugins.appointment.modules.resource.service.AppointmentResourceService;
43 import fr.paris.lutece.plugins.appointment.modules.resource.service.workflow.TaskSetAppointmentResource;
44 import fr.paris.lutece.plugins.appointment.service.AppointmentService;
45 import fr.paris.lutece.plugins.appointment.service.FormService;
46 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentDTO;
47 import fr.paris.lutece.plugins.appointment.web.dto.AppointmentFormDTO;
48 import fr.paris.lutece.plugins.resource.business.IResource;
49 import fr.paris.lutece.plugins.resource.service.ResourceService;
50 import fr.paris.lutece.plugins.workflow.web.task.AbstractTaskComponent;
51 import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
52 import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
53 import fr.paris.lutece.portal.service.i18n.I18nService;
54 import fr.paris.lutece.portal.service.message.AdminMessage;
55 import fr.paris.lutece.portal.service.message.AdminMessageService;
56 import fr.paris.lutece.portal.service.template.AppTemplateService;
57 import fr.paris.lutece.portal.service.util.AppPathService;
58 import fr.paris.lutece.util.ReferenceList;
59 import fr.paris.lutece.util.html.HtmlTemplate;
60 import fr.paris.lutece.util.url.UrlItem;
61
62 import org.apache.commons.lang.StringUtils;
63
64 import java.util.HashMap;
65 import java.util.List;
66 import java.util.Locale;
67 import java.util.Map;
68
69 import javax.inject.Inject;
70 import javax.inject.Named;
71 import javax.servlet.http.HttpServletRequest;
72
73
74
75
76 public class SetAppointmentResourceTaskComponent extends AbstractTaskComponent
77 {
78
79 private static final String TEMPLATE_TASK_FORM_SET_APPOINTMENT_RESOURCE = "admin/plugins/appointment/modules/resource/task_set_appointment_resource_form.html";
80 private static final String TEMPLATE_CONFIG_SET_APPOINTMENT_RESOURCE = "admin/plugins/appointment/modules/resource/task_set_appointment_resource_config.html";
81
82
83 private static final String MARK_REF_LIST_FORM_RESOURCE_TYPES = "refListFormResourceTypes";
84 private static final String MARK_REF_LIST_APPOINTMENT_FORM = "refListAppointmentForm";
85 private static final String MARK_REF_LIST_RESOURCES = "refListResources";
86 private static final String MARK_FORM_RESOURCE_TYPE = "formResourceType";
87
88
89 private static final String PARAMETER_ID_APPOINTMENT_FORM = "idAppointmentForm";
90 private static final String PARAMETER_ID_FORM_RESOURCE_TYPE = "idFormResourceType";
91 private static final String PARAMETER_REFRESH_APPOINTMENT_FORM = "refreshAppointmentForm";
92 private static final String PARAMETER_ID_TASK = "id_task";
93 private static final String PARAMETER_ID_RESOURCE = "idResource_";
94 private static final String PARAMETER_IS_MANDATORY = "isMandatory";
95
96
97 private static final String JSP_URL_MODIFY_TASK = "jsp/admin/plugins/workflow/ModifyTask.jsp";
98
99
100 private static final String MESSAGE_ERROR_MANDATORY_FIELDS = "module.appointment.resource.task_set_appointment_resource_config.mandatoryFields";
101 private static final String MESSAGE_NO_RESOURCE_TYPE = "module.appointment.resource.task_set_appointment_resource_config.noResourceType";
102 private static final String MESSAGE_APPOINTMENT_RESOURCE_SET = "module.appointment.resource.task_set_appointment_resource_config.history.appointmentResourceSet";
103 @Inject
104 @Named( TaskSetAppointmentResource.CONFIG_SERVICE_BEAN_NAME )
105 private ITaskConfigService _taskSetAppointmentResourceConfigService;
106
107
108
109
110 @Override
111 public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
112 {
113 Map<String, Object> model = new HashMap<String, Object>( );
114
115 TaskSetAppointmentResourceConfig config = _taskSetAppointmentResourceConfigService.findByPrimaryKey( task.getId( ) );
116
117 List<AppointmentFormDTO> listAppointmentform = FormService.buildAllAppointmentFormLight( );
118 ReferenceList refListAppointmentForm = new ReferenceList( );
119
120 for ( AppointmentFormDTO form : listAppointmentform )
121 {
122 refListAppointmentForm.addItem( form.getIdForm( ), form.getTitle( ) );
123 }
124
125 String strIdAppointmentForm;
126
127
128 if ( refListAppointmentForm.size( ) == 1 )
129 {
130 strIdAppointmentForm = refListAppointmentForm.get( 0 ).getCode( );
131 }
132 else
133 {
134 strIdAppointmentForm = request.getParameter( PARAMETER_ID_APPOINTMENT_FORM );
135
136 if ( ( config != null ) && ( StringUtils.isEmpty( strIdAppointmentForm ) || !StringUtils.isNumeric( strIdAppointmentForm ) ) )
137 {
138 AppointmentFormResourceType formResourceType = AppointmentFormResourceTypeHome.findByPrimaryKey( config.getIdFormResourceType( ) );
139 strIdAppointmentForm = Integer.toString( formResourceType.getIdAppointmentForm( ) );
140 }
141 }
142
143 if ( config != null )
144 {
145 model.put( PARAMETER_ID_FORM_RESOURCE_TYPE, Integer.toString( config.getIdFormResourceType( ) ) );
146 model.put( PARAMETER_IS_MANDATORY, config.getIsMandatory( ) );
147 }
148 else
149 {
150 model.put( PARAMETER_IS_MANDATORY, true );
151 }
152
153 ReferenceList refListFormTypes = new ReferenceList( );
154
155 if ( StringUtils.isNotEmpty( strIdAppointmentForm ) && StringUtils.isNumeric( strIdAppointmentForm ) )
156 {
157 model.put( PARAMETER_ID_APPOINTMENT_FORM, strIdAppointmentForm );
158
159 List<AppointmentFormResourceType> listFormResourceType = AppointmentFormResourceTypeHome.findResourceTypesListFromIdForm( Integer
160 .parseInt( strIdAppointmentForm ) );
161
162 for ( AppointmentFormResourceType formType : listFormResourceType )
163 {
164 refListFormTypes.addItem( formType.getId( ), formType.getDescription( ) );
165 }
166 }
167
168 model.put( MARK_REF_LIST_FORM_RESOURCE_TYPES, refListFormTypes );
169 model.put( MARK_REF_LIST_APPOINTMENT_FORM, refListAppointmentForm );
170
171 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CONFIG_SET_APPOINTMENT_RESOURCE, locale, model );
172
173 return template.getHtml( );
174 }
175
176
177
178
179 @Override
180 public String doSaveConfig( HttpServletRequest request, Locale locale, ITask task )
181 {
182 boolean bRefresh = Boolean.parseBoolean( request.getParameter( PARAMETER_REFRESH_APPOINTMENT_FORM ) );
183
184 if ( bRefresh )
185 {
186 String strIdAppointmentForm = request.getParameter( PARAMETER_ID_APPOINTMENT_FORM );
187
188 return getUrlModifyTask( request, task.getId( ), strIdAppointmentForm );
189 }
190
191 String strResourceType = request.getParameter( PARAMETER_ID_FORM_RESOURCE_TYPE );
192
193 if ( StringUtils.isEmpty( strResourceType ) || !StringUtils.isNumeric( strResourceType ) )
194 {
195 return AdminMessageService.getMessageUrl( request, MESSAGE_NO_RESOURCE_TYPE, AdminMessage.TYPE_STOP );
196 }
197
198 TaskSetAppointmentResourceConfig config = _taskSetAppointmentResourceConfigService.findByPrimaryKey( task.getId( ) );
199 boolean bCreate = false;
200
201 if ( config == null )
202 {
203 bCreate = true;
204 config = new TaskSetAppointmentResourceConfig( );
205 config.setIdTask( task.getId( ) );
206 }
207
208 boolean bIsMandatory = request.getParameter( PARAMETER_IS_MANDATORY ) != null;
209 config.setIsMandatory( bIsMandatory );
210
211 config.setIdFormResourceType( Integer.parseInt( strResourceType ) );
212
213 if ( bCreate )
214 {
215 _taskSetAppointmentResourceConfigService.create( config );
216 }
217 else
218 {
219 _taskSetAppointmentResourceConfigService.update( config );
220 }
221
222 return null;
223 }
224
225
226
227
228 @Override
229 public String getDisplayTaskForm( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
230 {
231 if ( !StringUtils.equals( Appointment.APPOINTMENT_RESOURCE_TYPE, strResourceType ) )
232 {
233 return null;
234 }
235
236 AppointmentDTO appointment = AppointmentService.buildAppointmentDTOFromIdAppointment( nIdResource );
237
238
239 if ( appointment == null )
240 {
241 return null;
242 }
243
244 Map<String, Object> model = new HashMap< >( );
245
246 TaskSetAppointmentResourceConfig config = _taskSetAppointmentResourceConfigService.findByPrimaryKey( task.getId( ) );
247
248 if ( config == null )
249 {
250 return null;
251 }
252
253 AppointmentFormResourceType formResourceType = AppointmentFormResourceTypeHome.findByPrimaryKey( config.getIdFormResourceType( ) );
254
255 List<IResource> listResources = ResourceService.getInstance( ).getListResources( formResourceType.getResourceTypeName( ) );
256 ReferenceList refListResources = new ReferenceList( );
257 AppointmentResourceService appointmentResourceService = AppointmentResourceService.getInstance( );
258
259 for ( IResource resource : listResources )
260 {
261 if ( appointmentResourceService.isResourceAvailableForAppointment( resource.getIdResource( ), resource.getResourceType( ),
262 config.getIdFormResourceType( ), appointment ) )
263 {
264 refListResources.addItem( resource.getIdResource( ), resource.getResourceName( ) );
265 }
266 }
267
268 model.put( MARK_REF_LIST_RESOURCES, refListResources );
269 model.put( MARK_FORM_RESOURCE_TYPE, formResourceType );
270 model.put( PARAMETER_IS_MANDATORY, config.getIsMandatory( ) );
271
272 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_FORM_SET_APPOINTMENT_RESOURCE, locale, model );
273
274 return template.getHtml( );
275 }
276
277
278
279
280 @Override
281 public String doValidateTask( int nIdResource, String strResourceType, HttpServletRequest request, Locale locale, ITask task )
282 {
283 TaskSetAppointmentResourceConfig config = _taskSetAppointmentResourceConfigService.findByPrimaryKey( task.getId( ) );
284
285 if ( ( config == null ) || !config.getIsMandatory( ) )
286 {
287 return null;
288 }
289
290 String strIdResource = request.getParameter( PARAMETER_ID_RESOURCE + config.getIdFormResourceType( ) );
291
292 if ( StringUtils.isEmpty( strIdResource ) )
293 {
294 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
295 }
296
297 AppointmentFormResourceType formResourceType = AppointmentFormResourceTypeHome.findByPrimaryKey( config.getIdFormResourceType( ) );
298
299 IResource resource = ResourceService.getInstance( ).getResource( strIdResource, formResourceType.getResourceTypeName( ) );
300
301 if ( resource == null )
302 {
303 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
304 }
305
306 return null;
307 }
308
309
310
311
312 @Override
313 public String getDisplayTaskInformation( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
314 {
315 List<SetAppointmentResourceHistory> listHistory = SetAppointmentResourceHistoryHome.findByIdHistory( nIdHistory );
316 StringBuilder sbHistory = new StringBuilder( );
317
318 for ( SetAppointmentResourceHistory history : listHistory )
319 {
320 AppointmentFormResourceType resourceType = AppointmentFormResourceTypeHome.findByPrimaryKey( history.getIdFormResourceType( ) );
321 IResource resource = ResourceService.getInstance( ).getResource( history.getIdResource( ), resourceType.getResourceTypeName( ) );
322
323 Object [ ] args = {
324 resource.getResourceName( ), resourceType.getDescription( )
325 };
326 sbHistory.append( I18nService.getLocalizedString( MESSAGE_APPOINTMENT_RESOURCE_SET, args, locale ) );
327 sbHistory.append( "<br />" );
328 }
329
330 return sbHistory.toString( );
331 }
332
333
334
335
336 @Override
337 public String getTaskInformationXml( int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
338 {
339 return null;
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353 private String getUrlModifyTask( HttpServletRequest request, int nIdTask, String strIdAppointmentForm )
354 {
355 UrlItem urlItem = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_URL_MODIFY_TASK );
356 urlItem.addParameter( PARAMETER_ID_TASK, nIdTask );
357 urlItem.addParameter( PARAMETER_ID_APPOINTMENT_FORM, strIdAppointmentForm );
358
359 return urlItem.getUrl( );
360 }
361 }