View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.announce.business;
35  
36  import fr.paris.lutece.plugins.announce.service.AnnounceCacheService;
37  import fr.paris.lutece.plugins.announce.service.AnnouncePlugin;
38  import fr.paris.lutece.plugins.announce.service.announcesearch.AnnounceSearchService;
39  import fr.paris.lutece.plugins.genericattributes.business.Response;
40  import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
41  import fr.paris.lutece.portal.business.file.FileHome;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  import fr.paris.lutece.portal.service.resource.ExtendableResourceRemovalListenerService;
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  import fr.paris.lutece.portal.service.spring.SpringContextService;
47  import fr.paris.lutece.portal.service.workflow.WorkflowService;
48  
49  import java.sql.Timestamp;
50  
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  /**
55   * This class provides instances management methods (create, find, ...) for Announce objects
56   */
57  public final class AnnounceHome
58  {
59      // Static variable pointed at the DAO instance
60      private static IAnnounceDAO _dao = SpringContextService.getBean( "announce.announceDAO" );
61      private static Plugin _plugin = PluginService.getPlugin( AnnouncePlugin.PLUGIN_NAME );
62  
63      /**
64       * Private constructor - this class need not be instantiated
65       */
66      private AnnounceHome( )
67      {
68      }
69  
70      /**
71       * Create an instance of the announce class
72       * 
73       * @return The instance of announce which has been created with its primary key.
74       * @param announce
75       *            The instance of the Announce which contains the informations to store
76       */
77      public static Announce../../../../../../fr/paris/lutece/plugins/announce/business/Announce.html#Announce">Announce create( Announce announce )
78      {
79          announce.setDateModification( new Timestamp( System.currentTimeMillis( ) ) );
80          updateAnnouncePublicationTime( announce );
81          _dao.insert( announce, _plugin );
82  
83          if ( announce.getPublished( ) && !announce.getSuspended( ) && !announce.getSuspendedByUser( ) )
84          {
85              AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_CREATE, _plugin );
86              AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
87          }
88  
89          return announce;
90      }
91  
92      /**
93       * Update of the announce which is specified in parameter
94       * 
95       * @return The instance of the announce which has been updated
96       * @param announce
97       *            The instance of the Announce which contains the data to store
98       */
99      public static Announce../../../../../../fr/paris/lutece/plugins/announce/business/Announce.html#Announce">Announce update( Announce announce )
100     {
101         announce.setDateModification( new Timestamp( System.currentTimeMillis( ) ) );
102         _dao.store( announce, _plugin );
103 
104         if ( announce.getPublished( ) && !announce.getSuspended( ) && !announce.getSuspendedByUser( ) )
105         {
106             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_MODIFY, _plugin );
107         }
108         else
109         {
110             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_DELETE, _plugin );
111         }
112 
113         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
114         AnnounceCacheService.getService( ).putInCache( AnnounceCacheService.getAnnounceCacheKey( announce.getId( ) ), announce );
115 
116         return announce;
117     }
118 
119     /**
120      * Remove the announce whose identifier is specified in parameter and every response associated with it
121      * 
122      * @param nAnnounceId
123      *            The announce Id
124      */
125     public static void remove( int nAnnounceId )
126     {
127         AnnounceSearchService.getInstance( ).addIndexerAction( nAnnounceId, IndexerAction.TASK_DELETE, _plugin );
128 
129         List<Integer> listIdResponse = findListIdResponse( nAnnounceId );
130 
131         for ( int nIdResponse : listIdResponse )
132         {
133             ResponseHome.remove( nIdResponse );
134         }
135 
136         removeAnnounceResponse( nAnnounceId );
137 
138         ExtendableResourceRemovalListenerService.doRemoveResourceExtentions( Announce.RESOURCE_TYPE, Integer.toString( nAnnounceId ) );
139 
140         if ( WorkflowService.getInstance( ).isAvailable( ) )
141         {
142             WorkflowService.getInstance( ).doRemoveWorkFlowResource( nAnnounceId, Announce.RESOURCE_TYPE );
143         }
144 
145         _dao.delete( nAnnounceId, _plugin );
146         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
147         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getAnnounceCacheKey( nAnnounceId ) );
148     }
149 
150     // /////////////////////////////////////////////////////////////////////////
151     // Finders
152 
153     /**
154      * Returns an instance of a announce whose identifier is specified in parameter
155      * 
156      * @param nKey
157      *            The announce primary key
158      * @return an instance of Announce
159      */
160     public static Announce findByPrimaryKey( int nKey )
161     {
162         Announce./../../../../fr/paris/lutece/plugins/announce/business/Announce.html#Announce">Announce announce = (Announce) AnnounceCacheService.getService( ).getFromCache( AnnounceCacheService.getAnnounceCacheKey( nKey ) );
163 
164         if ( announce == null )
165         {
166             announce = _dao.load( nKey, _plugin );
167 
168             if ( announce != null )
169             {
170                 AnnounceCacheService.getService( ).putInCache( AnnounceCacheService.getAnnounceCacheKey( announce.getId( ) ), announce );
171             }
172         }
173 
174         return announce;
175     }
176 
177     /**
178      * Returns the announce id from its image response id
179      * 
180      * @param nIdResponse
181      *            the id of the response
182      * @return The announce id, or null if there no announce that has this response as an image
183      */
184     public static Integer findIdByImageResponse( int nIdResponse )
185     {
186         return _dao.findIdByImageResponse( nIdResponse, _plugin );
187     }
188 
189     /**
190      * Load the data of all the announce objects and returns them in form of a list
191      * 
192      * @param announceSort
193      *            The sort
194      * @return the list which contains the data of all the announce objects
195      */
196     public static List<Integer> findAll( AnnounceSort announceSort )
197     {
198         return _dao.selectAll( announceSort, _plugin );
199     }
200 
201     /**
202      * Load the id of every published announce and returns them in form of a list
203      * 
204      * @param announceSort
205      *            The sort
206      * @return the list of id of every published announce
207      */
208     public static List<Integer> findAllPublishedId( AnnounceSort announceSort )
209     {
210         List<Integer> listIds = (List<Integer>) AnnounceCacheService.getService( ).getFromCache( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
211 
212         if ( listIds == null )
213         {
214             listIds = _dao.selectAllPublishedId( announceSort, _plugin );
215             AnnounceCacheService.getService( ).putInCache( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ), listIds );
216         }
217 
218         return _dao.selectAllPublishedId( announceSort, _plugin );
219     }
220 
221     /**
222      * Load the data of all the announce objects and returns them in form of a list
223      * 
224      * @param announceSort
225      *            The sort
226      * @return the list which contains the data of all the announce objects
227      */
228     public static List<Announce> findAllPublished( AnnounceSort announceSort )
229     {
230         return _dao.selectAllPublished( announceSort, _plugin );
231     }
232 
233     /**
234      * Get the list of announces from a list of ids
235      * 
236      * @param listIdAnnounces
237      *            The list of ids of announces to get
238      * @param announceSort
239      *            The sort
240      * @return The list of announces
241      */
242     public static List<Announce> findByListId( List<Integer> listIdAnnounces, AnnounceSort announceSort )
243     {
244         return _dao.findByListId( listIdAnnounces, announceSort, _plugin );
245     }
246 
247     /**
248      * selects all the announces for a user
249      * 
250      * @param user
251      *            the user
252      * @param announceSort
253      *            The sort
254      * @return the list of announces
255      */
256     public static List<Announce> getAnnouncesForUser( LuteceUser user, AnnounceSort announceSort )
257     {
258         return _dao.selectAllForUser( user.getName( ), announceSort, _plugin );
259     }
260 
261     /**
262      * selects all the announces for a user
263      * 
264      * @param user
265      *            the user name
266      * @param announceSort
267      *            The sort
268      * @return the list of announces
269      */
270     public static List<Announce> getAnnouncesForUser( String user, AnnounceSort announceSort )
271     {
272         return _dao.selectAllForUser( user, announceSort, _plugin );
273     }
274 
275     /**
276      * selects all the announces for a category
277      * 
278      * @param category
279      *            the category
280      * @param announceSort
281      *            The sort
282      * @return the list of announces
283      */
284     public static List<Integer> getPublishedAnnouncesForCategory( Category category, AnnounceSort announceSort )
285     {
286         return _dao.selectAllPublishedForCategory( category, announceSort, _plugin );
287     }
288 
289     /**
290      * publish or unpublish an announce
291      * 
292      * @param announce
293      *            the announce
294      */
295     public static void setPublished( Announce announce )
296     {
297         updateAnnouncePublicationTime( announce );
298         _dao.setPublished( announce, _plugin );
299 
300         if ( announce.getPublished( ) && !announce.getSuspended( ) && !announce.getSuspendedByUser( ) )
301         {
302             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_CREATE, _plugin );
303         }
304         else
305         {
306             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_DELETE, _plugin );
307         }
308 
309         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
310         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getAnnounceCacheKey( announce.getId( ) ) );
311     }
312 
313     public static void setHasNotifed( Announce announce )
314     {
315         _dao.setHasNotifed( announce, _plugin );
316 
317         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getAnnounceCacheKey( announce.getId( ) ) );
318     }
319 
320     /**
321      * suspend or UnSuspend an announce
322      * 
323      * @param announce
324      *            the announce
325      */
326     public static void setSuspended( Announce announce )
327     {
328         updateAnnouncePublicationTime( announce );
329         _dao.setSuspended( announce, _plugin );
330 
331         if ( announce.getPublished( ) && !announce.getSuspended( ) && !announce.getSuspendedByUser( ) )
332         {
333             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_CREATE, _plugin );
334         }
335         else
336         {
337             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_DELETE, _plugin );
338         }
339 
340         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
341         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getAnnounceCacheKey( announce.getId( ) ) );
342     }
343 
344     /**
345      * suspend or UnSuspend an announce
346      * 
347      * @param announce
348      *            the announce
349      */
350     public static void setSuspendedByUser( Announce announce )
351     {
352         updateAnnouncePublicationTime( announce );
353         _dao.setSuspendedByUser( announce, _plugin );
354 
355         if ( announce.getPublished( ) && !announce.getSuspended( ) && !announce.getSuspendedByUser( ) )
356         {
357             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_CREATE, _plugin );
358         }
359         else
360         {
361             AnnounceSearchService.getInstance( ).addIndexerAction( announce.getId( ), IndexerAction.TASK_DELETE, _plugin );
362         }
363 
364         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getListIdPublishedAnnouncesCacheKey( ) );
365         AnnounceCacheService.getService( ).removeKey( AnnounceCacheService.getAnnounceCacheKey( announce.getId( ) ) );
366     }
367 
368     /**
369      * Get the list of ids of announces that was created before the given date
370      * 
371      * @param timestamp
372      *            The timestamp
373      * @return The list of ids
374      */
375     public static List<Integer> findIdAnnouncesByDateCreation( Timestamp timestamp )
376     {
377         return _dao.findIdAnnouncesByDateCreation( timestamp, _plugin );
378     }
379 
380     /**
381      * Get the list of ids of announces that were created after a given time
382      * 
383      * @param lMinPublicationTime
384      *            The minimum publication time of announces to get
385      * @return The list of ids of announces
386      */
387     public static List<Integer> findIdAnnouncesByDatePublication( long lMinPublicationTime )
388     {
389         return _dao.findIdAnnouncesByDatePublication( lMinPublicationTime, _plugin );
390     }
391 
392     // -----------------------------------------------
393     // Announce response management
394     // -----------------------------------------------
395 
396     /**
397      * Associates a response to an Announce
398      * 
399      * @param nIdAnnounce
400      *            The id of the announce
401      * @param nIdResponse
402      *            The id of the response
403      * @param bIsImage
404      *            True if the response is an image, false otherwise
405      */
406     public static void insertAnnounceResponse( int nIdAnnounce, int nIdResponse, boolean bIsImage )
407     {
408         _dao.insertAnnounceResponse( nIdAnnounce, nIdResponse, bIsImage, _plugin );
409     }
410 
411     /**
412      * Get the list of id of responses associated with an announce
413      * 
414      * @param nIdAnnounce
415      *            the id of the announce
416      * @return the list of responses, or an empty list if no response was found
417      */
418     public static List<Integer> findListIdResponse( int nIdAnnounce )
419     {
420         return _dao.findListIdResponse( nIdAnnounce, _plugin );
421     }
422 
423     /**
424      * Get the list of id of image responses associated with an announce
425      * 
426      * @param nIdAnnounce
427      *            the id of the announce
428      * @return the list of responses, or an empty list if no response was found
429      */
430     public static List<Integer> findListIdImageResponse( int nIdAnnounce )
431     {
432         return _dao.findListIdImageResponse( nIdAnnounce, _plugin );
433     }
434 
435     /**
436      * Get the list of responses associated with an announce
437      * 
438      * @param nIdAnnounce
439      *            the id of the announce
440      * @param bLoadFiles
441      *            True to load files, false to ignore them. Note that physical files are never loaded by this method.
442      * @return the list of responses, or an empty list if no response was found
443      */
444     public static List<Response> findListResponse( int nIdAnnounce, boolean bLoadFiles )
445     {
446         List<Integer> listIdResponse = findListIdResponse( nIdAnnounce );
447         List<Response> listResponse = new ArrayList<>( listIdResponse.size( ) );
448 
449         for ( Integer nIdResponse : listIdResponse )
450         {
451             Response response = ResponseHome.findByPrimaryKey( nIdResponse );
452             
453             if ( response != null )
454             {
455                 if ( bLoadFiles && ( response.getFile( ) != null ) )
456                 {
457                     response.setFile( FileHome.findByPrimaryKey( response.getFile( ).getIdFile( ) ) );
458                 }
459 
460                 listResponse.add( response );
461             }
462         }
463 
464         return listResponse;
465     }
466 
467     /**
468      * Remove the association between an announce and responses
469      * 
470      * @param nIdAnnounce
471      *            The id of the announce
472      */
473     public static void removeAnnounceResponse( int nIdAnnounce )
474     {
475         _dao.deleteAnnounceResponse( nIdAnnounce, _plugin );
476     }
477 
478     /**
479      * Update the publication time of an announce according to its published, its suspended and its suspended by user parameters.<br />
480      * Note that the announce is not flushed in the database
481      * 
482      * @param announce
483      *            the announce to update
484      */
485     private static void updateAnnouncePublicationTime( Announce announce )
486     {
487         if ( announce.getPublished( ) && !announce.getSuspended( ) && !announce.getSuspendedByUser( ) )
488         {
489             announce.setTimePublication( System.currentTimeMillis( ) );
490         }
491     }
492 }