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.directory.business;
35
36 import fr.paris.lutece.plugins.directory.service.DirectoryService;
37 import fr.paris.lutece.plugins.directory.service.directorysearch.DirectoryIndexer;
38 import fr.paris.lutece.plugins.directory.service.directorysearch.DirectorySearchService;
39 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
40 import fr.paris.lutece.portal.service.plugin.Plugin;
41 import fr.paris.lutece.portal.service.spring.SpringContextService;
42 import fr.paris.lutece.portal.service.util.AppException;
43 import fr.paris.lutece.portal.service.workflow.WorkflowService;
44 import fr.paris.lutece.util.sql.TransactionManager;
45
46 import java.util.List;
47
48
49
50
51 public final class RecordHome
52 {
53
54 private static IRecordDAO _dao = SpringContextService.getBean( "directoryRecordDAO" );
55 private static final int STEP_DELETE = 50;
56
57
58
59
60 private RecordHome( )
61 {
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public static int create( Record record, Plugin plugin )
75 {
76 record.setDateModification( DirectoryUtils.getCurrentTimestamp( ) );
77
78 TransactionManager.beginTransaction( plugin );
79
80 try
81 {
82 record.setIdRecord( _dao.insert( record, plugin ) );
83
84 DirectorySearchService.getInstance( ).addIndexerAction( record.getIdRecord( ), IndexerAction.TASK_CREATE, plugin );
85
86 for ( RecordField recordField : record.getListRecordField( ) )
87 {
88 recordField.setRecord( record );
89 RecordFieldHome.create( recordField, plugin );
90 }
91
92 TransactionManager.commitTransaction( plugin );
93 }
94 catch( Exception e )
95 {
96 TransactionManager.rollBack( plugin );
97 throw new AppException( e.getMessage( ), e );
98 }
99
100 return record.getIdRecord( );
101 }
102
103
104
105
106
107
108
109
110
111
112
113 public static int copy( Record record, Plugin plugin )
114 {
115 record.setDateModification( DirectoryUtils.getCurrentTimestamp( ) );
116
117 RecordFieldFilter filter = new RecordFieldFilter( );
118 filter.setIdRecord( record.getIdRecord( ) );
119 record.setListRecordField( RecordFieldHome.getRecordFieldList( filter, plugin ) );
120
121 TransactionManager.beginTransaction( plugin );
122
123 try
124 {
125 record.setIdRecord( _dao.insert( record, plugin ) );
126
127 DirectorySearchService.getInstance( ).addIndexerAction( record.getIdRecord( ), IndexerAction.TASK_CREATE, plugin );
128
129 for ( RecordField recordField : record.getListRecordField( ) )
130 {
131 recordField.setRecord( record );
132
133
134 if ( !recordField.getEntry( ).getEntryType( ).getClassName( ).equals( EntryTypeNumbering.class.getName( ) ) )
135 {
136 RecordFieldHome.copy( recordField, plugin );
137 }
138 else
139 {
140
141 IEntry entryNumbering = EntryHome.findByPrimaryKey( recordField.getEntry( ).getIdEntry( ), plugin );
142 int numbering = DirectoryService.getInstance( ).getMaxNumber( entryNumbering );
143
144 if ( numbering != DirectoryUtils.CONSTANT_ID_NULL )
145 {
146 entryNumbering.getFields( ).get( 0 ).setValue( String.valueOf( numbering + 1 ) );
147 FieldHome.update( entryNumbering.getFields( ).get( 0 ), plugin );
148 recordField.setValue( String.valueOf( numbering ) );
149 RecordFieldHome.create( recordField, plugin );
150 }
151 }
152 }
153
154 TransactionManager.commitTransaction( plugin );
155 }
156 catch( Exception e )
157 {
158 TransactionManager.rollBack( plugin );
159 throw new AppException( e.getMessage( ), e );
160 }
161
162 return record.getIdRecord( );
163 }
164
165
166
167
168
169
170
171
172
173
174 public static void updateWidthRecordField( Record record, Plugin plugin )
175 {
176 record.setDateModification( DirectoryUtils.getCurrentTimestamp( ) );
177
178 RecordFieldFilter filter = new RecordFieldFilter( );
179 filter.setIdRecord( record.getIdRecord( ) );
180
181 TransactionManager.beginTransaction( plugin );
182
183 try
184 {
185 _dao.store( record, plugin );
186
187 DirectorySearchService.getInstance( ).addIndexerAction( record.getIdRecord( ), IndexerAction.TASK_MODIFY, plugin );
188
189
190 RecordFieldHome.removeByFilter( filter, plugin );
191
192
193 for ( RecordField recordField : record.getListRecordField( ) )
194 {
195 recordField.setRecord( record );
196 RecordFieldHome.create( recordField, plugin );
197 }
198
199 TransactionManager.commitTransaction( plugin );
200 }
201 catch( Exception e )
202 {
203 TransactionManager.rollBack( plugin );
204 throw new AppException( e.getMessage( ), e );
205 }
206 }
207
208
209
210
211
212
213
214
215
216
217 public static void update( Record record, Plugin plugin )
218 {
219 record.setDateModification( DirectoryUtils.getCurrentTimestamp( ) );
220 _dao.store( record, plugin );
221 DirectorySearchService.getInstance( ).addIndexerAction( record.getIdRecord( ), IndexerAction.TASK_MODIFY, plugin );
222 }
223
224
225
226
227
228
229
230
231
232 public static void remove( int nIdRecord, Plugin plugin )
233 {
234 TransactionManager.beginTransaction( plugin );
235
236 try
237 {
238 DirectorySearchService.getInstance( ).addIndexerAction( nIdRecord, IndexerAction.TASK_DELETE, plugin );
239 WorkflowService.getInstance( ).doRemoveWorkFlowResource( nIdRecord, Record.WORKFLOW_RESOURCE_TYPE );
240
241
242 RecordFieldFilter filter = new RecordFieldFilter( );
243 filter.setIdRecord( nIdRecord );
244 RecordFieldHome.removeByFilter( filter, true, plugin );
245 _dao.delete( nIdRecord, plugin );
246 TransactionManager.commitTransaction( plugin );
247 }
248 catch( Exception e )
249 {
250 TransactionManager.rollBack( plugin );
251 throw new AppException( e.getMessage( ), e );
252 }
253 }
254
255
256
257
258
259
260
261
262
263
264 public static void removeByIdDirectory( Integer nIdDirectory, Plugin plugin )
265 {
266 WorkflowService workflowService = WorkflowService.getInstance( );
267 boolean nWorkFlowServiceIsAvaible = workflowService.isAvailable( );
268
269 Directory directory = DirectoryHome.findByPrimaryKey( nIdDirectory, plugin );
270
271 RecordFieldFilter recordFilter = new RecordFieldFilter( );
272 recordFilter.setIdDirectory( nIdDirectory );
273
274 List<Integer> listRecordId = RecordHome.getListRecordId( recordFilter, plugin );
275
276
277 int nListRecordIdSize = listRecordId.size( );
278
279 if ( nListRecordIdSize > STEP_DELETE )
280 {
281 int nMax = nListRecordIdSize - STEP_DELETE;
282 int nIndex = 0;
283 List<Integer> subList;
284
285 for ( int i = 0; i < nMax; i += STEP_DELETE )
286 {
287 subList = listRecordId.subList( i, i + STEP_DELETE );
288 RecordFieldHome.removeByListRecordId( subList, plugin );
289
290 if ( nWorkFlowServiceIsAvaible )
291 {
292 workflowService.doRemoveWorkFlowResourceByListId( subList, Record.WORKFLOW_RESOURCE_TYPE, directory.getIdWorkflow( ) );
293 }
294
295 nIndex = i;
296 }
297
298 subList = listRecordId.subList( nIndex, nListRecordIdSize );
299 RecordFieldHome.removeByListRecordId( subList, plugin );
300
301 if ( nWorkFlowServiceIsAvaible )
302 {
303 workflowService.doRemoveWorkFlowResourceByListId( subList, Record.WORKFLOW_RESOURCE_TYPE, directory.getIdWorkflow( ) );
304 }
305 }
306 else
307 {
308 RecordFieldHome.removeByListRecordId( listRecordId, plugin );
309
310 if ( nWorkFlowServiceIsAvaible )
311 {
312 workflowService.doRemoveWorkFlowResourceByListId( listRecordId, Record.WORKFLOW_RESOURCE_TYPE, directory.getIdWorkflow( ) );
313 }
314 }
315
316
317 _dao.deleteRecordByDirectoryId( nIdDirectory, plugin );
318
319
320
321
322
323 DirectoryIndexer.appendListRecordToDelete( listRecordId );
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public static Record findByPrimaryKey( int nKey, Plugin plugin )
339 {
340 return _dao.load( nKey, plugin );
341 }
342
343
344
345
346
347
348
349
350
351
352 public static Boolean direcytoryRecordListHasWorkflow( int nIdDirectory, Plugin plugin )
353 {
354 return _dao.direcytoryRecordListHasWorkflow( nIdDirectory, plugin );
355 }
356
357
358
359
360
361
362
363
364
365
366 public static List<Record> loadListByListId( List<Integer> lIdList, Plugin plugin )
367 {
368 return _dao.loadList( lIdList, plugin );
369 }
370
371
372
373
374
375
376
377
378
379
380 public static List<Record> getListRecord( RecordFieldFilter filter, Plugin plugin )
381 {
382 return _dao.selectListByFilter( filter, plugin );
383 }
384
385
386
387
388
389
390
391
392
393
394 public static int getCountRecord( RecordFieldFilter filter, Plugin plugin )
395 {
396 return _dao.selectCountByFilter( filter, plugin );
397 }
398
399
400
401
402
403
404
405
406
407
408 public static List<Integer> getListRecordId( RecordFieldFilter filter, Plugin plugin )
409 {
410 return _dao.selectListIdByFilter( filter, plugin );
411 }
412
413
414
415
416
417
418
419
420
421
422 public static Integer getDirectoryIdByRecordId( Integer nRecordId, Plugin plugin )
423 {
424 return _dao.getDirectoryIdByRecordId( nRecordId, plugin );
425 }
426 }