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