1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 }