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.appointmentants.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.servlet.http.HttpServletRequest;
42
43 import org.apache.commons.lang3.math.NumberUtils;
44 import org.apache.commons.lang3.StringUtils;
45
46 import fr.paris.lutece.plugins.appointment.service.EntryService;
47 import fr.paris.lutece.plugins.appointment.service.FormService;
48 import fr.paris.lutece.plugins.genericattributes.business.Entry;
49 import fr.paris.lutece.plugins.workflow.modules.appointmentants.business.TaskAntsAppointmentConfig;
50 import fr.paris.lutece.plugins.workflow.utils.WorkflowUtils;
51 import fr.paris.lutece.plugins.workflow.web.task.NoFormTaskComponent;
52 import fr.paris.lutece.plugins.workflowcore.service.config.ITaskConfigService;
53 import fr.paris.lutece.plugins.workflowcore.service.task.ITask;
54 import fr.paris.lutece.portal.service.template.AppTemplateService;
55 import fr.paris.lutece.portal.service.util.AppPathService;
56 import fr.paris.lutece.util.ReferenceList;
57 import fr.paris.lutece.util.html.HtmlTemplate;
58 import fr.paris.lutece.util.url.UrlItem;
59
60
61
62
63
64
65 public abstract class AbstractTaskAntsAppointmentComponent extends NoFormTaskComponent {
66
67
68 private static final String MARK_CONFIG = "config";
69 private static final String MARK_FORMS_LIST = "forms_list";
70 private static final String MARK_FORM_FIELDS_LIST = "form_fields_list";
71 private static final String MARK_FORM_ID = "id_form";
72 private static final String MARK_TASK_TITLE = "taskTitle";
73
74
75 private static final String PARAMETER_SELECT_FORM_ID = "id_form_selection";
76 public static final String PARAMETER_SELECT_FIELD_ENTRY_TITLE = "id_form_field_entry_selection";
77 public static final String PARAMETER_SELECT_FORM = "selectForm";
78 private static final String PARAMETER_ID_TASK = "id_task";
79 private static final String PARAMETER_ID_FORM = "id_form";
80
81
82 private static final String TEMPLATE_TASK_ANTS_APPOINTMENT_CONFIG = "admin/plugins/workflow/modules/appointmentants/task_ants_appointment_config.html";
83
84
85 private static final String JSP_MODIFY_TASK = "jsp/admin/plugins/workflow/ModifyTask.jsp";
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public String getDisplayConfigForm( HttpServletRequest request, String taskTitle, Locale locale, ITask task, ITaskConfigService configService )
102 {
103
104 TaskAntsAppointmentConfig config = configService.findByPrimaryKey( task.getId( ) );
105
106
107 int idForm = getCurrentFormId( request, config );
108
109
110 ReferenceList formsList = FormService.findAllInReferenceList( );
111
112 ReferenceList entriesList = getFieldsList( idForm );
113
114 Map<String, Object> model = new HashMap<>( );
115
116 model.put( MARK_TASK_TITLE, taskTitle );
117 model.put( MARK_FORM_ID, idForm );
118 model.put( MARK_CONFIG, config );
119 model.put( MARK_FORMS_LIST, formsList );
120 model.put( MARK_FORM_FIELDS_LIST, entriesList );
121
122 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_ANTS_APPOINTMENT_CONFIG, locale, model );
123
124 return template.getHtml( );
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public String doSaveConfig( HttpServletRequest request, ITask task, ITaskConfigService configService )
140 {
141
142 String paramFormId = request.getParameter( PARAMETER_SELECT_FORM_ID );
143 String paramFieldEntryId = request.getParameter( PARAMETER_SELECT_FIELD_ENTRY_TITLE );
144
145 String paramActionSelect = request.getParameter( PARAMETER_SELECT_FORM );
146
147
148 if( StringUtils.equals( paramActionSelect, PARAMETER_SELECT_FORM ) &&
149 ( Integer.parseInt( paramFormId ) != WorkflowUtils.CONSTANT_ID_NULL ) )
150 {
151 int formId = Integer.parseInt( paramFormId );
152
153 return getUrlToTaskModificationPage( request, task.getId( ), formId );
154 }
155
156 TaskAntsAppointmentConfig config = configService.findByPrimaryKey( task.getId( ) );
157
158
159
160 boolean bCreate = false;
161
162
163 if( config == null )
164 {
165 config = new TaskAntsAppointmentConfig( );
166 config.setIdTask( task.getId( ) );
167 bCreate = true;
168 }
169
170
171 if( StringUtils.isNotBlank( paramFieldEntryId ) )
172 {
173 config.setIdFieldEntry( NumberUtils.toInt( paramFieldEntryId ) );
174 }
175
176
177 config.setIdForm( NumberUtils.toInt( paramFormId ) );
178
179
180 if ( bCreate )
181 {
182 configService.create( config );
183 }
184
185 else
186 {
187 configService.update( config );
188 }
189 return null;
190 }
191
192
193
194
195
196
197
198
199
200
201
202 private ReferenceList getFieldsList( int idForm )
203 {
204
205 if( idForm != -1 )
206 {
207 List<Entry> entriesList = EntryService.findListEntry( idForm );
208
209 ReferenceList entriesRefList = new ReferenceList( );
210 for ( Entry entry : entriesList )
211 {
212
213
214
215 if( StringUtils.isNotEmpty( entry.getTitle( ) ) )
216 {
217 entriesRefList.addItem( entry.getIdEntry( ), entry.getTitle( ) );
218 }
219 }
220 return entriesRefList;
221 }
222 else
223 {
224 return new ReferenceList( );
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240 private String getUrlToTaskModificationPage( HttpServletRequest request, int idTask, int idForm )
241 {
242 StringBuilder redirectUrl = new StringBuilder( AppPathService.getBaseUrl( request ) );
243 redirectUrl.append( JSP_MODIFY_TASK );
244
245 UrlItem url = new UrlItem( redirectUrl.toString( ) );
246 url.addParameter( PARAMETER_ID_TASK, idTask );
247 url.addParameter( PARAMETER_ID_FORM, idForm );
248
249 return url.getUrl( );
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263 private int getCurrentFormId( HttpServletRequest request, TaskAntsAppointmentConfig config )
264 {
265
266 String strIdForm = request.getParameter( PARAMETER_ID_FORM );
267
268 if( StringUtils.isNumeric( strIdForm ) )
269 {
270
271 return Integer.parseInt( strIdForm );
272 }
273
274
275 if( config != null && config.getIdForm( ) != 0 )
276 {
277 return config.getIdForm( );
278 }
279
280
281
282
283 return -1;
284 }
285
286
287
288
289 @Override
290 public String getDisplayTaskInformation(int nIdHistory, HttpServletRequest request, Locale locale, ITask task )
291 {
292 return null;
293 }
294
295
296
297
298 @Override
299 public String getDisplayConfigForm( HttpServletRequest request, Locale locale, ITask task )
300 {
301 return null;
302 }
303 }