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.Indicator;
37  import fr.paris.lutece.plugins.indicator.business.IndicatorHome;
38  import fr.paris.lutece.plugins.indicator.service.TimeCodeService;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44  import fr.paris.lutece.util.url.UrlItem;
45  
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class provides the user interface to manage Indicator features ( manage, create, modify, remove )
54   */
55  @Controller( controllerJsp = "ManageIndicators.jsp", controllerPath = "jsp/admin/plugins/indicator/", right = "INDICATOR_MANAGEMENT" )
56  public class IndicatorJspBean extends ManageIndicatorJspBean
57  {
58      ////////////////////////////////////////////////////////////////////////////
59      // Constants
60  
61      // templates
62      private static final String TEMPLATE_MANAGE_INDICATORS = "/admin/plugins/indicator/manage_indicators.html";
63      private static final String TEMPLATE_CREATE_INDICATOR = "/admin/plugins/indicator/create_indicator.html";
64      private static final String TEMPLATE_MODIFY_INDICATOR = "/admin/plugins/indicator/modify_indicator.html";
65  
66      // Parameters
67      private static final String PARAMETER_ID_INDICATOR = "id";
68  
69      // Properties for page titles
70      private static final String PROPERTY_PAGE_TITLE_MANAGE_INDICATORS = "indicator.manage_indicators.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_MODIFY_INDICATOR = "indicator.modify_indicator.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_CREATE_INDICATOR = "indicator.create_indicator.pageTitle";
73  
74      // Markers
75      private static final String MARK_INDICATOR_LIST = "indicator_list";
76      private static final String MARK_INDICATOR = "indicator";
77      private static final String MARK_PERIODS_LIST = "periods_list";
78      private static final String JSP_MANAGE_INDICATORS = "jsp/admin/plugins/indicator/ManageIndicators.jsp";
79  
80      // Properties
81      private static final String MESSAGE_CONFIRM_REMOVE_INDICATOR = "indicator.message.confirmRemoveIndicator";
82      private static final String VALIDATION_ATTRIBUTES_PREFIX = "indicator.model.entity.indicator.attribute.";
83  
84      // Views
85      private static final String VIEW_MANAGE_INDICATORS = "manageIndicators";
86      private static final String VIEW_CREATE_INDICATOR = "createIndicator";
87      private static final String VIEW_MODIFY_INDICATOR = "modifyIndicator";
88  
89      // Actions
90      private static final String ACTION_CREATE_INDICATOR = "createIndicator";
91      private static final String ACTION_MODIFY_INDICATOR = "modifyIndicator";
92      private static final String ACTION_REMOVE_INDICATOR = "removeIndicator";
93      private static final String ACTION_CONFIRM_REMOVE_INDICATOR = "confirmRemoveIndicator";
94  
95      // Infos
96      private static final String INFO_INDICATOR_CREATED = "indicator.info.indicator.created";
97      private static final String INFO_INDICATOR_UPDATED = "indicator.info.indicator.updated";
98      private static final String INFO_INDICATOR_REMOVED = "indicator.info.indicator.removed";
99      private static final long serialVersionUID = 1L;
100 
101     // Session variable to store working values
102     private Indicator _indicator;
103 
104     /**
105      * Build the Manage View
106      * @param request The HTTP request
107      * @return The page
108      */
109     @View( value = VIEW_MANAGE_INDICATORS, defaultView = true )
110     public String getManageIndicators( HttpServletRequest request )
111     {
112         _indicator = null;
113 
114         List<Indicator> listIndicators = (List<Indicator>) IndicatorHome.getIndicatorsList(  );
115         Map<String, Object> model = getPaginatedListModel( request, MARK_INDICATOR_LIST, listIndicators,
116                 JSP_MANAGE_INDICATORS );
117 
118         return getPage( PROPERTY_PAGE_TITLE_MANAGE_INDICATORS, TEMPLATE_MANAGE_INDICATORS, model );
119     }
120 
121     /**
122      * Returns the form to create a indicator
123      *
124      * @param request The Http request
125      * @return the html code of the indicator form
126      */
127     @View( VIEW_CREATE_INDICATOR )
128     public String getCreateIndicator( HttpServletRequest request )
129     {
130         _indicator = ( _indicator != null ) ? _indicator : new Indicator(  );
131 
132         Map<String, Object> model = getModel(  );
133         model.put( MARK_INDICATOR, _indicator );
134         model.put( MARK_PERIODS_LIST, TimeCodeService.getPeriod( getLocale(  ) ) );
135 
136         return getPage( PROPERTY_PAGE_TITLE_CREATE_INDICATOR, TEMPLATE_CREATE_INDICATOR, model );
137     }
138 
139     /**
140      * Process the data capture form of a new indicator
141      *
142      * @param request The Http Request
143      * @return The Jsp URL of the process result
144      */
145     @Action( ACTION_CREATE_INDICATOR )
146     public String doCreateIndicator( HttpServletRequest request )
147     {
148         populate( _indicator, request );
149 
150         // Check constraints
151         if ( !validateBean( _indicator, VALIDATION_ATTRIBUTES_PREFIX ) )
152         {
153             return redirectView( request, VIEW_CREATE_INDICATOR );
154         }
155 
156         IndicatorHome.create( _indicator );
157         addInfo( INFO_INDICATOR_CREATED, getLocale(  ) );
158 
159         return redirectView( request, VIEW_MANAGE_INDICATORS );
160     }
161 
162     /**
163      * Manages the removal form of a indicator whose identifier is in the http
164      * request
165      *
166      * @param request The Http request
167      * @return the html code to confirm
168      */
169     @Action( ACTION_CONFIRM_REMOVE_INDICATOR )
170     public String getConfirmRemoveIndicator( HttpServletRequest request )
171     {
172         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_INDICATOR ) );
173         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_INDICATOR ) );
174         url.addParameter( PARAMETER_ID_INDICATOR, nId );
175 
176         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_INDICATOR,
177                 url.getUrl(  ), AdminMessage.TYPE_CONFIRMATION );
178 
179         return redirect( request, strMessageUrl );
180     }
181 
182     /**
183      * Handles the removal form of a indicator
184      *
185      * @param request The Http request
186      * @return the jsp URL to display the form to manage indicators
187      */
188     @Action( ACTION_REMOVE_INDICATOR )
189     public String doRemoveIndicator( HttpServletRequest request )
190     {
191         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_INDICATOR ) );
192         IndicatorHome.remove( nId );
193         addInfo( INFO_INDICATOR_REMOVED, getLocale(  ) );
194 
195         return redirectView( request, VIEW_MANAGE_INDICATORS );
196     }
197 
198     /**
199      * Returns the form to update info about a indicator
200      *
201      * @param request The Http request
202      * @return The HTML form to update info
203      */
204     @View( VIEW_MODIFY_INDICATOR )
205     public String getModifyIndicator( HttpServletRequest request )
206     {
207         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_INDICATOR ) );
208 
209         if ( ( _indicator == null ) || ( _indicator.getId(  ) != nId ) )
210         {
211             _indicator = IndicatorHome.findByPrimaryKey( nId );
212         }
213 
214         Map<String, Object> model = getModel(  );
215         model.put( MARK_INDICATOR, _indicator );
216         model.put( MARK_PERIODS_LIST, TimeCodeService.getPeriod( getLocale(  ) ) );
217 
218         return getPage( PROPERTY_PAGE_TITLE_MODIFY_INDICATOR, TEMPLATE_MODIFY_INDICATOR, model );
219     }
220 
221     /**
222      * Process the change form of a indicator
223      *
224      * @param request The Http request
225      * @return The Jsp URL of the process result
226      */
227     @Action( ACTION_MODIFY_INDICATOR )
228     public String doModifyIndicator( HttpServletRequest request )
229     {
230         populate( _indicator, request );
231 
232         // Check constraints
233         if ( !validateBean( _indicator, VALIDATION_ATTRIBUTES_PREFIX ) )
234         {
235             return redirect( request, VIEW_MODIFY_INDICATOR, PARAMETER_ID_INDICATOR, _indicator.getId(  ) );
236         }
237 
238         IndicatorHome.update( _indicator );
239         addInfo( INFO_INDICATOR_UPDATED, getLocale(  ) );
240 
241         return redirectView( request, VIEW_MANAGE_INDICATORS );
242     }
243 }