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.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   * The Class AppointmentGruService.
58   */
59  public class AppointmentGruService
60  {
61      // Properties
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      // Beans
70      public static final String BEAN_NAME = "appointmentgru.appointmentGruService";
71  
72      /** Instance of the service. */
73      private static volatile AppointmentGruService _instance;
74  
75      /**
76       * Singleton AppointmentGruService Get an instance of the service.
77       *
78       * @return An instance of the service
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       * Gets the appointment gru.
92       *
93       * @param appointment
94       *            the appointment
95       * @param strKey
96       *            the str key
97       * @return the appointment gru
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         // call provisioning
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      * Builds an identity from appointment.
129      *
130      * @param appointment
131      *            the appointment
132      * @param strKey
133      *            the key
134      * @return the identity
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      * build an attributeDTO
158      * 
159      * @param strCode
160      *            the attribute code
161      * @param strValue
162      *            the attribute value
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      * Gets the mobile phone number.
175      *
176      * @param appointment
177      *            the appointment
178      * @param strKey
179      *            the str key
180      * @return the mobile phone number
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      * Gets the fixed phone number.
206      *
207      * @param appointment
208      *            the appointment
209      * @param strKey
210      *            the str key
211      * @return the fixed phone number
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 }