View Javadoc

1   /*
2    * Copyright (c) 2002-2014, 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.digglike.business;
35  
36  import fr.paris.lutece.plugins.digglike.business.attribute.DiggAttributeHome;
37  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
38  import fr.paris.lutece.portal.business.style.Theme;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.AppLogService;
42  
43  import java.lang.reflect.InvocationTargetException;
44  import java.util.List;
45  import java.util.Map;
46  
47  import org.apache.commons.beanutils.BeanUtils;
48  
49  
50  /**
51   * This class provides instances management methods (create, find, ...) for Digg
52   * objects
53   */
54  public final class DiggHome
55  {
56      // Static variable pointed at the DAO instance
57      private static IDiggDAO _dao = SpringContextService.getBean( "digglike.diggDAO" );
58  
59      /**
60       * Private constructor - this class need not be instantiated
61       */
62      private DiggHome(  )
63      {
64      }
65  
66      /**
67       * Creation of an instance of Digg
68       *
69       * @param digg The instance of the Digg which contains the informations to
70       *            store
71       * @param plugin the Plugin
72       * @return The primary key of the new digg.
73       */
74      public static int create( Digg digg, Plugin plugin )
75      {
76          if ( digg.getImage(  ) != null )
77          {
78              digg.setIdImageResource( ImageResourceHome.create( digg.getImage(  ), plugin ) );
79          }
80  
81          int nIdDigg = _dao.insert( digg, plugin );
82  
83          // Create directory attributes associated to the directory
84          Map<String, Object> mapAttributes = DiggUtils.depopulate( digg );
85          DiggAttributeHome.create( digg.getIdDigg(  ), mapAttributes );
86  
87          return nIdDigg;
88      }
89  
90      /**
91       * Copy of an instance of Digg
92       *
93       * @param digg The instance of the digg who must copy
94       * @param plugin the Plugin
95       *
96       */
97      public static void copy( Digg digg, Plugin plugin )
98      {
99          Digg diggCopy = digg;
100 
101         List<IEntry> listEntry;
102         EntryFilter filter = new EntryFilter(  );
103         filter.setIdDigg( digg.getIdDigg(  ) );
104         listEntry = EntryHome.getEntryList( filter, plugin );
105         diggCopy.setActive( false );
106         diggCopy.setDefaultDigg( false );
107         diggCopy.setIdDigg( create( digg, plugin ) );
108 
109         for ( IEntry entry : listEntry )
110         {
111             entry = EntryHome.findByPrimaryKey( entry.getIdEntry(  ), plugin );
112             entry.setDigg( diggCopy );
113             EntryHome.copy( entry, plugin );
114         }
115 
116         for ( Category category : diggCopy.getCategories(  ) )
117         {
118             CategoryHome.createDiggAssociation( diggCopy.getIdDigg(  ), category.getIdCategory(  ), plugin );
119         }
120 
121         for ( DiggSubmitType diggSubmitType : diggCopy.getDiggSubmitTypes(  ) )
122         {
123             DiggSubmitTypeHome.createDiggAssociation( diggCopy.getIdDigg(  ), diggSubmitType.getIdType(  ), plugin );
124         }
125     }
126 
127     /**
128      * Update data of the digg which is specified in parameter
129      *
130      * @param digg The instance of the digg which contains the informations to
131      *            update
132      * @param plugin the Plugin
133      *
134      */
135     public static void update( Digg digg, Plugin plugin )
136     {
137         if ( digg.getImage(  ) != null )
138         {
139             //remove old image if exist
140             if ( ( digg.getIdImageResource(  ) != null ) &&
141                     ( digg.getIdImageResource(  ) != DiggUtils.CONSTANT_ID_NULL ) )
142             {
143                 ImageResourceHome.remove( digg.getIdImageResource(  ), plugin );
144             }
145 
146             if ( digg.getImage(  ).getImage(  ) != null )
147             {
148                 digg.setIdImageResource( ImageResourceHome.create( digg.getImage(  ), plugin ) );
149             }
150             else
151             {
152                 digg.setIdImageResource( DiggUtils.CONSTANT_ID_NULL );
153             }
154         }
155 
156         // Remove directory attributes associated to the directory
157         DiggAttributeHome.remove( digg.getIdDigg(  ) );
158 
159         // Add directory Attribute
160         Map<String, Object> mapAttributes = DiggUtils.depopulate( digg );
161         DiggAttributeHome.create( digg.getIdDigg(  ), mapAttributes );
162 
163         _dao.store( digg, plugin );
164     }
165 
166     /**
167      * Remove thedigg whose identifier is specified in parameter
168      *
169      * @param nIdDigg The digg Id
170      * @param plugin the Plugin
171      */
172     public static void remove( int nIdDigg, Plugin plugin )
173     {
174         Digg digg = findByPrimaryKey( nIdDigg, plugin );
175         if ( digg == null )
176         {
177             return;
178         }
179         List<IEntry> listEntry;
180         List<Integer> listIdDiggSubmit;
181 
182         //delete image resource associate
183         if ( ( digg.getIdImageResource( ) != null ) &&
184                 ( digg.getIdImageResource(  ) != DiggUtils.CONSTANT_ID_NULL ) )
185         {
186             ImageResourceHome.remove( digg.getIdImageResource(  ), plugin );
187         }
188 
189         //delete association between digg and categories
190         for ( Category category : digg.getCategories(  ) )
191         {
192             CategoryHome.removeDiggAssociation( digg.getIdDigg(  ), category.getIdCategory(  ), plugin );
193         }
194 
195         //delete association between digg and digg submit type
196         for ( DiggSubmitType diggSubmitType : digg.getDiggSubmitTypes(  ) )
197         {
198             DiggSubmitTypeHome.removeDiggAssociation( digg.getIdDigg(  ), diggSubmitType.getIdType(  ), plugin );
199         }
200 
201         EntryFilter entryFilter = new EntryFilter(  );
202         entryFilter.setIdDigg( digg.getIdDigg(  ) );
203         listEntry = EntryHome.getEntryList( entryFilter, plugin );
204 
205         for ( IEntry entry : listEntry )
206         {
207             EntryHome.remove( entry.getIdEntry(  ), plugin );
208         }
209 
210         SubmitFilter responseFilter = new SubmitFilter(  );
211         responseFilter.setIdDigg( nIdDigg );
212         listIdDiggSubmit = DiggSubmitHome.getDiggSubmitListId( responseFilter, plugin );
213 
214         for ( Integer nIdDiggSubmit : listIdDiggSubmit )
215         {
216             DiggSubmitHome.remove( nIdDiggSubmit, plugin );
217         }
218 
219         // Remove digg attributes associated to the digg
220         DiggAttributeHome.remove( nIdDigg );
221 
222         _dao.delete( nIdDigg, plugin );
223     }
224 
225     ///////////////////////////////////////////////////////////////////////////
226     // Finders
227     /**
228      * Returns an instance of a digg whose identifier is specified in parameter
229      *
230      * @param nKey The digg primary key
231      * @param plugin the Plugin
232      * @return an instance of Digg
233      */
234     public static Digg findByPrimaryKey( int nKey, Plugin plugin )
235     {
236         Digg digg = _dao.load( nKey, plugin );
237 
238         if ( digg != null )
239         {
240             digg.setCategories( CategoryHome.getListByIdDigg( nKey, plugin ) );
241             digg.setDiggSubmiTypes( DiggSubmitTypeHome.getListByIdDigg( nKey, plugin ) );
242 
243             Map<String, Object> mapAttributes = DiggAttributeHome.findByPrimaryKey( nKey );
244 
245             try
246             {
247                 BeanUtils.populate( digg, mapAttributes );
248             }
249             catch ( IllegalAccessException e )
250             {
251                 AppLogService.error( e );
252             }
253             catch ( InvocationTargetException e )
254             {
255                 AppLogService.error( e );
256             }
257         }
258 
259         return digg;
260     }
261 
262     /**
263      * Load the data of all the diggs who verify the filter and returns them in
264      * a list
265      * @param filter the filter
266      * @param plugin the plugin
267      * @return the list of diggs, or an empty list if no digg was found
268      */
269     public static List<Digg> getDiggList( DiggFilter filter, Plugin plugin )
270     {
271         List<Digg> listDigg = _dao.selectDiggList( filter, plugin );
272 
273         for ( Digg digg : listDigg )
274         {
275             Map<String, Object> mapAttributes = DiggAttributeHome.findByPrimaryKey( digg.getIdDigg(  ) );
276 
277             try
278             {
279                 BeanUtils.populate( digg, mapAttributes );
280             }
281             catch ( IllegalAccessException e )
282             {
283                 AppLogService.error( e );
284             }
285             catch ( InvocationTargetException e )
286             {
287                 AppLogService.error( e );
288             }
289         }
290 
291         return listDigg;
292     }
293 
294     /**
295      * Update the value of the field which will be use to sort the diggSubmit
296      * @param nIdDigg the id of the diggSubmit
297      * @param nNewField the new number of order
298      * @param plugin The Plugin object
299      */
300     public static void updateDiggSortField( int nNewField, int nIdDigg, Plugin plugin )
301     {
302         _dao.storeDiggOrderField( nIdDigg, nNewField, plugin );
303     }
304 
305     /**
306      * Load the xpage themes for all diggs
307      * @param plugin the plugin
308      * @return the map containing the theme
309      *
310      */
311     public static Map<Integer, Theme> getXPageThemes( Plugin plugin )
312     {
313         return _dao.getXPageThemesMap( plugin );
314     }
315 }