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.style;
35  
36  import fr.paris.lutece.portal.business.portlet.PortletHome;
37  import fr.paris.lutece.portal.business.portlet.PortletImpl;
38  import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
39  import fr.paris.lutece.portal.business.style.Style;
40  import fr.paris.lutece.portal.business.style.StyleHome;
41  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
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.AppPropertiesService;
46  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
47  import fr.paris.lutece.portal.web.constants.Messages;
48  import fr.paris.lutece.portal.web.constants.Parameters;
49  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
50  import fr.paris.lutece.util.html.HtmlTemplate;
51  import fr.paris.lutece.util.html.Paginator;
52  import fr.paris.lutece.util.sort.AttributeComparator;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import java.util.Collection;
56  import java.util.Collections;
57  import java.util.HashMap;
58  import java.util.List;
59  import java.util.Map;
60  
61  import javax.servlet.http.HttpServletRequest;
62  
63  
64  /**
65   * This class provides the user interface to manage Styles features
66   */
67  public class StylesJspBean extends AdminFeaturesPageJspBean
68  {
69      //////////////////////////////////////////////////////////////////////////////////
70      // Constants
71  
72      // Right
73      /**
74       * Right to manage styles
75       */
76      public static final String RIGHT_MANAGE_STYLE = "CORE_STYLES_MANAGEMENT";
77  
78      /**
79       * Serial version UID
80       */
81      private static final long serialVersionUID = 7138319350433775587L;
82  
83      // Markers
84      private static final String MARK_STYLE_LIST = "style_list";
85      private static final String MARK_PORTLET_TYPE_LIST = "portlet_type_list";
86      private static final String MARK_PORTAL_COMPONENT_LIST = "portal_component_list";
87      private static final String MARK_STYLE = "style";
88      private static final String MARK_PAGINATOR = "paginator";
89      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
90  
91      // Properties
92      private static final String PROPERTY_STYLES_PER_PAGE = "paginator.style.itemsPerPage";
93  
94      // Templates files path
95      private static final String TEMPLATE_MANAGE_STYLES = "admin/style/manage_styles.html";
96      private static final String TEMPLATE_CREATE_STYLE = "admin/style/create_style.html";
97      private static final String TEMPLATE_MODIFY_STYLE = "admin/style/modify_style.html";
98  
99      // Portal Component definition
100     private static final int PORTAL_COMPONENT_ID_PORTLET = 0;
101 
102     // Jsp Definition
103     private static final String JSP_DO_REMOVE_STYLE = "jsp/admin/style/DoRemoveStyle.jsp";
104     private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";
105 
106     // Message keys
107     private static final String MESSAGE_CANT_DELETE_STYLE_PORTLETS = "portal.style.message.cannotDeleteStylePorlets";
108 
109     //    private static final String MESSAGE_CANT_DELETE_STYLE_XSL = "portal.style.message.cannotDeleteStyleXsl";
110     private static final String MESSAGE_CONFIRM_DELETE_STYLE = "portal.style.message.confirmDeleteStyle";
111     private static final String MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID = "portal.style.message.createStyle.InvalidIdFormat";
112     private static final String MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS = "portal.style.message.createStyle.idAlreadyExists";
113     private static final String MESSAGE_CREATE_STYLE_COMPONENT_EXISTS = "portal.style.message.createStyle.componentHasAlreadyAStyle";
114     private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
115     private int _nItemsPerPage;
116     private int _nDefaultItemsPerPage;
117     private String _strCurrentPageIndex;
118 
119     /**
120      * Displays the styles list
121      * @param request The HTTP request
122      * @return the html code for displaying the styles list
123      */
124     public String getStylesManagement( HttpServletRequest request )
125     {
126         List<Style> listStyles = (List<Style>) StyleHome.getStylesList(  );
127 
128         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
129         String strAscSort = null;
130 
131         if ( strSortedAttributeName != null )
132         {
133             strAscSort = request.getParameter( Parameters.SORTED_ASC );
134 
135             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
136 
137             Collections.sort( listStyles, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
138         }
139 
140         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLES_PER_PAGE, 10 );
141         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
142         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
143                 _nDefaultItemsPerPage );
144 
145         String strURL = getHomeUrl( request );
146 
147         if ( strSortedAttributeName != null )
148         {
149             strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
150         }
151 
152         if ( strAscSort != null )
153         {
154             strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
155         }
156 
157         LocalizedPaginator<Style> paginator = new LocalizedPaginator<Style>( listStyles, _nItemsPerPage, strURL,
158                 Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale(  ) );
159 
160         Map<String, Object> model = new HashMap<String, Object>(  );
161         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
162         model.put( MARK_PAGINATOR, paginator );
163         model.put( MARK_STYLE_LIST, paginator.getPageItems(  ) );
164 
165         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLES, getLocale(  ), model );
166 
167         return getAdminPage( template.getHtml(  ) );
168     }
169 
170     /**
171      * Returns the create form of a new style
172      * @param request The http request
173      * @return The html code for the create form of a new style
174      */
175     public String getCreateStyle( HttpServletRequest request )
176     {
177         Map<String, Object> model = new HashMap<String, Object>(  );
178         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale(  ) ) );
179         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList(  ) );
180 
181         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLE, getLocale(  ), model );
182 
183         return getAdminPage( template.getHtml(  ) );
184     }
185 
186     /**
187      * Processes the creation form of a new style by recovering the parameters in the http request
188      * @param request the http request
189      * @return The Jsp URL of the process result
190      */
191     public String doCreateStyle( HttpServletRequest request )
192     {
193         String strId = request.getParameter( Parameters.STYLE_ID );
194 
195         //Mandatory fields
196         if ( request.getParameter( Parameters.STYLE_ID ).equals( "" ) ||
197                 request.getParameter( Parameters.STYLE_NAME ).equals( "" ) )
198         {
199             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
200         }
201 
202         int nId;
203 
204         try
205         {
206             nId = Integer.parseInt( strId );
207         }
208         catch ( NumberFormatException nb )
209         {
210             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID,
211                 AdminMessage.TYPE_STOP );
212         }
213 
214         Style styleExisting = StyleHome.findByPrimaryKey( nId );
215 
216         if ( styleExisting != null )
217         {
218             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS,
219                 AdminMessage.TYPE_STOP );
220         }
221 
222         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
223 
224         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) &&
225                 ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET ) )
226         {
227             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS,
228                 AdminMessage.TYPE_STOP );
229         }
230 
231         //The style doesn't exist in the database, we can create it
232         Style style = new Style(  );
233         style.setId( nId );
234         style.setDescription( request.getParameter( Parameters.STYLE_NAME ) );
235         style.setPortalComponentId( nPortalComponentId );
236 
237         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
238         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
239         style.setPortletTypeId( strPortletTypeId );
240         StyleHome.create( style );
241 
242         return getHomeUrl( request );
243     }
244 
245     /**
246      * Returns the form to update a style whose identifer is stored in the http request
247      * @param request The http request
248      * @return The html code
249      */
250     public String getModifyStyle( HttpServletRequest request )
251     {
252         String strIdStyles = request.getParameter( Parameters.STYLE_ID );
253         int nStyleId = Integer.parseInt( strIdStyles );
254 
255         Map<String, Object> model = new HashMap<String, Object>(  );
256         model.put( MARK_STYLE, StyleHome.findByPrimaryKey( nStyleId ) );
257         model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale(  ) ) );
258         model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList(  ) );
259 
260         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLE, getLocale(  ), model );
261 
262         return getAdminPage( template.getHtml(  ) );
263     }
264 
265     /**
266      * Processes the updating form of a style whose new parameters are stored in the
267      * http request
268      * @param request The http request
269      * @return The Jsp URL of the process result
270      */
271     public String doModifyStyle( HttpServletRequest request )
272     {
273         int nStyleId = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );
274 
275         //the portlet type can be not present  the request if the portal component is not a portlet
276         String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
277         strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
278 
279         int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
280         String strStyleDescription = request.getParameter( Parameters.STYLE_NAME );
281 
282         if ( strStyleDescription.trim(  ).equals( "" ) )
283         {
284             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
285         }
286 
287         Style style = StyleHome.findByPrimaryKey( nStyleId );
288         int nPortalComponentOld = style.getPortalComponentId(  );
289 
290         if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) &&
291                 ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET ) && ( nPortalComponentId != nPortalComponentOld ) )
292         {
293             return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS,
294                 AdminMessage.TYPE_STOP );
295         }
296 
297         style.setPortletTypeId( strPortletTypeId );
298         style.setPortalComponentId( nPortalComponentId );
299         style.setDescription( strStyleDescription );
300         StyleHome.update( style );
301 
302         return getHomeUrl( request );
303     }
304 
305     /**
306      * Returns the confirm of removing the style whose identifier is in
307      * the http request
308      *
309      * @param request The Http request
310      * @return the html code for the remove confirmation page
311      */
312     public String getConfirmRemoveStyle( HttpServletRequest request )
313     {
314         String strId = request.getParameter( Parameters.STYLE_ID );
315         int nId = Integer.parseInt( strId );
316         Collection<PortletImpl> listPortlets = PortletHome.getPortletListByStyle( nId );
317         Collection<StyleSheet> listStyleSheets = StyleHome.getStyleSheetList( nId );
318 
319         if ( listPortlets.size(  ) > 0 )
320         {
321             return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_DELETE_STYLE_PORTLETS,
322                 AdminMessage.TYPE_STOP );
323         }
324 
325         if ( listStyleSheets.size(  ) > 0 )
326         {
327             for ( StyleSheet styleSheet : listStyleSheets )
328             {
329                 int nIdStyleSheet = styleSheet.getId(  );
330                 UrlItem urlStylesheet = new UrlItem( JSP_DO_REMOVE_STYLESHEET );
331                 urlStylesheet.addParameter( Parameters.STYLESHEET_ID, nIdStyleSheet );
332                 urlStylesheet.addParameter( Parameters.STYLE_ID, nId );
333 
334                 Object[] args = { styleSheet.getDescription(  ) };
335 
336                 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args,
337                     urlStylesheet.getUrl(  ), AdminMessage.TYPE_CONFIRMATION );
338             }
339         }
340 
341         UrlItem url = new UrlItem( JSP_DO_REMOVE_STYLE );
342         url.addParameter( Parameters.STYLE_ID, nId );
343 
344         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLE, url.getUrl(  ),
345             AdminMessage.TYPE_CONFIRMATION );
346     }
347 
348     /**
349      * Processes the deletion of a style
350      * @param request the http request
351      * @return The Jsp URL of the process result
352      */
353     public String doRemoveStyle( HttpServletRequest request )
354     {
355         String strId = request.getParameter( Parameters.STYLE_ID );
356         int nId = Integer.parseInt( strId );
357         StyleHome.remove( nId );
358 
359         return getHomeUrl( request );
360     }
361 }