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.List;
37  
38  import org.apache.commons.lang.StringUtils;
39  
40  import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
41  import fr.paris.lutece.plugins.appointment.service.AppointmentResponseService;
42  import fr.paris.lutece.plugins.appointmentgru.business.AppointmentGru;
43  import fr.paris.lutece.plugins.genericattributes.business.Entry;
44  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
45  import fr.paris.lutece.plugins.genericattributes.business.Response;
46  import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManager;
47  import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManagerHome;
48  import fr.paris.lutece.portal.service.spring.SpringContextService;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  
51  /**
52   * The Class AppointmentGruService.
53   */
54  public class AppointmentGruService
55  {
56      // Beans
57      public static final String BEAN_NAME = "appointmentgru.appointmentGruService";
58  
59      /** Instance of the service. */
60      private static volatile AppointmentGruService _instance;
61  
62      /**
63       * Singleton AppointmentGruService Get an instance of the service.
64       *
65       * @return An instance of the service
66       */
67      public static AppointmentGruService getService( )
68      {
69          if ( _instance == null )
70          {
71              _instance = SpringContextService.getBean( BEAN_NAME );
72          }
73  
74          return _instance;
75      }
76  
77      /**
78       * Gets the appointment gru.
79       *
80       * @param appointment
81       *            the appointment
82       * @param strMappingKey
83       *            the mappnig key
84       * @return the appointment gru
85       */
86      public AppointmentGru getAppointmentGru( Appointment appointment, String strMappingKey )
87      {
88          AppointmentGru/business/AppointmentGru.html#AppointmentGru">AppointmentGru appointmentGru = new AppointmentGru( appointment );
89          if ( AppLogService.isDebugEnabled( ) )
90          {
91              AppLogService.debug( "AppointmentGru  : GUID from appointment Cuid: " + appointment.getGuid( ) );
92          }
93  
94          // provisioning
95          if ( appointment != null )
96          {
97              appointmentGru.setGuid( appointment.getGuid( ) );
98             
99              String strMobilePhone = getMobilePhoneNumber( appointment, strMappingKey );
100 
101             if ( strMobilePhone != null )
102             {
103                 appointmentGru.setMobilePhoneNumber( strMobilePhone );
104             }
105         }
106         appointmentGru.setDemandeTypeId( getDemandeTypeId( strMappingKey ) );
107 
108         return appointmentGru;
109     }
110 
111     /**
112      * Gets the mobile phone number.
113      *
114      * @param appointment
115      *            the appointment
116      * @param strMappingKey
117      *            the mapping id
118      * @return the mobile phone number
119      */
120     private String getMobilePhoneNumber( Appointment appointment, String strMappingKey )
121     {
122         NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strMappingKey );
123         String strPhoneNumber = StringUtils.EMPTY;
124         List<Response> listResponses = AppointmentResponseService.findListResponse( appointment.getIdAppointment( ) );
125 
126         if ( mapping != null )
127         {
128             for ( Response response : listResponses )
129             {
130                 Entry entry = EntryHome.findByPrimaryKey( response.getEntry( ).getIdEntry( ) );
131 
132                 // (ignore conditional entries, that may have duplicate position number)
133                 if ( entry.getFieldDepend( ) == null && entry.getPosition( ) == mapping.getMobilePhoneNumber( ) )
134                 {
135                     strPhoneNumber = response.getResponseValue( );
136                     break;
137                 }                
138             }
139         }
140         return strPhoneNumber;
141     }
142 
143     /**
144      * get demand type id from Mapping
145      * @param strKey
146      * @return the demand type _id
147      */
148     private int getDemandeTypeId( String strMappingKey )
149     {
150         NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strMappingKey );
151 
152         if ( mapping != null )
153         {
154             return mapping.getDemandeTypeId( );
155         }
156         else
157         {
158             return 0;
159         }
160     }
161 }