View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * class RecordHome
50   */
51  public final class RecordHome
52  {
53      // Static variable pointed at the DAO instance
54      private static IRecordDAO _dao = SpringContextService.getBean( "directoryRecordDAO" );
55      private static final int STEP_DELETE = 50;
56  
57      /**
58       * Private constructor - this class need not be instantiated
59       */
60      private RecordHome( )
61      {
62      }
63  
64      /**
65       * Creation of an instance of record
66       *
67       * @param record
68       *            The instance of the record which contains the informations to store
69       * @param plugin
70       *            the Plugin
71       * @return the id of the new record
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      * Copy an instance of record
105      *
106      * @param record
107      *            The instance of the record who must copy
108      * @param plugin
109      *            the Plugin
110      * @return the id of the record
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                 // we don't copy numbering entry
134                 if ( !recordField.getEntry( ).getEntryType( ).getClassName( ).equals( EntryTypeNumbering.class.getName( ) ) )
135                 {
136                     RecordFieldHome.copy( recordField, plugin );
137                 }
138                 else
139                 {
140                     // update the number
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      * Update of the record which is specified in parameter
167      *
168      * @param record
169      *            The instance of the record which contains the informations to update
170      * @param plugin
171      *            the Plugin
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             // delete all record field in database associate
190             RecordFieldHome.removeByFilter( filter, plugin );
191 
192             // insert the new record Field
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      * Update of the record
210      *
211      * @param record
212      *            The instance of the record which contains the informations to update
213      * @param plugin
214      *            the Plugin
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      * Remove the record whose identifier is specified in parameter
226      *
227      * @param nIdRecord
228      *            The recordId
229      * @param plugin
230      *            the Plugin
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             // delete all record field in database associate
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      * Remove directory and workflow record by directory Id
257      * 
258      * @param nIdDirectory
259      *            The directory id
260      * @param plugin
261      *            The plugin
262      * @deprecated This function does not remove the associated files
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         // --- Suppress record fields & workflow resources ---
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         // --- Suppress records ---
317         _dao.deleteRecordByDirectoryId( nIdDirectory, plugin );
318 
319         // --- Update index ---
320         // Hack to bypass problem of primary key violation on table "directory_indexer_action"
321         // when inserting many records
322         // TODO : fixe me
323         DirectoryIndexer.appendListRecordToDelete( listRecordId );
324     }
325 
326     // /////////////////////////////////////////////////////////////////////////
327     // Finders
328 
329     /**
330      * Returns an instance of a recordwhose identifier is specified in parameter
331      *
332      * @param nKey
333      *            The formResponse primary key
334      * @param plugin
335      *            the Plugin
336      * @return an instance of FormResponse
337      */
338     public static Record findByPrimaryKey( int nKey, Plugin plugin )
339     {
340         return _dao.load( nKey, plugin );
341     }
342 
343     /**
344      * Test if the given directory record list has a worflow
345      * 
346      * @param nIdDirectory
347      *            directory Id
348      * @param plugin
349      *            the plugin
350      * @return true if has at least one
351      */
352     public static Boolean direcytoryRecordListHasWorkflow( int nIdDirectory, Plugin plugin )
353     {
354         return _dao.direcytoryRecordListHasWorkflow( nIdDirectory, plugin );
355     }
356 
357     /**
358      * Load a list of record
359      * 
360      * @param lIdList
361      *            list of record id
362      * @param plugin
363      *            the plugin
364      * @return list of Record
365      */
366     public static List<Record> loadListByListId( List<Integer> lIdList, Plugin plugin )
367     {
368         return _dao.loadList( lIdList, plugin );
369     }
370 
371     /**
372      * Load the data of all the record who verify the filter and returns them in a list
373      * 
374      * @param filter
375      *            the filter
376      * @param plugin
377      *            the plugin
378      * @return the list of record
379      */
380     public static List<Record> getListRecord( RecordFieldFilter filter, Plugin plugin )
381     {
382         return _dao.selectListByFilter( filter, plugin );
383     }
384 
385     /**
386      * Count record who verify the filter
387      * 
388      * @param filter
389      *            the filter
390      * @param plugin
391      *            the plugin
392      * @return the number of record
393      */
394     public static int getCountRecord( RecordFieldFilter filter, Plugin plugin )
395     {
396         return _dao.selectCountByFilter( filter, plugin );
397     }
398 
399     /**
400      * Load the data of all the record who verify the filter and returns them in a list
401      * 
402      * @param filter
403      *            the filter
404      * @param plugin
405      *            the plugin
406      * @return the list of record
407      */
408     public static List<Integer> getListRecordId( RecordFieldFilter filter, Plugin plugin )
409     {
410         return _dao.selectListIdByFilter( filter, plugin );
411     }
412 
413     /**
414      * Get directory id by by record id
415      * 
416      * @param nRecordId
417      *            the record id
418      * @param plugin
419      *            the plugin
420      * @return the directory id
421      */
422     public static Integer getDirectoryIdByRecordId( Integer nRecordId, Plugin plugin )
423     {
424         return _dao.getDirectoryIdByRecordId( nRecordId, plugin );
425     }
426 }