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.digglike.web;
35  
36  import fr.paris.lutece.plugins.digglike.business.Category;
37  import fr.paris.lutece.plugins.digglike.business.CategoryHome;
38  import fr.paris.lutece.plugins.digglike.service.CategoryResourceIdService;
39  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
40  import fr.paris.lutece.portal.business.rbac.RBAC;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.rbac.RBACService;
46  import fr.paris.lutece.portal.service.template.AppTemplateService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPathService;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
51  import fr.paris.lutece.util.html.HtmlTemplate;
52  import fr.paris.lutece.util.html.Paginator;
53  import fr.paris.lutece.util.url.UrlItem;
54  
55  import java.util.HashMap;
56  import java.util.List;
57  import java.util.Locale;
58  import java.util.Map;
59  
60  import javax.servlet.http.HttpServletRequest;
61  
62  
63  /**
64   *
65   * class CategoryJspBean
66   *
67   */
68  public class CategoryJspBean extends PluginAdminPageJspBean
69  {
70      private static final long serialVersionUID = -4972338182883838725L;
71  
72      //	templates
73      private static final String TEMPLATE_MANAGE_CATEGORY = "admin/plugins/digglike/manage_category.html";
74      private static final String TEMPLATE_CREATE_CATEGORY = "admin/plugins/digglike/create_category.html";
75      private static final String TEMPLATE_MODIFY_CATEGORY = "admin/plugins/digglike/modify_category.html";
76  
77      //	Markers
78      private static final String MARK_CATEGORY_LIST = "category_list";
79      private static final String MARK_CATEGORY = "category";
80      private static final String MARK_PAGINATOR = "paginator";
81      private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
82  
83      //	parameters form
84      private static final String PARAMETER_ID_CATEGORY = "id_category";
85      private static final String PARAMETER_TITLE = "title";
86      private static final String PARAMETER_PAGE_INDEX = "page_index";
87      private static final String PARAMETER_COLOR = "color";
88  
89      //	message
90      private static final String MESSAGE_CONFIRM_REMOVE_CATEGORY = "digglike.message.confirmRemoveCategory";
91      private static final String MESSAGE_MANDATORY_FIELD = "digglike.message.mandatory.field";
92      private static final String MESSAGE_CATEGORY_ASSOCIATE_TO_DIGG = "digglike.message.categoryAssociateToDigg";
93      private static final String FIELD_TITLE = "digglike.createCategory.labelTitle";
94      private static final String FIELD_COLOR = "digglike.createCategory.labelColor";
95  
96      //	properties
97      private static final String PROPERTY_ITEM_PER_PAGE = "digglike.itemsPerPage";
98      private static final String PROPERTY_MANAGE_CATEGORY_TITLE = "digglike.manageCategory.pageTitle";
99      private static final String PROPERTY_MODIFY_CATEGORY_TITLE = "digglike.modifyCategory.title";
100     private static final String PROPERTY_CREATE_CATEGORY_TITLE = "digglike.createCategory.title";
101 
102     //Jsp Definition
103     private static final String JSP_MANAGE_CATEGORY = "jsp/admin/plugins/digglike/ManageCategory.jsp";
104     private static final String JSP_DO_REMOVE_CATEGORY = "jsp/admin/plugins/digglike/DoRemoveCategory.jsp";
105 
106     //	session fields
107     private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_ITEM_PER_PAGE, 15 );
108     private String _strCurrentPageIndexExport;
109     private int _nItemsPerPageCategory;
110 
111     /**
112      * Return management category( list of category)
113      * @param request The Http request
114      * @return Html form
115      */
116     public String getManageCategory( HttpServletRequest request )
117     {
118         Plugin plugin = getPlugin(  );
119         Locale locale = getLocale(  );
120         Map<String, Object> model = new HashMap<String, Object>(  );
121         List<Category> listCategory = CategoryHome.getList( plugin );
122         listCategory = (List<Category>) RBACService.getAuthorizedCollection( listCategory,
123                 CategoryResourceIdService.PERMISSION_MANAGE, getUser(  ) );
124 
125         _strCurrentPageIndexExport = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX,
126                 _strCurrentPageIndexExport );
127         _nItemsPerPageCategory = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE,
128                 _nItemsPerPageCategory, _nDefaultItemsPerPage );
129 
130         Paginator<Category> paginator = new Paginator<Category>( listCategory, _nItemsPerPageCategory,
131                 getJspManageCategory( request ), PARAMETER_PAGE_INDEX, _strCurrentPageIndexExport );
132         model.put( MARK_PAGINATOR, paginator );
133         model.put( MARK_NB_ITEMS_PER_PAGE, DiggUtils.EMPTY_STRING + _nItemsPerPageCategory );
134         model.put( MARK_CATEGORY_LIST, paginator.getPageItems(  ) );
135         setPageTitleProperty( PROPERTY_MANAGE_CATEGORY_TITLE );
136 
137         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_CATEGORY, locale, model );
138 
139         return getAdminPage( templateList.getHtml(  ) );
140     }
141 
142     /**
143      * Gets the category creation page
144      * @param request The HTTP request
145      * @return The category creation page
146      */
147     public String getCreateCategory( HttpServletRequest request )
148     {
149         if ( !RBACService.isAuthorized( Category.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
150                     CategoryResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
151         {
152             return getManageCategory( request );
153         }
154 
155         Locale locale = getLocale(  );
156         Map<String, Object> model = new HashMap<String, Object>(  );
157         setPageTitleProperty( PROPERTY_CREATE_CATEGORY_TITLE );
158 
159         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_CATEGORY, locale, model );
160 
161         return getAdminPage( template.getHtml(  ) );
162     }
163 
164     /**
165      * Perform the category creation
166      * @param request The HTTP request
167      * @return The URL to go after performing the action
168      */
169     public String doCreateCategory( HttpServletRequest request )
170     {
171         if ( !RBACService.isAuthorized( Category.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
172                     CategoryResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
173         {
174             return getJspManageCategory( request );
175         }
176 
177         Category category = new Category(  );
178         String strError = getCategoryData( request, category );
179 
180         if ( strError != null )
181         {
182             return strError;
183         }
184 
185         CategoryHome.create( category, getPlugin(  ) );
186 
187         return getJspManageCategory( request );
188     }
189 
190     /**
191      * Gets the category modification page
192      * @param request The HTTP request
193      * @return the category modification page
194      */
195     public String getModifyCategory( HttpServletRequest request )
196     {
197         Plugin plugin = getPlugin(  );
198         Locale locale = getLocale(  );
199         Category category;
200         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
201         Map<String, Object> model = new HashMap<String, Object>(  );
202 
203         int nIdCategory = -1;
204 
205         if ( ( strIdCategory != null ) && !strIdCategory.equals( DiggUtils.EMPTY_STRING ) &&
206                 RBACService.isAuthorized( Category.RESOURCE_TYPE, strIdCategory,
207                     CategoryResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
208         {
209             try
210             {
211                 nIdCategory = Integer.parseInt( strIdCategory );
212             }
213             catch ( NumberFormatException ne )
214             {
215                 AppLogService.error( ne );
216 
217                 return getManageCategory( request );
218             }
219         }
220         else
221         {
222             return getManageCategory( request );
223         }
224 
225         category = CategoryHome.findByPrimaryKey( nIdCategory, plugin );
226         model.put( MARK_CATEGORY, category );
227         setPageTitleProperty( PROPERTY_MODIFY_CATEGORY_TITLE );
228 
229         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_CATEGORY, locale, model );
230 
231         return getAdminPage( template.getHtml(  ) );
232     }
233 
234     /**
235      * Perform the category modification
236      * @param request The HTTP request
237      * @return The URL to go after performing the action
238      */
239     public String doModifyCategory( HttpServletRequest request )
240     {
241         Plugin plugin = getPlugin(  );
242         Category category;
243         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
244         int nIdCategory = -1;
245 
246         if ( ( strIdCategory != null ) && !strIdCategory.equals( DiggUtils.EMPTY_STRING ) &&
247                 RBACService.isAuthorized( Category.RESOURCE_TYPE, strIdCategory,
248                     CategoryResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
249         {
250             try
251             {
252                 nIdCategory = Integer.parseInt( strIdCategory );
253             }
254             catch ( NumberFormatException ne )
255             {
256                 AppLogService.error( ne );
257 
258                 return getJspManageCategory( request );
259             }
260         }
261         else
262         {
263             return getJspManageCategory( request );
264         }
265 
266         category = new Category(  );
267         category.setIdCategory( nIdCategory );
268 
269         String strError = getCategoryData( request, category );
270 
271         if ( strError != null )
272         {
273             return strError;
274         }
275 
276         CategoryHome.update( category, plugin );
277 
278         return getJspManageCategory( request );
279     }
280 
281     /**
282      * Gets the confirmation page of delete category
283      * @param request The HTTP request
284      * @return the confirmation page of delete category
285      */
286     public String getConfirmRemoveCategory( HttpServletRequest request )
287     {
288         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
289 
290         if ( strIdCategory == null )
291         {
292             return getHomeUrl( request );
293         }
294 
295         UrlItem url = new UrlItem( JSP_DO_REMOVE_CATEGORY );
296         url.addParameter( PARAMETER_ID_CATEGORY, strIdCategory );
297 
298         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CATEGORY, url.getUrl(  ),
299             AdminMessage.TYPE_CONFIRMATION );
300     }
301 
302     /**
303      * Perform the category supression
304      * @param request The HTTP request
305      * @return The URL to go after performing the action
306      */
307     public String doRemoveCategory( HttpServletRequest request )
308     {
309         Plugin plugin = getPlugin(  );
310         String strIdCategory = request.getParameter( PARAMETER_ID_CATEGORY );
311         int nIdCategory = -1;
312 
313         if ( ( strIdCategory != null ) && !strIdCategory.equals( DiggUtils.EMPTY_STRING ) &&
314                 RBACService.isAuthorized( Category.RESOURCE_TYPE, strIdCategory,
315                     CategoryResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
316         {
317             try
318             {
319                 nIdCategory = Integer.parseInt( strIdCategory );
320             }
321             catch ( NumberFormatException ne )
322             {
323                 AppLogService.error( ne );
324             }
325         }
326 
327         if ( nIdCategory != -1 )
328         {
329             if ( CategoryHome.isAssociateToDigg( nIdCategory, plugin ) )
330             {
331                 return AdminMessageService.getMessageUrl( request, MESSAGE_CATEGORY_ASSOCIATE_TO_DIGG,
332                     AdminMessage.TYPE_STOP );
333             }
334 
335             CategoryHome.remove( nIdCategory, plugin );
336         }
337 
338         return getJspManageCategory( request );
339     }
340 
341     /**
342      * Get the request data and if there is no error insert the data in the
343      * regularExpression object specified in parameter.
344      * return null if there is no error or else return the error page url
345      * @param request the request
346      * @param category the category Object
347      * @return null if there is no error or else return the error page url
348      */
349     private String getCategoryData( HttpServletRequest request, Category category )
350     {
351         String strTitle = ( request.getParameter( PARAMETER_TITLE ) == null ) ? null
352                                                                               : request.getParameter( PARAMETER_TITLE )
353                                                                                        .trim(  );
354         String strColor = ( request.getParameter( PARAMETER_COLOR ) == null ) ? null
355                                                                               : request.getParameter( PARAMETER_COLOR )
356                                                                                        .trim(  );
357         String strFieldError = DiggUtils.EMPTY_STRING;
358 
359         if ( ( strTitle == null ) || strTitle.equals( DiggUtils.EMPTY_STRING ) )
360         {
361             strFieldError = FIELD_TITLE;
362         }
363 
364         if ( ( strColor == null ) || strColor.equals( DiggUtils.EMPTY_STRING ) )
365         {
366             strFieldError = FIELD_COLOR;
367         }
368 
369         //Mandatory fields
370         if ( !strFieldError.equals( DiggUtils.EMPTY_STRING ) )
371         {
372             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, getLocale(  ) ) };
373 
374             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
375                 AdminMessage.TYPE_STOP );
376         }
377 
378         category.setTitle( strTitle );
379         category.setColor( strColor );
380 
381         return null;
382     }
383 
384     /**
385      * return the url of manage category
386      * @param request the request
387      * @return the url of manage category
388      */
389     private String getJspManageCategory( HttpServletRequest request )
390     {
391         return AppPathService.getBaseUrl( request ) + JSP_MANAGE_CATEGORY;
392     }
393 }