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.features;
35  
36  import fr.paris.lutece.portal.business.right.Level;
37  import fr.paris.lutece.portal.business.right.LevelHome;
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
42  import fr.paris.lutece.portal.web.constants.Messages;
43  import fr.paris.lutece.portal.web.constants.Parameters;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  
46  import java.util.HashMap;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  
51  /**
52   * This class provides the user interface to manage levels features ( manage,
53   * create, modify )
54   */
55  public class LevelsJspBean extends AdminFeaturesPageJspBean
56  {
57      // Right 
58      public static final String RIGHT_MANAGE_LEVELS = "CORE_LEVEL_RIGHT_MANAGEMENT";
59  
60      // Properties for page titles                             
61      private static final String PROPERTY_PAGE_TITLE_LEVEL_LIST = "portal.features.manage_levels.pageTitle";
62      private static final String PROPERTY_PAGE_TITLE_CREATE_LEVEL = "portal.features.create_level.pageTitle";
63      private static final String PROPERTY_PAGE_TITLE_MODIFY_LEVEL = "portal.features.modify_level.pageTitle";
64  
65      // Markers            
66      private static final String MARK_LEVELS_LIST = "levels_list";
67      private static final String MARK_LEVEL = "level";
68  
69      // Templates files path    
70      private static final String TEMPLATE_MANAGE_LEVELS = "admin/features/manage_levels.html";
71      private static final String TEMPLATE_CREATE_LEVEL = "admin/features/create_level.html";
72      private static final String TEMPLATE_MODIFY_LEVEL = "admin/features/modify_level.html";
73  
74      // Jsp definition
75      private static final String JSP_MANAGE_LEVEL = "ManageLevels.jsp";
76  
77      /**
78       * Returns the list of levels
79       *
80       * @param request The Http request
81       * @return the html code for display the levels list
82       */
83      public String getManageLevels( HttpServletRequest request )
84      {
85          setPageTitleProperty( PROPERTY_PAGE_TITLE_LEVEL_LIST );
86  
87          HashMap<String, Object> model = new HashMap<String, Object>(  );
88          model.put( MARK_LEVELS_LIST, LevelHome.getLevelsList(  ) );
89  
90          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_LEVELS, getLocale(  ), model );
91  
92          return getAdminPage( template.getHtml(  ) );
93      }
94  
95      /**
96       * Returns the level form of creation
97       *
98       * @param request The Http request
99       * @return the html code of the level
100      */
101     public String getCreateLevel( HttpServletRequest request )
102     {
103         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_LEVEL );
104 
105         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_LEVEL, getLocale(  ) );
106 
107         return getAdminPage( template.getHtml(  ) );
108     }
109 
110     /**
111      * Processes the creation form of a new level by recovering the parameters
112      * in the http request
113      *
114      * @param request the http request
115      * @return The Jsp URL of the process result
116      */
117     public String doCreateLevel( HttpServletRequest request )
118     {
119         String strName = request.getParameter( Parameters.LEVEL_NAME );
120 
121         //Mandatory fields
122         if ( strName.equals( "" ) )
123         {
124             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
125         }
126 
127         Level level = new Level(  );
128         level.setName( strName );
129         LevelHome.create( level );
130 
131         // If the process is successfull, redirects towards the theme view
132         return JSP_MANAGE_LEVEL;
133     }
134 
135     /**
136      * Returns the level form of update
137      *
138      * @param request The Http request
139      * @return the html code of the level form
140      */
141     public String getModifyLevel( HttpServletRequest request )
142     {
143         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_LEVEL );
144 
145         String strId = request.getParameter( Parameters.LEVEL_ID );
146 
147         Level level = LevelHome.findByPrimaryKey( Integer.parseInt( strId ) );
148 
149         if ( level == null )
150         {
151             return getManageLevels( request );
152         }
153 
154         HashMap<String, Object> model = new HashMap<String, Object>(  );
155         model.put( MARK_LEVEL, level );
156 
157         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_LEVEL, getLocale(  ), model );
158 
159         return getAdminPage( template.getHtml(  ) );
160     }
161 
162     /**
163      * Processes the updating form of a level whose new parameters are stored in
164      * the http request
165      *
166      * @param request The http request
167      * @return The Jsp URL of the process result
168      */
169     public String doModifyLevel( HttpServletRequest request )
170     {
171         String strId = request.getParameter( Parameters.LEVEL_ID );
172         String strName = request.getParameter( Parameters.LEVEL_NAME );
173 
174         //Mandatory fields
175         if ( strName.equals( "" ) )
176         {
177             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
178         }
179 
180         Level level = LevelHome.findByPrimaryKey( Integer.parseInt( strId ) );
181         level.setName( strName );
182         LevelHome.update( level );
183 
184         // If the process is successfull, redirects towards the level management page
185         return JSP_MANAGE_LEVEL;
186     }
187 }