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.crm.service.demand;
35
36 import fr.paris.lutece.plugins.crm.business.demand.Demand;
37 import fr.paris.lutece.plugins.crm.business.demand.DemandFilter;
38 import fr.paris.lutece.plugins.crm.business.demand.DemandHome;
39 import fr.paris.lutece.plugins.crm.business.demand.DemandSort;
40 import fr.paris.lutece.plugins.crm.business.demand.DemandStatusCRM;
41 import fr.paris.lutece.plugins.crm.business.demand.DemandType;
42 import fr.paris.lutece.plugins.crm.business.demand.IPaginationProperties;
43 import fr.paris.lutece.plugins.crm.business.demand.PaginationFilterSortManager;
44 import fr.paris.lutece.plugins.crm.service.notification.NotificationService;
45 import fr.paris.lutece.portal.service.spring.SpringContextService;
46 import fr.paris.lutece.portal.service.util.AppLogService;
47 import fr.paris.lutece.util.html.IPaginator;
48 import fr.paris.lutece.util.httpaccess.HttpAccessException;
49
50 import org.apache.commons.lang3.StringUtils;
51
52 import java.sql.Timestamp;
53
54 import java.util.ArrayList;
55 import java.util.Date;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Locale;
59 import java.util.Map;
60
61
62
63
64
65
66 public class DemandService
67 {
68 private static final String BEAN_CRM_DEMANDSERVICE = "crm.demandService";
69
70
71
72
73 protected DemandService( )
74 {
75 }
76
77
78
79
80
81
82 public static DemandService getService( )
83 {
84 return SpringContextService.getBean( BEAN_CRM_DEMANDSERVICE );
85 }
86
87
88
89
90
91
92
93
94 public Demand findByPrimaryKey( int nIdDemand )
95 {
96 return DemandHome.findByPrimaryKey( nIdDemand );
97 }
98
99
100
101
102
103
104
105
106
107
108 public Demand findByRemoteKey( String strRemoteId, int nIdDemandType )
109 {
110 return DemandHome.findByRemoteKey( strRemoteId, nIdDemandType );
111 }
112
113
114
115
116
117
118
119
120 public int create( Demand demand )
121 {
122 int nIdDemand = -1;
123
124 if ( demand != null )
125 {
126 demand.setDateModification( new Timestamp( new Date( ).getTime( ) ) );
127 nIdDemand = DemandHome.create( demand );
128 }
129
130 return nIdDemand;
131 }
132
133
134
135
136
137
138
139 public void update( Demand demand )
140 {
141 if ( demand != null )
142 {
143 demand.setDateModification( new Timestamp( new Date( ).getTime( ) ) );
144 DemandHome.update( demand );
145 }
146 }
147
148
149
150
151
152
153
154 public void remove( int nIdDemand )
155 {
156
157 NotificationService.getService( ).removeByIdDemand( nIdDemand );
158 DemandHome.remove( nIdDemand );
159 }
160
161
162
163
164
165
166
167 public void removeWithItsResource( int nIdDemand, boolean bByDaemon )
168 {
169 Demand demand = findByPrimaryKey( nIdDemand );
170
171 if ( demand != null )
172 {
173 DemandType demandType = DemandTypeService.getService( ).findByPrimaryKey( demand.getIdDemandType( ) );
174
175 if ( demandType != null )
176 {
177 if ( StringUtils.isNotBlank( demandType.getUrlDelete( ) ) && bByDaemon )
178 {
179 try
180 {
181 DemandWebService.getService( ).sendRemoveDraft( demandType.getUrlDelete( ), nIdDemand, demand.getData( ).replace( "\"", "'" ) );
182 remove( nIdDemand );
183 }
184 catch( HttpAccessException e )
185 {
186 String strError = "CRM Demand - Error connecting to '" + demandType.getUrlDelete( ) + "' : ";
187 AppLogService.error( strError + e.getMessage( ), e );
188 }
189 }
190 else
191 {
192 try
193 {
194 DemandWebService.getService( ).sendRemoveDraft( demandType.getUrlResource( ), nIdDemand, demand.getData( ).replace( "\"", "'" ) );
195 remove( nIdDemand );
196 }
197 catch( HttpAccessException e )
198 {
199 String strError = "CRM Demand - Error connecting to '" + demandType.getUrlResource( ) + "' : ";
200 AppLogService.error( strError + e.getMessage( ), e );
201 }
202 }
203 }
204 }
205 }
206
207
208
209
210
211
212
213 public void removeByIdDemandType( int nIdDemandType )
214 {
215 DemandFilterm/business/demand/DemandFilter.html#DemandFilter">DemandFilter dFilter = new DemandFilter( );
216 dFilter.setIdDemandType( nIdDemandType );
217
218 for ( Demand demand : findByFilter( dFilter ) )
219 {
220 removeWithItsResource( demand.getIdDemand( ), false );
221 }
222 }
223
224
225
226
227
228
229 public List<Demand> findAll( )
230 {
231 return DemandHome.findAll( );
232 }
233
234
235
236
237
238
239
240
241 public List<Demand> findByFilter( DemandFilter dFilter )
242 {
243 return DemandHome.findByFilter( dFilter );
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 public Map<String, List<Demand>> findByIdCRMUser( int nIdCRMUser, Locale locale, int nIdStatusToSort, List<DemandSort> listDemandSort )
260 {
261 Map<String, List<Demand>> map = new HashMap<String, List<Demand>>( );
262
263 for ( DemandStatusCRM statusCRM : DemandStatusCRMService.getService( ).getAllStatusCRM( locale ) )
264 {
265 DemandFilterm/business/demand/DemandFilter.html#DemandFilter">DemandFilter dFilter = new DemandFilter( );
266 dFilter.setIdCRMUser( nIdCRMUser );
267 dFilter.setIdStatusCRM( statusCRM.getIdStatusCRM( ) );
268
269
270 if ( nIdStatusToSort == statusCRM.getIdStatusCRM( ) )
271 {
272 dFilter.setListDemandSort( listDemandSort );
273 }
274
275 List<Demand> listDemands = findByFilter( dFilter );
276 map.put( Integer.toString( statusCRM.getIdStatusCRM( ) ), listDemands );
277 }
278
279 return map;
280 }
281
282
283
284
285
286
287
288
289
290
291 public Map<String, List<Demand>> findByFilterMap( DemandFilter dFilter, Locale locale, PaginationFilterSortManager paginationFilterSortManager )
292 {
293 Map<String, List<Demand>> map = new HashMap<String, List<Demand>>( );
294
295 for ( DemandStatusCRM statusCRM : DemandStatusCRMService.getService( ).getAllStatusCRM( locale ) )
296 {
297 int nIdStatus = statusCRM.getIdStatusCRM( );
298 DemandFilteress/demand/DemandFilter.html#DemandFilter">DemandFilter filterWithSort = new DemandFilter( );
299 filterWithSort.setDateModification( dFilter.getDateModification( ) );
300 filterWithSort.setIdCRMUser( dFilter.getIdCRMUser( ) );
301 filterWithSort.setIdDemandType( dFilter.getIdDemandType( ) );
302 filterWithSort.setIdStatusCRM( nIdStatus );
303 filterWithSort.setIsWideSearch( dFilter.getIsWideSearch( ) );
304 filterWithSort.setNotification( dFilter.getNotification( ) );
305 filterWithSort.setOperatorDateModification( dFilter.getOperatorDateModification( ) );
306
307 DemandSort sort = paginationFilterSortManager.retrieveSort( nIdStatus );
308 List<DemandSort> listDemandSort = new ArrayList<DemandSort>( );
309
310 if ( sort != null )
311 {
312 listDemandSort.add( sort );
313 }
314
315 filterWithSort.setListDemandSort( listDemandSort );
316
317 int nTotalResult = this.countByFilter( filterWithSort );
318
319 List<Demand> listDemands = this.findByFilterWithPagination( filterWithSort,
320 paginationFilterSortManager.retrievePaginationProperties( nIdStatus, nTotalResult ) );
321
322 IPaginator<Demand> paginator = paginationFilterSortManager.createAndStorePaginator( nIdStatus, listDemands, nTotalResult );
323
324 map.put( Integer.toString( nIdStatus ), paginator.getPageItems( ) );
325 }
326
327 return map;
328 }
329
330
331
332
333
334
335
336
337
338
339 public List<Demand> findByFilterWithPagination( DemandFilter dFilter, IPaginationProperties paginationProperties )
340 {
341 return DemandHome.findByFilter( dFilter, paginationProperties );
342 }
343
344
345
346
347
348
349
350
351 public int countByFilter( DemandFilter dFilter )
352 {
353 return DemandHome.countByFilter( dFilter );
354 }
355 }