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.portal.service.plugin.Plugin;
37  import fr.paris.lutece.portal.service.spring.SpringContextService;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * This class provides instances management methods (create, find, ...) for Entry objects
44   */
45  public final class EntryHome
46  {
47      // Static variable pointed at the DAO instance
48      private static IEntryDAO _dao = SpringContextService.getBean( "directoryEntryDAO" );
49  
50      /**
51       * Private constructor - this class need not be instantiated
52       */
53      private EntryHome( )
54      {
55      }
56  
57      /**
58       * Creation of an instance of Entry
59       *
60       * @param entry
61       *            The instance of the Entry which contains the informations to store
62       * @param plugin
63       *            the Plugin
64       * @return The primary key of the new entry.
65       */
66      public static int create( IEntry entry, Plugin plugin )
67      {
68          return _dao.insert( entry, plugin );
69      }
70  
71      /**
72       * Copy of an instance of Entry
73       *
74       * @param entry
75       *            The instance of the Entry who must copy
76       * @param plugin
77       *            the Plugin
78       *
79       */
80      public static void copy( IEntry entry, Plugin plugin )
81      {
82          IEntry entryCopy = entry;
83          List<Field> listField;
84          listField = FieldHome.getFieldListByIdEntry( entry.getIdEntry( ), plugin );
85          entryCopy.setIdEntry( create( entry, plugin ) );
86  
87          for ( Field field : listField )
88          {
89              field = FieldHome.findByPrimaryKey( field.getIdField( ), plugin );
90              field.setEntry( entryCopy );
91              FieldHome.copy( field, plugin );
92          }
93  
94          if ( entryCopy.getEntryType( ).getGroup( ) )
95          {
96              for ( IEntry entryChild : entry.getChildren( ) )
97              {
98                  entryChild = EntryHome.findByPrimaryKey( entryChild.getIdEntry( ), plugin );
99                  entryChild.setParent( entryCopy );
100                 entryChild.setDirectory( entryCopy.getDirectory( ) );
101                 copy( entryChild, plugin );
102             }
103         }
104     }
105 
106     /**
107      * Update of the entry which is specified in parameter
108      *
109      * @param entry
110      *            The instance of the Entry which contains the informations to update
111      * @param plugin
112      *            the Plugin
113      *
114      */
115     public static void update( IEntry entry, Plugin plugin )
116     {
117         _dao.store( entry, plugin );
118     }
119 
120     /**
121      * Remove the entry whose identifier is specified in parameter
122      *
123      * @param nIdEntry
124      *            The entry Id
125      * @param plugin
126      *            the Plugin
127      */
128     public static void remove( int nIdEntry, Plugin plugin )
129     {
130         IEntry entry = findByPrimaryKey( nIdEntry, plugin );
131 
132         if ( entry != null )
133         {
134             for ( Field field : entry.getFields( ) )
135             {
136                 FieldHome.remove( field.getIdField( ), plugin );
137             }
138 
139             for ( IEntry entryChild : entry.getChildren( ) )
140             {
141                 remove( entryChild.getIdEntry( ), plugin );
142             }
143 
144             _dao.delete( nIdEntry, plugin );
145         }
146     }
147 
148     // /////////////////////////////////////////////////////////////////////////
149     // Finders
150 
151     /**
152      * Returns an instance of a Entry whose identifier is specified in parameter
153      *
154      * @param nKey
155      *            The entry primary key
156      * @param plugin
157      *            the Plugin
158      * @return an instance of Entry
159      */
160     public static IEntry findByPrimaryKey( int nKey, Plugin plugin )
161     {
162         IEntry entry = _dao.load( nKey, plugin );
163 
164         if ( entry != null )
165         {
166             EntryFilter filter = new EntryFilter( );
167             filter.setIdEntryParent( entry.getIdEntry( ) );
168             entry.setChildren( getEntryList( filter, plugin ) );
169             entry.setFields( FieldHome.getFieldListByIdEntry( nKey, plugin ) );
170         }
171 
172         return entry;
173     }
174 
175     /**
176      * Load the data of all the entry who verify the filter and returns them in a list
177      * 
178      * @param filter
179      *            the filter
180      * @param plugin
181      *            the plugin
182      * @return the list of entry
183      */
184     public static List<IEntry> getEntryList( EntryFilter filter, Plugin plugin )
185     {
186         return _dao.selectEntryListByFilter( filter, plugin );
187     }
188 
189     /**
190      * Load the data of all the entry which type is contained in typeEntryList and verify the filter to return them in a list
191      * 
192      * @param idTypeEntryList
193      *            a of entry type needed (by id)
194      * @param filter
195      *            the filter
196      * @param plugin
197      *            the plugin
198      * @return the list of entry
199      */
200     public static List<IEntry> getEntryListByTypeList( List<Integer> idTypeEntryList, EntryFilter filter, Plugin plugin )
201     {
202         return _dao.selectEntryListByTypeByFilter( idTypeEntryList, filter, plugin );
203     }
204 
205     /**
206      * Return the number of entry who verify the filter
207      * 
208      * @param filter
209      *            the filter
210      * @param plugin
211      *            the plugin
212      * @return the number of entry who verify the filter
213      */
214     public static int getNumberEntryByFilter( EntryFilter filter, Plugin plugin )
215     {
216         return _dao.selectNumberEntryByFilter( filter, plugin );
217     }
218 
219     /**
220      * Get the list of entries that should be anonymized when a record is anonymized
221      * 
222      * @param plugin
223      *            The plugin
224      * @return The list of entries to anonymize
225      */
226     public static List<IEntry> getEntryListAnonymizeStatus( Plugin plugin )
227     {
228         List<IEntry> listEntry = _dao.getEntryListAnonymizeStatus( plugin );
229         List<IEntry> listEntryRes = new ArrayList<IEntry>( );
230 
231         for ( IEntry entry : listEntry )
232         {
233             try
234             {
235                 IEntry iEntry = (IEntry) Class.forName( ( entry.getEntryType( ).getClassName( ) ) ).newInstance( );
236 
237                 if ( iEntry.isAnonymizable( ) )
238                 {
239                     listEntryRes.add( entry );
240                 }
241             }
242             catch( Exception e )
243             {
244                 // Do nothing
245             }
246         }
247 
248         return listEntryRes;
249     }
250 
251     /**
252      * Update an entry anonymize status
253      * 
254      * @param nEntryId
255      *            The id of the entry to update
256      * @param bAnonymize
257      *            True if the entry should be anonymized, false otherwise
258      * @param plugin
259      *            The plugin
260      */
261     public static void updateEntryAnonymizeStatus( Integer nEntryId, Boolean bAnonymize, Plugin plugin )
262     {
263         _dao.updateEntryAnonymizeStatus( nEntryId, bAnonymize, plugin );
264     }
265 
266     /**
267      * Finds all the entries without any parent
268      *
269      * @param plugin
270      *            the plugin
271      * @param nIdDirectory
272      *            the id of the concerned directory
273      * @return List of IEntry the list of all the entries without parent
274      */
275     public static List<IEntry> findEntriesWithoutParent( Plugin plugin, int nIdDirectory )
276     {
277         List<IEntry> listEntry = _dao.findEntriesWithoutParent( plugin, nIdDirectory );
278 
279         for ( IEntry entry : listEntry )
280         {
281             if ( entry != null )
282             {
283                 EntryFilter filter = new EntryFilter( );
284                 filter.setIdEntryParent( entry.getIdEntry( ) );
285                 entry.setChildren( getEntryList( filter, plugin ) );
286             }
287         }
288 
289         return listEntry;
290     }
291 }