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.business.portlet;
35  
36  import java.sql.Date;
37  import java.util.ArrayList;
38  import java.util.GregorianCalendar;
39  import java.util.HashMap;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Locale;
43  import java.util.Set;
44  
45  import fr.paris.lutece.plugins.blog.business.BlogPageTemplate;
46  import fr.paris.lutece.plugins.blog.business.BlogPageTemplateHome;
47  import fr.paris.lutece.plugins.blog.business.Blog;
48  import fr.paris.lutece.plugins.blog.business.BlogFilter;
49  import fr.paris.lutece.plugins.blog.business.BlogHome;
50  import fr.paris.lutece.plugins.blog.service.PublishingService;
51  import fr.paris.lutece.plugins.blog.utils.BlogUtils;
52  import fr.paris.lutece.portal.business.portlet.PortletHtmlContent;
53  import fr.paris.lutece.portal.service.template.AppTemplateService;
54  import fr.paris.lutece.util.html.HtmlTemplate;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  /**
59   * This class represents business objects BlogsList Portlet
60   */
61  public class BlogListPortlet extends PortletHtmlContent
62  {
63      public static final String RESOURCE_ID = "BLOG_LIST_PORTLET";
64  
65      // ///////////////////////////////////////////////////////////////////////////////
66      public static final String MARK_LIST_BLOG_PUBLISHED = "blog_list_published";
67      public static final String MARK_PAGE_TEMPLATE = "page_template";
68      public static final String MARK_PORTLET_ID = "portlet_id";
69      public static final String MARK_PORTLET_NAME = "portlet_name";
70  
71      // ///////////////////////////////////////////////////////////////////////////////
72      // Constants
73      private int _nPageTemplateDocument;
74      private int _nPortletId;
75      private List<BlogPublication> _arrayBlogs = new ArrayList<>( );
76      private Set<Integer> _removedBlogsId = new HashSet<>( );
77  
78      /**
79       * Sets the identifier of the portlet type to the value specified in the BlogsListPortletHome class
80       */
81      public BlogListPortlet( )
82      {
83          setPortletTypeId( BlogListPortletHome.getInstance( ).getPortletTypeId( ) );
84      }
85  
86      @Override
87      public String getHtmlContent( HttpServletRequest request )
88      {
89          GregorianCalendar calendar = new java.util.GregorianCalendar( );
90          Date date = new Date( calendar.getTimeInMillis( ) );
91          BlogFiltersiness/BlogFilter.html#BlogFilter">BlogFilter documentFilter = new BlogFilter( );
92          List<Integer> listIdDoc = PublishingService.getPublishedBlogsIdsListByPortletIds( new int [ ] {
93                  this.getId( )
94          }, date, date, BlogUtils.getPlugin( ) );
95          Integer [ ] docId = listIdDoc.toArray( new Integer [ listIdDoc.size( )] );
96          // Default we published a blog that as id=0
97          if ( docId == null || docId.length == 0 )
98          {
99  
100             docId = new Integer [ 1];
101             docId [0] = 0;
102         }
103         documentFilter.setIds( docId );
104         documentFilter.setLoadBinaries( true );
105         documentFilter.setOrderInPortlet( true );
106         documentFilter.setPortletId( this.getId( ) );
107 
108         List<Blog> listBlogsPublished = BlogHome.findByFilter( documentFilter );
109         BlogPageTemplate pageTemplate = BlogPageTemplateHome.findByPrimaryKey( this.getPageTemplateDocument( ) );
110 
111         HashMap<String, Object> model = new HashMap<>( );
112         model.put( MARK_LIST_BLOG_PUBLISHED, listBlogsPublished );
113         model.put( MARK_PAGE_TEMPLATE, pageTemplate );
114         model.put( MARK_PORTLET_ID, this.getId( ) );
115         if ( this.getDisplayPortletTitle( ) == 0 )
116         {
117 
118             model.put( MARK_PORTLET_NAME, this.getName( ) );
119 
120         }
121         Locale locale = null;
122         if ( request != null )
123         {
124             locale = request.getLocale( );
125         }
126 
127         HtmlTemplate template = AppTemplateService.getTemplate( pageTemplate.getFile( ), locale, model );
128 
129         return template.getHtml( );
130     }
131 
132     /**
133      * Updates the current instance of the Blogs List Portlet object
134      */
135     public void update( )
136     {
137         BlogListPortletHome.getInstance( ).update( this );
138     }
139 
140     /**
141      * Removes the current instance of the Blogs List Portlet object
142      */
143     @Override
144     public void remove( )
145     {
146         BlogListPortletHome.getInstance( ).remove( this );
147     }
148 
149     /**
150      * Returns the nPortletId
151      *
152      * @return The nPortletId
153      */
154     public int getPortletId( )
155     {
156         return _nPortletId;
157     }
158 
159     /**
160      * Sets the IdPortlet
161      *
162      * @param nPortletId
163      *            The nPortletId
164      */
165     public void setPortletId( int nPortletId )
166     {
167         _nPortletId = nPortletId;
168     }
169 
170     /**
171      * Sets the parent page identifier of the portlet to the value specified in parameter
172      *
173      * @param nPageTemplateDocument
174      *            the code
175      */
176     public void setPageTemplateDocument( int nPageTemplateDocument )
177     {
178         _nPageTemplateDocument = nPageTemplateDocument;
179     }
180 
181     /**
182      * Returns the identifier of the parent page of the portlet
183      *
184      * @return the parent page identifier
185      */
186     public int getPageTemplateDocument( )
187     {
188         return _nPageTemplateDocument;
189     }
190 
191     /**
192      * @return the _arrayBlogs
193      */
194     public List<BlogPublication> getArrayBlogs( )
195     {
196         return _arrayBlogs;
197     }
198 
199     /**
200      * @param arrayBlogs
201      *            the _arrayBlogs to set
202      */
203     public void setArrayBlogs( List<BlogPublication> arrayBlogs )
204     {
205         _arrayBlogs = arrayBlogs;
206     }
207 
208     /**
209      * BlogDoPublication in the list _arrayBlogs
210      * 
211      * @param doc
212      *            The blog publication
213      */
214     public void addIdBlogs( BlogPublication doc )
215     {
216 
217         boolean bool = false;
218         for ( BlogPublication dc : _arrayBlogs )
219         {
220 
221             if ( doc.getIdBlog( ) == dc.getIdBlog( ) )
222             {
223                 bool = true;
224                 break;
225             }
226 
227         }
228         if ( !bool )
229         {
230             _arrayBlogs.add( doc );
231         }
232         _removedBlogsId.remove( doc.getIdBlog( ) );
233     }
234 
235     /**
236      * set BlogPublication in the list by order
237      * 
238      * @param order
239      *            The order in portlet publication
240      * @param doc
241      *            The blog
242      */
243     public void addIdBlogs( int order, BlogPublication doc )
244     {
245 
246         boolean bool = false;
247         BlogPublication blog = null;
248         for ( BlogPublication dc : _arrayBlogs )
249         {
250 
251             if ( doc.getIdBlog( ) == dc.getIdBlog( ) )
252             {
253                 bool = true;
254                 blog = dc;
255                 break;
256             }
257 
258         }
259         if ( bool )
260         {
261 
262             _arrayBlogs.remove( blog );
263 
264         }
265         _arrayBlogs.add( order, doc );
266         _removedBlogsId.remove( doc.getIdBlog( ) );
267     }
268 
269     /**
270      * Remove blog publication
271      * 
272      * @param doc
273      *            The blog Publication
274      */
275     public void removeBlogs( BlogPublication doc )
276     {
277 
278         for ( BlogPublication dc : _arrayBlogs )
279         {
280 
281             if ( doc.getIdBlog( ) == dc.getIdBlog( ) )
282             {
283                 _arrayBlogs.remove( dc );
284                 break;
285             }
286 
287         }
288         _removedBlogsId.add( doc.getIdBlog( ) );
289     }
290 
291     public Set<Integer> getRemovedBlogsId( )
292     {
293         return new HashSet<>( _removedBlogsId );
294     }
295 }