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