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 fr.paris.lutece.plugins.blog.business.Blog;
37  import fr.paris.lutece.plugins.blog.business.BlogFilter;
38  import fr.paris.lutece.plugins.blog.business.BlogHome;
39  import fr.paris.lutece.plugins.blog.business.IndexerAction;
40  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublication;
41  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
42  import fr.paris.lutece.plugins.blog.business.portlet.BlogPortletHome;
43  import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
44  import fr.paris.lutece.portal.business.portlet.Portlet;
45  import fr.paris.lutece.portal.business.portlet.PortletHome;
46  import fr.paris.lutece.portal.business.portlet.PortletType;
47  import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.plugin.PluginService;
50  
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.Date;
54  import java.util.HashSet;
55  import java.util.List;
56  import java.util.Locale;
57  import java.util.Set;
58  
59  import org.apache.commons.collections.CollectionUtils;
60  
61  /**
62   * Publishing service
63   */
64  public class PublishingService
65  {
66      private static PublishingServicePublishingService.html#PublishingService">PublishingService _singleton = new PublishingService( );
67  
68      /**
69       * Get the unique instance of the service
70       * 
71       * @return The unique instance
72       */
73      public static PublishingService getInstance( )
74      {
75          return _singleton;
76      }
77  
78      /**
79       * Assign {@link Blog} to a {@link Portlet}
80       *
81       * @param nBlogId
82       *            The {@link Blog} identifier
83       * @param nPortletId
84       *            The {@link Portlet} identifier
85       */
86      public void assign( int nBlogId, int nPortletId )
87      {
88          BlogPublicationrtlet/BlogPublication.html#BlogPublication">BlogPublication blogPublication = new BlogPublication( );
89          blogPublication.setIdPortlet( nPortletId );
90          blogPublication.setIdBlog( nBlogId );
91          BlogPublicationHome.create( blogPublication );
92      }
93  
94      /**
95       * Publishing blogs assigned to a portlet at the begin of the list
96       *
97       * @param nBlogId
98       *            the Blog id
99       * @param nPortletId
100      *            the portlet identifier
101      */
102     public void publish( int nBlogId, int nPortletId )
103     {
104         // Publishing of blog : set status to Published
105         BlogPublication blogPublication = BlogPublicationHome.findDocPublicationByPimaryKey( nPortletId, nBlogId );
106 
107         if ( blogPublication != null )
108         {
109             blogPublication.setIdPortlet( nPortletId );
110             blogPublication.setIdBlog( nBlogId );
111             BlogPublicationHome.update( blogPublication );
112 
113         }
114 
115         BlogSearchService.getInstance( ).addIndexerAction( nBlogId, IndexerAction.TASK_MODIFY );
116 
117     }
118 
119     /**
120      * unAssign {@link Blog} to a {@link Portlet}
121      *
122      * @param nBlogId
123      *            The {@link Blog} identifier
124      * @param nPortletId
125      *            The {@link Portlet} identifier
126      */
127     public void unAssign( int nBlogId, int nPortletId )
128     {
129         BlogPublicationHome.remove( nPortletId, nBlogId );
130     }
131 
132     /**
133      * Check if the specified {@link Blog} is published into the specified {@link Portlet}
134      * 
135      * @param nBlogId
136      *            The {@link Blog} identifier
137      * @param nPortletId
138      *            The {@link Portlet} identifier
139      * @return True if {@link Blog} is published, false else (unpublished or not assigned)
140      */
141     public boolean isPublished( int nBlogId, int nPortletId )
142     {
143         BlogPublication blogPublication = BlogPublicationHome.findDocPublicationByPimaryKey( nPortletId, nBlogId );
144 
145         return blogPublication != null;
146     }
147 
148     /**
149      * Check if the specified {@link Blog} is assigned (unpublished or published) into at least one {@link Portlet}
150      * 
151      * @param nBlogId
152      *            The {@link Blog} identifier
153      * @return True if {@link Blog} is assigned (published or unpublished), false else (not assigned)
154      */
155     public boolean isAssigned( int nBlogId )
156     {
157         Collection<BlogPublication> listBlogPublication = BlogPublicationHome.getDocPublicationByIdDoc( nBlogId );
158         return CollectionUtils.isNotEmpty( listBlogPublication );
159     }
160 
161     /**
162      * Check if the specified {@link Blog} is assigned (unpublished or published) into the specified {@link Portlet}
163      * 
164      * @param nBlogId
165      *            The {@link Blog} identifier
166      * @param nPortletId
167      *            The {@link Portlet} identifier
168      * @return True if {@link Blog} is assigned (published or unpublished), false else (not assigned)
169      */
170     public boolean isAssigned( int nBlogId, int nPortletId )
171     {
172         BlogPublication blogPublication = BlogPublicationHome.findDocPublicationByPimaryKey( nPortletId, nBlogId );
173 
174         return ( blogPublication != null );
175     }
176 
177     /**
178      * Return a {@link BlogPublication} from a {@link Portlet} identifier and {@link Blog} identifier
179      * 
180      * @param nPortletId
181      *            the {@link Portlet} identifier
182      * @param nBlogId
183      *            the {@link Blog} identifier
184      * @return a {@link BlogPublication} or null if no object match
185      */
186     public BlogPublication getBlogPublication( int nPortletId, int nBlogId )
187     {
188         return BlogPublicationHome.findDocPublicationByPimaryKey( nPortletId, nBlogId );
189     }
190 
191     /**
192      * Loads the list of portlets
193      *
194      * @return the {@link Collection} of the portlets
195      */
196     public Collection<Portlet> getBlogsPortlets( )
197     {
198         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
199         Collection<Portlet> listPortletsAll = new ArrayList<>( );
200 
201         for ( PortletType portletType : plugin.getPortletTypes( ) )
202         {
203             listPortletsAll.addAll( PortletHome.findByType( portletType.getId( ) ) );
204         }
205 
206         return listPortletsAll;
207     }
208 
209     /**
210      * Loads the list of portlets blogs empty and blogslist
211      *
212      * @return the {@link Collection} of the portlets
213      */
214     public Collection<Portlet> getBlogsPortletstoPublish( )
215     {
216         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
217         Collection<Portlet> listPortletsAll = new ArrayList<>( );
218 
219         for ( PortletType portletType : plugin.getPortletTypes( ) )
220         {
221             List<Portlet> listPortlet = PortletHome.findByType( portletType.getId( ) );
222             String className = BlogPortletHome.class.getName( );
223             String strPortletTypeId = PortletTypeHome.getPortletTypeId( className );
224 
225             if ( portletType.getId( ).equals( strPortletTypeId ) )
226             {
227                 for ( Portlet pt : listPortlet )
228                 {
229                     if ( CollectionUtils.isEmpty( BlogPublicationHome.getDocPublicationByPortlet( pt.getId( ) ) ) )
230                     {
231 
232                         listPortletsAll.addAll( listPortlet );
233                     }
234 
235                 }
236 
237             }
238             else
239             {
240 
241                 listPortletsAll.addAll( listPortlet );
242             }
243         }
244 
245         return listPortletsAll;
246     }
247 
248     /**
249      * Loads the list of the portlets whoes contain blog specified by id
250      *
251      * @param strBlogId
252      *            the blog identifier
253      * @return the {@link Collection} of the portlets
254      */
255     public Collection<Portlet> getPortletsByBlogId( String strBlogId )
256     {
257         Collection<BlogPublication> listBlogPublication = BlogPublicationHome.getDocPublicationByIdDoc( Integer.parseInt( strBlogId ) );
258         Collection<Portlet> listPortlets = new ArrayList<>( );
259 
260         for ( BlogPublication blogPublication : listBlogPublication )
261         {
262             listPortlets.add( PortletHome.findByPrimaryKey( blogPublication.getIdPortlet( ) ) );
263         }
264 
265         return listPortlets;
266     }
267 
268     /**
269      * Loads the list of the blog whose filter and date publication is specified Return published blogs since the publication date. The is also filtered with
270      * the blogFilter
271      *
272      * @param datePublishing
273      *            The start publication date
274      * @param dateEndPublishing
275      *            The end publication date
276      * @param blogFilter
277      *            The filter for the published blog. The filter can be null or empty. The array of Ids will not be taked in account.
278      * @param locale
279      *            The locale is used to get the list of blogs with the findByFilter method
280      * @return the list of the blog in form of a List. return null if datePublishing is null
281      */
282     public Collection<Blog> getPublishedBlogsSinceDate( Date datePublishing, Date dateEndPublishing, BlogFilter blogFilter, Locale locale )
283     {
284         if ( datePublishing == null )
285         {
286             return null;
287         }
288 
289         Collection<BlogPublication> listBlogPublication = BlogPublicationHome.findSinceDatePublishingAndStatus( datePublishing, dateEndPublishing, 1 );
290 
291         if ( CollectionUtils.isEmpty( listBlogPublication ) )
292         {
293             return new ArrayList<>( );
294         }
295 
296         Set<Integer> sIds = new HashSet<>( );
297         BlogFilter publishedBlogFilter = blogFilter;
298 
299         if ( publishedBlogFilter == null )
300         {
301             publishedBlogFilter = new BlogFilter( );
302         }
303 
304         for ( BlogPublication blogPublication : listBlogPublication )
305         {
306             sIds.add( blogPublication.getIdBlog( ) );
307         }
308 
309         publishedBlogFilter.setIds( sIds.toArray( new Integer [ sIds.size( )] ) );
310 
311         return BlogHome.findByFilter( publishedBlogFilter );
312     }
313 
314     /**
315      * Get the list of id of published blogs associated with a given collection of portlets.
316      * 
317      * @param nPortletsIds
318      *            The list of portlet ids.
319      * @param datePublishing
320      *            The publishing date
321      * @param dateEndPublishing
322      *            The publishing end date
323      * @param plugin
324      *            The blog plugin
325      * @return The list of blogs id.
326      */
327     public static List<Integer> getPublishedBlogsIdsListByPortletIds( int [ ] nPortletsIds, Date datePublishing, Date dateEndPublishing, Plugin plugin )
328     {
329         return BlogPublicationHome.getPublishedBlogsIdsListByPortletIds( nPortletsIds, datePublishing, dateEndPublishing, plugin );
330     }
331 
332     /**
333      * Get the list of id of published Blogs, associated with a given collection of porlets, which has been updated since the dateUpdated
334      * 
335      * @param nPortletsIds
336      *            The list of portlet ids.
337      * @param dateUpdated
338      *            The date from the blogs had to be updated
339      * @param plugin
340      *            The plugin
341      * @return The list of Blogs id.
342      */
343     public static List<Integer> getLastPublishedBlogsIdsListByPortletIds( int [ ] nPortletsIds, Date dateUpdated, Plugin plugin )
344     {
345         return BlogPublicationHome.getLastPublishedDocumentsIdsListByPortletIds( nPortletsIds, dateUpdated, plugin );
346     }
347 
348 }