View Javadoc
1   /*
2    * Copyright (c) 2002-2015, 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.exportuserpreferences.web;
35  
36  import fr.paris.lutece.plugins.exportuserpreferences.business.Key;
37  import fr.paris.lutece.plugins.exportuserpreferences.business.KeyHome;
38  import fr.paris.lutece.plugins.exportuserpreferences.utils.CsvUtils;
39  import fr.paris.lutece.plugins.exportuserpreferences.utils.export.UserPreferencesExportUtils;
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.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
46  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
47  import fr.paris.lutece.util.url.UrlItem;
48  
49  import java.io.IOException;
50  import java.io.OutputStream;
51  
52  import java.util.ArrayList;
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  
60  
61  /**
62   * This class provides the user interface to manage Key features ( manage, create, modify, remove )
63   */
64  @Controller( controllerJsp = "ManageKeys.jsp", controllerPath = "jsp/admin/plugins/exportuserpreferences/", right = "EXPORTUSERPREFERENCES_MANAGEMENT" )
65  public class KeyJspBean extends ManageExportuserpreferencesJspBean
66  {
67      ////////////////////////////////////////////////////////////////////////////
68      // Constants
69  
70      // templates
71      private static final String TEMPLATE_MANAGE_KEYS = "/admin/plugins/exportuserpreferences/manage_keys.html";
72      private static final String TEMPLATE_CREATE_KEY = "/admin/plugins/exportuserpreferences/create_key.html";
73      private static final String TEMPLATE_MODIFY_KEY = "/admin/plugins/exportuserpreferences/modify_key.html";
74  
75      // Parameters
76      private static final String PARAMETER_PREF_KEY = "pref_key";
77  
78      // Properties for page titles
79      private static final String PROPERTY_PAGE_TITLE_MANAGE_KEYS = "exportuserpreferences.manage_keys.pageTitle";
80      private static final String PROPERTY_PAGE_TITLE_MODIFY_KEY = "exportuserpreferences.modify_key.pageTitle";
81      private static final String PROPERTY_PAGE_TITLE_CREATE_KEY = "exportuserpreferences.create_key.pageTitle";
82  
83      // Markers
84      private static final String MARK_KEY_LIST = "key_list";
85      private static final String MARK_KEY = "key";
86      private static final String MARK_LIST_AVAILABLE_KEYS = "list_available_keys";
87      private static final String JSP_MANAGE_KEYS = "jsp/admin/plugins/exportuserpreferences/ManageKeys.jsp";
88  
89      // Properties
90      private static final String MESSAGE_CONFIRM_REMOVE_KEY = "exportuserpreferences.message.confirmRemoveKey";
91      private static final String PROPERTY_DEFAULT_LIST_KEY_PER_PAGE = "exportuserpreferences.listKeys.itemsPerPage";
92      private static final String VALIDATION_ATTRIBUTES_PREFIX = "exportuserpreferences.model.entity.key.attribute.";
93  
94      // Views
95      private static final String VIEW_MANAGE_KEYS = "manageKeys";
96      private static final String VIEW_CREATE_KEY = "createKey";
97      private static final String VIEW_MODIFY_KEY = "modifyKey";
98  
99      // Actions
100     private static final String ACTION_CREATE_KEY = "createKey";
101     private static final String ACTION_MODIFY_KEY = "modifyKey";
102     private static final String ACTION_REMOVE_KEY = "removeKey";
103     private static final String ACTION_CONFIRM_REMOVE_KEY = "confirmRemoveKey";
104 
105     // Infos
106     private static final String INFO_KEY_CREATED = "exportuserpreferences.info.key.created";
107     private static final String INFO_KEY_UPDATED = "exportuserpreferences.info.key.updated";
108     private static final String INFO_KEY_REMOVED = "exportuserpreferences.info.key.removed";
109     private static final String PROPERTY_CSV_EXTENSION = "exportuserpreferences.csv.extension";
110     private static final String PROPERTY_CSV_FILE_NAME = "exportuserpreferences.csv.file.name";
111     public static final String MARK_USERPREFERENCES = "USERPREFERENCES";
112 
113     // Session variable to store working values
114     private Key _key;
115 
116     @View( value = VIEW_MANAGE_KEYS, defaultView = true )
117     public String getManageKeys( HttpServletRequest request )
118     {
119         _key = null;
120 
121         List<Key> listKeys = (List<Key>) KeyHome.getKeysList(  );
122         Map<String, Object> model = getPaginatedListModel( request, MARK_KEY_LIST, listKeys, JSP_MANAGE_KEYS );
123 
124         return getPage( PROPERTY_PAGE_TITLE_MANAGE_KEYS, TEMPLATE_MANAGE_KEYS, model );
125     }
126 
127     /**
128      * Returns the form to create a key
129      *
130      * @param request The Http request
131      * @return the html code of the key form
132      */
133     @View( VIEW_CREATE_KEY )
134     public String getCreateKey( HttpServletRequest request )
135     {
136         _key = ( _key != null ) ? _key : new Key(  );
137 
138         Map<String, Object> model = getModel(  );
139         model.put( MARK_KEY, _key );
140 
141         List<String> listAvailableKeys = (List<String>) KeyHome.getAvailableKeysList(  );
142         model.put( MARK_LIST_AVAILABLE_KEYS, listAvailableKeys );
143 
144         return getPage( PROPERTY_PAGE_TITLE_CREATE_KEY, TEMPLATE_CREATE_KEY, model );
145     }
146 
147     /**
148      * Process the data capture form of a new key
149      *
150      * @param request The Http Request
151      * @return The Jsp URL of the process result
152      */
153     @Action( ACTION_CREATE_KEY )
154     public String doCreateKey( HttpServletRequest request )
155     {
156         populate( _key, request );
157 
158         // Check constraints
159         if ( !validateBean( _key, VALIDATION_ATTRIBUTES_PREFIX ) )
160         {
161             return redirectView( request, VIEW_CREATE_KEY );
162         }
163 
164         KeyHome.create( _key );
165         addInfo( INFO_KEY_CREATED, getLocale(  ) );
166 
167         return redirectView( request, VIEW_MANAGE_KEYS );
168     }
169 
170     /**
171      * Manages the removal form of a key whose identifier is in the http
172      * request
173      *
174      * @param request The Http request
175      * @return the html code to confirm
176      */
177     @Action( ACTION_CONFIRM_REMOVE_KEY )
178     public String getConfirmRemoveKey( HttpServletRequest request )
179     {
180         String strPrefKey = String.valueOf( request.getParameter( PARAMETER_PREF_KEY ) );
181         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_KEY ) );
182         url.addParameter( PARAMETER_PREF_KEY, strPrefKey );
183 
184         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_KEY, url.getUrl(  ),
185                 AdminMessage.TYPE_CONFIRMATION );
186 
187         return redirect( request, strMessageUrl );
188     }
189 
190     /**
191      * Handles the removal form of a key
192      *
193      * @param request The Http request
194      * @return the jsp URL to display the form to manage keys
195      */
196     @Action( ACTION_REMOVE_KEY )
197     public String doRemoveKey( HttpServletRequest request )
198     {
199         String strPrefKey = String.valueOf( request.getParameter( PARAMETER_PREF_KEY ) );
200         KeyHome.remove( strPrefKey );
201         addInfo( INFO_KEY_REMOVED, getLocale(  ) );
202 
203         return redirectView( request, VIEW_MANAGE_KEYS );
204     }
205 
206     /**
207      * Returns the form to update info about a key
208      *
209      * @param request The Http request
210      * @return The HTML form to update info
211      */
212     @View( VIEW_MODIFY_KEY )
213     public String getModifyKey( HttpServletRequest request )
214     {
215         String strPrefKey = String.valueOf( request.getParameter( PARAMETER_PREF_KEY ) );
216 
217         if ( ( _key == null ) || ( _key.getPrefKey(  ) != strPrefKey ) )
218         {
219             _key = KeyHome.findByPrimaryKey( strPrefKey );
220         }
221 
222         Map<String, Object> model = getModel(  );
223         model.put( MARK_KEY, _key );
224 
225         return getPage( PROPERTY_PAGE_TITLE_MODIFY_KEY, TEMPLATE_MODIFY_KEY, model );
226     }
227 
228     /**
229      * Process the change form of a key
230      *
231      * @param request The Http request
232      * @return The Jsp URL of the process result
233      */
234     @Action( ACTION_MODIFY_KEY )
235     public String doModifyKey( HttpServletRequest request )
236     {
237         populate( _key, request );
238 
239         // Check constraints
240         if ( !validateBean( _key, VALIDATION_ATTRIBUTES_PREFIX ) )
241         {
242             Map<String, String> mapParameters = new HashMap<String, String>(  );
243             mapParameters.put( PARAMETER_PREF_KEY, _key.getPrefKey(  ) );
244 
245             return redirect( request, VIEW_MODIFY_KEY, mapParameters );
246         }
247 
248         KeyHome.update( _key );
249         addInfo( INFO_KEY_UPDATED, getLocale(  ) );
250 
251         return redirectView( request, VIEW_MANAGE_KEYS );
252     }
253 
254     /**
255      * Export the values from core_user_preferences into csv file
256      * @param request The Http request
257      * @param response The Http response
258      */
259     public String doExportCSV( HttpServletRequest request, HttpServletResponse response )
260     {
261         try
262         {
263             Map<String, ArrayList<String>> listValues = (Map<String, ArrayList<String>>) KeyHome.getValuesList(  );
264             //Génère le CSV
265             String strFormatExtension = AppPropertiesService.getProperty( PROPERTY_CSV_EXTENSION );
266             String strFileName = AppPropertiesService.getProperty( PROPERTY_CSV_FILE_NAME ) + "." + strFormatExtension;
267             UserPreferencesExportUtils.addHeaderResponse( request, response, strFileName, strFormatExtension );
268 
269             OutputStream os = response.getOutputStream(  );
270 
271             //say how to decode the csv file, with utf8
272             byte[] bom = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; // BOM values
273             os.write( bom ); // adds BOM 
274             CsvUtils.ecrireCsv( MARK_USERPREFERENCES, listValues, os, getLocale(  ) );
275 
276             os.flush(  );
277             os.close(  );
278             
279         }
280         catch ( IOException e )
281         {
282             AppLogService.error( e );
283         }
284         catch ( Exception e )
285         {
286             return AdminMessageService.getMessageUrl( request, UserPreferencesExportUtils.ERROR_MISSING_HEADER, AdminMessage.TYPE_ERROR );
287         }
288         
289         return redirectView( request, VIEW_MANAGE_KEYS );
290     }
291 }