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.GregorianCalendar;
38  import java.util.HashMap;
39  import java.util.Locale;
40  
41  import fr.paris.lutece.portal.business.portlet.PortletHtmlContent;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.plugins.blog.business.BlogPageTemplate;
44  import fr.paris.lutece.plugins.blog.business.BlogPageTemplateHome;
45  import fr.paris.lutece.plugins.blog.service.BlogService;
46  import fr.paris.lutece.plugins.blog.service.PublishingService;
47  import fr.paris.lutece.plugins.blog.business.Blog;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  /**
53   * This class represents business objects BlogsPortlet
54   */
55  public class BlogPortlet extends PortletHtmlContent
56  {
57      public static final String RESOURCE_ID = "BLOG_PORTLET";
58      public static final String MARK_BLOG = "blog";
59      public static final String MARK_PORTLET_NAME = "portlet_name";
60      public static final String MARK_PORTLET_ID = "portlet_id";
61  
62      private int _nPageTemplateDocument;
63  
64      /**
65       * Sets the identifier of the portlet type to value specified
66       */
67      public BlogPortlet( )
68      {
69          setPortletTypeId( BlogPortletHome.getInstance( ).getPortletTypeId( ) );
70      }
71  
72      private int _nContentId;
73  
74      private String _strName;
75  
76      private BlogPublication _blogPublication;
77  
78      /**
79       * Returns the HTML code of the blogsPortlet portlet with XML heading
80       *
81       * @param request
82       *            The HTTP servlet request
83       * @return the HTML code of the blogsPortlet portlet
84       */
85      @Override
86      public String getHtmlContent( HttpServletRequest request )
87      {
88          GregorianCalendar calendar = new java.util.GregorianCalendar( );
89          Blog blog = BlogService.getInstance( ).loadBlog( this.getContentId( ) );
90          BlogPublication docPub = PublishingService.getInstance( ).getBlogPublication( this.getId( ), this.getContentId( ) );
91  
92          HashMap<String, Object> model = new HashMap<>( );
93          BlogPageTemplate pageTemplate = BlogPageTemplateHome.findByPrimaryKey( this.getPageTemplateDocument( ) );
94  
95          if ( docPub != null && docPub.getIdBlog( ) != 0 && docPub.getDateBeginPublishing( ).before( new Date( calendar.getTimeInMillis( ) ) )
96                  && docPub.getDateEndPublishing( ).after( new Date( calendar.getTimeInMillis( ) ) ) )
97          {
98              if ( this.getDisplayPortletTitle( ) == 0 )
99              {
100 
101                 model.put( MARK_PORTLET_NAME, this.getName( ) );
102 
103             }
104             model.put( MARK_BLOG, blog );
105         }
106         model.put( MARK_PORTLET_ID, this.getId( ) );
107         Locale locale = null;
108         if ( request != null )
109         {
110             locale = request.getLocale( );
111         }
112 
113         HtmlTemplate template = AppTemplateService.getTemplate( pageTemplate.getFile( ), locale, model );
114 
115         return template.getHtml( );
116 
117     }
118 
119     /**
120      * Updates the current instance of the blogsPortlet object
121      */
122     public void update( )
123     {
124         BlogPortletHome.getInstance( ).update( this );
125     }
126 
127     /**
128      * Removes the current instance of the blogsPortlet object
129      */
130     @Override
131     public void remove( )
132     {
133         BlogPublicationHome.removeByIdPortlet( this.getId( ) );
134         BlogPortletHome.getInstance( ).remove( this );
135     }
136 
137     /**
138      * Sets the id of the content
139      *
140      * @param nContentId
141      *            id of the content
142      */
143     public void setContentId( int nContentId )
144     {
145         _nContentId = nContentId;
146     }
147 
148     /**
149      * Get the id of the html document
150      *
151      * @return the id of the document
152      */
153     public int getContentId( )
154     {
155         return _nContentId;
156     }
157 
158     /**
159      * Sets the BlogPublication of the html document
160      *
161      * @param blogPublication
162      *            the publication of the document
163      */
164     public void setBlogPublication( BlogPublication blogPublication )
165     {
166         _blogPublication = blogPublication;
167     }
168 
169     /**
170      * Get the BlogPublication of the html document
171      *
172      * @return the BlogPublication of the document
173      */
174     public BlogPublication getBlogPublication( )
175     {
176         return _blogPublication;
177     }
178 
179     /**
180      * Sets the name of the html document
181      *
182      * @param strName
183      *            the name of the document
184      */
185     public void setPortletName( String strName )
186     {
187         _strName = strName;
188     }
189 
190     /**
191      * Get the name of the html document
192      *
193      * @return the name of the document
194      */
195     public String getPortletName( )
196     {
197         return _strName;
198     }
199 
200     /**
201      * Sets the parent page identifier of the portlet to the value specified in parameter
202      *
203      * @param nPageTemplateDocument
204      *            the code
205      */
206     public void setPageTemplateDocument( int nPageTemplateDocument )
207     {
208         _nPageTemplateDocument = nPageTemplateDocument;
209     }
210 
211     /**
212      * Returns the identifier of the parent page of the portlet
213      *
214      * @return the parent page identifier
215      */
216     public int getPageTemplateDocument( )
217     {
218         return _nPageTemplateDocument;
219     }
220 }