View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.campagnebp.web;
35  
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.plugins.campagnebp.business.CampagnePhase;
42  import fr.paris.lutece.plugins.campagnebp.business.CampagnePhaseHome;
43  import fr.paris.lutece.plugins.campagnebp.service.CampagnesService;
44  import fr.paris.lutece.portal.service.message.AdminMessage;
45  import fr.paris.lutece.portal.service.message.AdminMessageService;
46  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
47  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
48  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
49  import fr.paris.lutece.util.url.UrlItem;
50  
51  /**
52   * This class provides the user interface to manage CampagnePhase features ( manage, create, modify, remove )
53   */
54  @Controller( controllerJsp = "ManageCampagnePhases.jsp", controllerPath = "jsp/admin/plugins/ideation/", right = "CAMPAGNEBP_MANAGEMENT" )
55  public class CampagnePhaseJspBean extends ManageCampagnebpJspBean
56  {
57  
58      ////////////////////////////////////////////////////////////////////////////
59      // Constants
60  
61      // templates
62      private static final String TEMPLATE_MANAGE_CAMPAGNEPHASES = "/admin/plugins/campagnebp/manage_campagnephases.html";
63      private static final String TEMPLATE_CREATE_CAMPAGNEPHASE = "/admin/plugins/campagnebp/create_campagnephase.html";
64      private static final String TEMPLATE_MODIFY_CAMPAGNEPHASE = "/admin/plugins/campagnebp/modify_campagnephase.html";
65  
66      // Parameters
67      private static final String PARAMETER_ID_CAMPAGNEPHASE = "id";
68  
69      // Properties for page titles
70      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNEPHASES = "campagnebp.manage_campagnephases.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNEPHASE = "campagnebp.modify_campagnephase.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAGNEPHASE = "campagnebp.create_campagnephase.pageTitle";
73  
74      // Markers
75      private static final String MARK_CAMPAGNEPHASE_LIST = "campagnephase_list";
76      private static final String MARK_CAMPAGNEPHASE = "campagnephase";
77  
78      private static final String JSP_MANAGE_CAMPAGNEPHASES = "jsp/admin/plugins/campagnebp/ManageCampagnePhases.jsp";
79  
80      // Properties
81      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAGNEPHASE = "campagnebp.message.confirmRemoveCampagnePhase";
82      private static final String PROPERTY_DEFAULT_LIST_CAMPAGNEPHASE_PER_PAGE = "campagnebp.listCampagnePhases.itemsPerPage";
83  
84      private static final String VALIDATION_ATTRIBUTES_PREFIX = "campagnebp.model.entity.campagnephase.attribute.";
85  
86      // Views
87      private static final String VIEW_MANAGE_CAMPAGNEPHASES = "manageCampagnePhases";
88      private static final String VIEW_CREATE_CAMPAGNEPHASE = "createCampagnePhase";
89      private static final String VIEW_MODIFY_CAMPAGNEPHASE = "modifyCampagnePhase";
90  
91      // Actions
92      private static final String ACTION_CREATE_CAMPAGNEPHASE = "createCampagnePhase";
93      private static final String ACTION_MODIFY_CAMPAGNEPHASE = "modifyCampagnePhase";
94      private static final String ACTION_REMOVE_CAMPAGNEPHASE = "removeCampagnePhase";
95      private static final String ACTION_CONFIRM_REMOVE_CAMPAGNEPHASE = "confirmRemoveCampagnePhase";
96  
97      // Infos
98      private static final String INFO_CAMPAGNEPHASE_CREATED = "campagnebp.info.campagnephase.created";
99      private static final String INFO_CAMPAGNEPHASE_UPDATED = "campagnebp.info.campagnephase.updated";
100     private static final String INFO_CAMPAGNEPHASE_REMOVED = "campagnebp.info.campagnephase.removed";
101 
102     // Session variable to store working values
103     private CampagnePhase _campagnephase;
104 
105     /**
106      * Build the Manage View
107      * 
108      * @param request
109      *            The HTTP request
110      * @return The page
111      */
112     @View( value = VIEW_MANAGE_CAMPAGNEPHASES, defaultView = true )
113     public String getManageCampagnePhases( HttpServletRequest request )
114     {
115         _campagnephase = null;
116         List<CampagnePhase> listCampagnePhases = (List<CampagnePhase>) CampagnePhaseHome.getCampagnePhasesList( );
117         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAGNEPHASE_LIST, listCampagnePhases, JSP_MANAGE_CAMPAGNEPHASES );
118 
119         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNEPHASES, TEMPLATE_MANAGE_CAMPAGNEPHASES, model );
120     }
121 
122     /**
123      * Returns the form to create a campagnephase
124      *
125      * @param request
126      *            The Http request
127      * @return the html code of the campagnephase form
128      */
129     @View( VIEW_CREATE_CAMPAGNEPHASE )
130     public String getCreateCampagnePhase( HttpServletRequest request )
131     {
132         _campagnephase = ( _campagnephase != null ) ? _campagnephase : new CampagnePhase( );
133 
134         Map<String, Object> model = getModel( );
135         model.put( MARK_CAMPAGNEPHASE, _campagnephase );
136 
137         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAGNEPHASE, TEMPLATE_CREATE_CAMPAGNEPHASE, model );
138     }
139 
140     /**
141      * Process the data capture form of a new campagnephase
142      *
143      * @param request
144      *            The Http Request
145      * @return The Jsp URL of the process result
146      */
147     @Action( ACTION_CREATE_CAMPAGNEPHASE )
148     public String doCreateCampagnePhase( HttpServletRequest request )
149     {
150         populate( _campagnephase, request );
151 
152         // Check constraints
153         if ( !validateBean( _campagnephase, VALIDATION_ATTRIBUTES_PREFIX ) )
154         {
155             return redirectView( request, VIEW_CREATE_CAMPAGNEPHASE );
156         }
157 
158         CampagnePhaseHome.create( _campagnephase );
159         addInfo( INFO_CAMPAGNEPHASE_CREATED, getLocale( ) );
160 
161         CampagnesService.getInstance( ).reset( );
162 
163         return redirectView( request, VIEW_MANAGE_CAMPAGNEPHASES );
164     }
165 
166     /**
167      * Manages the removal form of a campagnephase whose identifier is in the http request
168      *
169      * @param request
170      *            The Http request
171      * @return the html code to confirm
172      */
173     @Action( ACTION_CONFIRM_REMOVE_CAMPAGNEPHASE )
174     public String getConfirmRemoveCampagnePhase( HttpServletRequest request )
175     {
176         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNEPHASE ) );
177         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAGNEPHASE ) );
178         url.addParameter( PARAMETER_ID_CAMPAGNEPHASE, nId );
179 
180         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAGNEPHASE, url.getUrl( ),
181                 AdminMessage.TYPE_CONFIRMATION );
182 
183         return redirect( request, strMessageUrl );
184     }
185 
186     /**
187      * Handles the removal form of a campagnephase
188      *
189      * @param request
190      *            The Http request
191      * @return the jsp URL to display the form to manage campagnephases
192      */
193     @Action( ACTION_REMOVE_CAMPAGNEPHASE )
194     public String doRemoveCampagnePhase( HttpServletRequest request )
195     {
196         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNEPHASE ) );
197         CampagnePhaseHome.remove( nId );
198         addInfo( INFO_CAMPAGNEPHASE_REMOVED, getLocale( ) );
199 
200         CampagnesService.getInstance( ).reset( );
201 
202         return redirectView( request, VIEW_MANAGE_CAMPAGNEPHASES );
203     }
204 
205     /**
206      * Returns the form to update info about a campagnephase
207      *
208      * @param request
209      *            The Http request
210      * @return The HTML form to update info
211      */
212     @View( VIEW_MODIFY_CAMPAGNEPHASE )
213     public String getModifyCampagnePhase( HttpServletRequest request )
214     {
215         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNEPHASE ) );
216 
217         if ( _campagnephase == null || ( _campagnephase.getId( ) != nId ) )
218         {
219             _campagnephase = CampagnePhaseHome.findByPrimaryKey( nId );
220         }
221 
222         Map<String, Object> model = getModel( );
223         model.put( MARK_CAMPAGNEPHASE, _campagnephase );
224 
225         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNEPHASE, TEMPLATE_MODIFY_CAMPAGNEPHASE, model );
226     }
227 
228     /**
229      * Process the change form of a campagnephase
230      *
231      * @param request
232      *            The Http request
233      * @return The Jsp URL of the process result
234      */
235     @Action( ACTION_MODIFY_CAMPAGNEPHASE )
236     public String doModifyCampagnePhase( HttpServletRequest request )
237     {
238         populate( _campagnephase, request );
239 
240         // Check constraints
241         if ( !validateBean( _campagnephase, VALIDATION_ATTRIBUTES_PREFIX ) )
242         {
243             return redirect( request, VIEW_MODIFY_CAMPAGNEPHASE, PARAMETER_ID_CAMPAGNEPHASE, _campagnephase.getId( ) );
244         }
245 
246         CampagnePhaseHome.update( _campagnephase );
247         addInfo( INFO_CAMPAGNEPHASE_UPDATED, getLocale( ) );
248 
249         CampagnesService.getInstance( ).reset( );
250 
251         return redirectView( request, VIEW_MANAGE_CAMPAGNEPHASES );
252     }
253 }