View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.example.web;
35  
36  import fr.paris.lutece.plugins.example.business.Project;
37  import fr.paris.lutece.plugins.example.business.ProjectHome;
38  import fr.paris.lutece.plugins.example.service.ProjectCacheService;
39  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
40  import fr.paris.lutece.portal.web.xpages.XPage;
41  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
43  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
44  import fr.paris.lutece.util.url.UrlItem;
45  import fr.paris.lutece.portal.service.message.SiteMessageService;
46  import fr.paris.lutece.portal.service.message.SiteMessage;
47  import fr.paris.lutece.portal.service.message.SiteMessageException;
48  import fr.paris.lutece.portal.web.resource.ExtendableResourcePluginActionManager;
49  
50  import java.util.Map;
51  import javax.servlet.http.HttpServletRequest;
52  
53  /**
54   * This class provides the user interface to manage Project xpages ( manage, create, modify, remove )
55   */
56  @Controller( xpageName = "project", pageTitleI18nKey = "example.xpage.project.pageTitle", pagePathI18nKey = "example.xpage.project.pagePathLabel" )
57  public class ProjectXPage extends MVCApplication
58  {
59      // Templates
60      private static final String TEMPLATE_MANAGE_PROJECTS = "/skin/plugins/example/manage_projects.html";
61      private static final String TEMPLATE_CREATE_PROJECT = "/skin/plugins/example/create_project.html";
62      private static final String TEMPLATE_MODIFY_PROJECT = "/skin/plugins/example/modify_project.html";
63      private static final String TEMPLATE_DETAIL_PROJECT = "/skin/plugins/example/detail_project.html";
64  
65      // JSP
66      private static final String JSP_PAGE_PORTAL = "jsp/site/Portal.jsp";
67  
68      // Parameters
69      private static final String PARAMETER_ID_PROJECT = "id";
70      private static final String PARAM_ACTION = "action";
71      private static final String PARAM_PAGE = "page";
72  
73      // Markers
74      private static final String MARK_PROJECT_LIST = "project_list";
75      private static final String MARK_PROJECT = "project";
76  
77      // Message
78      private static final String MESSAGE_CONFIRM_REMOVE_PROJECT = "example.message.confirmRemoveProject";
79  
80      // Views
81      private static final String VIEW_MANAGE_PROJECTS = "manageProjects";
82      private static final String VIEW_CREATE_PROJECT = "createProject";
83      private static final String VIEW_MODIFY_PROJECT = "modifyProject";
84      private static final String VIEW_DETAIL_PROJECT = "detailsProject";
85  
86      // Actions
87      private static final String ACTION_CREATE_PROJECT = "createProject";
88      private static final String ACTION_MODIFY_PROJECT = "modifyProject";
89      private static final String ACTION_REMOVE_PROJECT = "removeProject";
90      private static final String ACTION_CONFIRM_REMOVE_PROJECT = "confirmRemoveProject";
91  
92      // Infos
93      private static final String INFO_PROJECT_CREATED = "example.info.project.created";
94      private static final String INFO_PROJECT_UPDATED = "example.info.project.updated";
95      private static final String INFO_PROJECT_REMOVED = "example.info.project.removed";
96  
97      // Session variable to store working values
98      private Project _project;
99  
100     @View( value = VIEW_MANAGE_PROJECTS, defaultView = true )
101     public XPage getManageProjects( HttpServletRequest request )
102     {
103         _project = null;
104         Map<String, Object> model = getModel( );
105         model.put( MARK_PROJECT_LIST, ProjectHome.getProjectsList( ) );
106 
107         return getXPage( TEMPLATE_MANAGE_PROJECTS, getLocale( request ), model );
108     }
109 
110     /**
111      * Returns the form to create a project
112      *
113      * @param request
114      *            The Http request
115      * @return the html code of the project form
116      */
117     @View( VIEW_CREATE_PROJECT )
118     public XPage getCreateProject( HttpServletRequest request )
119     {
120         _project = ( _project != null ) ? _project : new Project( );
121 
122         Map<String, Object> model = getModel( );
123         model.put( MARK_PROJECT, _project );
124 
125         return getXPage( TEMPLATE_CREATE_PROJECT, getLocale( request ), model );
126     }
127 
128     /**
129      * Process the data capture form of a new project
130      *
131      * @param request
132      *            The Http Request
133      * @return The Jsp URL of the process result
134      */
135     @Action( ACTION_CREATE_PROJECT )
136     public XPage doCreateProject( HttpServletRequest request )
137     {
138         populate( _project, request );
139 
140         // Check constraints
141         if ( !validateBean( _project, getLocale( request ) ) )
142         {
143             return redirectView( request, VIEW_CREATE_PROJECT );
144         }
145 
146         ProjectHome.create( _project );
147         addInfo( INFO_PROJECT_CREATED, getLocale( request ) );
148 
149         return redirectView( request, VIEW_MANAGE_PROJECTS );
150     }
151 
152     /**
153      * Manages the removal form of a project whose identifier is in the http request
154      *
155      * @param request
156      *            The Http request
157      * @return the html code to confirm
158      * @throws fr.paris.lutece.portal.service.message.SiteMessageException
159      */
160     @Action( ACTION_CONFIRM_REMOVE_PROJECT )
161     public XPage getConfirmRemoveProject( HttpServletRequest request ) throws SiteMessageException
162     {
163         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
164         UrlItem url = new UrlItem( JSP_PAGE_PORTAL );
165         url.addParameter( PARAM_PAGE, MARK_PROJECT );
166         url.addParameter( PARAM_ACTION, ACTION_REMOVE_PROJECT );
167         url.addParameter( PARAMETER_ID_PROJECT, nId );
168 
169         SiteMessageService.setMessage( request, MESSAGE_CONFIRM_REMOVE_PROJECT, SiteMessage.TYPE_CONFIRMATION, url.getUrl( ) );
170         return null;
171     }
172 
173     /**
174      * Handles the removal form of a project
175      *
176      * @param request
177      *            The Http request
178      * @return the jsp URL to display the form to manage projects
179      */
180     @Action( ACTION_REMOVE_PROJECT )
181     public XPage doRemoveProject( HttpServletRequest request )
182     {
183         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
184         ProjectHome.remove( nId );
185         addInfo( INFO_PROJECT_REMOVED, getLocale( request ) );
186 
187         return redirectView( request, VIEW_MANAGE_PROJECTS );
188     }
189 
190     /**
191      * Returns the form to update info about a project
192      *
193      * @param request
194      *            The Http request
195      * @return The HTML form to update info
196      */
197     @View( VIEW_MODIFY_PROJECT )
198     public XPage getModifyProject( HttpServletRequest request )
199     {
200         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
201 
202         if ( _project == null || ( _project.getId( ) != nId ) )
203         {
204             _project = ProjectHome.findByPrimaryKey( nId );
205         }
206 
207         Map<String, Object> model = getModel( );
208         model.put( MARK_PROJECT, _project );
209 
210         return getXPage( TEMPLATE_MODIFY_PROJECT, getLocale( request ), model );
211     }
212 
213     /**
214      * Process the change form of a project
215      *
216      * @param request
217      *            The Http request
218      * @return The Jsp URL of the process result
219      */
220     @Action( ACTION_MODIFY_PROJECT )
221     public XPage doModifyProject( HttpServletRequest request )
222     {
223         populate( _project, request );
224 
225         // Check constraints
226         if ( !validateBean( _project, getLocale( request ) ) )
227         {
228             return redirect( request, VIEW_MODIFY_PROJECT, PARAMETER_ID_PROJECT, _project.getId( ) );
229         }
230 
231         ProjectHome.update( _project );
232         addInfo( INFO_PROJECT_UPDATED, getLocale( request ) );
233 
234         return redirectView( request, VIEW_MANAGE_PROJECTS );
235     }
236 
237     /**
238      * Returns the form to create a project
239      *
240      * @param request
241      *            The Http request
242      * @return the html code of the project form
243      */
244     @View( VIEW_DETAIL_PROJECT )
245     public XPage getDetailProject( HttpServletRequest request )
246     {
247         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
248 
249         if ( _project == null || ( _project.getId( ) != nId ) )
250         {
251             _project = ProjectCacheService.getInstance( ).getResource( String.valueOf( nId ), null );
252         }
253 
254         Map<String, Object> model = getModel( );
255         model.put( MARK_PROJECT, _project );
256 
257         return getXPage( TEMPLATE_DETAIL_PROJECT, getLocale( request ), model );
258     }
259 
260 }