View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.pluginwizard.web;
35  
36  import fr.paris.lutece.plugins.pluginwizard.business.ConfigurationKey;
37  import fr.paris.lutece.plugins.pluginwizard.business.ConfigurationKeyHome;
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.service.util.AppPropertiesService;
42  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
43  import fr.paris.lutece.portal.web.constants.Messages;
44  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  import fr.paris.lutece.util.html.Paginator;
47  import fr.paris.lutece.util.url.UrlItem;
48  
49  import java.util.Collection;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   * This class provides the user interface to manage ConfigurationKey features ( manage, create, modify, remove )
58   */
59  public class PluginwizardJspBean extends PluginAdminPageJspBean
60  {
61      // //////////////////////////////////////////////////////////////////////////
62      // Constants
63  
64      /**
65       * 
66       */
67      private static final long serialVersionUID = -4166510254090826796L;
68  
69      // Right
70      /**
71       *
72       */
73      public static final String RIGHT_MANAGE_PLUGINWIZARD = "PLUGINWIZARD_MANAGEMENT";
74  
75      // Parameters
76      private static final String PARAMETER_PAGE_INDEX = "page_index";
77      private static final String PARAMETER_ID_KEY = "configurationkey_id_key";
78      private static final String PARAMETER_KEY_DESCRIPTION = "configurationkey_key_description";
79      private static final String PARAMETER_KEY_VALUE = "configurationkey_key_value";
80  
81      // templates
82      private static final String TEMPLATE_MANAGE_CONFIGURATIONKEYS = "/admin/plugins/pluginwizard/manage_configurationkey.html";
83      private static final String TEMPLATE_CREATE_CONFIGURATIONKEY = "/admin/plugins/pluginwizard/create_configurationkey.html";
84      private static final String TEMPLATE_MODIFY_CONFIGURATIONKEY = "/admin/plugins/pluginwizard/modify_configurationkey.html";
85  
86      // Properties for page titles
87      private static final String PROPERTY_PAGE_TITLE_MANAGE_CONFIGURATIONKEYS = "pluginwizard.manage_configurationkeys.pageTitle";
88      private static final String PROPERTY_PAGE_TITLE_MODIFY_CONFIGURATIONKEY = "pluginwizard.modify_configurationkey.pageTitle";
89      private static final String PROPERTY_PAGE_TITLE_CREATE_CONFIGURATIONKEY = "pluginwizard.create_configurationkey.pageTitle";
90  
91      // Markers
92      private static final String MARK_CONFIGURATIONKEY_LIST = "configurationkey_list";
93      private static final String MARK_CONFIGURATIONKEY = "configurationkey";
94      private static final String MARK_PAGINATOR = "paginator";
95      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
96  
97      // Jsp Definition
98      private static final String JSP_DO_REMOVE_CONFIGURATIONKEY = "jsp/admin/plugins/pluginwizard/DoRemoveConfigurationKey.jsp";
99      private static final String JSP_MANAGE_CONFIGURATIONKEYS = "jsp/admin/plugins/pluginwizard/ManageConfigurationKeys.jsp";
100     private static final String JSP_REDIRECT_TO_MANAGE_CONFIGURATIONKEYS = "ManageConfigurationKeys.jsp";
101 
102     // Properties
103     private static final String PROPERTY_DEFAULT_LIST_CONFIGURATIONKEY_PER_PAGE = "pluginwizard.listConfigurationKeys.itemsPerPage";
104 
105     // Messages
106     private static final String MESSAGE_CONFIRM_REMOVE_CONFIGURATIONKEY = "pluginwizard.message.confirmRemoveConfigurationKey";
107 
108     // Variables
109     private int _nDefaultItemsPerPage;
110     private String _strCurrentPageIndex;
111     private int _nItemsPerPage;
112 
113     /**
114      * Returns the list of configurationkey
115      *
116      * @param request
117      *            The Http request
118      * @return the configurationkeys list
119      */
120     public String getManageConfigurationKeys( HttpServletRequest request )
121     {
122         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_CONFIGURATIONKEYS );
123 
124         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
125         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_CONFIGURATIONKEY_PER_PAGE, 5 );
126         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
127 
128         UrlItem url = new UrlItem( JSP_MANAGE_CONFIGURATIONKEYS );
129         String strUrl = url.getUrl( );
130         Collection<ConfigurationKey> listCONFIGURATIONKEYs = ConfigurationKeyHome.getConfigurationKeysList( );
131         LocalizedPaginator paginator = new LocalizedPaginator( (List<ConfigurationKey>) listCONFIGURATIONKEYs, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX,
132                 _strCurrentPageIndex, getLocale( ) );
133 
134         Map<String, Object> model = new HashMap<String, Object>( );
135 
136         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
137         model.put( MARK_PAGINATOR, paginator );
138         model.put( MARK_CONFIGURATIONKEY_LIST, paginator.getPageItems( ) );
139 
140         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CONFIGURATIONKEYS, getLocale( ), model );
141 
142         return getAdminPage( templateList.getHtml( ) );
143     }
144 
145     /**
146      * Returns the form to create a configurationkey
147      *
148      * @param request
149      *            The Http request
150      * @return the html code of the configurationkey form
151      */
152     public String getCreateConfigurationKey( HttpServletRequest request )
153     {
154         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_CONFIGURATIONKEY );
155 
156         Map<String, Object> model = new HashMap<String, Object>( );
157 
158         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CONFIGURATIONKEY, getLocale( ), model );
159 
160         return getAdminPage( template.getHtml( ) );
161     }
162 
163     /**
164      * Process the data capture form of a new configurationkey
165      *
166      * @param request
167      *            The Http Request
168      * @return The Jsp URL of the process result
169      */
170     public String doCreateConfigurationKey( HttpServletRequest request )
171     {
172         ConfigurationKeyess/ConfigurationKey.html#ConfigurationKey">ConfigurationKey configurationkey = new ConfigurationKey( );
173         String strKeyDescription = request.getParameter( PARAMETER_KEY_DESCRIPTION );
174         String strKeyValue = request.getParameter( PARAMETER_KEY_VALUE );
175 
176         if ( strKeyDescription.equals( "" ) )
177         {
178             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
179         }
180 
181         if ( strKeyValue.equals( "" ) )
182         {
183             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
184         }
185 
186         configurationkey.setKeyDescription( strKeyDescription );
187         configurationkey.setKeyValue( strKeyValue );
188 
189         ConfigurationKeyHome.create( configurationkey );
190 
191         return JSP_REDIRECT_TO_MANAGE_CONFIGURATIONKEYS;
192     }
193 
194     /**
195      * Manages the removal form of a configurationkey whose identifier is in the http request
196      *
197      * @param request
198      *            The Http request
199      * @return the html code to confirm
200      */
201     public String getConfirmRemoveConfigurationKey( HttpServletRequest request )
202     {
203         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_KEY ) );
204         UrlItem url = new UrlItem( JSP_DO_REMOVE_CONFIGURATIONKEY );
205         url.addParameter( PARAMETER_ID_KEY, nId );
206 
207         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CONFIGURATIONKEY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
208     }
209 
210     /**
211      * Handles the removal form of a configurationkey
212      *
213      * @param request
214      *            The Http request
215      * @return the jsp URL to display the form to manage configurationkeys
216      */
217     public String doRemoveConfigurationKey( HttpServletRequest request )
218     {
219         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_KEY ) );
220         ConfigurationKeyHome.remove( nId );
221 
222         return JSP_REDIRECT_TO_MANAGE_CONFIGURATIONKEYS;
223     }
224 
225     /**
226      * Returns the form to update info about a configurationkey
227      *
228      * @param request
229      *            The Http request
230      * @return The HTML form to update info
231      */
232     public String getModifyConfigurationKey( HttpServletRequest request )
233     {
234         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_CONFIGURATIONKEY );
235 
236         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_KEY ) );
237         ConfigurationKey configurationkey = ConfigurationKeyHome.findByPrimaryKey( nId );
238 
239         Map<String, Object> model = new HashMap<String, Object>( );
240         model.put( MARK_CONFIGURATIONKEY, configurationkey );
241 
242         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CONFIGURATIONKEY, getLocale( ), model );
243 
244         return getAdminPage( template.getHtml( ) );
245     }
246 
247     /**
248      * Process the change form of a configurationkey
249      *
250      * @param request
251      *            The Http request
252      * @return The Jsp URL of the process result
253      */
254     public String doModifyConfigurationKey( HttpServletRequest request )
255     {
256         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_KEY ) );
257         String strKeyDescription = request.getParameter( PARAMETER_KEY_DESCRIPTION );
258         String strValue = request.getParameter( PARAMETER_KEY_VALUE );
259         ConfigurationKey configurationkey = ConfigurationKeyHome.findByPrimaryKey( nId );
260 
261         if ( ( nId == 0 ) || ( strValue == null ) || ( strKeyDescription == null ) || strValue.equals( "" ) || strKeyDescription.equals( "" ) )
262         {
263             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
264         }
265 
266         configurationkey.setIdKey( nId );
267         configurationkey.setKeyDescription( strKeyDescription );
268         configurationkey.setKeyValue( strValue );
269         ConfigurationKeyHome.update( configurationkey );
270 
271         return JSP_REDIRECT_TO_MANAGE_CONFIGURATIONKEYS;
272     }
273 }