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