View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appstore.web;
35  
36  import fr.paris.lutece.plugins.appstore.business.Category;
37  import fr.paris.lutece.plugins.appstore.business.CategoryHome;
38  import fr.paris.lutece.portal.service.message.AdminMessage;
39  import fr.paris.lutece.portal.service.message.AdminMessageService;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.portal.web.constants.Messages;
43  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
44  import fr.paris.lutece.util.html.HtmlTemplate;
45  import fr.paris.lutece.util.html.Paginator;
46  import fr.paris.lutece.util.url.UrlItem;
47  
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   * Category JSP bean
56   */
57  public class CategoryJspBean extends AppStoreJspBean
58  {
59      private static final String TEMPLATE_MANAGE_CATEGORYS = "/admin/plugins/appstore/manage_category.html";
60      private static final String TEMPLATE_CREATE_CATEGORY = "/admin/plugins/appstore/create_category.html";
61      private static final String TEMPLATE_MODIFY_CATEGORY = "/admin/plugins/appstore/modify_category.html";
62      private static final String PARAMETER_CATEGORY_ID_CATEGORY = "category_id_category";
63      private static final String PARAMETER_CATEGORY_NAME = "category_name";
64      private static final String PARAMETER_CATEGORY_CATEGORY_ORDER = "category_category_order";
65      private static final String PROPERTY_PAGE_TITLE_MANAGE_CATEGORYS = "appstore.manage_categorys.pageTitle";
66      private static final String PROPERTY_PAGE_TITLE_MODIFY_CATEGORY = "appstore.modify_category.pageTitle";
67      private static final String PROPERTY_PAGE_TITLE_CREATE_CATEGORY = "appstore.create_category.pageTitle";
68      private static final String MARK_CATEGORY_LIST = "category_list";
69      private static final String MARK_CATEGORY = "category";
70      private static final String JSP_DO_REMOVE_CATEGORY = "jsp/admin/plugins/appstore/DoRemoveCategory.jsp";
71      private static final String JSP_MANAGE_CATEGORYS = "jsp/admin/plugins/appstore/ManageCategories.jsp";
72      private static final String JSP_REDIRECT_TO_MANAGE_CATEGORYS = "ManageCategories.jsp";
73      private static final String PROPERTY_DEFAULT_LIST_CATEGORY_PER_PAGE = "appstore.listCategorys.itemsPerPage";
74      private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "appstore.message.confirmRemoveCategory";
75  
76      /**
77       * Returns the list of category
78       *
79       * @param request
80       *            The Http request
81       * @return the categorys list
82       */
83      public String getManageCategories( HttpServletRequest request )
84      {
85          setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_CATEGORYS );
86  
87          _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
88          _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_CATEGORY_PER_PAGE, 50 );
89          _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
90  
91          UrlItem url = new UrlItem( JSP_MANAGE_CATEGORYS );
92          String strUrl = url.getUrl( );
93          List<Category> listCategorys = (List<Category>) CategoryHome.getCategorysList( );
94  
95          // PAGINATOR
96          LocalizedPaginator paginator = new LocalizedPaginator( listCategorys, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
97  
98          Map<String, Object> model = new HashMap<String, Object>( );
99  
100         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
101         model.put( MARK_PAGINATOR, paginator );
102 
103         model.put( MARK_CATEGORY_LIST, paginator.getPageItems( ) );
104 
105         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORYS, getLocale( ), model );
106 
107         return getAdminPage( templateList.getHtml( ) );
108     }
109 
110     /**
111      * Returns the form to create a category
112      *
113      * @param request
114      *            The Http request
115      * @return the html code of the category form
116      */
117     public String getCreateCategory( HttpServletRequest request )
118     {
119         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_CATEGORY );
120 
121         Map<String, Object> model = new HashMap<String, Object>( );
122 
123         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CATEGORY, getLocale( ), model );
124 
125         return getAdminPage( template.getHtml( ) );
126     }
127 
128     /**
129      * Process the data capture form of a new category
130      *
131      * @param request
132      *            The Http Request
133      * @return The Jsp URL of the process result
134      */
135     public String doCreateCategory( HttpServletRequest request )
136     {
137         Category category = new Category( );
138 
139         if ( request.getParameter( PARAMETER_CATEGORY_NAME ).equals( "" ) )
140         {
141             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
142         }
143 
144         category.setName( request.getParameter( PARAMETER_CATEGORY_NAME ) );
145 
146         if ( request.getParameter( PARAMETER_CATEGORY_CATEGORY_ORDER ).equals( "" ) )
147         {
148             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
149         }
150 
151         int nCategoryOrder = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_CATEGORY_ORDER ) );
152         category.setOrder( nCategoryOrder );
153 
154         CategoryHome.create( category );
155 
156         return JSP_REDIRECT_TO_MANAGE_CATEGORYS;
157     }
158 
159     /**
160      * Manages the removal form of a category whose identifier is in the http request
161      *
162      * @param request
163      *            The Http request
164      * @return the html code to confirm
165      */
166     public String getConfirmRemoveCategory( HttpServletRequest request )
167     {
168         int nId = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY ) );
169         UrlItem url = new UrlItem( JSP_DO_REMOVE_CATEGORY );
170         url.addParameter( PARAMETER_CATEGORY_ID_CATEGORY, nId );
171 
172         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
173     }
174 
175     /**
176      * Handles the removal form of a category
177      *
178      * @param request
179      *            The Http request
180      * @return the jsp URL to display the form to manage categorys
181      */
182     public String doRemoveCategory( HttpServletRequest request )
183     {
184         int nId = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY ) );
185         CategoryHome.remove( nId );
186 
187         return JSP_REDIRECT_TO_MANAGE_CATEGORYS;
188     }
189 
190     /**
191      * Returns the form to update info about a category
192      *
193      * @param request
194      *            The Http request
195      * @return The HTML form to update info
196      */
197     public String getModifyCategory( HttpServletRequest request )
198     {
199         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_CATEGORY );
200 
201         int nId = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY ) );
202         Category category = CategoryHome.findByPrimaryKey( nId );
203 
204         Map<String, Object> model = new HashMap<String, Object>( );
205         model.put( MARK_CATEGORY, category );
206 
207         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CATEGORY, getLocale( ), model );
208 
209         return getAdminPage( template.getHtml( ) );
210     }
211 
212     /**
213      * Process the change form of a category
214      *
215      * @param request
216      *            The Http request
217      * @return The Jsp URL of the process result
218      */
219     public String doModifyCategory( HttpServletRequest request )
220     {
221         int nId = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY ) );
222         Category category = CategoryHome.findByPrimaryKey( nId );
223 
224         if ( request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY ).equals( "" ) )
225         {
226             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
227         }
228 
229         int nIdCategory = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_ID_CATEGORY ) );
230         category.setId( nIdCategory );
231 
232         if ( request.getParameter( PARAMETER_CATEGORY_NAME ).equals( "" ) )
233         {
234             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
235         }
236 
237         category.setName( request.getParameter( PARAMETER_CATEGORY_NAME ) );
238 
239         if ( request.getParameter( PARAMETER_CATEGORY_CATEGORY_ORDER ).equals( "" ) )
240         {
241             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
242         }
243 
244         int nCategoryOrder = Integer.parseInt( request.getParameter( PARAMETER_CATEGORY_CATEGORY_ORDER ) );
245         category.setOrder( nCategoryOrder );
246         CategoryHome.update( category );
247 
248         return JSP_REDIRECT_TO_MANAGE_CATEGORYS;
249     }
250 }