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.web.portlet;
35  
36  import fr.paris.lutece.plugins.blog.business.Blog;
37  import fr.paris.lutece.plugins.blog.business.BlogHome;
38  import fr.paris.lutece.plugins.blog.business.portlet.BlogListPortletHome;
39  import fr.paris.lutece.plugins.blog.business.portlet.BlogPortlet;
40  import fr.paris.lutece.plugins.blog.business.portlet.BlogPortletHome;
41  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
42  import fr.paris.lutece.plugins.blog.service.BlogService;
43  import fr.paris.lutece.portal.business.portlet.PortletHome;
44  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  import fr.paris.lutece.portal.service.util.AppPathService;
47  import fr.paris.lutece.portal.service.admin.AdminUserService;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  import org.apache.commons.lang3.StringUtils;
53  
54  import java.util.Date;
55  import java.util.HashMap;
56  import java.util.List;
57  
58  /**
59   * This class provides the user interface to manage BlogsPortlet features
60   */
61  public class BlogPortletJspBean extends PortletJspBean
62  {
63  
64      private static final long serialVersionUID = 5744334133144418317L;
65      public static final String MARK_HTML_CONTENT = "htmlcontent";
66      public static final String MARK_EDIT_COMMENT = "editcomment";
67      public static final String MARK_WEBAPP_URL = "webapp_url";
68      public static final String MARK_LIST_HTMLDOC = "blog_list";
69      public static final String MARK_LIST_PAGES = "pages_list";
70      public static final String MARK_BLOG_ID = "blog_id";
71  
72      public static final String PARAMETER_CONTENT_ID = "content_id";
73      public static final String PARAMETER_HTML_CONTENT = "html_content";
74      public static final String PARAMETER_EDIT_COMMENT = "edit_comment";
75      public static final String PARAMETER_PORTLET_NAME = "portlet_name";
76      public static final String PARAMETER_HTMLDOC_SELECTED = "blog_selected";
77      private static final String PARAMETER_PAGE_TEMPLATE_CODE = "page_template_code";
78  
79      public static final String TEMPLATE_MODIFY_PORTLET = "admin/portlet/modify_portlet.html";
80  
81      /**
82       * Returns the BlogPortlet form of creation
83       *
84       * @param request
85       *            The Http rquest
86       * @return the html code of the Blog portlet form
87       */
88      @Override
89      public String getCreate( HttpServletRequest request )
90      {
91          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
92          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
93          List<Blog> listBlog = BlogHome.getBlogsList( );
94          HashMap<String, Object> model = new HashMap<>( );
95  
96          model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
97          model.put( MARK_LIST_HTMLDOC, listBlog );
98          model.put( MARK_LIST_PAGES, BlogListPortletHome.loadPages( BlogPortlet.RESOURCE_ID ) );
99  
100         HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId, model );
101 
102         return template.getHtml( );
103     }
104 
105     /**
106      * Returns the BlogPortlet form for update
107      * 
108      * @param request
109      *            The Http request
110      * @return the html code of the BlogPortlet form
111      */
112     @Override
113     public String getModify( HttpServletRequest request )
114     {
115         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
116         int nPortletId = Integer.parseInt( strPortletId );
117         BlogPortlet../../../../../fr/paris/lutece/plugins/blog/business/portlet/BlogPortlet.html#BlogPortlet">BlogPortlet portlet = (BlogPortlet) PortletHome.findByPrimaryKey( nPortletId );
118         Blog blog = BlogHome.findByPrimaryKey( portlet.getContentId( ) );
119         HashMap<String, Object> model = new HashMap<>( );
120 
121         model.put( MARK_HTML_CONTENT, blog.getHtmlContent( ) );
122         model.put( MARK_EDIT_COMMENT, blog.getEditComment( ) );
123         model.put( MARK_LIST_PAGES, BlogListPortletHome.loadPages( BlogPortlet.RESOURCE_ID ) );
124         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
125         model.put( MARK_BLOG_ID, blog.getId( ) );
126 
127         HtmlTemplate template = getModifyTemplate( portlet, model );
128 
129         return template.getHtml( );
130     }
131 
132     /**
133      * Treats the creation form of a new Blog portlet
134      *
135      * @param request
136      *            The Http request
137      * @return The jsp URL which displays the view of the created BlogPortlet portlet
138      */
139     @Override
140     public String doCreate( HttpServletRequest request )
141     {
142         BlogPortletlog/business/portlet/BlogPortlet.html#BlogPortlet">BlogPortlet portlet = new BlogPortlet( );
143         AdminUser user = AdminUserService.getAdminUser( request );
144         String strSelectedBlog = request.getParameter( PARAMETER_HTMLDOC_SELECTED );
145         String strTemplateCode = request.getParameter( PARAMETER_PAGE_TEMPLATE_CODE );
146 
147         // recovers portlet specific attributes
148         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
149         int nPageId = Integer.parseInt( strPageId );
150         Blog/plugins/blog/business/Blog.html#Blog">Blog blog = new Blog( );
151         if ( strSelectedBlog == null || StringUtils.isEmpty( strSelectedBlog ) || !StringUtils.isNumeric( strSelectedBlog ) )
152         {
153             blog.setContentLabel( request.getParameter( PARAMETER_PORTLET_NAME ) );
154             blog.setVersion( 1 );
155             blog.setCreationDate( getSqlDate( ) );
156             blog.setUpdateDate( getSqlDate( ) );
157             blog.setHtmlContent( request.getParameter( PARAMETER_HTML_CONTENT ) );
158             // TODO error validation on edit comment length
159             blog.setEditComment( request.getParameter( PARAMETER_EDIT_COMMENT ) );
160             blog.setUser( user.getAccessCode( ) );
161             blog.setUserCreator( user.getAccessCode( ) );
162             BlogHome.addInitialVersion( blog );
163         }
164         else
165         {
166             blog = BlogHome.findByPrimaryKey( Integer.parseInt( strSelectedBlog ) );
167         }
168         int nContentId = blog.getId( );
169 
170         // get portlet common attributes
171         String strErrorUrl = setPortletCommonData( request, portlet );
172 
173         if ( strErrorUrl != null )
174         {
175             return strErrorUrl;
176         }
177 
178         portlet.setPageTemplateDocument( Integer.parseInt( strTemplateCode ) );
179         portlet.setPageId( nPageId );
180         portlet.setContentId( nContentId );
181         portlet.setPortletName( request.getParameter( PARAMETER_PORTLET_NAME ) );
182 
183         // Creates the portlet
184         BlogPortletHome.getInstance( ).create( portlet );
185         blog.setAttachedPortletId( portlet.getId( ) );
186         BlogHome.update( blog );
187         int nbPublication = BlogPublicationHome.countPublicationByIdBlogAndDate( blog.getId( ), new Date( ) );
188         // First publication of this blog -> indexing needed
189         if ( nbPublication == 1 )
190         {
191             BlogService.getInstance( ).fireCreateBlogEvent( blog.getId( ) );
192         }
193 
194         // Displays the page with the new Portlet
195         return getPageUrl( nPageId );
196     }
197 
198     /**
199      * Treats the update form of the BlogPortlet portlet whose identifier is in the http request
200      *
201      * @param request
202      *            The Http request
203      * @return The jsp URL which displays the view of the updated portlet
204      */
205     @Override
206     public String doModify( HttpServletRequest request )
207     {
208         // fetches portlet attributes
209         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
210         // recovers portlet attributes
211         String strDocumentTypeCode = request.getParameter( PARAMETER_PAGE_TEMPLATE_CODE );
212         int nPortletId = Integer.parseInt( strPortletId );
213         BlogPortlet../../../../../fr/paris/lutece/plugins/blog/business/portlet/BlogPortlet.html#BlogPortlet">BlogPortlet portlet = (BlogPortlet) PortletHome.findByPrimaryKey( nPortletId );
214         Blog blog = BlogHome.findByPrimaryKey( portlet.getContentId( ) );
215         // retrieve portlet common attributes
216         String strErrorUrl = setPortletCommonData( request, portlet );
217 
218         if ( strErrorUrl != null )
219         {
220             return strErrorUrl;
221         }
222         portlet.setPageTemplateDocument( Integer.parseInt( strDocumentTypeCode ) );
223         // updates the blog
224         blog.setHtmlContent( request.getParameter( PARAMETER_HTML_CONTENT ) );
225         blog.setEditComment( request.getParameter( PARAMETER_EDIT_COMMENT ) );
226         blog.setUpdateDate( getSqlDate( ) );
227         blog.setVersion( blog.getVersion( ) + 1 );
228         BlogHome.addNewVersion( blog );
229 
230         portlet.setBlogPublication( BlogPublicationHome.findDocPublicationByPimaryKey( nPortletId, portlet.getContentId( ) ) );
231         // updates the portlet
232         portlet.update( );
233 
234         // update of this blog -> re-indexing needed
235         BlogService.getInstance( ).fireUpdateBlogEvent( blog.getId( ) );
236         // displays the page with the updated portlet
237         return getPageUrl( portlet.getPageId( ) );
238     }
239 
240     /**
241      * Gets the current date in sql format
242      *
243      * @return the current date in sql format
244      */
245     protected java.sql.Timestamp getSqlDate( )
246     {
247         java.util.Date utilDate = new java.util.Date( );
248         java.sql.Timestamp sqlDate = new java.sql.Timestamp( utilDate.getTime( ) );
249 
250         return ( sqlDate );
251     }
252 }