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.blog.service;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import org.apache.commons.collections4.CollectionUtils;
40  
41  import fr.paris.lutece.plugins.blog.business.Blog;
42  import fr.paris.lutece.plugins.blog.business.BlogFilter;
43  import fr.paris.lutece.plugins.blog.business.BlogHome;
44  import fr.paris.lutece.plugins.blog.business.DocContent;
45  import fr.paris.lutece.plugins.blog.business.DocContentHome;
46  import fr.paris.lutece.plugins.blog.business.IndexerAction;
47  import fr.paris.lutece.plugins.blog.business.Tag;
48  import fr.paris.lutece.plugins.blog.business.TagHome;
49  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
50  import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
51  import fr.paris.lutece.plugins.blog.utils.BlogUtils;
52  import fr.paris.lutece.portal.business.event.ResourceEvent;
53  import fr.paris.lutece.portal.service.event.ResourceEventManager;
54  import fr.paris.lutece.portal.service.util.AppException;
55  import fr.paris.lutece.util.sql.TransactionManager;
56  
57  /**
58   * This Service manages document actions (create, update, delete, validate ...) .
59   */
60  public class BlogService
61  {
62  
63      private static BlogServicervice/BlogService.html#BlogService">BlogService _singleton = new BlogService( );
64  
65      /**
66       * Get the unique instance of the service
67       *
68       * @return The unique instance
69       */
70      public static BlogService getInstance( )
71      {
72          return _singleton;
73      }
74  
75      /**
76       * Create an blog
77       * 
78       * @param blog
79       *            The Blog
80       */
81      public void createBlog( Blog blog )
82  
83      {
84          createBlog( blog, blog.getDocContent( ) );
85      }
86  
87      /**
88       * Update an Blog
89       * 
90       * @param blog
91       *            The Blog
92       */
93      public void updateBlog( Blog blog )
94  
95      {
96          updateBlog( blog, blog.getDocContent( ) );
97  
98      }
99  
100     /**
101      * Remove an blog
102      * 
103      * @param nId
104      *            The blog id
105      */
106     public void deleteBlog( int nId )
107 
108     {
109         TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
110 
111         try
112         {
113             TagHome.removeTagDoc( nId );
114             DocContentHome.remove( nId );
115             BlogHome.remove( nId );
116             BlogHome.removeVersions( nId );
117             TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
118         }
119         catch( Exception e )
120         {
121             TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
122             throw new AppException( e.getMessage( ), e );
123         }
124         BlogSearchService.getInstance( ).addIndexerAction( nId, IndexerAction.TASK_DELETE );
125     }
126 
127     /**
128      * Delete the specified version from a blog's history
129      * 
130      * @param nIdBlog
131      *            The ID of the blog to process
132      * @param nIdVersion
133      *            The value of the version to delete
134      */
135     public void deleteBlogVersion( int nIdBlog, int nVersion )
136     {
137         BlogHome.removeSpecificVersion( nIdBlog, nVersion );
138     }
139 
140     /**
141      * Revert the content of a blog to its previous version's content, and delete the one currently used (no new version is created)
142      * 
143      * @param blog
144      *            The Blog Object that will be processed
145      */
146     public void revertBlogToPreviousVersion( Blog blog )
147     {
148         // Get the last 2 versions of the blog (the current and the previous one)
149         List<Blog> blogVersionsList = getLastBlogVersionsList( blog.getId( ), 2 );
150 
151         // Make sure there is a version to revert to
152         if ( CollectionUtils.isNotEmpty( blogVersionsList ) && blogVersionsList.size( ) > 1 )
153         {
154             TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
155 
156             try
157             {
158                 // Get the content of the previous blog version
159                 Blog previousBlogVersion = blogVersionsList.get( 1 );
160 
161                 // Update the content of the blog with the content of its previous version
162                 BlogHome.update( previousBlogVersion );
163 
164                 // Delete the current blog's version
165                 BlogHome.removeSpecificVersion( blog.getId( ), blog.getVersion( ) );
166 
167                 TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
168             }
169             catch( Exception e )
170             {
171                 TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
172                 throw new AppException( e.getMessage( ), e );
173             }
174         }
175     }
176 
177     /**
178      * Retrieve the last nLimit versions of a specific blog and returns them as a list
179      * 
180      * @param nIdBlog
181      *            The ID of the blog to process
182      * @param nLimit
183      *            Maximum amount of Blog Object versions to return
184      * @return The list which contains the data of nLimit last modified Blog objects
185      */
186     public List<Blog> getLastBlogVersionsList( int nIdBlog, int nLimit )
187     {
188         return BlogHome.getLastBlogVersionsList( nIdBlog, nLimit );
189     }
190 
191     /**
192      * Create an blog
193      * 
194      * @param blog
195      *            The Blog
196      * @param docContent
197      *            The Doc content
198      */
199     public void createBlog( Blog blog, List<DocContent> docContent )
200 
201     {
202 
203         TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
204 
205         try
206         {
207             BlogHome.addInitialVersion( blog );
208             for ( Tag tag : blog.getTag( ) )
209             {
210 
211                 TagHome.create( tag.getIdTag( ), blog.getId( ), tag.getPriority( ) );
212             }
213             if ( docContent != null )
214             {
215                 for ( DocContent docCont : docContent )
216                 {
217                     DocContentHome.insertInBlog( blog.getId( ), docCont.getId( ), docCont.getPriority( ) );
218 
219                 }
220 
221             }
222             TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
223         }
224         catch( Exception e )
225         {
226             TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
227             throw new AppException( e.getMessage( ), e );
228         }
229 
230         BlogSearchService.getInstance( ).addIndexerAction( blog.getId( ), IndexerAction.TASK_CREATE );
231     }
232 
233     /**
234      * Update an BlogContent
235      * 
236      * @param docContent
237      *            The Doc Content
238      */
239     private void updateDocContent( DocContent docContent, int nIdBlog )
240     {
241         if ( docContent != null && docContent.getId( ) != 0 )
242         {
243             DocContentHome.removeInBlogById( docContent.getId( ) );
244             DocContentHome.insertInBlog( nIdBlog, docContent.getId( ), docContent.getPriority( ) );
245 
246         }
247         else
248             if ( docContent != null )
249             {
250                 DocContentHome.create( docContent );
251                 DocContentHome.insertInBlog( nIdBlog, docContent.getId( ), docContent.getPriority( ) );
252             }
253 
254     }
255 
256     /**
257      * Update an Blog
258      * 
259      * @param blog
260      *            The Blog
261      * @param docContent
262      *            The Doc Content
263      */
264     public void updateBlog( Blog blog, List<DocContent> docContent )
265 
266     {
267         TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
268 
269         try
270         {
271             BlogHome.addNewVersion( blog );
272             if ( docContent != null )
273             {
274                 updateDocContentList( blog, docContent );
275             }
276 
277             TagHome.removeTagDoc( blog.getId( ) );
278             for ( Tag tag : blog.getTag( ) )
279             {
280                 TagHome.create( tag.getIdTag( ), blog.getId( ), tag.getPriority( ) );
281             }
282             TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
283         }
284         catch( Exception e )
285         {
286             TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
287             throw new AppException( e.getMessage( ), e );
288         }
289 
290         fireUpdateBlogEvent( blog.getId( ) );
291     }
292 
293     private void updateDocContentList( Blog blog, List<DocContent> docContent )
294     {
295         List<DocContent> listDocContent = DocContentHome.getDocsContentByHtmlDoc( blog.getId( ) );
296         List<DocContent> listToCompare = new ArrayList<>( );
297         listToCompare.addAll( listDocContent );
298 
299         for ( DocContent docCont : docContent )
300         {
301             if ( listDocContent.isEmpty( ) || listDocContent.removeIf( t -> t.getId( ) == docCont.getId( ) ) || docCont.getId( ) == 0 )
302             {
303                 if ( listToCompare.stream( ).noneMatch( c -> ( c.getId( ) == docCont.getId( ) ) && ( c.getPriority( ) == docCont.getPriority( ) ) ) )
304                 {
305 
306                     updateDocContent( docCont, blog.getId( ) );
307                 }
308             }
309         }
310 
311         for ( DocContent docCont : listDocContent )
312         {
313             DocContentHome.removeInBlogById( docCont.getId( ) );
314         }
315     }
316 
317     /**
318      * Update an Blog
319      * 
320      * @param blog
321      *            The blog
322      * @param docContent
323      *            The Doc Content
324      */
325     public void updateBlogWithoutVersion( Blog blog, List<DocContent> docContent )
326 
327     {
328         TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
329         BlogHome.update( blog );
330         try
331         {
332             if ( docContent != null )
333             {
334                 updateDocContentList( blog, docContent );
335             }
336 
337             TagHome.removeTagDoc( blog.getId( ) );
338             for ( Tag tag : blog.getTag( ) )
339             {
340 
341                 TagHome.create( tag.getIdTag( ), blog.getId( ), tag.getPriority( ) );
342             }
343             TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
344         }
345         catch( Exception e )
346         {
347             TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
348             throw new AppException( e.getMessage( ), e );
349         }
350 
351         fireUpdateBlogEvent( blog.getId( ) );
352     }
353 
354     /**
355      * Returns an instance of a blog whose identifier is specified in parameter
356      * 
357      * @param nIdDocument
358      *            The blog primary key
359      * @return an instance of blog
360      */
361     public Blog loadBlog( int nIdDocument )
362 
363     {
364         Blog blog = BlogHome.findByPrimaryKey( nIdDocument );
365         if ( blog != null )
366         {
367             List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( nIdDocument );
368             blog.setDocContent( docContent );
369             blog.setTag( TagHome.loadByDoc( nIdDocument ) );
370             blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( nIdDocument ) );
371         }
372         return blog;
373 
374     }
375 
376     /**
377      * Returns an instance of a blog whose identifier and version is specified in parameter
378      *
379      * @param nIdDocument
380      *            The blog primary key
381      * @param nIdDocument
382      *            The blog primary key
383      * @return an instance of blog
384      */
385     public Blog loadBlogByVersion( int nIdDocument, int nVersion )
386 
387     {
388         Blog blog = BlogHome.findVersion( nIdDocument, nVersion );
389         if ( blog != null )
390         {
391             List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( nIdDocument );
392             blog.setDocContent( docContent );
393             blog.setTag( TagHome.loadByDoc( nIdDocument ) );
394             blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( nIdDocument ) );
395         }
396         return blog;
397 
398     }
399 
400     /**
401      * Returns an instance of a blog without binairie file whose identifier is specified in parameter
402      * 
403      * @param nIdDocument
404      *            The blog primary key
405      * @return an instance of blog
406      */
407     public Blog findByPrimaryKeyWithoutBinaries( int nIdDocument )
408 
409     {
410         Blog blog = BlogHome.findByPrimaryKey( nIdDocument );
411         if ( blog != null )
412         {
413             blog.setTag( TagHome.loadByDoc( nIdDocument ) );
414             blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( nIdDocument ) );
415         }
416         return blog;
417 
418     }
419 
420     /**
421      * Load the data of all the blog objects and returns them as a list
422      * 
423      * @return the list which contains the data of all the blog objects
424      */
425     public List<Blog> getListBlogWhithBinaries( )
426 
427     {
428         List<Blog> listBlogs = getListBlogWithoutBinaries( );
429 
430         for ( Blog doc : listBlogs )
431         {
432 
433             List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( doc.getId( ) );
434             doc.setDocContent( docContent );
435 
436         }
437 
438         return listBlogs;
439 
440     }
441 
442     /**
443      * Returns a list of a blog without binairie file whose identifier is specified in parameter
444      * 
445      * @return the list which contains the data of all the blog objects
446      */
447     public List<Blog> getListBlogWithoutBinaries( )
448 
449     {
450         List<Blog> blogList = BlogHome.selectWithoutBinaries( );
451         for ( Blog blog : blogList )
452         {
453             blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( blog.getId( ) ) );
454             blog.setTag( TagHome.getTagListByDoc( blog.getId( ) ) );
455         }
456         return blogList;
457 
458     }
459 
460     /**
461      * Load the data of all the blog objects whose tag is specified in parameter
462      * 
463      * @param nIdTag
464      *            idTag param
465      * @return the list which contains the data of all the blog objects
466      */
467     public List<Blog> searchListBlogByTag( int nIdTag )
468 
469     {
470         return BlogHome.getBlogByTag( nIdTag );
471 
472     }
473 
474     /**
475      * Returns a collection of blog objects
476      * 
477      * @param filter
478      *            The filter
479      * @return The list of blog post
480      */
481     public List<Blog> findByFilter( BlogFilter filter )
482     {
483         return BlogHome.findByFilter( filter );
484     }
485 
486     /**
487      * Load the data of nLimit last modified Blog objects and returns them as a list
488      * 
489      * @param nLimit
490      *            number of Blog to load
491      * @return The list which contains the data of of nLimit last modified Blog objects
492      */
493     public List<Blog> getLastModifiedBlogsList( int nLimit )
494     {
495         List<Blog> listBlog = BlogHome.getLastModifiedBlogsList( nLimit );
496         for ( Blog blog : listBlog )
497         {
498             blog.setTag( TagHome.getTagListByDoc( blog.getId( ) ) );
499             List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( blog.getId( ) );
500             blog.setDocContent( docContent );
501         }
502 
503         return listBlog;
504     }
505 
506     public void fireCreateBlogEvent( int blogId )
507     {
508         ResourceEvent formResponseEvent = new ResourceEvent( );
509         formResponseEvent.setIdResource( String.valueOf( blogId ) );
510         formResponseEvent.setTypeResource( BlogUtils.CONSTANT_TYPE_RESOURCE );
511         ResourceEventManager.fireAddedResource( formResponseEvent );
512         BlogSearchService.getInstance( ).addIndexerAction( blogId, IndexerAction.TASK_CREATE );
513     }
514 
515     public void fireUpdateBlogEvent( int blogId )
516     {
517         ResourceEvent formResponseEvent = new ResourceEvent( );
518         formResponseEvent.setIdResource( String.valueOf( blogId ) );
519         formResponseEvent.setTypeResource( BlogUtils.CONSTANT_TYPE_RESOURCE );
520         ResourceEventManager.fireUpdatedResource( formResponseEvent );
521         BlogSearchService.getInstance( ).addIndexerAction( blogId, IndexerAction.TASK_MODIFY );
522     }
523 
524     public void fireDeleteBlogEvent( int blogId )
525     {
526         ResourceEvent formResponseEvent = new ResourceEvent( );
527         formResponseEvent.setIdResource( String.valueOf( blogId ) );
528         formResponseEvent.setTypeResource( BlogUtils.CONSTANT_TYPE_RESOURCE );
529         ResourceEventManager.fireDeletedResource( formResponseEvent );
530         BlogSearchService.getInstance( ).addIndexerAction( blogId, IndexerAction.TASK_MODIFY );
531     }
532 }