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.service.CommentSubmitService;
37  import fr.paris.lutece.plugins.digglike.service.search.DigglikeIndexer;
38  import fr.paris.lutece.plugins.digglike.utils.DiggIndexerUtils;
39  import fr.paris.lutece.portal.business.indexeraction.IndexerAction;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.search.IndexationService;
42  import fr.paris.lutece.portal.service.spring.SpringContextService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  
49  /**
50   *class FormSubmitHome
51   */
52  public final class DiggSubmitHome
53  {
54      // Static variable pointed at the DAO instance
55      private static IDiggSubmitDAO _dao = SpringContextService.getBean( "digglike.diggSubmitDAO" );
56  
57      /**
58       * Private constructor - this class need not be instantiated
59       */
60      private DiggSubmitHome(  )
61      {
62      }
63  
64      /**
65       * Creation of an instance of diggSubmit
66       *
67       * @param diggSubmit The instance of the diggSubmit which contains the informations to store
68       * @param plugin the Plugin
69       * @return the id of the new digg submit
70       *
71       */
72      public static int create( DiggSubmit diggSubmit, Plugin plugin )
73      {
74          return _dao.insert( diggSubmit, plugin );
75      }
76  
77      /**
78       * Update of the diggSubmit which is specified in parameter
79       *
80       * @param diggSubmit The instance of the diggSubmit which contains the informations to update
81       * @param plugin the Plugin
82       *
83       */
84      public static void update( DiggSubmit diggSubmit, Plugin plugin )
85      {
86          update( diggSubmit, true, plugin );
87      }
88  
89      /**
90       * Update of the diggSubmit which is specified in parameter
91       * @param diggSubmit The digg submit
92       * @param bUpdateIndex The update index
93       * @param plugin The plugin
94       */
95      public static void update( DiggSubmit diggSubmit, boolean bUpdateIndex, Plugin plugin )
96      {
97          if ( bUpdateIndex )
98          {
99              DiggSubmit diggSubmitStored = findByPrimaryKey( diggSubmit.getIdDiggSubmit(  ), plugin );
100 
101             //if state has changed
102             if ( diggSubmit.getDiggSubmitState(  ).getIdDiggSubmitState(  ) != diggSubmitStored.getDiggSubmitState(  )
103                                                                                                    .getIdDiggSubmitState(  ) )
104             {
105                 String strIdDiggSubmit = Integer.toString( diggSubmit.getIdDiggSubmit(  ) );
106 
107                 if ( diggSubmit.getDiggSubmitState(  ).getIdDiggSubmitState(  ) == DiggSubmit.STATE_PUBLISH )
108                 {
109                     DiggIndexerUtils.addIndexerAction( strIdDiggSubmit, IndexerAction.TASK_CREATE );
110                 }
111                 else
112                 {
113                     DiggIndexerUtils.addIndexerAction( strIdDiggSubmit, IndexerAction.TASK_DELETE );
114                 }
115             }
116 
117             IndexationService.addIndexerAction( Integer.toString( diggSubmit.getIdDiggSubmit(  ) ),
118                 AppPropertiesService.getProperty( DigglikeIndexer.PROPERTY_INDEXER_NAME ), IndexerAction.TASK_MODIFY );
119         }
120 
121         _dao.store( diggSubmit, plugin );
122     }
123 
124     /**
125      * Remove the DiggSubmit whose identifier is specified in parameter
126      *
127      * @param nIdDiggSubmit The diggSubmitId
128      * @param plugin the Plugin
129      */
130     public static void remove( int nIdDiggSubmit, Plugin plugin )
131     {
132         SubmitFilter filter = new SubmitFilter(  );
133         filter.setIdDiggSubmit( nIdDiggSubmit );
134 
135         List<Response> listResponses = ResponseHome.getResponseList( filter, plugin );
136 
137         for ( Response response : listResponses )
138         {
139             if ( response.getIdImageResource(  ) != null )
140             {
141                 ImageResourceHome.remove( response.getIdImageResource(  ), plugin );
142             }
143 
144             ResponseHome.remove( response.getIdResponse(  ), plugin );
145         }
146 
147         List<CommentSubmit> listComments = CommentSubmitService.getService(  ).getCommentSubmitList( filter, plugin );
148 
149         for ( CommentSubmit comment : listComments )
150         {
151             CommentSubmitService.getService(  ).remove( comment.getIdCommentSubmit(  ), plugin );
152         }
153 
154         //remove all reported associated to the digg submit
155         ReportedMessageHome.removeByDiggSubmit( nIdDiggSubmit, plugin );
156 
157         DiggSubmit diggSubmit = DiggSubmitHome.findByPrimaryKey( nIdDiggSubmit, plugin );
158 
159         if ( diggSubmit.getDiggSubmitState(  ).getIdDiggSubmitState(  ) == DiggSubmit.STATE_PUBLISH )
160         {
161             String strIdDiggSubmit = Integer.toString( nIdDiggSubmit );
162             IndexationService.addIndexerAction( strIdDiggSubmit + "_" + DigglikeIndexer.SHORT_NAME,
163                 AppPropertiesService.getProperty( DigglikeIndexer.PROPERTY_INDEXER_NAME ), IndexerAction.TASK_DELETE );
164 
165             DiggIndexerUtils.addIndexerAction( strIdDiggSubmit, IndexerAction.TASK_DELETE );
166         }
167 
168         _dao.delete( nIdDiggSubmit, plugin );
169     }
170 
171     ///////////////////////////////////////////////////////////////////////////
172     // Finders
173 
174     /**
175      * Returns an instance of a DiggSubmit whose identifier is specified in parameter
176      *
177      * @param nKey The diggSubmit primary key
178      * @param bLoadCommentList true if the comment list must be get
179      * @param plugin the Plugin
180      * @return an instance of DiggSubmit
181      */
182     public static DiggSubmit findByPrimaryKey( int nKey, Plugin plugin )
183     {
184         DiggSubmit diggSubmit = _dao.load( nKey, plugin );
185 
186         if ( diggSubmit != null )
187         {
188             if ( diggSubmit.getDiggSubmitType(  ) != null )
189             {
190                 diggSubmit.setDiggSubmitType( DiggSubmitTypeHome.findByPrimaryKey( 
191                         diggSubmit.getDiggSubmitType(  ).getIdType(  ), plugin ) );
192             }
193 
194             if ( diggSubmit.getCategory(  ) != null )
195             {
196                 diggSubmit.setCategory( CategoryHome.findByPrimaryKey( diggSubmit.getCategory(  ).getIdCategory(  ),
197                         plugin ) );
198             }
199         }
200 
201         return diggSubmit;
202     }
203 
204     /**
205      * Load the data of all the diggSubmit who verify the filter and returns them in a  list
206      * @param filter the filter
207      * @param plugin the plugin
208      * @return  the list of diggSubmit
209      */
210     public static List<DiggSubmit> getDiggSubmitList( SubmitFilter filter, Plugin plugin )
211     {
212         List<DiggSubmit> listDiggSubmit = _dao.selectListByFilter( filter, plugin );
213 
214         if ( listDiggSubmit != null )
215         {
216             SubmitFilter submmitFilterComment = new SubmitFilter(  );
217 
218             for ( DiggSubmit diggSubmit : listDiggSubmit )
219             {
220                 submmitFilterComment.setIdDiggSubmit( diggSubmit.getIdDiggSubmit(  ) );
221                 submmitFilterComment.setIdCommentSubmitState( CommentSubmit.STATE_ENABLE );
222                 diggSubmit.setComments( CommentSubmitService.getService(  )
223                                                             .getCommentSubmitList( submmitFilterComment, plugin ) );
224 
225                 if ( diggSubmit.getDiggSubmitType(  ) != null )
226                 {
227                     diggSubmit.setDiggSubmitType( DiggSubmitTypeHome.findByPrimaryKey( 
228                             diggSubmit.getDiggSubmitType(  ).getIdType(  ), plugin ) );
229                 }
230 
231                 if ( diggSubmit.getCategory(  ) != null )
232                 {
233                     diggSubmit.setCategory( CategoryHome.findByPrimaryKey( 
234                             diggSubmit.getCategory(  ).getIdCategory(  ), plugin ) );
235                 }
236             }
237         }
238 
239         return listDiggSubmit;
240     }
241 
242     /**
243      * Load the id of all the diggSubmit who verify the filter and returns them in a  list
244      * @param filter the filter
245      * @param plugin the plugin
246      * @return  the list of diggSubmit id
247      */
248     public static List<Integer> getDiggSubmitListId( SubmitFilter filter, Plugin plugin )
249     {
250         return _dao.selectIdListByFilter( filter, plugin );
251     }
252 
253     /**
254      * Load the data of all the diggSubmit who verify the filter and returns them in a  list
255      * @param filter the filter
256      * @param plugin the plugin
257      * @param nNumberMaxDiggSubmit Max Number of Diggsubmit return
258      * @return  the list of diggSubmit
259      */
260     public static List<DiggSubmit> getDiggSubmitList( SubmitFilter filter, Plugin plugin, int nNumberMaxDiggSubmit )
261     {
262         List<Integer> diggSubmitListId = getDiggSubmitListId( filter, plugin );
263         List<DiggSubmit> diggSubmitList = new ArrayList<DiggSubmit>(  );
264         DiggSubmit diggSubmit = null;
265         Object[] diggSubmitArrayId = diggSubmitListId.toArray(  );
266 
267         for ( int cpt = 0; cpt < diggSubmitArrayId.length; cpt++ )
268         {
269             if ( cpt < nNumberMaxDiggSubmit )
270             {
271                 diggSubmit = findByPrimaryKey( (Integer) diggSubmitArrayId[cpt], plugin );
272 
273                 if ( diggSubmit != null )
274                 {
275                     diggSubmitList.add( diggSubmit );
276                 }
277             }
278             else
279             {
280                 break;
281             }
282         }
283 
284         return diggSubmitList;
285     }
286 
287     /**
288      * Load the number of all the diggSubmit who verify the filter
289      * @param filter the filter
290      * @param plugin the plugin
291      * @return  the list of diggSubmit
292      */
293     public static int getCountDiggSubmit( SubmitFilter filter, Plugin plugin )
294     {
295         return _dao.selectCountByFilter( filter, plugin );
296     }
297 
298     /**
299      * Update the number order of diggSubmit
300      * @param nIdDiggSubmit the id of the diggSubmit
301      * @param nNewOrder the new number of order
302      * @param plugin The Plugin object
303      */
304     public static void updateDiggSubmitOrder( int nNewOrder, int nIdDiggSubmit, Plugin plugin )
305     {
306         _dao.storeDiggSubmitOrder( nNewOrder, nIdDiggSubmit, plugin );
307     }
308 
309     /**
310      * Search the max order number of contacts for one list
311      * @param nIdDigg the Id of the Digg
312      * @return int the max order
313      * @param plugin The Plugin object
314      */
315     public static int getMaxOrderList( int nIdDigg, boolean bListPinned, Plugin plugin )
316     {
317         return _dao.maxOrderDiggSubmit( nIdDigg, bListPinned, plugin );
318     }
319 }