View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.childpages.web.portlet;
35  
36  import fr.paris.lutece.plugins.childpages.business.portlet.ChildPagesPortlet;
37  import fr.paris.lutece.plugins.childpages.business.portlet.ChildPagesPortletHome;
38  import fr.paris.lutece.portal.business.page.PageHome;
39  import fr.paris.lutece.portal.business.portlet.PortletHome;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppException;
44  import fr.paris.lutece.portal.web.constants.Messages;
45  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.HashMap;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  
53  /**
54   * This class provides the user interface to manage ChildPages Portlet
55   */
56  public class ChildPagesPortletJspBean extends PortletJspBean
57  {
58      ////////////////////////////////////////////////////////////////////////////
59      // Constants
60  
61      // Right
62      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
63  
64      // Messages
65      private static final String MESSAGE_PORTLET_CHILD_PAGE_INEXISTENT = "childpages.message.portlet.childpageInexistent";
66      private static final String MESSAGE_PORTLET_CHILD_PAGE_PARENT_NOT_VALID = "childpages.message.portlet.childpageParentNotValid";
67  
68      // Templates
69      private static final String TEMPLATE_HELP_PARENT_PAGE = "admin/plugins/childpages/help_parent_page.html";
70  
71      // Parameters
72      private static final String PARAMETER_PARENT_ID = "parent_id";
73  
74      // Bookmarks
75      private static final String MARK_PARENT_ID = "page_id_parent";
76  
77      /**
78       * Returns portlet's properties prefix
79       *
80       * @return prefix
81       */
82      public String getPropertiesPrefix(  )
83      {
84          return "portlet.child.pages";
85      }
86  
87      /**
88       * Returns the Download portlet creation form
89       *
90       * @param request The http request
91       * @return The HTML form
92       */
93      public String getCreate( HttpServletRequest request )
94      {
95          String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
96          String strIdPortletType = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
97          HashMap model = new HashMap(  );
98          model.put( MARK_PARENT_ID, strIdPage );
99  
100         HtmlTemplate template = getCreateTemplate( strIdPage, strIdPortletType, model );
101 
102         return template.getHtml(  );
103     }
104 
105     public String getModify( HttpServletRequest request )
106     {
107         String strIdPortlet = request.getParameter( PARAMETER_PORTLET_ID );
108         int nIdPortlet = Integer.parseInt( strIdPortlet );
109         ChildPagesPortlet../../../fr/paris/lutece/plugins/childpages/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet portlet = (ChildPagesPortlet) PortletHome.findByPrimaryKey( nIdPortlet );
110 
111         //initialization of nParentPageId
112         int nIdParentPage = portlet.getParentPageId(  );
113 
114         HashMap model = new HashMap(  );
115         model.put( MARK_PARENT_ID, nIdParentPage );
116 
117         HtmlTemplate template = getModifyTemplate( portlet, model );
118 
119         return template.getHtml(  );
120     }
121 
122     /**
123      * Process portlet's creation
124      *
125      * @param request The Http request
126      * @return The Jsp management URL of the process result
127      */
128     public String doCreate( HttpServletRequest request )
129     {
130         ChildPagesPortletges/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet portlet = new ChildPagesPortlet(  );
131         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
132         int nIdPage = Integer.parseInt( strIdPage );
133 
134         //gets the identifier of the parent page
135         String strParentPage = request.getParameter( PARAMETER_PARENT_ID );
136 
137         //Mandatory fields
138         if ( strParentPage.trim(  ).equals( "" ) || ( strParentPage == null ) )
139         {
140             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
141         }
142 
143         int nIdParentPage;
144 
145         try
146         {
147             nIdParentPage = Integer.parseInt( strParentPage );
148         }
149         catch ( NumberFormatException nb )
150         {
151             //the format of the identifier of the page is not valid
152             return AdminMessageService.getMessageUrl( request, MESSAGE_PORTLET_CHILD_PAGE_PARENT_NOT_VALID,
153                 AdminMessage.TYPE_STOP );
154         }
155 
156         try
157         {
158             PageHome.getPage( nIdParentPage );
159         }
160         catch ( AppException ex )
161         {
162             //The exception is thrown if the page doesn't exist
163             return AdminMessageService.getMessageUrl( request, MESSAGE_PORTLET_CHILD_PAGE_INEXISTENT,
164                 AdminMessage.TYPE_STOP );
165         }
166 
167         // get portlet common attributes
168         String strErrorUrl = setPortletCommonData( request, portlet );
169 
170         if ( strErrorUrl != null )
171         {
172             return strErrorUrl;
173         }
174 
175         portlet.setPageId( nIdPage );
176 
177         //gets the specific parameters
178         portlet.setParentPageId( nIdParentPage );
179 
180         //Portlet creation
181         ChildPagesPortletHome.getInstance(  ).create( portlet );
182 
183         //Displays the page with the new Portlet
184         return getPageUrl( nIdPage );
185     }
186 
187     /**
188      * Process portlet's modification
189      *
190      * @param request The http request
191      * @return Management's Url
192      */
193     public String doModify( HttpServletRequest request )
194     {
195         //recovery of the portlet
196         String strIdPortlet = request.getParameter( PARAMETER_PORTLET_ID );
197         int nIdPortlet = Integer.parseInt( strIdPortlet );
198         ChildPagesPortlet../../../fr/paris/lutece/plugins/childpages/business/portlet/ChildPagesPortlet.html#ChildPagesPortlet">ChildPagesPortlet portlet = (ChildPagesPortlet) PortletHome.findByPrimaryKey( nIdPortlet );
199 
200         // get portlet common attributes
201         String strErrorUrl = setPortletCommonData( request, portlet );
202 
203         if ( strErrorUrl != null )
204         {
205             return strErrorUrl;
206         }
207 
208         //recovery of the identifier of the parent page
209         String strParentPage = request.getParameter( PARAMETER_PARENT_ID );
210 
211         if ( strParentPage.trim(  ).equals( "" ) || ( strParentPage == null ) )
212         {
213             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
214         }
215 
216         int nIdPageMere;
217 
218         try
219         {
220             nIdPageMere = Integer.parseInt( strParentPage );
221         }
222         catch ( NumberFormatException nb )
223         {
224             //the format of the identifier of the page is not valid
225             return AdminMessageService.getMessageUrl( request, MESSAGE_PORTLET_CHILD_PAGE_PARENT_NOT_VALID,
226                 AdminMessage.TYPE_STOP );
227         }
228 
229         try
230         {
231             PageHome.getPage( nIdPageMere );
232         }
233         catch ( AppException ex )
234         {
235             //The exception is thrown if the page doesn't exist
236             return AdminMessageService.getMessageUrl( request, MESSAGE_PORTLET_CHILD_PAGE_INEXISTENT,
237                 AdminMessage.TYPE_STOP );
238         }
239 
240         //gets the specific attributes of the portlet
241         portlet.setParentPageId( nIdPageMere );
242 
243         //Update of the portlet
244         portlet.update(  );
245 
246         //Displays the page with the updated portlet
247         return getPageUrl( portlet.getPageId(  ) );
248     }
249 
250     /**
251      * displays the form to help the parent page input of the child page portlet
252      *
253      * @return the html code to help
254      */
255     public String getHelpParentPage( HttpServletRequest request )
256     {
257         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_HELP_PARENT_PAGE );
258 
259         return template.getHtml(  );
260     }
261 }