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.gru.service.demand;
35
36 import fr.paris.lutece.plugins.gru.business.NotifiedDemand;
37 import fr.paris.lutece.plugins.gru.business.demandtype.DemandType;
38 import fr.paris.lutece.plugins.gru.business.demandtype.DemandTypeHome;
39 import fr.paris.lutece.plugins.gru.business.domain.BusinessDomain;
40 import fr.paris.lutece.plugins.gru.service.demandtype.DemandTypeService;
41 import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
42 import fr.paris.lutece.plugins.grubusiness.business.demand.Demand;
43 import fr.paris.lutece.plugins.grubusiness.business.notification.Notification;
44 import fr.paris.lutece.portal.business.user.AdminUser;
45 import fr.paris.lutece.portal.service.rbac.RBACService;
46 import fr.paris.lutece.portal.service.spring.SpringContextService;
47 import fr.paris.lutece.portal.service.util.AppException;
48 import fr.paris.lutece.portal.service.util.AppLogService;
49
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.Comparator;
54 import java.util.List;
55
56
57
58
59 public final class DemandService
60 {
61 private static final String BEAN_DEMAND_SERVICE = "gru.demandService";
62 private static fr.paris.lutece.plugins.grubusiness.business.demand.DemandService _service = SpringContextService.getBean( BEAN_DEMAND_SERVICE );;
63 private static Comparator<Demand> _comparatorDemands = new Comparator<Demand>( )
64 {
65 @Override
66 public int compare( Demand demand1, Demand demand2 )
67 {
68 int nResult = -1;
69
70 switch( demand1.getStatusId( ) )
71 {
72 case Demand.STATUS_INPROGRESS:
73 switch( demand2.getStatusId( ) )
74 {
75 case Demand.STATUS_INPROGRESS:
76 nResult = Long.compare( demand2.getCreationDate( ), demand1.getCreationDate( ) );
77 break;
78
79 case Demand.STATUS_CLOSED:
80 nResult = 1;
81 break;
82
83 default:
84 nResult = 1;
85 break;
86 }
87 break;
88
89 case Demand.STATUS_CLOSED:
90 switch( demand2.getStatusId( ) )
91 {
92 case Demand.STATUS_INPROGRESS:
93 nResult = -1;
94 break;
95
96 case Demand.STATUS_CLOSED:
97 nResult = Long.compare( demand2.getClosureDate( ), demand1.getClosureDate( ) );
98 break;
99
100 default:
101 nResult = 1;
102 break;
103 }
104 break;
105
106 default:
107 nResult = -1;
108 }
109
110 return nResult;
111 }
112 };
113
114
115
116
117 private DemandService( )
118 {
119
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 public static Demand getDemand( String strDemandId, String strDemandTypeId, AdminUser user )
134 {
135 Demand demand = _service.findByPrimaryKey( strDemandId, strDemandTypeId );
136
137 demand.setTitle( DemandTypeService.getTypeLabel( strDemandTypeId ) );
138 demand.setShowDetails( isDetailsAuthorized( strDemandTypeId, user ) );
139
140 return demand;
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 public static List<Demand> getDemands( Customer customer, AdminUser user, int nStatus )
155 {
156 Collection<Demand> collectionBase = _service.findByCustomerId( customer.getId( ) );
157 List<Demand> listDemand = new ArrayList<Demand>( );
158
159 for ( Demand base : collectionBase )
160 {
161 if ( base.getStatusId( ) == nStatus && isAuthorized( base, user ) )
162 {
163 listDemand.add( DemandTypeService.setDemandActions( base, customer, user ) );
164 }
165 }
166
167 Collections.sort( listDemand, _comparatorDemands );
168
169 return listDemand;
170 }
171
172
173
174
175
176
177
178
179
180
181 public static List<Demand> getDemandsByRef( String reference, AdminUser user )
182 {
183 Collection<Demand> collectionBase = _service.findByReference( reference );
184 List<Demand> listDemand = new ArrayList<Demand>( );
185
186 for ( Demand base : collectionBase )
187 {
188 if ( isAuthorized( base, user ) )
189 {
190 listDemand.add( DemandTypeService.setDemandActions( base, base.getCustomer( ), user ) );
191 }
192 }
193
194 Collections.sort( listDemand, _comparatorDemands );
195
196 return listDemand;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 public static List<Demand> getDemandsExcludingTypes( Customer customer, List<String> listExcludedTypes, AdminUser user )
211 {
212 Collection<Demand> collectionBase = _service.findByCustomerId( customer.getId( ) );
213 List<Demand> listDemand = new ArrayList<Demand>( );
214
215 for ( Demand base : collectionBase )
216 {
217 if ( !listExcludedTypes.contains( base.getTypeId( ) ) && isAuthorized( base, user ) )
218 {
219 listDemand.add( DemandTypeService.setDemandActions( base, customer, user ) );
220 }
221 }
222
223 Collections.sort( listDemand, _comparatorDemands );
224
225 return listDemand;
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239 public static List<Demand> getDemandsIncludingTypes( Customer customer, List<String> listIncludedTypes, AdminUser user )
240 {
241 Collection<Demand> collectionBase = _service.findByCustomerId( customer.getId( ) );
242 List<Demand> listDemand = new ArrayList<Demand>( );
243
244 for ( Demand base : collectionBase )
245 {
246 if ( listIncludedTypes.contains( base.getTypeId( ) ) && isAuthorized( base, user ) )
247 {
248 listDemand.add( DemandTypeService.setDemandActions( base, customer, user ) );
249 }
250 }
251
252 Collections.sort( listDemand, _comparatorDemands );
253
254 return listDemand;
255 }
256
257
258
259
260
261
262
263
264
265
266 private static boolean isAuthorized( Demand base, AdminUser user )
267 {
268 DemandType type = DemandTypeHome.findByTypeId( base.getTypeId( ) );
269
270 if ( type == null )
271 {
272 AppLogService.error( "Demand Type missing for ID : " + base.getTypeId( ) + " of demand ID : " + base.getId( ) );
273
274 return false;
275 }
276
277 String strBusinessDomainId = String.valueOf( type.getBusinessDomainId( ) );
278
279 return RBACService.isAuthorized( BusinessDomain.RESOURCE_TYPE, strBusinessDomainId, BusinessDomain.PERMISSION_VIEW_SUMMARY, user )
280 || RBACService.isAuthorized( BusinessDomain.RESOURCE_TYPE, strBusinessDomainId, BusinessDomain.PERMISSION_VIEW_DETAILS, user );
281 }
282
283
284
285
286
287
288
289
290
291
292 private static boolean isDetailsAuthorized( String strDemandTypeId, AdminUser user )
293 {
294 DemandType type = DemandTypeHome.findByTypeId( strDemandTypeId );
295
296 if ( type == null )
297 {
298 throw new AppException( "Demand Type missing for ID : " + strDemandTypeId );
299 }
300
301 String strBusinessDomainId = String.valueOf( type.getBusinessDomainId( ) );
302
303 return RBACService.isAuthorized( BusinessDomain.RESOURCE_TYPE, strBusinessDomainId, BusinessDomain.PERMISSION_VIEW_DETAILS, user );
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317 public static List<NotifiedDemand> getNotifiedDemands( Customer customer, AdminUser user, int nStatus )
318 {
319 List<Demand> listDemand = getDemands( customer, user, nStatus );
320 List<NotifiedDemand> listNotifiedDemand = new ArrayList<NotifiedDemand>( );
321 NotifiedDemand notifiedDemand = null;
322
323 for ( Demand demand : listDemand )
324 {
325 notifiedDemand = updateNotifiedDemandStatus( demand );
326 listNotifiedDemand.add( notifiedDemand );
327 }
328
329 return listNotifiedDemand;
330 }
331
332
333
334
335
336
337
338
339 public static NotifiedDemand updateNotifiedDemandStatus( Demand demand )
340 {
341 boolean bIsAgentStatusFound = false;
342 boolean bIsCustomerStatusFound = false;
343
344 NotifiedDemand notifiedDemand = NotifiedDemand.toDemand( demand );
345
346 if ( notifiedDemand != null && notifiedDemand.getNotifications( ) != null )
347 {
348 for ( Notification notification : notifiedDemand.getNotifications( ) )
349 {
350 if ( !bIsAgentStatusFound && ( notification.getBackofficeNotification( ) != null ) )
351 {
352 notifiedDemand.setAgentStatus( notification.getBackofficeNotification( ).getStatusText( ) );
353 bIsAgentStatusFound = true;
354 }
355
356 if ( !bIsCustomerStatusFound && ( notification.getMyDashboardNotification( ) != null ) )
357 {
358 notifiedDemand.setCustomerStatus( notification.getMyDashboardNotification( ).getStatusText( ) );
359 bIsCustomerStatusFound = true;
360 }
361
362 if ( bIsAgentStatusFound && bIsCustomerStatusFound )
363 {
364 break;
365 }
366 }
367 }
368 return notifiedDemand;
369 }
370
371 }