View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.wrapper.web;
35  
36  import fr.paris.lutece.plugins.wrapper.business.Wrapper;
37  import fr.paris.lutece.plugins.wrapper.business.WrapperHome;
38  import fr.paris.lutece.portal.business.role.RoleHome;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
44  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
45  import fr.paris.lutece.portal.web.constants.Messages;
46  import fr.paris.lutece.util.ReferenceList;
47  import fr.paris.lutece.util.html.HtmlTemplate;
48  import fr.paris.lutece.util.html.Paginator;
49  import fr.paris.lutece.util.url.UrlItem;
50  
51  import java.util.Collection;
52  import java.util.HashMap;
53  import java.util.List;
54  import java.util.Map;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  
59  /**
60   *
61   * @author lenaini
62   */
63  public class WrapperJspBean extends PluginAdminPageJspBean
64  {
65      // Right
66      public static final String RIGHT_MANAGE_WRAPPER = "WRAPPER_MANAGEMENT";
67  
68      // properties for page titles
69      private static final String PROPERTY_PAGE_TITLE_WRAPPER_LIST = "wrapper.manage_wrapper.pageTitle";
70      private static final String PROPERTY_PAGE_TITLE_MODIFY = "wrapper.modify_wrapper.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_CREATE = "wrapper.create_wrapper.pageTitle";
72  
73      // Properties
74      private static final String PROPERTY_DEFAULT_WRAPPER_LIST_PER_PAGE = "wrapper.wrapperList.itemsPerPage";
75  
76      // Messages
77      private static final String MESSAGE_CONFIRM_REMOVE_WRAPPER = "wrapper.message.confirmRemoveWrapper";
78  
79      // Markers
80      private static final String MARK_LIST_WRAPPER_LIST = "wrapper_list";
81      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
82      private static final String MARK_WORKGROUPS_LIST = "workgroups_list";    
83      private static final String MARK_LOCALE = "locale";    
84      private static final String MARK_WRAPPER = "wrapper";
85      private static final String MARK_ROLES_LIST = "roles_list";
86      private static final String MARK_PAGINATOR = "paginator";
87  
88      // parameters
89      private static final String PARAMETER_WRAPPER_ID = "id_wrapper";
90      private static final String PARAMETER_WRAPPER_DESCRIPTION = "description";
91      private static final String PARAMETER_WRAPPER_STATUS = "status";
92      private static final String PARAMETER_WRAPPER_WORKGROUP = "workgroup";
93      private static final String PARAMETER_WRAPPER_URL = "wrapper_url";
94      private static final String PARAMETER_WRAPPER_STYLES = "wrapper_styles";
95      private static final String PARAMETER_ID_WRAPPER_LIST = "wrapper_list_id";
96      private static final String PARAMETER_WRAPPER_ROLE = "role";
97      private static final String PARAMETER_PAGE_INDEX = "page_index";
98  
99      // templates
100     private static final String TEMPLATE_MANAGE_WRAPPER = "/admin/plugins/wrapper/manage_wrapper.html";
101     private static final String TEMPLATE_CREATE_WRAPPER = "/admin/plugins/wrapper/create_wrapper.html";
102     private static final String TEMPLATE_MODIFY_WRAPPER = "/admin/plugins/wrapper/modify_wrapper.html";
103 
104     // Jsp Definition
105     private static final String JSP_DO_REMOVE_WRAPPER = "jsp/admin/plugins/wrapper/DoRemoveWrapper.jsp";
106     private static final String JSP_REDIRECT_TO_MANAGE_WRAPPER = "ManageWrapper.jsp";
107 
108     //Variables
109     private int _nDefaultItemsPerPage;
110     private String _strCurrentPageIndex;
111     private int _nItemsPerPage;
112 
113     /**
114      * returns the template of the WrapperLists management
115      * @param request The HttpRequest
116      * @return template of lists management
117      */
118     public String getManageWrapper( HttpServletRequest request )
119     {
120         setPageTitleProperty( PROPERTY_PAGE_TITLE_WRAPPER_LIST );
121 
122         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
123         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_WRAPPER_LIST_PER_PAGE, 50 );
124         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
125                 _nDefaultItemsPerPage );
126 
127         Collection<Wrapper> listWrapperList = WrapperHome.findAll( getPlugin(  ) );
128         listWrapperList = AdminWorkgroupService.getAuthorizedCollection( listWrapperList, getUser(  ) );
129 
130         Paginator paginator = new Paginator( (List<Wrapper>) listWrapperList, _nItemsPerPage, getHomeUrl( request ),
131                 PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
132 
133         Map<String, Object> model = new HashMap<String, Object>(  );
134         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
135         model.put( MARK_PAGINATOR, paginator );
136         model.put( MARK_LIST_WRAPPER_LIST, paginator.getPageItems(  ) );
137 
138         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_WRAPPER, getLocale(  ), model );
139 
140         return getAdminPage( templateList.getHtml(  ) );
141     }
142 
143     /**
144       * Returns the form to create a wrapper
145       *
146       * @param request The Http request
147       * @return the html code of the wrapper form
148       */
149     public String getCreateWrapper( HttpServletRequest request )
150     {
151         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE );
152 
153         Map<String, Object> model = new HashMap<String, Object>(  );
154         ReferenceList workgroupsList = AdminWorkgroupService.getUserWorkgroups( getUser(  ), getLocale(  ) );
155         model.put( MARK_WORKGROUPS_LIST, workgroupsList );
156         model.put( MARK_LOCALE, getLocale(  ) );        
157         model.put( MARK_ROLES_LIST, RoleHome.getRolesList(  ) );
158 
159         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_WRAPPER, getLocale(  ), model );
160 
161         return getAdminPage( template.getHtml(  ) );
162     }
163 
164     /**
165      * Process the data capture form of a new wrapper
166      *
167      * @param request The Http Request
168      * @return The Jsp URL of the process result
169      */
170     public String doCreateWrapper( HttpServletRequest request )
171     {
172         String strDescription = request.getParameter( PARAMETER_WRAPPER_DESCRIPTION );
173         String strWrapperUrl = request.getParameter( PARAMETER_WRAPPER_URL );
174         String strWrapperStyles = request.getParameter( PARAMETER_WRAPPER_STYLES );
175         String strStatus = request.getParameter( PARAMETER_WRAPPER_STATUS );
176         String strWorkgroup = request.getParameter( PARAMETER_WRAPPER_WORKGROUP );
177         String strRole = request.getParameter( PARAMETER_WRAPPER_ROLE );
178 
179         // Mandatory fields
180         if ( ( strDescription == null ) || strDescription.trim(  ).equals( "" ) )
181         {
182             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
183         }
184 
185         Wrapper wrapper = new Wrapper(  );
186 
187         wrapper.setDescription( strDescription );
188         wrapper.setWrapperUrl( strWrapperUrl );
189         wrapper.setWrapperStyles( strWrapperStyles );
190         wrapper.setStatus( Integer.parseInt( strStatus ) );
191         wrapper.setWorkgroup( strWorkgroup );
192         wrapper.setRole( strRole );
193 
194         WrapperHome.create( wrapper, getPlugin(  ) );
195 
196         // if the operation occurred well, redirects towards the list of the Wrappers
197         return JSP_REDIRECT_TO_MANAGE_WRAPPER;
198     }
199 
200     /**
201      * Process the data capture form of a new wrapper from copy of other
202      *
203      * @param request The Http Request
204      * @return The Jsp URL of the process result
205      */
206     public String doDuplicateWrapper( HttpServletRequest request )
207     {
208         int nId = Integer.parseInt( request.getParameter( PARAMETER_WRAPPER_ID ) );
209 
210         Wrapper wrapper = WrapperHome.findByPrimaryKey( nId, getPlugin(  ) );
211 
212         Wrapper duplicateWrapper = new Wrapper(  );
213         duplicateWrapper.setDescription( wrapper.getDescription(  ) );
214         duplicateWrapper.setWrapperUrl( wrapper.getWrapperUrl(  ) );
215         duplicateWrapper.setWrapperStyles( wrapper.getWrapperStyles(  ) );
216         duplicateWrapper.setStatus( wrapper.getStatus(  ) );
217         duplicateWrapper.setWorkgroup( wrapper.getWorkgroup(  ) );
218         duplicateWrapper.setRole( wrapper.getRole(  ) );
219 
220         WrapperHome.create( duplicateWrapper, getPlugin(  ) );
221 
222         // if the operation occurred well, redirects towards the list of the Wrappers
223         return JSP_REDIRECT_TO_MANAGE_WRAPPER;
224     }
225 
226     /**
227      * Returns the form to update info about a wrapper
228      *
229      * @param request The Http request
230      * @return The HTML form to update info
231      */
232     public String getModifyWrapper( HttpServletRequest request )
233     {
234         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY );
235 
236         int nId = Integer.parseInt( request.getParameter( PARAMETER_WRAPPER_ID ) );
237         Wrapper wrapper = WrapperHome.findByPrimaryKey( nId, getPlugin(  ) );
238 
239         Map<String, Object> model = new HashMap<String, Object>(  );
240         ReferenceList workgroupsList = AdminWorkgroupService.getUserWorkgroups( getUser(  ), getLocale(  ) );
241         model.put( MARK_WORKGROUPS_LIST, workgroupsList );        
242         model.put( MARK_LOCALE, getLocale(  ) );
243         model.put( MARK_WRAPPER, wrapper );
244         model.put( MARK_ROLES_LIST, RoleHome.getRolesList(  ) );
245 
246         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_WRAPPER, getLocale(  ), model );
247 
248         return getAdminPage( template.getHtml(  ) );
249     }
250 
251     /**
252      * Process the change form of a wrapper
253      *
254      * @param request The Http request
255      * @return The Jsp URL of the process result
256      */
257     public String doModifyWrapper( HttpServletRequest request )
258     {
259         // Mandatory fields
260         if ( request.getParameter( PARAMETER_WRAPPER_DESCRIPTION ).equals( "" ) ||
261                 request.getParameter( PARAMETER_WRAPPER_URL ).equals( "" ) )
262         {
263             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
264         }
265 
266         int nId = Integer.parseInt( request.getParameter( PARAMETER_WRAPPER_ID ) );
267 
268         Wrapper wrapper = WrapperHome.findByPrimaryKey( nId, getPlugin(  ) );
269         wrapper.setDescription( request.getParameter( PARAMETER_WRAPPER_DESCRIPTION ) );
270         wrapper.setWrapperUrl( request.getParameter( PARAMETER_WRAPPER_URL ) );
271         wrapper.setWrapperStyles( request.getParameter( PARAMETER_WRAPPER_STYLES ) );
272         wrapper.setStatus( Integer.parseInt( request.getParameter( PARAMETER_WRAPPER_STATUS ) ) );
273         wrapper.setWorkgroup( request.getParameter( PARAMETER_WRAPPER_WORKGROUP ) );
274         wrapper.setRole( request.getParameter( PARAMETER_WRAPPER_ROLE ) );
275 
276         WrapperHome.update( wrapper, getPlugin(  ) );
277 
278         // if the operation occurred well, redirects towards the list of the Wrappers
279         return JSP_REDIRECT_TO_MANAGE_WRAPPER;
280     }
281 
282     /**
283      * Manages the removal form of a wrapper whose identifier is in the http request
284      *
285      * @param request The Http request
286      * @return the html code to confirm
287      */
288     public String getConfirmRemoveWrapper( HttpServletRequest request )
289     {
290         int nIdWrapper = Integer.parseInt( request.getParameter( PARAMETER_WRAPPER_ID ) );
291 
292         UrlItem url = new UrlItem( JSP_DO_REMOVE_WRAPPER );
293         url.addParameter( PARAMETER_WRAPPER_ID, nIdWrapper );
294         url.addParameter( PARAMETER_ID_WRAPPER_LIST, request.getParameter( PARAMETER_ID_WRAPPER_LIST ) );
295 
296         Object[] args = { request.getParameter( PARAMETER_WRAPPER_DESCRIPTION ) };
297 
298         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_WRAPPER, args, url.getUrl(  ),
299             AdminMessage.TYPE_CONFIRMATION );
300     }
301 
302     /**
303      * Treats the removal form of a wrapper
304      *
305      * @param request The Http request
306      * @return the jsp URL to display the form to manage wrappers
307      */
308     public String doRemoveWrapper( HttpServletRequest request )
309     {
310         int nIdWrapper = Integer.parseInt( request.getParameter( PARAMETER_WRAPPER_ID ) );
311 
312         Wrapper wrapper = WrapperHome.findByPrimaryKey( nIdWrapper, getPlugin(  ) );
313         WrapperHome.remove( wrapper, getPlugin(  ) );
314 
315         // if the operation occurred well, redirects towards the list of the Wrappers
316         return JSP_REDIRECT_TO_MANAGE_WRAPPER;
317     }
318 }