AppointmentGruService.java
/*
* Copyright (c) 2002-2022, City of Paris
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice
* and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* License 1.0
*/
package fr.paris.lutece.plugins.appointmentgru.services;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import fr.paris.lutece.plugins.appointment.business.appointment.Appointment;
import fr.paris.lutece.plugins.appointment.service.AppointmentResponseService;
import fr.paris.lutece.plugins.appointmentgru.business.AppointmentGru;
import fr.paris.lutece.plugins.genericattributes.business.Entry;
import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
import fr.paris.lutece.plugins.genericattributes.business.Response;
import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManager;
import fr.paris.lutece.plugins.modulenotifygrumappingmanager.business.NotifygruMappingManagerHome;
import fr.paris.lutece.portal.service.spring.SpringContextService;
import fr.paris.lutece.portal.service.util.AppLogService;
/**
* The Class AppointmentGruService.
*/
public class AppointmentGruService
{
// Beans
public static final String BEAN_NAME = "appointmentgru.appointmentGruService";
/** Instance of the service. */
private static volatile AppointmentGruService _instance;
/**
* Singleton AppointmentGruService Get an instance of the service.
*
* @return An instance of the service
*/
public static AppointmentGruService getService( )
{
if ( _instance == null )
{
_instance = SpringContextService.getBean( BEAN_NAME );
}
return _instance;
}
/**
* Gets the appointment gru.
*
* @param appointment
* the appointment
* @param strMappingKey
* the mappnig key
* @return the appointment gru
*/
public AppointmentGru getAppointmentGru( Appointment appointment, String strMappingKey )
{
AppointmentGru appointmentGru = new AppointmentGru( appointment );
if ( AppLogService.isDebugEnabled( ) )
{
AppLogService.debug( "AppointmentGru : GUID from appointment Cuid: " + appointment.getGuid( ) );
}
// provisioning
if ( appointment != null )
{
appointmentGru.setGuid( appointment.getGuid( ) );
String strMobilePhone = getMobilePhoneNumber( appointment, strMappingKey );
if ( strMobilePhone != null )
{
appointmentGru.setMobilePhoneNumber( strMobilePhone );
}
}
appointmentGru.setDemandeTypeId( getDemandeTypeId( strMappingKey ) );
return appointmentGru;
}
/**
* Gets the mobile phone number.
*
* @param appointment
* the appointment
* @param strMappingKey
* the mapping id
* @return the mobile phone number
*/
private String getMobilePhoneNumber( Appointment appointment, String strMappingKey )
{
NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strMappingKey );
String strPhoneNumber = StringUtils.EMPTY;
List<Response> listResponses = AppointmentResponseService.findListResponse( appointment.getIdAppointment( ) );
if ( mapping != null )
{
for ( Response response : listResponses )
{
Entry entry = EntryHome.findByPrimaryKey( response.getEntry( ).getIdEntry( ) );
// (ignore conditional entries, that may have duplicate position number)
if ( entry.getFieldDepend( ) == null && entry.getPosition( ) == mapping.getMobilePhoneNumber( ) )
{
strPhoneNumber = response.getResponseValue( );
break;
}
}
}
return strPhoneNumber;
}
/**
* get demand type id from Mapping
* @param strKey
* @return the demand type _id
*/
private int getDemandeTypeId( String strMappingKey )
{
NotifygruMappingManager mapping = NotifygruMappingManagerHome.findByPrimaryKey( strMappingKey );
if ( mapping != null )
{
return mapping.getDemandeTypeId( );
}
else
{
return 0;
}
}
}