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.demand;
35  
36  import fr.paris.lutece.plugins.crm.business.demand.DemandType;
37  import fr.paris.lutece.plugins.crm.business.demand.DemandTypeFilter;
38  import fr.paris.lutece.plugins.crm.service.category.CategoryService;
39  import fr.paris.lutece.plugins.crm.service.demand.DemandService;
40  import fr.paris.lutece.plugins.crm.service.demand.DemandTypeService;
41  import fr.paris.lutece.plugins.crm.service.parameters.AdvancedParametersService;
42  import fr.paris.lutece.plugins.crm.util.OperatorEnum;
43  import fr.paris.lutece.plugins.crm.util.TargetEnum;
44  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
45  import fr.paris.lutece.portal.service.i18n.I18nService;
46  import fr.paris.lutece.portal.service.message.AdminMessage;
47  import fr.paris.lutece.portal.service.message.AdminMessageService;
48  import fr.paris.lutece.portal.service.security.SecurityService;
49  import fr.paris.lutece.portal.service.template.AppTemplateService;
50  import fr.paris.lutece.portal.service.util.AppException;
51  import fr.paris.lutece.portal.service.util.AppPathService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
54  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
55  import fr.paris.lutece.portal.web.constants.Messages;
56  import fr.paris.lutece.portal.web.constants.Parameters;
57  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
58  import fr.paris.lutece.util.date.DateUtil;
59  import fr.paris.lutece.util.html.HtmlTemplate;
60  import fr.paris.lutece.util.html.Paginator;
61  import fr.paris.lutece.util.sort.AttributeComparator;
62  import fr.paris.lutece.util.url.UrlItem;
63  
64  import org.apache.commons.lang3.StringUtils;
65  
66  import java.util.Collections;
67  import java.util.Date;
68  import java.util.HashMap;
69  import java.util.List;
70  import java.util.Map;
71  
72  import javax.servlet.http.HttpServletRequest;
73  
74  /**
75   *
76   * DemandTypeJspBean
77   *
78   */
79  public class DemandTypeJspBean extends PluginAdminPageJspBean
80  {
81      public static final String RIGHT_MANAGE_CRM_DEMAND_TYPES = "CRM_DEMAND_TYPES_MANAGEMENT";
82  
83      // TEMPLATES
84      private static final String TEMPLATE_MANAGE_DEMAND_TYPES = "/admin/plugins/crm/demand/manage_demand_types.html";
85      private static final String TEMPLATE_CREATE_DEMAND_TYPE = "/admin/plugins/crm/demand/create_demand_type.html";
86      private static final String TEMPLATE_MODIFY_DEMAND_TYPE = "/admin/plugins/crm/demand/modify_demand_type.html";
87      private static final String TEMPLATE_MANAGE_ADVANCED_PARAMETERS = "/admin/plugins/crm/demand/manage_advanced_parameters.html";
88  
89      // JSP
90      private static final String JSP_MANAGE_DEMAND_TYPES = "jsp/admin/plugins/crm/ManageDemandTypes.jsp";
91      private static final String JSP_DO_REMOVE_DEMAND_TYPE = "jsp/admin/plugins/crm/DoRemoveDemandType.jsp";
92      private static final String JSP_DO_PURGE_DEMAND_TYPE = "jsp/admin/plugins/crm/DoPurgeDemandType.jsp";
93  
94      // VARIABLES
95      private AdvancedParametersService _advancedParametersService = AdvancedParametersService.getService( );
96      private DemandTypeService _demandTypeService = DemandTypeService.getService( );
97      private DemandService _demandService = DemandService.getService( );
98      private CategoryService _categoryService = CategoryService.getService( );
99      private DemandTypeFilter _dtFilter;
100     private int _nDefaultItemsPerPage;
101     private String _strCurrentPageIndex;
102     private int _nItemsPerPage;
103 
104     /**
105      * Get interface of managing the list of demand types
106      * 
107      * @param request
108      *            {@link HttpServletRequest}
109      * @return the html code
110      */
111     public String getManageDemandTypes( HttpServletRequest request )
112     {
113         setPageTitleProperty( CRMConstants.PROPERTY_MANAGE_DEMAND_TYPES_PAGE_TITLE );
114 
115         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
116         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( CRMConstants.PROPERTY_DEFAULT_LIST_DEMAND_TYPES_PER_PAGE, 50 );
117         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
118 
119         UrlItem url = getUrlManageDemandTypes( request );
120         List<DemandType> listDemandTypes = getDemandTypesList( request );
121 
122         LocalizedPaginator<DemandType> paginator = new LocalizedPaginator<DemandType>( listDemandTypes, _nItemsPerPage, url.getUrl( ),
123                 CRMConstants.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
124 
125         Map<String, Object> model = new HashMap<String, Object>( );
126 
127         model.put( CRMConstants.MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
128         model.put( CRMConstants.MARK_PAGINATOR, paginator );
129         model.put( CRMConstants.MARK_DEMAND_TYPES_LIST, paginator.getPageItems( ) );
130         model.put( CRMConstants.MARK_DEMAND_TYPE_FILTER, _dtFilter );
131         model.put( CRMConstants.MARK_CATEGORIES_LIST, _categoryService.getCategories( getLocale( ), true, false ) );
132         model.put( CRMConstants.MARK_IS_WELL_ORDERED, _demandTypeService.isWellOrdered( ) );
133         model.put( CRMConstants.MARK_OPERATORS_LIST, _demandTypeService.getOperatorsList( ) );
134         model.put( CRMConstants.MARK_LOCALE, request.getLocale( ) );
135 
136         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DEMAND_TYPES, getLocale( ), model );
137 
138         return getAdminPage( templateList.getHtml( ) );
139     }
140 
141     /**
142      * Get interface of advanced parameters
143      * 
144      * @param request
145      *            {@link HttpServletRequest}
146      * @return the html code
147      */
148     public String getManageAdvancedParameters( HttpServletRequest request )
149     {
150         setPageTitleProperty( CRMConstants.PROPERTY_MANAGE_DEMAND_TYPES_PAGE_TITLE );
151 
152         // get the actual displayDraft parameter
153         Boolean bDisplayDraft = _advancedParametersService.isParameterValueByKey( CRMConstants.CONSTANT_DISPLAYDRAFT );
154         Boolean bUseIdCrmUser = _advancedParametersService.isParameterValueByKey( CRMConstants.CONSTANT_USE_ID_CRM_USER );
155         Map<String, Object> model = new HashMap<String, Object>( );
156 
157         model.put( CRMConstants.MARK_DISPLAYDRAFT, bDisplayDraft );
158         model.put( CRMConstants.MARK_USE_IDCRMUSER, bUseIdCrmUser );
159 
160         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ADVANCED_PARAMETERS, getLocale( ), model );
161 
162         return getAdminPage( templateList.getHtml( ) );
163     }
164 
165     /**
166      * Returns the form to create a demand type
167      * 
168      * @param request
169      *            The Http request
170      * @return the html code of the demand type form
171      */
172     public String getCreateDemandType( HttpServletRequest request )
173     {
174         setPageTitleProperty( CRMConstants.PROPERTY_CREATE_DEMAND_TYPE_PAGE_TITLE );
175 
176         Map<String, Object> model = new HashMap<String, Object>( );
177         model.put( CRMConstants.MARK_CATEGORIES_LIST, _categoryService.getCategories( getLocale( ), false, false ) );
178         model.put( CRMConstants.MARK_USER_WORKGROUP_REF_LIST, AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) ) );
179         model.put( CRMConstants.MARK_MAX_ORDER, _demandTypeService.findMaxOrder( ) );
180         model.put( CRMConstants.MARK_TARGETS_LIST, _demandTypeService.getTargetsList( ) );
181         model.put( CRMConstants.MARK_LOCALE, request.getLocale( ) );
182 
183         if ( SecurityService.isAuthenticationEnable( ) )
184         {
185             model.put( CRMConstants.MARK_ROLE_REF_LIST, _demandTypeService.getRolesList( ) );
186         }
187 
188         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_DEMAND_TYPE, getLocale( ), model );
189 
190         return getAdminPage( template.getHtml( ) );
191     }
192 
193     /**
194      * Process the data capture form of a new demand type
195      * 
196      * @param request
197      *            The Http Request
198      * @return The Jsp URL of the process result
199      */
200     public String doCreateDemandType( HttpServletRequest request )
201     {
202         DemandType/business/demand/DemandType.html#DemandType">DemandType demandType = new DemandType( );
203         String strUrl = getDemandTypeData( request, demandType );
204 
205         if ( StringUtils.isBlank( strUrl ) )
206         {
207             // The newly created demand type is placed at the head of the list
208             _demandTypeService.create( demandType );
209             strUrl = getUrlManageDemandTypes( request ).getUrl( );
210         }
211 
212         return strUrl;
213     }
214 
215     /**
216      * Manages the removal form of a demand type whose identifier is in the http request
217      * 
218      * @param request
219      *            The Http request
220      * @return the html code to confirm
221      */
222     public String getConfirmRemoveDemandType( HttpServletRequest request )
223     {
224         String strUrl = StringUtils.EMPTY;
225         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
226 
227         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
228         {
229             int nIdDemandType = Integer.parseInt( strIdDemandType );
230             UrlItem url = new UrlItem( JSP_DO_REMOVE_DEMAND_TYPE );
231             url.addParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE, nIdDemandType );
232 
233             strUrl = AdminMessageService
234                     .getMessageUrl( request, CRMConstants.MESSAGE_CONFIRM_REMOVE_DEMAND_TYPE, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
235         }
236         else
237         {
238             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
239         }
240 
241         return strUrl;
242     }
243 
244     /**
245      * Handles the removal form of a demand type
246      * 
247      * @param request
248      *            The Http request
249      * @return the jsp URL to display the form to manage demand types
250      */
251     public String doRemoveDemandType( HttpServletRequest request )
252     {
253         String strUrl = StringUtils.EMPTY;
254         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
255 
256         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
257         {
258             int nIdDemandType = Integer.parseInt( strIdDemandType );
259             _demandTypeService.remove( nIdDemandType );
260             strUrl = getUrlManageDemandTypes( request ).getUrl( );
261         }
262         else
263         {
264             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
265         }
266 
267         return strUrl;
268     }
269 
270     /**
271      * Returns the form to update info about a demand type
272      * 
273      * @param request
274      *            The Http request
275      * @return The HTML form to update info
276      */
277     public String getModifyDemandType( HttpServletRequest request )
278     {
279         setPageTitleProperty( CRMConstants.PROPERTY_MODIFY_DEMAND_TYPE_PAGE_TITLE );
280 
281         String strHtml = StringUtils.EMPTY;
282         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
283 
284         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
285         {
286             int nIdDemandType = Integer.parseInt( strIdDemandType );
287             DemandType demandType = _demandTypeService.findByPrimaryKey( nIdDemandType );
288 
289             if ( demandType != null )
290             {
291                 Map<String, Object> model = new HashMap<String, Object>( );
292                 model.put( CRMConstants.MARK_DEMAND_TYPE, demandType );
293                 model.put( CRMConstants.MARK_CATEGORIES_LIST, _categoryService.getCategories( getLocale( ), false, false ) );
294                 model.put( CRMConstants.MARK_MAX_ORDER, _demandTypeService.findMaxOrder( ) );
295                 model.put( CRMConstants.MARK_USER_WORKGROUP_REF_LIST, AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) ) );
296                 model.put( CRMConstants.MARK_TARGETS_LIST, _demandTypeService.getTargetsList( ) );
297                 model.put( CRMConstants.MARK_LOCALE, request.getLocale( ) );
298 
299                 if ( SecurityService.isAuthenticationEnable( ) )
300                 {
301                     model.put( CRMConstants.MARK_ROLE_REF_LIST, _demandTypeService.getRolesList( ) );
302                 }
303 
304                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_DEMAND_TYPE, getLocale( ), model );
305 
306                 strHtml = getAdminPage( template.getHtml( ) );
307             }
308             else
309             {
310                 throw new AppException( I18nService.getLocalizedString( CRMConstants.MESSAGE_ERROR, request.getLocale( ) ) );
311             }
312         }
313         else
314         {
315             throw new AppException( I18nService.getLocalizedString( CRMConstants.MESSAGE_ERROR, request.getLocale( ) ) );
316         }
317 
318         return strHtml;
319     }
320 
321     /**
322      * Process the change form of a demand type
323      * 
324      * @param request
325      *            The Http request
326      * @return The Jsp URL of the process result
327      */
328     public String doModifyDemandType( HttpServletRequest request )
329     {
330         String strUrl = StringUtils.EMPTY;
331         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
332 
333         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
334         {
335             int nIdDemandType = Integer.parseInt( strIdDemandType );
336             DemandType/business/demand/DemandType.html#DemandType">DemandType demandType = new DemandType( );
337             demandType.setIdDemandType( nIdDemandType );
338             strUrl = getDemandTypeData( request, demandType );
339 
340             if ( StringUtils.isBlank( strUrl ) )
341             {
342                 _demandTypeService.update( demandType );
343                 strUrl = getUrlManageDemandTypes( request ).getUrl( );
344             }
345         }
346         else
347         {
348             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
349         }
350 
351         return strUrl;
352     }
353 
354     /**
355      * Process the parameter change for "display draft"
356      * 
357      * @param request
358      *            The Http request
359      * @return The Jsp URL of the process result
360      */
361     public String doModifyAdvancedParameters( HttpServletRequest request )
362     {
363         String strModifyDraftDisplay = request.getParameter( CRMConstants.PARAMETER_CHECKBOX_DRAFT_DISPLAY );
364         String strUseIdCrmUser = request.getParameter( CRMConstants.PARAMETER_CHECKBOX_USE_IDCRMUSER );
365 
366         if ( StringUtils.isNotBlank( strModifyDraftDisplay ) )
367         {
368             _advancedParametersService.modifyParameterStringValueByKey( CRMConstants.CONSTANT_DISPLAYDRAFT, CRMConstants.CONSTANT_TRUE );
369         }
370         else
371         {
372             _advancedParametersService.modifyParameterStringValueByKey( CRMConstants.CONSTANT_DISPLAYDRAFT, CRMConstants.CONSTANT_FALSE );
373         }
374 
375         _advancedParametersService.modifyParameterStringValueByKey( CRMConstants.CONSTANT_USE_ID_CRM_USER,
376                 StringUtils.isNotBlank( strUseIdCrmUser ) ? CRMConstants.CONSTANT_TRUE : CRMConstants.CONSTANT_FALSE );
377 
378         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_DEMAND_TYPES );
379         url.addParameter( CRMConstants.CONSTANT_PLUGIN_NAME, CRMConstants.CONSTANT_CRM );
380 
381         return url.getUrl( );
382     }
383 
384     /**
385      * Do reorder the demand types
386      * 
387      * @param request
388      *            {@link HttpServletRequest}
389      * @return the jsp URL to display the form to manage demand types
390      */
391     public String doReorderDemandTypes( HttpServletRequest request )
392     {
393         _demandTypeService.doReorderDemandTypes( );
394 
395         return getUrlManageDemandTypes( request ).getUrl( );
396     }
397 
398     /**
399      * Manages the purge form of a demand type whose identifier is in the http request
400      * 
401      * @param request
402      *            The Http request
403      * @return the html code to confirm
404      */
405     public String getConfirmPurgeDemandType( HttpServletRequest request )
406     {
407         String strUrl = StringUtils.EMPTY;
408         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
409 
410         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
411         {
412             int nIdDemandType = Integer.parseInt( strIdDemandType );
413             UrlItem url = new UrlItem( JSP_DO_PURGE_DEMAND_TYPE );
414             url.addParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE, nIdDemandType );
415 
416             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_CONFIRM_PURGE_DEMAND_TYPE, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
417         }
418         else
419         {
420             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
421         }
422 
423         return strUrl;
424     }
425 
426     /**
427      * Handles the purge form of a demand type
428      * 
429      * @param request
430      *            The Http request
431      * @return the jsp URL to display the form to manage demand types
432      */
433     public String doPurgeDemandType( HttpServletRequest request )
434     {
435         String strUrl = StringUtils.EMPTY;
436         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
437 
438         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
439         {
440             int nIdDemandType = Integer.parseInt( strIdDemandType );
441             _demandService.removeByIdDemandType( nIdDemandType );
442             strUrl = getUrlManageDemandTypes( request ).getUrl( );
443         }
444         else
445         {
446             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
447         }
448 
449         return strUrl;
450     }
451 
452     /**
453      * Enable a demand type
454      * 
455      * @param request
456      *            The Http request
457      * @return the jsp URL to display the form to manage demand types
458      */
459     public String doEnableDemandType( HttpServletRequest request )
460     {
461         String strUrl = StringUtils.EMPTY;
462         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
463 
464         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
465         {
466             int nIdDemandType = Integer.parseInt( strIdDemandType );
467             DemandType demandType = _demandTypeService.findByPrimaryKey( nIdDemandType );
468 
469             if ( demandType != null )
470             {
471                 demandType.setDateEnd( null );
472                 _demandTypeService.update( demandType );
473             }
474 
475             strUrl = getUrlManageDemandTypes( request ).getUrl( );
476         }
477         else
478         {
479             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
480         }
481 
482         return strUrl;
483     }
484 
485     /**
486      * Disable a demand type
487      * 
488      * @param request
489      *            The Http request
490      * @return the jsp URL to display the form to manage demand types
491      */
492     public String doDisableDemandType( HttpServletRequest request )
493     {
494         String strUrl = StringUtils.EMPTY;
495         String strIdDemandType = request.getParameter( CRMConstants.PARAMETER_ID_DEMAND_TYPE );
496 
497         if ( StringUtils.isNotBlank( strIdDemandType ) && StringUtils.isNumeric( strIdDemandType ) )
498         {
499             int nIdDemandType = Integer.parseInt( strIdDemandType );
500             DemandType demandType = _demandTypeService.findByPrimaryKey( nIdDemandType );
501 
502             if ( demandType != null )
503             {
504                 demandType.setDateEnd( new Date( ) );
505                 _demandTypeService.update( demandType );
506             }
507 
508             strUrl = getUrlManageDemandTypes( request ).getUrl( );
509         }
510         else
511         {
512             strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR, AdminMessage.TYPE_STOP );
513         }
514 
515         return strUrl;
516     }
517 
518     // PRIVATE METHODS
519 
520     /**
521      * Get the data of the demand type
522      * 
523      * @param request
524      *            {@link HttpServletRequest}
525      * @param demandType
526      *            the {@link DemandType}
527      * @return an empty string if there are no errors, messages otherwise
528      */
529     private String getDemandTypeData( HttpServletRequest request, DemandType demandType )
530     {
531         String strUrl = StringUtils.EMPTY;
532         String strLabel = request.getParameter( CRMConstants.PARAMETER_DEMAND_TYPE_LABEL );
533         String strUrlResource = request.getParameter( CRMConstants.PARAMETER_URL_RESOURCE );
534 
535         if ( StringUtils.isNotBlank( strLabel ) && StringUtils.isNotBlank( strUrlResource ) )
536         {
537             String strUrlInfo = request.getParameter( CRMConstants.PARAMETER_URL_INFO );
538             String strUrlContact = request.getParameter( CRMConstants.PARAMETER_URL_CONTACT );
539             String strDateBegin = request.getParameter( CRMConstants.PARAMETER_DATE_BEGIN );
540             String strDateEnd = request.getParameter( CRMConstants.PARAMETER_DATE_END );
541             String strOrder = request.getParameter( CRMConstants.PARAMETER_ORDER );
542             String strIdCategory = request.getParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY );
543             String strWorkgroupKey = request.getParameter( CRMConstants.PARAMETER_WORKGROUP_KEY );
544             String strRoleKey = request.getParameter( CRMConstants.PARAMETER_ROLE_KEY );
545             String strTarget = request.getParameter( CRMConstants.PARAMETER_TARGET );
546             String strUrlDelete = request.getParameter( CRMConstants.PARAMETER_URL_DELETE );
547             String strIncludeIdCrmUser = request.getParameter( CRMConstants.PARAMETER_INCLUDE_ID_CRM_USER );
548             String strNeedAuthentication = request.getParameter( CRMConstants.PARAMETER_NEED_AUTHENTICATION );
549             String strNeedValidation = request.getParameter( CRMConstants.PARAMETER_NEED_VALIDATION );
550 
551             int nOrder = 0;
552 
553             if ( StringUtils.isNotBlank( strOrder ) && StringUtils.isNumeric( strOrder ) )
554             {
555                 nOrder = Integer.parseInt( strOrder );
556             }
557 
558             int nIdCategory = -1;
559 
560             if ( StringUtils.isNotBlank( strIdCategory ) && StringUtils.isNumeric( strIdCategory ) )
561             {
562                 nIdCategory = Integer.parseInt( strIdCategory );
563             }
564 
565             int nTarget = 0;
566 
567             if ( StringUtils.isNotBlank( strTarget ) && StringUtils.isNumeric( strTarget ) )
568             {
569                 nTarget = Integer.parseInt( strTarget );
570             }
571 
572             Date dateBegin = null;
573             Date dateEnd = null;
574 
575             if ( StringUtils.isNotBlank( strDateBegin ) )
576             {
577                 dateBegin = DateUtil.formatDateLongYear( strDateBegin, getLocale( ) );
578 
579                 if ( dateBegin != null )
580                 {
581                     if ( dateBegin.before( new Date( 0 ) ) )
582                     {
583                         strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_INVALID_DATE_BEFORE_70, AdminMessage.TYPE_STOP );
584                     }
585                 }
586                 else
587                 {
588                     strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_INVALID_DATEBEGIN, AdminMessage.TYPE_STOP );
589                 }
590             }
591 
592             if ( StringUtils.isBlank( strUrl ) && StringUtils.isNotBlank( strDateEnd ) )
593             {
594                 dateEnd = DateUtil.formatDateLongYear( strDateEnd, getLocale( ) );
595 
596                 if ( dateEnd != null )
597                 {
598                     if ( dateEnd.before( new Date( 0 ) ) )
599                     {
600                         strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_INVALID_DATE_BEFORE_70, AdminMessage.TYPE_STOP );
601                     }
602                 }
603                 else
604                 {
605                     strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_INVALID_DATEEND, AdminMessage.TYPE_STOP );
606                 }
607             }
608 
609             // validate period (dateEnd > dateBegin )
610             if ( StringUtils.isBlank( strUrl ) && ( dateBegin != null ) && ( dateEnd != null ) )
611             {
612                 if ( dateEnd.before( dateBegin ) )
613                 {
614                     strUrl = AdminMessageService.getMessageUrl( request, CRMConstants.MESSAGE_ERROR_DATEEND_BEFORE_DATEBEGIN, AdminMessage.TYPE_STOP );
615                 }
616             }
617 
618             if ( StringUtils.isBlank( strUrl ) )
619             {
620                 demandType.setLabel( strLabel );
621                 demandType.setUrlResource( StringUtils.isNotBlank( strUrlResource ) ? strUrlResource : StringUtils.EMPTY );
622                 demandType.setUrlInfo( StringUtils.isNotBlank( strUrlInfo ) ? strUrlInfo : StringUtils.EMPTY );
623                 demandType.setUrlContact( StringUtils.isNotBlank( strUrlContact ) ? strUrlContact : StringUtils.EMPTY );
624                 demandType.setOrder( nOrder );
625                 demandType.setIdCategory( nIdCategory );
626                 demandType.setDateBegin( dateBegin );
627                 demandType.setDateEnd( dateEnd );
628                 demandType.setWorkgroup( StringUtils.isNotBlank( strWorkgroupKey ) ? strWorkgroupKey : StringUtils.EMPTY );
629                 demandType.setRole( StringUtils.isNotBlank( strRoleKey ) ? strRoleKey : StringUtils.EMPTY );
630                 demandType.setTarget( TargetEnum.getTarget( nTarget ) );
631                 demandType.setUrlDelete( StringUtils.isNotBlank( strUrlDelete ) ? strUrlDelete : StringUtils.EMPTY );
632                 demandType.setIncludeIdCrmUser( strIncludeIdCrmUser != null );
633                 demandType.setNeedAuthentication( strNeedAuthentication != null );
634                 demandType.setNeedValidation( strNeedValidation != null );
635             }
636         }
637         else
638         {
639             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
640         }
641 
642         return strUrl;
643     }
644 
645     /**
646      * Get the list of demand types
647      * 
648      * @param request
649      *            {@link HttpServletRequest}
650      * @return a list of {@link DemandType}
651      */
652     private List<DemandType> getDemandTypesList( HttpServletRequest request )
653     {
654         List<DemandType> listDemandTypes;
655         String strSession = request.getParameter( CRMConstants.PARAMETER_SESSION );
656 
657         // Check if it s a search or not
658         if ( StringUtils.isNotBlank( strSession ) )
659         {
660             // find demand types from search
661             listDemandTypes = doSearchDemandTypesList( request );
662         }
663         else
664         {
665             // Find all demand types
666             // Reinit the filter stored in the session
667             _dtFilter = null;
668             listDemandTypes = _demandTypeService.findAll( );
669         }
670 
671         // Get only the demand types the user is authorized to ses
672         listDemandTypes = (List<DemandType>) AdminWorkgroupService.getAuthorizedCollection( listDemandTypes, getUser( ) );
673 
674         // Sort the list
675         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
676 
677         if ( StringUtils.isNotBlank( strSortedAttributeName ) )
678         {
679             String strAscSort = request.getParameter( Parameters.SORTED_ASC );
680             boolean bIsAscSort = true;
681 
682             if ( StringUtils.isNotBlank( strAscSort ) )
683             {
684                 bIsAscSort = Boolean.parseBoolean( strAscSort );
685             }
686 
687             Collections.sort( listDemandTypes, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
688         }
689 
690         return listDemandTypes;
691     }
692 
693     /**
694      * Search the list of demand type
695      * 
696      * @param request
697      *            {@link HttpServletRequest}
698      * @return a list of {@link DemandType}
699      */
700     private List<DemandType> doSearchDemandTypesList( HttpServletRequest request )
701     {
702         String strSearch = request.getParameter( CRMConstants.PARAMETER_SEARCH );
703 
704         if ( StringUtils.isNotBlank( strSearch ) || ( _dtFilter == null ) )
705         {
706             _dtFilter = new DemandTypeFilter( );
707 
708             String strLabel = request.getParameter( CRMConstants.PARAMETER_LABEL );
709             String strUrlForm = request.getParameter( CRMConstants.PARAMETER_URL_RESOURCE );
710             String strIdCategory = request.getParameter( CRMConstants.PARAMETER_CATEGORY_ID_CATEGORY );
711             String strDateBegin = request.getParameter( CRMConstants.PARAMETER_DATE_BEGIN );
712             String strDateEnd = request.getParameter( CRMConstants.PARAMETER_DATE_END );
713             String strOperatorDateBegin = request.getParameter( CRMConstants.PARAMETER_OPERATOR_DATE_BEGIN );
714             String strOperatorDateEnd = request.getParameter( CRMConstants.PARAMETER_OPERATOR_DATE_END );
715             String strUrlDelete = request.getParameter( CRMConstants.PARAMETER_URL_DELETE );
716 
717             if ( StringUtils.isNotBlank( strLabel ) )
718             {
719                 _dtFilter.setLabel( strLabel );
720             }
721 
722             if ( StringUtils.isNotBlank( strUrlForm ) )
723             {
724                 _dtFilter.setUrlResource( strUrlForm );
725             }
726 
727             if ( StringUtils.isNotBlank( strIdCategory ) && StringUtils.isNumeric( strIdCategory ) )
728             {
729                 int nIdCategory = Integer.parseInt( strIdCategory );
730                 _dtFilter.setIdCategory( nIdCategory );
731             }
732 
733             if ( StringUtils.isNotBlank( strDateBegin ) )
734             {
735                 Date dateBegin = DateUtil.formatDateLongYear( strDateBegin, getLocale( ) );
736 
737                 if ( dateBegin != null )
738                 {
739                     if ( dateBegin.after( new Date( 0 ) ) )
740                     {
741                         _dtFilter.setDateBegin( dateBegin );
742                     }
743                 }
744             }
745 
746             if ( StringUtils.isNotBlank( strDateEnd ) )
747             {
748                 Date dateEnd = DateUtil.formatDateLongYear( strDateEnd, getLocale( ) );
749 
750                 if ( dateEnd != null )
751                 {
752                     if ( dateEnd.after( new Date( 0 ) ) )
753                     {
754                         _dtFilter.setDateEnd( dateEnd );
755                     }
756                 }
757             }
758 
759             if ( StringUtils.isNotBlank( strOperatorDateBegin ) && StringUtils.isNumeric( strOperatorDateBegin ) )
760             {
761                 int nIdOperatorDateBegin = Integer.parseInt( strOperatorDateBegin );
762 
763                 if ( nIdOperatorDateBegin < OperatorEnum.values( ).length )
764                 {
765                     _dtFilter.setOperatorDateBegin( OperatorEnum.values( ) [nIdOperatorDateBegin] );
766                 }
767             }
768 
769             if ( StringUtils.isNotBlank( strOperatorDateEnd ) && StringUtils.isNumeric( strOperatorDateBegin ) )
770             {
771                 int nIdOperatorDateEnd = Integer.parseInt( strOperatorDateEnd );
772 
773                 if ( nIdOperatorDateEnd < OperatorEnum.values( ).length )
774                 {
775                     _dtFilter.setOperatorDateEnd( OperatorEnum.values( ) [nIdOperatorDateEnd] );
776                 }
777             }
778 
779             if ( StringUtils.isNotBlank( strUrlDelete ) )
780             {
781                 _dtFilter.setUrlDelete( strUrlDelete );
782             }
783         }
784 
785         return _demandTypeService.findByFilter( _dtFilter );
786     }
787 
788     /**
789      * Get the url of the interface that manages the demand types
790      * 
791      * @param request
792      *            {@link HttpServletRequest}
793      * @return a {@link UrlItem}
794      */
795     private UrlItem getUrlManageDemandTypes( HttpServletRequest request )
796     {
797         UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + JSP_MANAGE_DEMAND_TYPES );
798         url.addParameter( CRMConstants.PARAMETER_SESSION, CRMConstants.PARAMETER_SESSION );
799 
800         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
801 
802         if ( StringUtils.isNotBlank( strSortedAttributeName ) )
803         {
804             String strAscSort = request.getParameter( Parameters.SORTED_ASC );
805 
806             if ( StringUtils.isNotBlank( strAscSort ) )
807             {
808                 url.addParameter( Parameters.SORTED_ASC, strAscSort );
809             }
810 
811             url.addParameter( Parameters.SORTED_ATTRIBUTE_NAME, strSortedAttributeName );
812         }
813 
814         return url;
815     }
816 }