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.indicator.web;
35  
36  import fr.paris.lutece.plugins.indicator.business.IndicatorHistory;
37  import fr.paris.lutece.plugins.indicator.business.IndicatorHistoryHome;
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
41  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
43  import fr.paris.lutece.util.url.UrlItem;
44  
45  import java.util.List;
46  import java.util.Map;
47  import java.util.concurrent.ConcurrentHashMap;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class provides the user interface to manage IndicatorHistory features ( manage, create, modify, remove )
54   */
55  @Controller( controllerJsp = "ManageHistory.jsp", controllerPath = "jsp/admin/plugins/indicator/", right = "INDICATOR_MANAGEMENT" )
56  public class IndicatorHistoryJspBean extends ManageIndicatorJspBean
57  {
58      ////////////////////////////////////////////////////////////////////////////
59      // Constants
60  
61      // templates
62      private static final String TEMPLATE_MANAGE_HISTORY = "/admin/plugins/indicator/manage_history.html";
63      private static final String TEMPLATE_CREATE_HISTORY = "/admin/plugins/indicator/create_history.html";
64      private static final String TEMPLATE_MODIFY_HISTORY = "/admin/plugins/indicator/modify_history.html";
65  
66      // Parameters
67      private static final String PARAMETER_KEY = "key";
68      private static final String PARAMETER_TIMECODE = "timecode";
69  
70      // Properties for page titles
71      private static final String PROPERTY_PAGE_TITLE_MANAGE_HISTORY = "indicator.manage_history.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_MODIFY_HISTORY = "indicator.modify_history.pageTitle";
73      private static final String PROPERTY_PAGE_TITLE_CREATE_HISTORY = "indicator.create_history.pageTitle";
74  
75      // Markers
76      private static final String MARK_INDICATORHISTORY_LIST = "history_list";
77      private static final String MARK_INDICATORHISTORY = "history";
78      private static final String MARK_KEY = "key";
79      private static final String JSP_MANAGE_HISTORY = "jsp/admin/plugins/indicator/ManageHistory.jsp";
80  
81      // Properties
82      private static final String MESSAGE_CONFIRM_REMOVE_HISTORY = "indicator.message.confirmRemoveIndicatorHistory";
83      private static final String VALIDATION_ATTRIBUTES_PREFIX = "indicator.model.entity.history.attribute.";
84  
85      // Views
86      private static final String VIEW_MANAGE_HISTORY = "manageIndicatorHistorys";
87      private static final String VIEW_CREATE_HISTORY = "createIndicatorHistory";
88      private static final String VIEW_MODIFY_HISTORY = "modifyIndicatorHistory";
89  
90      // Actions
91      private static final String ACTION_CREATE_HISTORY = "createIndicatorHistory";
92      private static final String ACTION_MODIFY_HISTORY = "modifyIndicatorHistory";
93      private static final String ACTION_REMOVE_HISTORY = "removeIndicatorHistory";
94      private static final String ACTION_CONFIRM_REMOVE_HISTORY = "confirmRemoveIndicatorHistory";
95  
96      // Infos
97      private static final String INFO_HISTORY_CREATED = "indicator.info.history.created";
98      private static final String INFO_HISTORY_UPDATED = "indicator.info.history.updated";
99      private static final String INFO_HISTORY_REMOVED = "indicator.info.history.removed";
100     private static final long serialVersionUID = 1L;
101 
102     // Session variable to store working values
103     private IndicatorHistory _history;
104 
105     /**
106      * Build the Manage View
107      * @param request The HTTP request
108      * @return The page
109      */
110     @View( value = VIEW_MANAGE_HISTORY, defaultView = true )
111     public String getManageHistory( HttpServletRequest request )
112     {
113         _history = null;
114 
115         String strKey = request.getParameter( PARAMETER_KEY );
116         List<IndicatorHistory> listIndicatorHistorys = (List<IndicatorHistory>) IndicatorHistoryHome.getIndicatorHistorysList( strKey );
117         Map<String, Object> model = getPaginatedListModel( request, MARK_INDICATORHISTORY_LIST, listIndicatorHistorys,
118                 JSP_MANAGE_HISTORY );
119         model.put( MARK_KEY, strKey );
120 
121         return getPage( PROPERTY_PAGE_TITLE_MANAGE_HISTORY, TEMPLATE_MANAGE_HISTORY, model );
122     }
123 
124     /**
125      * Returns the form to create a history
126      *
127      * @param request The Http request
128      * @return the html code of the history form
129      */
130     @View( VIEW_CREATE_HISTORY )
131     public String getCreateIndicatorHistory( HttpServletRequest request )
132     {
133         String strKey = request.getParameter( PARAMETER_KEY );
134         _history = ( _history != null ) ? _history : new IndicatorHistory(  );
135         _history.setIndKey( strKey );
136 
137         Map<String, Object> model = getModel(  );
138         model.put( MARK_INDICATORHISTORY, _history );
139 
140         return getPage( PROPERTY_PAGE_TITLE_CREATE_HISTORY, TEMPLATE_CREATE_HISTORY, model );
141     }
142 
143     /**
144      * Process the data capture form of a new history
145      *
146      * @param request The Http Request
147      * @return The Jsp URL of the process result
148      */
149     @Action( ACTION_CREATE_HISTORY )
150     public String doCreateIndicatorHistory( HttpServletRequest request )
151     {
152         populate( _history, request );
153 
154         // Check constraints
155         if ( !validateBean( _history, VALIDATION_ATTRIBUTES_PREFIX ) )
156         {
157             Map<String, String> mapParameters = new ConcurrentHashMap<String, String>(  );
158             mapParameters.put( PARAMETER_KEY, _history.getIndKey(  ) );
159 
160             return redirect( request, VIEW_CREATE_HISTORY, mapParameters );
161         }
162 
163         try
164         {
165             IndicatorHistoryHome.create( _history );
166             addInfo( INFO_HISTORY_CREATED, getLocale(  ) );
167 
168             Map<String, String> mapParameters = new ConcurrentHashMap<String, String>(  );
169             mapParameters.put( PARAMETER_KEY, _history.getIndKey(  ) );
170 
171             return redirect( request, VIEW_MANAGE_HISTORY, mapParameters );
172         }
173         catch ( Exception e )
174         {
175             if ( e.getCause(  ) != null )
176             {
177                 addError( e.getCause(  ).getMessage(  ) );
178             }
179             else
180             {
181                 addError( e.getMessage(  ) );
182             }
183 
184             Map<String, String> mapParameters = new ConcurrentHashMap<String, String>(  );
185             mapParameters.put( PARAMETER_KEY, _history.getIndKey(  ) );
186 
187             return redirect( request, VIEW_CREATE_HISTORY, mapParameters );
188         }
189     }
190 
191     /**
192      * Manages the removal form of a history whose identifier is in the http
193      * request
194      *
195      * @param request The Http request
196      * @return the html code to confirm
197      */
198     @Action( ACTION_CONFIRM_REMOVE_HISTORY )
199     public String getConfirmRemoveIndicatorHistory( HttpServletRequest request )
200     {
201         String strKey = request.getParameter( PARAMETER_KEY );
202         String strTimecode = request.getParameter( PARAMETER_TIMECODE );
203         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_HISTORY ) );
204         url.addParameter( PARAMETER_KEY, strKey );
205         url.addParameter( PARAMETER_TIMECODE, strTimecode );
206 
207         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_HISTORY,
208                 url.getUrl(  ), AdminMessage.TYPE_CONFIRMATION );
209 
210         return redirect( request, strMessageUrl );
211     }
212 
213     /**
214      * Handles the removal form of a history
215      *
216      * @param request The Http request
217      * @return the jsp URL to display the form to manage history
218      */
219     @Action( ACTION_REMOVE_HISTORY )
220     public String doRemoveIndicatorHistory( HttpServletRequest request )
221     {
222         String strKey = request.getParameter( PARAMETER_KEY );
223         String strTimecode = request.getParameter( PARAMETER_TIMECODE );
224         IndicatorHistoryHome.remove( strKey, strTimecode );
225         addInfo( INFO_HISTORY_REMOVED, getLocale(  ) );
226 
227         Map<String, String> mapParameters = new ConcurrentHashMap<String, String>(  );
228         mapParameters.put( PARAMETER_KEY, strKey );
229 
230         return redirect( request, VIEW_MANAGE_HISTORY, mapParameters );
231     }
232 
233     /**
234      * Returns the form to update info about a history
235      *
236      * @param request The Http request
237      * @return The HTML form to update info
238      */
239     @View( VIEW_MODIFY_HISTORY )
240     public String getModifyIndicatorHistory( HttpServletRequest request )
241     {
242         String strKey = request.getParameter( PARAMETER_KEY );
243         String strTimecode = request.getParameter( PARAMETER_TIMECODE );
244 
245         if ( ( _history == null ) || ( !_history.getIndKey(  ).equals( strKey ) ) ||
246                 ( _history.getTimeCode(  ).equals( strTimecode ) ) )
247         {
248             _history = IndicatorHistoryHome.findByPrimaryKey( strKey, strTimecode );
249         }
250 
251         Map<String, Object> model = getModel(  );
252         model.put( MARK_INDICATORHISTORY, _history );
253 
254         return getPage( PROPERTY_PAGE_TITLE_MODIFY_HISTORY, TEMPLATE_MODIFY_HISTORY, model );
255     }
256 
257     /**
258      * Process the change form of a history
259      *
260      * @param request The Http request
261      * @return The Jsp URL of the process result
262      */
263     @Action( ACTION_MODIFY_HISTORY )
264     public String doModifyIndicatorHistory( HttpServletRequest request )
265     {
266         populate( _history, request );
267 
268         // Check constraints
269         if ( !validateBean( _history, VALIDATION_ATTRIBUTES_PREFIX ) )
270         {
271             Map<String, String> mapParameters = new ConcurrentHashMap<String, String>(  );
272             mapParameters.put( PARAMETER_KEY, _history.getIndKey(  ) );
273             mapParameters.put( PARAMETER_TIMECODE, _history.getTimeCode(  ) );
274 
275             return redirect( request, VIEW_MODIFY_HISTORY, mapParameters );
276         }
277 
278         IndicatorHistoryHome.update( _history );
279         addInfo( INFO_HISTORY_UPDATED, getLocale(  ) );
280 
281         Map<String, String> mapParameters = new ConcurrentHashMap<String, String>(  );
282         mapParameters.put( PARAMETER_KEY, _history.getIndKey(  ) );
283 
284         return redirect( request, VIEW_MANAGE_HISTORY, mapParameters );
285     }
286 }