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 fr.paris.lutece.plugins.campagnebp.business.Campagne;
37  import fr.paris.lutece.plugins.campagnebp.business.CampagneHome;
38  import fr.paris.lutece.plugins.campagnebp.service.CampagnesService;
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   * This class provides the user interface to manage Campagne features ( manage, create, modify, remove )
53   */
54  @Controller( controllerJsp = "ManageCampagnebp.jsp", controllerPath = "jsp/admin/plugins/ideation/", right = "CAMPAGNEBP_MANAGEMENT" )
55  public class CampagneJspBean extends ManageCampagnebpJspBean
56  {
57  
58      ////////////////////////////////////////////////////////////////////////////
59      // Constants
60  
61      // templates
62      private static final String TEMPLATE_MANAGE_CAMPAGNES = "/admin/plugins/campagnebp/manage_campagnes.html";
63      private static final String TEMPLATE_CREATE_CAMPAGNE = "/admin/plugins/campagnebp/create_campagne.html";
64      private static final String TEMPLATE_MODIFY_CAMPAGNE = "/admin/plugins/campagnebp/modify_campagne.html";
65  
66      // Parameters
67      private static final String PARAMETER_ID_CAMPAGNE = "id";
68  
69      // Properties for page titles
70      private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNES = "campagnebp.manage_campagnes.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNE = "campagnebp.modify_campagne.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAGNE = "campagnebp.create_campagne.pageTitle";
73  
74      // Markers
75      private static final String MARK_CAMPAGNE_LIST = "campagne_list";
76      private static final String MARK_CAMPAGNE = "campagne";
77  
78      private static final String JSP_MANAGE_CAMPAGNES = "jsp/admin/plugins/campagnebp/ManageCampagnebp.jsp";
79  
80      // Properties
81      private static final String MESSAGE_CONFIRM_REMOVE_CAMPAGNE = "campagnebp.message.confirmRemoveCampagne";
82      private static final String PROPERTY_DEFAULT_LIST_CAMPAGNE_PER_PAGE = "campagnebp.listCampagnes.itemsPerPage";
83  
84      private static final String VALIDATION_ATTRIBUTES_PREFIX = "campagnebp.model.entity.campagne.attribute.";
85  
86      // Views
87      private static final String VIEW_MANAGE_CAMPAGNES = "manageCampagnes";
88      private static final String VIEW_CREATE_CAMPAGNE = "createCampagne";
89      private static final String VIEW_MODIFY_CAMPAGNE = "modifyCampagne";
90  
91      // Actions
92      private static final String ACTION_CREATE_CAMPAGNE = "createCampagne";
93      private static final String ACTION_MODIFY_CAMPAGNE = "modifyCampagne";
94      private static final String ACTION_REMOVE_CAMPAGNE = "removeCampagne";
95      private static final String ACTION_CONFIRM_REMOVE_CAMPAGNE = "confirmRemoveCampagne";
96  
97      // Infos
98      private static final String INFO_CAMPAGNE_CREATED = "campagnebp.info.campagne.created";
99      private static final String INFO_CAMPAGNE_UPDATED = "campagnebp.info.campagne.updated";
100     private static final String INFO_CAMPAGNE_REMOVED = "campagnebp.info.campagne.removed";
101 
102     // Session variable to store working values
103     private Campagne _campagne;
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_CAMPAGNES, defaultView = true )
113     public String getManageCampagnes( HttpServletRequest request )
114     {
115         _campagne = null;
116         List<Campagne> listCampagnes = (List<Campagne>) CampagneHome.getCampagnesList( );
117         Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAGNE_LIST, listCampagnes, JSP_MANAGE_CAMPAGNES );
118 
119         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAGNES, TEMPLATE_MANAGE_CAMPAGNES, model );
120     }
121 
122     /**
123      * Returns the form to create a campagne
124      *
125      * @param request
126      *            The Http request
127      * @return the html code of the campagne form
128      */
129     @View( VIEW_CREATE_CAMPAGNE )
130     public String getCreateCampagne( HttpServletRequest request )
131     {
132         _campagne = ( _campagne != null ) ? _campagne : new Campagne( );
133 
134         Map<String, Object> model = getModel( );
135         model.put( MARK_CAMPAGNE, _campagne );
136 
137         return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAGNE, TEMPLATE_CREATE_CAMPAGNE, model );
138     }
139 
140     /**
141      * Process the data capture form of a new campagne
142      *
143      * @param request
144      *            The Http Request
145      * @return The Jsp URL of the process result
146      */
147     @Action( ACTION_CREATE_CAMPAGNE )
148     public String doCreateCampagne( HttpServletRequest request )
149     {
150         populate( _campagne, request );
151 
152         // Check constraints
153         if ( !validateBean( _campagne, VALIDATION_ATTRIBUTES_PREFIX ) )
154         {
155             return redirectView( request, VIEW_CREATE_CAMPAGNE );
156         }
157 
158         CampagneHome.create( _campagne );
159         addInfo( INFO_CAMPAGNE_CREATED, getLocale( ) );
160 
161         CampagnesService.getInstance( ).reset( );
162 
163         return redirectView( request, VIEW_MANAGE_CAMPAGNES );
164     }
165 
166     /**
167      * Manages the removal form of a campagne 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_CAMPAGNE )
174     public String getConfirmRemoveCampagne( HttpServletRequest request )
175     {
176         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNE ) );
177         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAGNE ) );
178         url.addParameter( PARAMETER_ID_CAMPAGNE, nId );
179 
180         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAGNE, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
181 
182         return redirect( request, strMessageUrl );
183     }
184 
185     /**
186      * Handles the removal form of a campagne
187      *
188      * @param request
189      *            The Http request
190      * @return the jsp URL to display the form to manage campagnes
191      */
192     @Action( ACTION_REMOVE_CAMPAGNE )
193     public String doRemoveCampagne( HttpServletRequest request )
194     {
195         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNE ) );
196         CampagneHome.remove( nId );
197         addInfo( INFO_CAMPAGNE_REMOVED, getLocale( ) );
198 
199         CampagnesService.getInstance( ).reset( );
200 
201         return redirectView( request, VIEW_MANAGE_CAMPAGNES );
202     }
203 
204     /**
205      * Returns the form to update info about a campagne
206      *
207      * @param request
208      *            The Http request
209      * @return The HTML form to update info
210      */
211     @View( VIEW_MODIFY_CAMPAGNE )
212     public String getModifyCampagne( HttpServletRequest request )
213     {
214         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAGNE ) );
215 
216         if ( _campagne == null || ( _campagne.getId( ) != nId ) )
217         {
218             _campagne = CampagneHome.findByPrimaryKey( nId );
219         }
220 
221         Map<String, Object> model = getModel( );
222         model.put( MARK_CAMPAGNE, _campagne );
223 
224         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAGNE, TEMPLATE_MODIFY_CAMPAGNE, model );
225     }
226 
227     /**
228      * Process the change form of a campagne
229      *
230      * @param request
231      *            The Http request
232      * @return The Jsp URL of the process result
233      */
234     @Action( ACTION_MODIFY_CAMPAGNE )
235     public String doModifyCampagne( HttpServletRequest request )
236     {
237         populate( _campagne, request );
238 
239         // Check constraints
240         if ( !validateBean( _campagne, VALIDATION_ATTRIBUTES_PREFIX ) )
241         {
242             return redirect( request, VIEW_MODIFY_CAMPAGNE, PARAMETER_ID_CAMPAGNE, _campagne.getId( ) );
243         }
244 
245         CampagneHome.update( _campagne );
246         addInfo( INFO_CAMPAGNE_UPDATED, getLocale( ) );
247 
248         CampagnesService.getInstance( ).reset( );
249 
250         return redirectView( request, VIEW_MANAGE_CAMPAGNES );
251     }
252 }