View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.portal.web.search;
35  
36  import fr.paris.lutece.portal.business.rbac.RBAC;
37  import fr.paris.lutece.portal.business.search.SearchParameterHome;
38  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
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.rbac.RBACService;
42  import fr.paris.lutece.portal.service.search.SearchResourceIdService;
43  import fr.paris.lutece.portal.service.search.SearchService;
44  import fr.paris.lutece.portal.service.security.SecurityTokenService;
45  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
48  import fr.paris.lutece.util.ReferenceItem;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   * This class provides the user interface to manage app search features ( manage filters )
56   */
57  public class SearchJspBean extends AdminFeaturesPageJspBean
58  {
59      /** Unique name for the right to manage search parameters */
60      public static final String RIGHT_SEARCH_MANAGEMENT = "CORE_SEARCH_MANAGEMENT";
61  
62      /**
63       * Serial version UID
64       */
65      private static final long serialVersionUID = -2095709285081142039L;
66  
67      // //////////////////////////////////////////////////////////////////////////
68      // Constants
69      private static final String EMPTY_STRING = "";
70  
71      // Jsp url
72      private static final String ANCHOR_ADMIN_DASHBOARDS = "search";
73  
74      // Parameters
75      private static final String PARAMETER_CANCEL = "cancel";
76      private static final String PARAMETER_DATE_FILTER = "date_filter";
77      private static final String PARAMETER_DEFAULT_OPERATOR = "default_operator";
78      private static final String PARAMETER_HELP_MESSAGE = "help_message";
79      private static final String PARAMETER_TAG_FILTER = "tag_filter";
80      private static final String PARAMETER_TYPE_FILTER = "type_filter";
81  
82      /**
83       * Processes the data capture form of advanced parameters
84       * 
85       * @param request
86       *            the HTTP request
87       * @return the jsp URL of the process result
88       * @throws AccessDeniedException
89       *             if permission to manage advanced parameters on search has not been granted to the user
90       */
91      public String doModifyAdvancedParameters( HttpServletRequest request ) throws AccessDeniedException
92      {
93          if ( !RBACService.isAuthorized( SearchService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, SearchResourceIdService.PERMISSION_MANAGE_ADVANCED_PARAMETERS,
94                  getUser( ) ) )
95          {
96              throw new AccessDeniedException(
97                      "User " + getUser( ) + " is not authorized to permission " + SearchResourceIdService.PERMISSION_MANAGE_ADVANCED_PARAMETERS );
98          }
99          if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
100         {
101             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
102         }
103 
104         if ( request.getParameter( PARAMETER_CANCEL ) == null )
105         {
106             String strTypeFilter = request.getParameter( PARAMETER_TYPE_FILTER );
107             String strDefaultOperator = request.getParameter( PARAMETER_DEFAULT_OPERATOR );
108             String strHelpMessage = request.getParameter( PARAMETER_HELP_MESSAGE );
109             String strDateFilter = request.getParameter( PARAMETER_DATE_FILTER );
110             String strTagFilter = request.getParameter( PARAMETER_TAG_FILTER );
111 
112             // mandatory field
113             if ( StringUtils.isBlank( strTypeFilter ) || StringUtils.isBlank( strDefaultOperator ) || StringUtils.isBlank( strDateFilter )
114                     || StringUtils.isBlank( strTagFilter ) )
115             {
116                 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
117             }
118 
119             ReferenceItemceItem.html#ReferenceItem">ReferenceItem param = new ReferenceItem( );
120             param.setCode( PARAMETER_TYPE_FILTER );
121             param.setName( strTypeFilter );
122             SearchParameterHome.update( param );
123 
124             param = new ReferenceItem( );
125             param.setCode( PARAMETER_DEFAULT_OPERATOR );
126             param.setName( strDefaultOperator );
127             SearchParameterHome.update( param );
128 
129             param = new ReferenceItem( );
130             param.setCode( PARAMETER_HELP_MESSAGE );
131             param.setName( StringUtils.isNotBlank( strHelpMessage ) ? strHelpMessage : EMPTY_STRING );
132             SearchParameterHome.update( param );
133 
134             param = new ReferenceItem( );
135             param.setCode( PARAMETER_DATE_FILTER );
136             param.setName( strDateFilter );
137             SearchParameterHome.update( param );
138 
139             param = new ReferenceItem( );
140             param.setCode( PARAMETER_TAG_FILTER );
141             param.setName( strTagFilter );
142             SearchParameterHome.update( param );
143         }
144 
145         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
146     }
147 }