View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.plugins.mylutece.modules.database.authentication.business;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  import fr.paris.lutece.util.url.UrlItem;
39  
40  import java.io.UnsupportedEncodingException;
41  
42  import java.net.URLEncoder;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  /**
47   * This class describes a workgroup used by the administration
48   */
49  public class GroupFilter
50  {
51      // Constants
52      private static final String EQUAL = "=";
53      private static final String AMPERSAND = "&";
54  
55      // Parameters
56      private static final String PARAMETER_SEARCH_IS_SEARCH = "search_is_search";
57      private static final String PARAMETER_SEARCH_KEY = "search_key";
58      private static final String PARAMETER_SEARCH_DESCRIPTION = "search_description";
59  
60      // Properties
61      private static final String PROPERTY_ENCODING_URL = "lutece.encoding.url";
62      private String _strKey;
63      private String _strDescription;
64  
65      /**
66       * Initialize each component of the object
67       */
68      public void init( )
69      {
70          _strKey = "";
71          _strDescription = "";
72      }
73  
74      /**
75       * Gets the workgroup key
76       * 
77       * @return Returns the Key.
78       */
79      public String getKey( )
80      {
81          return _strKey;
82      }
83  
84      /**
85       * Sets the workgroup key
86       *
87       * @param strKey
88       *            The Key
89       */
90      public void setKey( String strKey )
91      {
92          _strKey = strKey;
93      }
94  
95      /**
96       * Returns the workgroup's description
97       * 
98       * @return Returns the Description.
99       */
100     public String getDescription( )
101     {
102         return _strDescription;
103     }
104 
105     /**
106      * Sets the workgroup's description
107      *
108      * @param strDescription
109      *            The workgroup's description
110      */
111     public void setDescription( String strDescription )
112     {
113         _strDescription = strDescription;
114     }
115 
116     /**
117      * Gets the workgroup key
118      *
119      * @return The workgroup key
120      */
121     public String getWorkgroup( )
122     {
123         return getKey( );
124     }
125 
126     /**
127      * Set the value of the AdminWorkgroupFilter
128      * 
129      * @param request
130      *            HttpServletRequest
131      * @return true if there is a search
132      */
133     public boolean setGroupFilter( HttpServletRequest request )
134     {
135         boolean bIsSearch = false;
136         String strIsSearch = request.getParameter( PARAMETER_SEARCH_IS_SEARCH );
137 
138         if ( strIsSearch != null )
139         {
140             bIsSearch = true;
141             _strKey = request.getParameter( PARAMETER_SEARCH_KEY );
142             _strDescription = request.getParameter( PARAMETER_SEARCH_DESCRIPTION );
143         }
144         else
145         {
146             init( );
147         }
148 
149         return bIsSearch;
150     }
151 
152     /**
153      * Build url attributes
154      * 
155      * @param url
156      *            the url
157      */
158     public void setUrlAttributes( UrlItem url )
159     {
160         url.addParameter( PARAMETER_SEARCH_IS_SEARCH, Boolean.TRUE.toString( ) );
161 
162         try
163         {
164             url.addParameter( PARAMETER_SEARCH_KEY, URLEncoder.encode( _strKey, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
165             url.addParameter( PARAMETER_SEARCH_DESCRIPTION, URLEncoder.encode( _strDescription, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
166         }
167         catch( UnsupportedEncodingException e )
168         {
169             AppLogService.error( e );
170         }
171     }
172 
173     /**
174      * Build url attributes
175      * 
176      * @return the url attributes
177      */
178     public String getUrlAttributes( )
179     {
180         StringBuilder sbUrlAttributes = new StringBuilder( );
181         sbUrlAttributes.append( PARAMETER_SEARCH_IS_SEARCH + EQUAL + Boolean.TRUE );
182 
183         try
184         {
185             sbUrlAttributes.append(
186                     AMPERSAND + PARAMETER_SEARCH_KEY + EQUAL + URLEncoder.encode( _strKey, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
187             sbUrlAttributes.append( AMPERSAND + PARAMETER_SEARCH_DESCRIPTION + EQUAL
188                     + URLEncoder.encode( _strDescription, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
189         }
190         catch( UnsupportedEncodingException e )
191         {
192             AppLogService.error( e );
193         }
194 
195         return sbUrlAttributes.toString( );
196     }
197 }