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.plugins.appointment.modules.solrsearchapp.service;
35  
36  import java.util.AbstractMap.SimpleImmutableEntry;
37  import java.time.format.DateTimeFormatter;
38  import java.util.Arrays;
39  import java.util.Collections;
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  
47  public final class Utilities
48  {
49      public static final String PROPERTY_DATE_FORMAT_INPUT_DATE = "appointment-solrsearchapp.app.date.format";
50  
51      public static final String PARAMETER_DAYS_OF_WEEK = "days_of_week";
52      public static final String PARAMETER_FROM_DATE = "from_date";
53      public static final String PARAMETER_FROM_TIME = "from_time";
54      public static final String PARAMETER_TO_DATE = "to_date";
55      public static final String PARAMETER_TO_TIME = "to_time";
56      public static final String PARAMETER_FROM_DAY_MINUTE = "from_day_minute";
57      public static final String PARAMETER_TO_DAY_MINUTE = "to_day_minute";
58      public static final String PARAMETER_NB_SLOTS = "nb_consecutive_slots";
59      public static final String PARAMETER_ROLE = "role";
60      public static final String DEFAULT_DATE_FORMAT_INPUT_DATE = "dd/MM/yyyy";
61      // Bad because it will not reload on property reload, but then caching DateTimeFormatter becomes a pain..
62      public static final String DATE_FORMAT_INPUT_DATE = AppPropertiesService.getProperty( PROPERTY_DATE_FORMAT_INPUT_DATE, DEFAULT_DATE_FORMAT_INPUT_DATE );
63      public static final String DATE_FORMAT_INPUT_TIME = "HH:mm";
64      public static final String FORMAT_INPUT_DATE = DATE_FORMAT_INPUT_DATE + " " + DATE_FORMAT_INPUT_TIME;
65      public static final DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern( FORMAT_INPUT_DATE );
66      public static final String FORMAT_OUTPUT_DATE = "yyyy-MM-dd'T'HH:mm:ss'Z'";
67      public static final DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern( FORMAT_OUTPUT_DATE );
68  
69      public static final String PARAMETER_SITE = "site";
70      public static final String PARAMETER_CATEGORY = "category";
71      public static final String PARAMETER_FORM = "form";
72  
73      public static final String MARK_ITEM_SITES = "items_sites";
74      public static final String MARK_ITEM_CATEGORIES = "items_categories";
75      public static final String MARK_ITEM_FORMS = "items_forms";
76  
77      public static final String [ ] LIST_DAYS_CODE = {
78              "1", "2", "3", "4", "5", "6", "7"
79      };
80      public static final List<SimpleImmutableEntry<String, String>> LIST_DAYS = Collections
81              .unmodifiableList( Arrays.asList( new SimpleImmutableEntry<>( "1", "module.appointment.solrsearchapp.days.1" ),
82                      new SimpleImmutableEntry<>( "2", "module.appointment.solrsearchapp.days.2" ),
83                      new SimpleImmutableEntry<>( "3", "module.appointment.solrsearchapp.days.3" ),
84                      new SimpleImmutableEntry<>( "4", "module.appointment.solrsearchapp.days.4" ),
85                      new SimpleImmutableEntry<>( "5", "module.appointment.solrsearchapp.days.5" ),
86                      new SimpleImmutableEntry<>( "6", "module.appointment.solrsearchapp.days.6" ),
87                      new SimpleImmutableEntry<>( "7", "module.appointment.solrsearchapp.days.7" ) ) );
88  
89      private Utilities( )
90      {
91          // private constructor
92      }
93  
94      public static String getSearchParameterValue( String parameter, HttpServletRequest request, Map<String, String> savedSearchParameters )
95      {
96          String requestValue = getSearchParameter( parameter, request, savedSearchParameters );
97          if ( requestValue != null )
98          {
99              String [ ] requestSplit = requestValue.split( "\\|" );
100             return requestSplit [0];
101         }
102         else
103         {
104             return null;
105         }
106     }
107 
108     public static String getSearchParameter( String parameter, HttpServletRequest request, Map<String, String> savedSearchParameters )
109     {
110         String result = request.getParameter( parameter );
111         if ( result != null )
112         {
113             return result;
114         }
115         result = savedSearchParameters.get( parameter );
116         return result;
117     }
118 
119     public static String [ ] getSearchMultiParameter( String parameter, HttpServletRequest request, Map<String, String [ ]> savedMultiSearchParameters )
120     {
121         String [ ] result = request.getParameterValues( parameter );
122         if ( result != null )
123         {
124             return result;
125         }
126         result = savedMultiSearchParameters.get( parameter );
127         return result;
128     }
129 
130     public static String getSearchParameterLabel( String parameter, HttpServletRequest request, Map<String, String> savedSearchParameters )
131     {
132         String requestValue = getSearchParameter( parameter, request, savedSearchParameters );
133         if ( requestValue != null )
134         {
135             String [ ] requestSplit = requestValue.split( "\\|" );
136             return requestSplit [requestSplit.length - 1];
137         }
138         else
139         {
140             return null;
141         }
142     }
143 
144 }