View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.genericattributes.business;
35  
36  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
37  import fr.paris.lutece.plugins.genericattributes.util.CopyEntryEventParam;
38  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
39  import fr.paris.lutece.portal.business.event.ResourceEvent;
40  import fr.paris.lutece.portal.business.file.FileHome;
41  import fr.paris.lutece.portal.service.event.ResourceEventManager;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.portal.service.util.AppException;
45  import fr.paris.lutece.util.sql.TransactionManager;
46  
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * This class provides instances management methods (create, find, ...) for Entry objects
52   */
53  public final class EntryHome
54  {
55      // Static variable pointed at the DAO instance
56      private static IEntryDAO _dao = SpringContextService.getBean( "genericattributes.entryDAO" );
57      private static Plugin _plugin;
58  
59      /**
60       * Private constructor - this class need not be instantiated
61       */
62      private EntryHome( )
63      {
64      }
65  
66      /**
67       * Creation of an instance of Entry
68       * 
69       * @param entry
70       *            The instance of the Entry which contains the informations to store
71       * @return The primary key of the new entry.
72       */
73      public static int create( Entry entry )
74      {
75          return _dao.insert( entry, getPlugin( ) );
76      }
77  
78      /**
79       * Copy of an instance of Entry
80       * 
81       * @param entry
82       *            The instance of the Entry who must copy
83       * @return the copied Entry
84       */
85      public static Entryref="../../../../../../fr/paris/lutece/plugins/genericattributes/business/Entry.html#Entry">Entry copy( Entry entry )
86      {
87          int oldId = entry.getIdEntry( );
88          Entry/../../../../../fr/paris/lutece/plugins/genericattributes/business/Entry.html#Entry">Entry entryCopy = (Entry) entry.clone( );
89          List<Field> listField = FieldHome.getFieldListByIdEntry( entry.getIdEntry( ) );
90  
91          TransactionManager.beginTransaction( getPlugin( ) );
92  
93          try
94          {
95              entryCopy.setIdEntry( create( entry ) );
96  
97              for ( Field field : listField )
98              {
99                  field = FieldHome.findByPrimaryKey( field.getIdField( ) );
100 
101                 for ( Entry entryConditionnal : field.getConditionalQuestions( ) )
102                 {
103                     entryConditionnal.setIdResource( entry.getIdResource( ) );
104                     entryConditionnal.setResourceType( entry.getResourceType( ) );
105                 }
106 
107                 field.setParentEntry( entryCopy );
108                 FieldHome.copy( field );
109             }
110 
111             if ( Boolean.TRUE.equals( entryCopy.getEntryType( ).getGroup( ) ) )
112             {
113                 for ( Entry entryChild : entry.getChildren( ) )
114                 {
115                     entryChild = EntryHome.findByPrimaryKey( entryChild.getIdEntry( ) );
116                     if ( entryChild != null )
117                     {
118                         entryChild.setParent( entryCopy );
119                         entryChild.setIdResource( entryCopy.getIdResource( ) );
120                         entryChild.setResourceType( entryCopy.getResourceType( ) );
121                         copy( entryChild );
122                     }
123                 }
124             }
125             ResourceEvent event = new ResourceEvent( );
126             event.setIdResource( String.valueOf( entryCopy.getIdEntry( ) ) );
127             event.setTypeResource( entry.getResourceType( ) );
128             event.setParam( new CopyEntryEventParam( oldId ) );
129             ResourceEventManager.fireAddedResource( event );
130 
131             TransactionManager.commitTransaction( getPlugin( ) );
132             return entryCopy;
133         }
134         catch( Exception e )
135         {
136             TransactionManager.rollBack( getPlugin( ) );
137             throw new AppException( e.getMessage( ), e );
138         }
139     }
140 
141     /**
142      * Update of the entry which is specified in parameter
143      * 
144      * @param entry
145      *            The instance of the Entry which contains the informations to update
146      */
147     public static void update( Entry entry )
148     {
149         _dao.store( entry, getPlugin( ) );
150         ResourceEvent event = new ResourceEvent( );
151         event.setIdResource( String.valueOf( entry.getIdEntry( ) ) );
152         event.setTypeResource( entry.getResourceType( ) );
153         ResourceEventManager.fireUpdatedResource( event );
154     }
155 
156     /**
157      * Remove the entry whose identifier is specified in parameter
158      * 
159      * @param nIdEntry
160      *            The entry Id
161      */
162     public static void remove( int nIdEntry )
163     {
164         Entry entry = findByPrimaryKey( nIdEntry );
165 
166         if ( entry != null )
167         {
168             TransactionManager.beginTransaction( getPlugin( ) );
169 
170             try
171             {
172                 for ( Field field : entry.getFields( ) )
173                 {
174                     if ( IEntryTypeService.FIELD_DOWNLOADABLE_FILE.equals( field.getCode( ) ) )
175                     {
176                         FileHome.remove( Integer.valueOf( field.getValue( ) ) );
177                     }
178                     if ( IEntryTypeService.FIELD_ANSWER_CHOICE.equals( field.getCode( ) ) )
179                     {
180                         ReferenceItemFieldHome.removeByField( field.getIdField( ) );
181                     }
182                     FieldHome.remove( field.getIdField( ) );
183                 }
184 
185                 // Remove the Responses of an Entry
186                 removeEntryResponses( nIdEntry );
187 
188                 for ( Entry entryChild : entry.getChildren( ) )
189                 {
190                     remove( entryChild.getIdEntry( ) );
191                 }
192 
193                 _dao.delete( nIdEntry, getPlugin( ) );
194 
195                 ResourceEvent event = new ResourceEvent( );
196                 event.setIdResource( String.valueOf( nIdEntry ) );
197                 event.setTypeResource( entry.getResourceType( ) );
198                 ResourceEventManager.fireDeletedResource( event );
199 
200                 TransactionManager.commitTransaction( getPlugin( ) );
201             }
202             catch( Exception e )
203             {
204                 TransactionManager.rollBack( getPlugin( ) );
205                 throw new AppException( e.getMessage( ), e );
206             }
207         }
208     }
209 
210     /**
211      * Remove the Responses of an Entry
212      * 
213      * @param nIdEntry
214      *            The identifier of the Entry to remove
215      */
216     private static void removeEntryResponses( int nIdEntry )
217     {
218         ResponseFiltertes/business/ResponseFilter.html#ResponseFilter">ResponseFilter responseFilter = new ResponseFilter( );
219         responseFilter.setIdEntry( nIdEntry );
220         List<Response> listResponse = ResponseHome.getResponseList( responseFilter );
221         if ( listResponse != null && !listResponse.isEmpty( ) )
222         {
223             for ( Response response : listResponse )
224             {
225                 ResponseHome.remove( response.getIdResponse( ) );
226             }
227         }
228     }
229 
230     // /////////////////////////////////////////////////////////////////////////
231     // Finders
232 
233     /**
234      * Returns an instance of a Entry whose identifier is specified in parameter
235      * 
236      * @param nKey
237      *            The entry primary key
238      * @return an instance of Entry
239      */
240     public static Entry findByPrimaryKey( int nKey )
241     {
242         Entry entry = _dao.load( nKey, getPlugin( ) );
243 
244         if ( entry != null )
245         {
246             EntryFilterericattributes/business/EntryFilter.html#EntryFilter">EntryFilter filter = new EntryFilter( );
247             filter.setIdEntryParent( entry.getIdEntry( ) );
248             entry.setChildren( getEntryList( filter ) );
249             entry.setFields( FieldHome.getFieldListByIdEntry( nKey ) );
250         }
251 
252         return entry;
253     }
254 
255     /**
256      * Returns a list of a Entry whose identifier is specified in parameter
257      * 
258      * @param idList
259      *            The primary keys
260      * @return an instance of Entry
261      */
262     public static List<Entry> findByPrimaryKeyList( List<Integer> idList )
263     {
264         return _dao.loadMultiple( idList, getPlugin( ) );
265     }
266 
267     /**
268      * Load the data of all the entry who verify the filter and returns them in a list
269      * 
270      * @param filter
271      *            the filter
272      * @return the list of entry
273      */
274     public static List<Entry> getEntryList( EntryFilter filter )
275     {
276         return _dao.selectEntryListByFilter( filter, getPlugin( ) );
277     }
278 
279     /**
280      * Return the number of entry who verify the filter
281      * 
282      * @param filter
283      *            the filter
284      * @return the number of entry who verify the filter
285      */
286     public static int getNumberEntryByFilter( EntryFilter filter )
287     {
288         return _dao.selectNumberEntryByFilter( filter, getPlugin( ) );
289     }
290 
291     /**
292      * Finds all the entries without any parent associated with a given resource
293      * 
294      * @param nIdResource
295      *            the id of the resource
296      * @param strResourceType
297      *            The resource type
298      * @return the list of all the entries without parent
299      */
300     public static List<Entry> findEntriesWithoutParent( int nIdResource, String strResourceType )
301     {
302         List<Entry> listEntry = _dao.findEntriesWithoutParent( getPlugin( ), nIdResource, strResourceType );
303 
304         for ( Entry entry : listEntry )
305         {
306             if ( entry != null )
307             {
308                 EntryFilterericattributes/business/EntryFilter.html#EntryFilter">EntryFilter filter = new EntryFilter( );
309                 filter.setIdEntryParent( entry.getIdEntry( ) );
310                 entry.setChildren( getEntryList( filter ) );
311             }
312         }
313 
314         return listEntry;
315     }
316 
317     /**
318      * Finds the entry (conditional question) with a given order, idDependField, id resource and resource type
319      * 
320      * @param nOrder
321      *            the order
322      * @param nIdField
323      *            the id of the field
324      * @param nIdResource
325      *            the id of the resource
326      * @param strResourceType
327      *            The resource type of the entry to get
328      * @return the list of all the entries without parent
329      */
330     public static Entry findByOrderAndIdFieldAndIdResource( int nOrder, int nIdField, int nIdResource, String strResourceType )
331     {
332         return _dao.findByOrderAndIdFieldAndIdResource( getPlugin( ), nOrder, nIdField, nIdResource, strResourceType );
333     }
334 
335     /**
336      * Decrements the order of all the entries (conditional questions) after the one which will be removed
337      * 
338      * @param nOrder
339      *            the order of the entry which will be removed
340      * @param nIdField
341      *            the id of the field
342      * @param nIdResource
343      *            the id of the resource
344      * @param strResourceType
345      *            The resource type
346      */
347     public static void decrementOrderByOne( int nOrder, int nIdField, int nIdResource, String strResourceType )
348     {
349         _dao.decrementOrderByOne( getPlugin( ), nOrder, nIdField, nIdResource, strResourceType );
350     }
351 
352     /**
353      * Get the generic attributes plugin
354      * 
355      * @return The generic attributes plugin
356      */
357     private static Plugin getPlugin( )
358     {
359         if ( _plugin == null )
360         {
361             _plugin = GenericAttributesUtils.getPlugin( );
362         }
363 
364         return _plugin;
365     }
366 
367     /**
368      *
369      * @param plugin
370      *            the plugin
371      * @param nIdForm
372      *            id form
373      * @return list id entry with their titles
374      */
375     public static Map<Integer, String> findEntryByForm( Plugin plugin, int nIdForm )
376     {
377         return _dao.findEntryByForm( plugin, nIdForm );
378     }
379 
380     /**
381      *
382      * @param plugin
383      *            the plugin
384      * @param nIdEntry
385      *            id entry
386      * @param nIdResponse
387      *            id entry response
388      * @return entry value
389      */
390     public static String getEntryValueByIdResponse( Plugin plugin, int nIdEntry, int nIdResponse )
391     {
392         return _dao.getEntryValueByIdResponse( plugin, nIdEntry, nIdResponse );
393     }
394 }