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.tipiforms.service.task;
35
36 import java.util.List;
37 import java.util.Locale;
38
39 import javax.inject.Inject;
40
41 import org.apache.commons.lang3.StringUtils;
42 import org.apache.commons.lang3.math.NumberUtils;
43
44 import fr.paris.lutece.plugins.forms.business.FormQuestionResponse;
45 import fr.paris.lutece.plugins.forms.business.FormQuestionResponseHome;
46 import fr.paris.lutece.plugins.forms.business.FormResponse;
47 import fr.paris.lutece.plugins.forms.business.FormResponseHome;
48 import fr.paris.lutece.plugins.forms.business.Question;
49 import fr.paris.lutece.plugins.genericattributes.business.Response;
50 import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
51 import fr.paris.lutece.plugins.workflow.modules.tipi.service.ITipiRefDetHistoryService;
52 import fr.paris.lutece.plugins.workflow.modules.tipi.service.ITipiService;
53 import fr.paris.lutece.plugins.workflow.modules.tipi.service.task.AbstractTipiProviderTask;
54 import fr.paris.lutece.plugins.workflow.modules.tipiforms.business.task.TaskTipiFormsProviderConfig;
55 import fr.paris.lutece.plugins.workflow.modules.tipiforms.business.task.TaskTipiFormsProviderConfigDAO;
56 import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
57 import fr.paris.lutece.plugins.workflowcore.service.resource.IResourceHistoryService;
58 import fr.paris.lutece.portal.service.i18n.I18nService;
59 import fr.paris.lutece.portal.service.util.AppException;
60
61
62
63
64
65
66 public class TipiFormsProviderTask extends AbstractTipiProviderTask
67 {
68
69 private static final String MESSAGE_TASK_TITLE = "module.workflow.tipiforms.task_forms_provider_title";
70
71 private final TaskTipiFormsProviderConfigDAO _taskTipiFormsProviderConfigDAO;
72
73
74
75
76
77
78
79
80
81
82
83
84
85 @Inject
86 public TipiFormsProviderTask( TaskTipiFormsProviderConfigDAO taskTipiFormsProviderConfigDAO, IResourceHistoryService resourceHistoryService,
87 ITipiService tipiService, ITipiRefDetHistoryService tipiRefDetHistoryService )
88 {
89 super( resourceHistoryService, tipiService, tipiRefDetHistoryService );
90
91 _taskTipiFormsProviderConfigDAO = taskTipiFormsProviderConfigDAO;
92 }
93
94
95
96
97 @Override
98 public String getTitle( Locale local )
99 {
100 return I18nService.getLocalizedString( MESSAGE_TASK_TITLE, local );
101 }
102
103
104
105
106 @Override
107 protected String provideRefDet( ResourceHistory resourceHistory )
108 {
109 FormResponse formResponse = findFormResponseFrom( resourceHistory );
110 TaskTipiFormsProviderConfig config = findConfig( );
111
112 return findResponseValue( formResponse, config.getIdRefDetQuestion( ) );
113 }
114
115
116
117
118
119
120
121
122 private FormResponse findFormResponseFrom( ResourceHistory resourceHistory )
123 {
124 FormResponse formResponse = null;
125
126 if ( FormResponse.RESOURCE_TYPE.equals( resourceHistory.getResourceType( ) ) )
127 {
128 formResponse = FormResponseHome.findByPrimaryKey( resourceHistory.getIdResource( ) );
129 }
130 else
131 {
132 throw new AppException( "This task must be used with a form" );
133 }
134
135 return formResponse;
136 }
137
138
139
140
141
142
143 private TaskTipiFormsProviderConfig findConfig( )
144 {
145 return _taskTipiFormsProviderConfigDAO.load( getId( ) );
146 }
147
148
149
150
151
152
153
154
155
156
157 private String findResponseValue( FormResponse formResponse, int nIdQuestion )
158 {
159 String strValue = StringUtils.EMPTY;
160 boolean bFound = false;
161
162 for ( FormQuestionResponse formQuestionResponse : FormQuestionResponseHome.getFormQuestionResponseListByFormResponse( formResponse.getId( ) ) )
163 {
164 Question question = formQuestionResponse.getQuestion( );
165
166 if ( question.getId( ) == nIdQuestion )
167 {
168 List<Response> listResponse = formQuestionResponse.getEntryResponse( );
169
170 if ( listResponse.size( ) > 1 )
171 {
172 throw new AppException( "The question contains several responses !" );
173 }
174
175
176 strValue= EntryTypeServiceManager.getEntryTypeService( question.getEntry( ) ).getResponseValueForExport( question.getEntry( ), null, listResponse.get( 0 ), Locale.FRENCH );
177 bFound = true;
178 }
179 }
180
181 if ( !bFound )
182 {
183 throw new AppException( "The question with the following id does not exist : " + nIdQuestion );
184 }
185
186 return strValue;
187 }
188
189
190
191
192 @Override
193 protected int provideAmount( ResourceHistory resourceHistory )
194 {
195 FormResponse formResponse = findFormResponseFrom( resourceHistory );
196 TaskTipiFormsProviderConfig config = findConfig( );
197
198 return NumberUtils.toInt( findResponseValue( formResponse, config.getIdAmountQuestion( ) ) );
199 }
200
201
202
203
204 @Override
205 protected String provideEmail( ResourceHistory resourceHistory )
206 {
207 FormResponse formResponse = findFormResponseFrom( resourceHistory );
208 TaskTipiFormsProviderConfig config = findConfig( );
209
210 return findResponseValue( formResponse, config.getIdEmailQuestion( ) );
211 }
212
213
214
215
216 @Override
217 public void doRemoveConfig( )
218 {
219 _taskTipiFormsProviderConfigDAO.delete( getId( ) );
220 }
221 }