View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.crm.web.category;
35  
36  import fr.paris.lutece.plugins.crm.business.demand.category.Category;
37  import fr.paris.lutece.plugins.crm.service.category.CategoryService;
38  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
39  import fr.paris.lutece.portal.service.i18n.I18nService;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppException;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  import fr.paris.lutece.util.html.Paginator;
50  import fr.paris.lutece.util.url.UrlItem;
51  
52  import org.apache.commons.lang3.StringUtils;
53  
54  import java.util.Collection;
55  import java.util.HashMap;
56  import java.util.List;
57  import java.util.Map;
58  
59  import javax.servlet.http.HttpServletRequest;
60  
61  /**
62   *
63   * CategoryJspBean
64   *
65   */
66  public class CategoryJspBean extends PluginAdminPageJspBean
67  {
68      private static final long serialVersionUID = 1L;
69      
70      // TEMPLATES
71      private static final String TEMPLATE_MANAGE_CATEGORIES = "/admin/plugins/crm/category/manage_categories.html";
72      private static final String TEMPLATE_CREATE_CATEGORY = "/admin/plugins/crm/category/create_category.html";
73      private static final String TEMPLATE_MODIFY_CATEGORY = "/admin/plugins/crm/category/modify_category.html";
74  
75      // JSP
76      private static final String JSP_DO_REMOVE_CATEGORY = "jsp/admin/plugins/crm/DoRemoveCategory.jsp";
77      private static final String JSP_MANAGE_CATEGORIES = "jsp/admin/plugins/crm/ManageCategories.jsp";
78      private static final String JSP_REDIRECT_TO_MANAGE_CATEGORIES = "ManageCategories.jsp";
79  
80      // VARIABLES
81      private CategoryService _categoryService = CategoryService.getService( );
82      private int _nDefaultItemsPerPage;
83      private String _strCurrentPageIndex;
84      private int _nItemsPerPage;
85  
86      /**
87       * Returns the list of categories
88       * 
89       * @param request
90       *            The Http request
91       * @return the categorys list
92       */
93      public String getManageCategories( HttpServletRequest request )
94      {
95          setPageTitleProperty( CRMConstants.PROPERTY_PAGE_TITLE_MANAGE_CATEGORIES );
96  
97          _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
98          _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( CRMConstants.PROPERTY_DEFAULT_LIST_CATEGORY_PER_PAGE, 50 );
99          _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
100 
101         UrlItem url = new UrlItem( JSP_MANAGE_CATEGORIES );
102         String strUrl = url.getUrl( );
103         Collection<Category> listCategories = _categoryService.getCategoriesList( );
104         LocalizedPaginator<Category> paginator = new LocalizedPaginator<Category>( (List<Category>) listCategories, _nItemsPerPage, strUrl,
105                 CRMConstants.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
106 
107         Map<String, Object> model = new HashMap<String, Object>( );
108 
109         model.put( CRMConstants.MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
110         model.put( CRMConstants.MARK_PAGINATOR, paginator );
111         model.put( CRMConstants.MARK_CATEGORIES_LIST, paginator.getPageItems( ) );
112 
113         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORIES, getLocale( ), model );
114 
115         return getAdminPage( templateList.getHtml( ) );
116     }
117 
118     /**
119      * Returns the form to create a category
120      * 
121      * @param request
122      *            The Http request
123      * @return the html code of the category form
124      */
125     public String getCreateCategory( HttpServletRequest request )
126     {
127         setPageTitleProperty( CRMConstants.PROPERTY_PAGE_TITLE_CREATE_CATEGORY );
128 
129         Map<String, Object> model = new HashMap<String, Object>( );
130 
131         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CATEGORY, getLocale( ), model );
132 
133         return getAdminPage( template.getHtml( ) );
134     }
135 
136     /**
137      * Process the data capture form of a new category
138      * 
139      * @param request
140      *            The Http Request
141      * @return The Jsp URL of the process result
142      */
143     public String doCreateCategory( HttpServletRequest request )
144     {
145         String strUrl = StringUtils.EMPTY;
146         String strName = request.getParameter( CRMConstants.PARAMETER_CATEGORY_NAME );
147         String strDescription = request.getParameter( CRMConstants.PARAMETER_CATEGORY_DESCRIPTION );
148         String strCode = request.getParameter( CRMConstants.PARAMETER_CATEGORY_CODE );
149 
150         if ( StringUtils.isNotBlank( strName ) && StringUtils.isNotBlank( strDescription ) && StringUtils.isNotBlank( strCode ) )
151         {
152             Category/crm/business/demand/category/Category.html#Category">Category category = new Category( );
153             category.setName( strName );
154             category.setDescription( strDescription );
155             category.setCode( strCode );
156 
157             _categoryService.createCategory( category );
158 
159             strUrl = JSP_REDIRECT_TO_MANAGE_CATEGORIES;
160         }
161         else
162         {
163             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
164         }
165 
166         return strUrl;
167     }
168 
169     /**
170      * Manages the removal form of a category whose identifier is in the http request
171      * 
172      * @param request
173      *            The Http request
174      * @return the html code to confirm
175      */
176     public String getConfirmRemoveCategory( HttpServletRequest request )
177     {
178         String strUrl = StringUtils.EMPTY;
179         String strCategoryId = request.getParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY );
180 
181         if ( StringUtils.isNotBlank( strCategoryId ) && StringUtils.isNumeric( strCategoryId ) )
182         {
183             int nId = Integer.parseInt( strCategoryId );
184             UrlItem url = new UrlItem( JSP_DO_REMOVE_CATEGORY );
185             url.addParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY, nId );
186 
187             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
188         }
189         else
190         {
191             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
192         }
193 
194         return strUrl;
195     }
196 
197     /**
198      * Handles the removal form of a category
199      * 
200      * @param request
201      *            The Http request
202      * @return the jsp URL to display the form to manage categorys
203      */
204     public String doRemoveCategory( HttpServletRequest request )
205     {
206         String strUrl = StringUtils.EMPTY;
207         String strCategoryId = request.getParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY );
208 
209         if ( StringUtils.isNotBlank( strCategoryId ) && StringUtils.isNumeric( strCategoryId ) )
210         {
211             int nId = Integer.parseInt( strCategoryId );
212 
213             String strError = _categoryService.removeCategory( nId, getLocale( ) );
214 
215             if ( StringUtils.isBlank( strError ) )
216             {
217                 strUrl = JSP_REDIRECT_TO_MANAGE_CATEGORIES;
218             }
219             else
220             {
221                 Object [ ] args = {
222                     strError
223                 };
224                 strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_CANNOT_REMOVE_CATEGORY, args, AdminMessage.TYPE_STOP );
225             }
226         }
227         else
228         {
229             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
230         }
231 
232         return strUrl;
233     }
234 
235     /**
236      * Returns the form to update info about a category
237      * 
238      * @param request
239      *            The Http request
240      * @return The HTML form to update info
241      */
242     public String getModifyCategory( HttpServletRequest request )
243     {
244         setPageTitleProperty( CRMConstants.PROPERTY_PAGE_TITLE_MODIFY_CATEGORY );
245 
246         String strUrl = StringUtils.EMPTY;
247         String strCategoryId = request.getParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY );
248 
249         if ( StringUtils.isNotBlank( strCategoryId ) && StringUtils.isNumeric( strCategoryId ) )
250         {
251             int nId = Integer.parseInt( strCategoryId );
252             Category category = _categoryService.findByPrimaryKey( nId );
253 
254             Map<String, Object> model = new HashMap<String, Object>( );
255             model.put( CRMConstants.MARK_CATEGORY, category );
256 
257             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CATEGORY, getLocale( ), model );
258 
259             strUrl = getAdminPage( template.getHtml( ) );
260         }
261         else
262         {
263             throw new AppException( I18nService.getLocalizedString( CRMConstants.MESSAGE_ERROR, request.getLocale( ) ) );
264         }
265 
266         return strUrl;
267     }
268 
269     /**
270      * Process the change form of a category
271      * 
272      * @param request
273      *            The Http request
274      * @return The Jsp URL of the process result
275      */
276     public String doModifyCategory( HttpServletRequest request )
277     {
278         String strUrl = StringUtils.EMPTY;
279         String strCategoryId = request.getParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY );
280 
281         if ( StringUtils.isNotBlank( strCategoryId ) && StringUtils.isNumeric( strCategoryId ) )
282         {
283             String strName = request.getParameter( CRMConstants.PARAMETER_CATEGORY_NAME );
284             String strDescription = request.getParameter( CRMConstants.PARAMETER_CATEGORY_DESCRIPTION );
285             String strCode = request.getParameter( CRMConstants.PARAMETER_CATEGORY_CODE );
286 
287             if ( StringUtils.isNotBlank( strName ) && StringUtils.isNotBlank( strDescription ) && StringUtils.isNotBlank( strCode ) )
288             {
289                 int nId = Integer.parseInt( strCategoryId );
290                 Category category = _categoryService.findByPrimaryKey( nId );
291 
292                 if ( category != null )
293                 {
294                     category.setName( strName );
295                     category.setDescription( strDescription );
296                     category.setCode( strCode );
297                     _categoryService.updateCategory( category );
298 
299                     strUrl = JSP_REDIRECT_TO_MANAGE_CATEGORIES;
300                 }
301                 else
302                 {
303                     strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
304                 }
305             }
306             else
307             {
308                 strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
309             }
310         }
311         else
312         {
313             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
314         }
315 
316         return strUrl;
317     }
318 }