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.appointmentgru.services;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 import org.apache.commons.lang.StringUtils;
41
42 import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
43 import fr.paris.lutece.plugins.appointment.service.AppointmentResponseService;
44 import fr.paris.lutece.plugins.appointmentgru.business.AppointmentGru;
45 import fr.paris.lutece.plugins.genericattributes.business.Entry;
46 import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
47 import fr.paris.lutece.plugins.genericattributes.business.Response;
48 import fr.paris.lutece.plugins.identitystore.v2.web.rs.dto.AttributeDto;
49 import fr.paris.lutece.plugins.identitystore.v2.web.rs.dto.IdentityDto;
50 import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManager;
51 import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManagerHome;
52 import fr.paris.lutece.portal.service.spring.SpringContextService;
53 import fr.paris.lutece.portal.service.util.AppLogService;
54 import fr.paris.lutece.portal.service.util.AppPropertiesService;
55
56
57
58
59 public class AppointmentGruService
60 {
61
62 private static final String PROPERTIES_ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER = "appointmentgru.attribute.user.home-info.telecom.telephone.number";
63 private static final String PROPERTIES_ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER = "appointmentgru.attribute.user.home-info.telecom.mobile.number";
64 private static final String ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER = AppPropertiesService
65 .getProperty( PROPERTIES_ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER );
66 private static final String ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER = AppPropertiesService
67 .getProperty( PROPERTIES_ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER );
68
69
70 public static final String BEAN_NAME = "appointmentgru.appointmentGruService";
71
72
73 private static volatile AppointmentGruService _instance;
74
75
76
77
78
79
80 public static AppointmentGruService getService( )
81 {
82 if ( _instance == null )
83 {
84 _instance = SpringContextService.getBean( BEAN_NAME );
85 }
86
87 return _instance;
88 }
89
90
91
92
93
94
95
96
97
98
99 public AppointmentGru getAppointmentGru( Appointment appointment, String strKey )
100 {
101 AppointmentGru/business/AppointmentGru.html#AppointmentGru">AppointmentGru appointmentGru = new AppointmentGru( appointment );
102 if ( AppLogService.isDebugEnabled( ) )
103 {
104 AppLogService.debug( "AppointmentGru : GUID from appointment Cuid: " + appointment.getGuid( ) );
105 }
106
107 IdentityDto identityDto = buildIdentity( appointment, strKey );
108
109
110 if ( identityDto != null )
111 {
112 appointmentGru.setGuid( identityDto.getConnectionId( ) );
113 appointmentGru.setCuid( identityDto.getCustomerId( ) );
114
115 AttributeDto attributeMobilePhone = identityDto.getAttributes( ).get( ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER );
116
117 if ( attributeMobilePhone != null )
118 {
119 appointmentGru.setMobilePhoneNumber( attributeMobilePhone.getValue( ) );
120 }
121 }
122 appointmentGru.setDemandeTypeId( getDemandeTypeId( appointment, strKey ) );
123
124 return appointmentGru;
125 }
126
127
128
129
130
131
132
133
134
135
136 private IdentityDto buildIdentity( Appointment appointment, String strKey )
137 {
138 IdentityDto identityDto = null;
139 Map<String, AttributeDto> mapAttributes = new HashMap<>( );
140
141 if ( appointment != null )
142 {
143 identityDto = new IdentityDto( );
144
145 identityDto.setConnectionId( appointment.getGuid( ) );
146 mapAttributes.put( ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER,
147 buildAttribute( ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER, getMobilePhoneNumber( appointment, strKey ) ) );
148 mapAttributes.put( ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER,
149 buildAttribute( ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER, getFixedPhoneNumber( appointment, strKey ) ) );
150 identityDto.setAttributes( mapAttributes );
151 }
152
153 return identityDto;
154 }
155
156
157
158
159
160
161
162
163
164 private static AttributeDto buildAttribute( String strCode, String strValue )
165 {
166 AttributeDto attributeDto = new AttributeDto( );
167 attributeDto.setKey( strCode );
168 attributeDto.setValue( strValue );
169
170 return attributeDto;
171 }
172
173
174
175
176
177
178
179
180
181
182 private String getMobilePhoneNumber( Appointment appointment, String strKey )
183 {
184 NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strKey );
185 String strPhoneNumber = StringUtils.EMPTY;
186 List<Response> listResponses = AppointmentResponseService.findListResponse( appointment.getIdAppointment( ) );
187
188 if ( mapping != null )
189 {
190 for ( Response response : listResponses )
191 {
192 Entry entry = EntryHome.findByPrimaryKey( response.getEntry( ).getIdEntry( ) );
193
194 if ( entry.getPosition( ) == mapping.getMobilePhoneNumber( ) )
195 {
196 strPhoneNumber = response.getResponseValue( );
197 }
198 }
199 }
200
201 return strPhoneNumber;
202 }
203
204
205
206
207
208
209
210
211
212
213 private String getFixedPhoneNumber( Appointment appointment, String strKey )
214 {
215 NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strKey );
216 String strPhoneNumber = StringUtils.EMPTY;
217 List<Response> listResponses = AppointmentResponseService.findListResponse( appointment.getIdAppointment( ) );
218
219 if ( mapping != null )
220 {
221 for ( Response response : listResponses )
222 {
223 Entry entry = EntryHome.findByPrimaryKey( response.getEntry( ).getIdEntry( ) );
224
225 if ( entry.getPosition( ) == mapping.getFixedPhoneNumber( ) )
226 {
227 strPhoneNumber = response.getResponseValue( );
228 }
229 }
230 }
231
232 return strPhoneNumber;
233 }
234
235 private int getDemandeTypeId( Appointment appointment, String strKey )
236 {
237 NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strKey );
238
239 if ( mapping != null )
240 {
241 return mapping.getDemandeTypeId( );
242 }
243 else
244 {
245 return 0;
246 }
247 }
248 }