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.portal.web.portlet;
35  
36  import fr.paris.lutece.portal.business.page.PageHome;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
38  import fr.paris.lutece.portal.business.portlet.PortletHome;
39  import fr.paris.lutece.portal.business.portlet.PortletType;
40  import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
41  import fr.paris.lutece.portal.business.role.RoleHome;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
48  import fr.paris.lutece.portal.web.constants.Messages;
49  import fr.paris.lutece.portal.web.constants.Parameters;
50  import fr.paris.lutece.util.ReferenceList;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  
53  import java.util.HashMap;
54  import java.util.Locale;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  
60  /**
61   * This class represents user interface Portlet. It is the base class of all
62   * user interface portlets. It is abstract
63   * and the implementation of the interface PortletJspBeanInterface is
64   * compulsary.
65   */
66  public abstract class PortletJspBean extends AdminFeaturesPageJspBean
67  {
68      ////////////////////////////////////////////////////////////////////////////
69      // Constants
70      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
71  
72      // Parameters
73      protected static final String PARAMETER_PAGE_ID = "page_id";
74      protected static final String PARAMETER_PORTLET_ID = "portlet_id";
75      protected static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
76      private static final long serialVersionUID = -3546292252642160812L;
77  
78      // Markers
79      private static final String MARK_PORTLET = "portlet";
80      private static final String MARK_PORTLET_TYPE = "portletType";
81      private static final String MARK_PORTLET_PAGE_ID = "portlet_page_id";
82      private static final String MARK_PORTLET_ORDER_COMBO = "portlet_order_combo";
83      private static final String MARK_PORTLET_COLUMNS_COMBO = "portlet_columns_combo";
84      private static final String MARK_PORTLET_STYLES_COMBO = "portlet_style_combo";
85      private static final String MARK_PORTLET_ROLES_COMBO = "portlet_role_combo";
86      private static final String MARK_SMALL_CHECKED = "small_checked";
87      private static final String MARK_NORMAL_CHECKED = "normal_checked";
88      private static final String MARK_LARGE_CHECKED = "large_checked";
89      private static final String MARK_XLARGE_CHECKED = "xlarge_checked";
90      private static final String VALUE_CHECKED = "checked=\"checked\"";
91      private static final String VALUE_UNCHECKED = "";
92  
93      // Templates
94      private static final String TEMPLATE_CREATE_PORTLET = "admin/portlet/create_portlet.html";
95      private static final String TEMPLATE_MODIFY_PORTLET = "admin/portlet/modify_portlet.html";
96  
97      // Properties
98      private static final String PROPERTY_LIST_ORDER_MAX = "list.order.max";
99      private static final String PROPERTY_COLUMN_NUM_MAX = "nb.columns";
100 
101     // Messages
102     private static final String MESSAGE_INVALID_PAGE_ID = "portal.site.message.pageIdInvalid";
103 
104     // Jsp
105     private static final String JSP_ADMIN_SITE = "../../site/AdminSite.jsp";
106 
107     /**
108      * Displays the portlet's creation form
109      *
110      * @param request The http request
111      * @return The html code for displaying the creation form
112      */
113     public abstract String getCreate( HttpServletRequest request );
114 
115     /**
116      * Processes portlet's creation
117      *
118      * @param request The http request
119      * @return The Management jsp url
120      */
121     public abstract String doCreate( HttpServletRequest request );
122 
123     /**
124      * Displays the portlet's modification form
125      *
126      * @param request The http request
127      * @return The html code for displaying the modification form
128      */
129     public abstract String getModify( HttpServletRequest request );
130 
131     /**
132      * Processes portlet's modification
133      *
134      * @param request The http request
135      * @return The Management jsp url
136      */
137     public abstract String doModify( HttpServletRequest request );
138 
139     ////////////////////////////////////////////////////////////////////////////
140     // Management of the combos common to all portlets
141     // Order combo
142 
143     /**
144      * Returns a list of orders
145      *
146      * @return the list of orders in form of ReferenceList
147      */
148     private ReferenceList getOrdersList(  )
149     {
150         ReferenceList list = new ReferenceList(  );
151         int nOrderMax = AppPropertiesService.getPropertyInt( PROPERTY_LIST_ORDER_MAX, 15 );
152 
153         for ( int i = 1; i <= nOrderMax; i++ )
154         {
155             list.addItem( i, String.valueOf( i ) );
156         }
157 
158         return list;
159     }
160 
161     /**
162      * Returns the list of the columns of the page where the portlet can be
163      * positioned
164      *
165      * @return the list of the page columns in form of ReferenceList
166      */
167     private ReferenceList getColumnsList(  )
168     {
169         ReferenceList list = new ReferenceList(  );
170         int nColumnNumMax = AppPropertiesService.getPropertyInt( PROPERTY_COLUMN_NUM_MAX, 1 );
171 
172         for ( int i = 1; i <= nColumnNumMax; i++ )
173         {
174             list.addItem( i, String.valueOf( i ) );
175         }
176 
177         return list;
178     }
179 
180     /**
181      * Recovers the common attributes of the portlet
182      *
183      * @param request the http request
184      * @param portlet The instance of the portlet
185      * @return An error Key if error, otherwise null.
186      */
187     protected String setPortletCommonData( HttpServletRequest request, Portlet portlet )
188     {
189         String strErrorKey = null;
190 
191         // get portlet attributes
192         String strName = request.getParameter( Parameters.PORTLET_NAME );
193         String strStyleId = request.getParameter( Parameters.STYLE );
194         String strColumn = request.getParameter( Parameters.COLUMN );
195         String strOrder = request.getParameter( Parameters.ORDER );
196         String strAcceptAlias = request.getParameter( Parameters.ACCEPT_ALIAS );
197         String strAcceptPortletTitle = request.getParameter( Parameters.DISPLAY_PORTLET_TITLE );
198         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE_ID );
199         String strRole = request.getParameter( Parameters.ROLE );
200         String strDisplaySmall = request.getParameter( Parameters.DISPLAY_ON_SMALL_DEVICE );
201         String strDisplayNormal = request.getParameter( Parameters.DISPLAY_ON_NORMAL_DEVICE );
202         String strDisplayLarge = request.getParameter( Parameters.DISPLAY_ON_LARGE_DEVICE );
203         String strDisplayXLarge = request.getParameter( Parameters.DISPLAY_ON_XLARGE_DEVICE );
204 
205         strName = strName.replaceAll( "\"", "" );
206 
207         // Check Mandatory fields
208         if ( strName.equals( "" ) || strOrder.equals( "" ) || strColumn.equals( "" ) || strAcceptAlias.equals( "" ) ||
209                 strAcceptPortletTitle.equals( "" ) )
210         {
211             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
212         }
213 
214         // style id is not mandatory if the content is not generated from XML and XSL 
215         if ( portlet.isContentGeneratedByXmlAndXsl(  ) )
216         {
217             if ( ( strStyleId == null ) || strStyleId.trim(  ).equals( "" ) )
218             {
219                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
220             }
221         }
222 
223         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
224         int nPageId;
225 
226         // Test format of the id and the existence of the page
227         try
228         {
229             nPageId = Integer.parseInt( strPageId );
230 
231             if ( !PageHome.checkPageExist( nPageId ) )
232             {
233                 return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PAGE_ID, AdminMessage.TYPE_STOP );
234             }
235         }
236         catch ( NumberFormatException e )
237         {
238             AppLogService.error( e.getMessage(  ), e );
239 
240             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PAGE_ID, AdminMessage.TYPE_STOP );
241         }
242 
243         int nOrder = Integer.parseInt( strOrder );
244         int nColumn = Integer.parseInt( strColumn );
245         int nAcceptAlias = Integer.parseInt( strAcceptAlias );
246         int nAcceptPortletTitle = Integer.parseInt( strAcceptPortletTitle );
247 
248         int nStyleId = portlet.isContentGeneratedByXmlAndXsl(  ) ? Integer.parseInt( strStyleId ) : 0;
249 
250         int nDeviceDisplayFlags = 0;
251 
252         if ( strDisplaySmall != null )
253         {
254             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE;
255         }
256 
257         if ( strDisplayNormal != null )
258         {
259             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE;
260         }
261 
262         if ( strDisplayLarge != null )
263         {
264             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE;
265         }
266 
267         if ( strDisplayXLarge != null )
268         {
269             nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE;
270         }
271 
272         portlet.setName( strName );
273         portlet.setOrder( nOrder );
274         portlet.setColumn( nColumn );
275         portlet.setStyleId( nStyleId );
276         portlet.setPageId( nPageId );
277         portlet.setAcceptAlias( nAcceptAlias );
278         portlet.setDisplayPortletTitle( nAcceptPortletTitle );
279         portlet.setPortletTypeId( strPortletTypeId );
280         portlet.setRole( strRole );
281         portlet.setDeviceDisplayFlags( nDeviceDisplayFlags );
282 
283         return strErrorKey;
284     }
285 
286     /**
287      * Fills templates with common values shared by portlet creation form
288      *
289      * @param strPageId the page identifier
290      * @param strPortletTypeId the Portlet type identifier
291      * @return the template filled
292      */
293     protected HtmlTemplate getCreateTemplate( String strPageId, String strPortletTypeId )
294     {
295         return getCreateTemplate( strPageId, strPortletTypeId, new HashMap<String, Object>(  ) );
296     }
297 
298     /**
299      * Fills templates with common values shared by portlet creation form
300      *
301      * @param strPageId the page identifier
302      * @param strPortletTypeId the Portlet type identifier
303      * @param model Specific data stored in a hashtable
304      * @return the template filled
305      */
306     protected HtmlTemplate getCreateTemplate( String strPageId, String strPortletTypeId, Map<String, Object> model )
307     {
308         PortletType portletType = PortletTypeHome.findByPrimaryKey( strPortletTypeId );
309         Locale locale = getLocale(  );
310         portletType.setLocale( locale );
311 
312         model.put( MARK_PORTLET_TYPE, portletType );
313         model.put( MARK_PORTLET_PAGE_ID, strPageId );
314         model.put( MARK_PORTLET_ORDER_COMBO, getOrdersList(  ) );
315         model.put( MARK_PORTLET_COLUMNS_COMBO, getColumnsList(  ) );
316         model.put( MARK_PORTLET_STYLES_COMBO, PortletHome.getStylesList( strPortletTypeId ) );
317         model.put( MARK_PORTLET_ROLES_COMBO, RoleHome.getRolesList( getUser(  ) ) );
318 
319         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_PORTLET, locale, model );
320 
321         return template;
322     }
323 
324     /**
325      * Fills update template with portlet values
326      *
327      * @param portlet the object to update
328      * @return the update template filled
329      */
330     protected HtmlTemplate getModifyTemplate( Portlet portlet )
331     {
332         return getModifyTemplate( portlet, new HashMap<String, Object>(  ) );
333     }
334 
335     /**
336      * Fills update template with portlet values
337      *
338      * @param portlet the object to update
339      * @param model The Data model
340      * @return the update template filled
341      */
342     protected HtmlTemplate getModifyTemplate( Portlet portlet, Map<String, Object> model )
343     {
344         PortletType portletType = PortletTypeHome.findByPrimaryKey( portlet.getPortletTypeId(  ) );
345         portletType.setLocale( getLocale(  ) );
346         model.put( MARK_PORTLET_TYPE, portletType );
347         model.put( MARK_PORTLET, portlet );
348         model.put( MARK_PORTLET_ORDER_COMBO, getOrdersList(  ) );
349         model.put( MARK_PORTLET_COLUMNS_COMBO, getColumnsList(  ) );
350         model.put( MARK_PORTLET_STYLES_COMBO, PortletHome.getStylesList( portlet.getPortletTypeId(  ) ) );
351         model.put( MARK_PORTLET_ROLES_COMBO, RoleHome.getRolesList( getUser(  ) ) );
352         putCheckBox( model, MARK_SMALL_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE ) );
353         putCheckBox( model, MARK_NORMAL_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE ) );
354         putCheckBox( model, MARK_LARGE_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE ) );
355         putCheckBox( model, MARK_XLARGE_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE ) );
356 
357         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PORTLET, getLocale(  ), model );
358 
359         return template;
360     }
361 
362     /**
363      * Put check box.
364      *
365      * @param model the model
366      * @param strMarkerChecked the str marker checked
367      * @param bChecked the b checked
368      */
369     protected void putCheckBox( Map<String, Object> model, String strMarkerChecked, boolean bChecked )
370     {
371         String strChecked = ( bChecked ) ? VALUE_CHECKED : VALUE_UNCHECKED;
372         model.put( strMarkerChecked, strChecked );
373     }
374 
375     /**
376      * Gets the page URL
377      * @param nIdPage Page ID
378      * @return The page URL
379      */
380     protected String getPageUrl( int nIdPage )
381     {
382         return JSP_ADMIN_SITE + "?" + PARAMETER_PAGE_ID + "=" + nIdPage;
383     }
384 }