View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.rest.service;
35  
36  import com.fasterxml.jackson.core.JsonProcessingException;
37  import com.fasterxml.jackson.databind.ObjectMapper;
38  import fr.paris.lutece.plugins.appointment.modules.rest.business.providers.IAppointmentDataProvider;
39  import fr.paris.lutece.plugins.appointment.modules.rest.business.providers.SolrProvider;
40  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.MeetingPointPOJO;
41  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.SolrMeetingPointPOJO;
42  import fr.paris.lutece.plugins.appointment.modules.rest.pojo.SolrResponseMeetingPointPOJO;
43  import fr.paris.lutece.portal.service.util.AppException;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.util.httpaccess.HttpAccessException;
47  
48  import java.io.IOException;
49  import java.util.ArrayList;
50  import java.util.List;
51  import java.util.regex.Matcher;
52  import java.util.regex.Pattern;
53  
54  public class AppointmentMeetingPointsService
55  {
56  
57      private static final String PROPERTY_WEBSITE_URL = "appointment-rest.website.url";
58  
59      private static IAppointmentDataProvider _dataProvider;
60      private static AppointmentMeetingPointsService _instance;
61      private static String _strWebsiteURL;
62      private static final Pattern ZIP_CITY_PATTERN = Pattern.compile( "(.*)(\\d{5})\\s+(.+)" );
63  
64      public static synchronized AppointmentMeetingPointsService getInstance( )
65      {
66          if ( _instance == null )
67          {
68              _instance = new AppointmentMeetingPointsService( );
69              _instance.init( );
70          }
71  
72          return _instance;
73      }
74  
75      private synchronized void init( )
76      {
77          if ( _dataProvider == null )
78          {
79              _dataProvider = SolrProvider.getInstance( );
80              AppLogService.info( "DatatProvider loaded : " + _dataProvider.getName( ) );
81          }
82  
83          _strWebsiteURL = AppPropertiesService.getProperty( PROPERTY_WEBSITE_URL, MeetingPointPOJO.DEFAULT_WEBSITE_URL_RDV );
84      }
85  
86      public List<MeetingPointPOJO> getManagedMeetingPoints( )
87      {
88          List<MeetingPointPOJO> manegedPoints;
89  
90          try
91          {
92              String response = null;
93              response = _dataProvider.getManagedMeetingPoints( );
94  
95              ObjectMapper objectMapper = new ObjectMapper( );
96              SolrResponseMeetingPointPOJO solrResponse = null;
97  
98              solrResponse = objectMapper.readValue( response, SolrResponseMeetingPointPOJO.class );
99  
100             List<SolrMeetingPointPOJO> solrMeetings = new ArrayList<>( );
101 
102             for ( SolrResponseMeetingPointPOJO.Group group : solrResponse.getGrouped( ).getGroupedByUidForm( ).getGroups( ) )
103             {
104                 if ( group.getGroupValue( ) != null )
105                 {
106                     solrMeetings.addAll( group.getDocList( ).getDocs( ) );
107                 }
108             }
109 
110             manegedPoints = transform( solrMeetings );
111 
112             return manegedPoints;
113         }
114         catch( IOException | HttpAccessException e )
115         {
116             AppLogService.error( e.getMessage( ), e );
117             throw new AppException( e.getMessage( ), e );
118         }
119     }
120 
121     public List<MeetingPointPOJO> transform( List<SolrMeetingPointPOJO> solrMeetings )
122     {
123         List<MeetingPointPOJO> meetingPoints = new ArrayList<>( );
124 
125         for ( SolrMeetingPointPOJO solrMeeting : solrMeetings )
126         {
127             MeetingPointPOJOointment/modules/rest/pojo/MeetingPointPOJO.html#MeetingPointPOJO">MeetingPointPOJO meeting = new MeetingPointPOJO( );
128             meeting.setId( solrMeeting.getUid( ) );
129             if ( solrMeeting.getGeoloc( ) != null && solrMeeting.getGeoloc( ).contains( "," ) )
130             {
131                 String[] geoloc = solrMeeting.getGeoloc( ).split( "," );
132                 meeting.setLatitude( geoloc[0].trim( ) );
133                 meeting.setLongitude( geoloc[1].trim( ) );
134             }
135             if ( solrMeeting.getAddressText( ) != null )
136             {
137                 Matcher matcher = ZIP_CITY_PATTERN.matcher( solrMeeting.getAddressText( ) );
138                 if ( matcher.find( ) )
139                 {
140                     String publicAddress = matcher.group(1).trim();
141 
142                     while (publicAddress.endsWith(","))
143                     {
144                         publicAddress = publicAddress.substring(0, publicAddress.length() - 1).trim();
145                     }
146                     meeting.setPublicAdress( publicAddress );
147                     meeting.setZipCode( matcher.group( 2 ) );
148                     meeting.setCityName( matcher.group( 3 ).trim( ) );
149                 }
150                 else
151                 {
152                     meeting.setPublicAdress( solrMeeting.getAddressText( ) );
153                 }
154             }
155 
156             meeting.setName( solrMeeting.getTitle( ) );
157             meeting.setWebsite( _strWebsiteURL );
158             meetingPoints.add( meeting );
159             meeting.setCityLogo( solrMeeting.getIconUrl( ) );
160         }
161 
162         return meetingPoints;
163     }
164 
165 }