import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.expedia.e3.platform.foundation.core.beans.IInitializingBean;
import com.expedia.e3.platform.foundation.core.configuration.frontend.ISettingFamily;
import com.expedia.e3.platform.foundation.core.logging.EventLogEntry;
/**
* Timer task that fetches the mapping data and updates ecom to hcom and hcom to ecom mapping
*
* @author <a href="mailto:[email protected]">Prashanth</a>
*
*/
public class HotelIdMapper extends TimerTask implements IHotelIdMapper, IInitializingBean
{
private static final String ZIP_FILE_ENTRY_NAME_SETTING = "zipFileEntryName";
private static final String ZIP_FILE_NAME_SETTING = "zipFileName";
private static final String ENTRY_ELEMENT = "Entry";
private static final String HCOM_ID_ATTRIBUTE = "hcomId";
private static final String EXPEDIA_ID_ATTRIBUTE = "expediaId";
private static final String HOTEL_ID_MAPPING_UPDATE_INTERVAL = "updateMillis";
private static final Logger LOGGER = Logger.getLogger(HotelIdMapper.class);
private Map<Integer, Integer> m_eComToHComHotelIdMap = new HashMap<Integer, Integer>();
private Map<Integer, Integer> m_hComToEComHotelIdMap = new HashMap<Integer, Integer>();
private ISettingFamily m_settingFamily;
/**
* maps an e.com hotel id to an h.com hotelid
* @param eComHotelId
* @return corresponding h.com id. null if none is found
*/
public Integer getHComHotelId(int eComHotelId)
{
if (eComHotelId <= 0 || !m_eComToHComHotelIdMap.containsKey(eComHotelId))
{
return null;
}
else
{
return m_eComToHComHotelIdMap.get(eComHotelId);
}
}
/**
* maps an c.com hotel id to an e.com hotel id
* @param hComHotelId
* @return corresponding e.com id. null if none is found
*/
public Integer getEComHotelId(int hComHotelId)
{
if (hComHotelId <= 0 || !m_hComToEComHotelIdMap.containsKey(hComHotelId))
{
return null;
}
else
{
return m_hComToEComHotelIdMap.get(hComHotelId);
}
}
/**
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception
{
run();
Timer timer = new Timer();
long period = m_settingFamily.getLong(HOTEL_ID_MAPPING_UPDATE_INTERVAL);
timer.schedule(this, period, period);
}
/**
* Setter of setting family
*
* @param settingFamily
*/
public void setSettingFamily(ISettingFamily settingFamily)
{
m_settingFamily = settingFamily;
}
@Override
public void run()
{
try
{
String zipFileName = m_settingFamily.getString(ZIP_FILE_NAME_SETTING);
String zipFileEntryName = m_settingFamily.getString(ZIP_FILE_ENTRY_NAME_SETTING);
updateMappingFromZipFile(zipFileName, zipFileEntryName);
}
catch (IOException ioe)
{
EventLogEntry.logEvent(LOGGER, Level.ERROR, EventLogIds.HOTELID_MAPPING_UNAVAILABLE,
ErrorMessage.HOTEL_ID_MAPPING_FAILURE, ioe);
}
catch (XMLStreamException xse)
{
EventLogEntry.logEvent(LOGGER, Level.ERROR, EventLogIds.HOTELID_MAPPING_UNAVAILABLE,
ErrorMessage.HOTEL_ID_MAPPING_FAILURE, xse);
}
}
private void updateMappingFromZipFile(String zipFilePath, String zipEntryName)
throws IOException, XMLStreamException
{
Map<Integer, Integer> newEToHComHotelIdMap = new HashMap<Integer, Integer>();
Map<Integer, Integer> newHToEComHotelIdMap = new HashMap<Integer, Integer>();
ZipFile zipFile = new ZipFile(zipFilePath);
ZipEntry zipEntry = zipFile.getEntry(zipEntryName);
if(zipEntry != null)
{
try
{
InputStream inputStream = zipFile.getInputStream(zipEntry);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(inputStream);
int expediaId = 0;
int hcomId = 0;
//stream the xml file and create ecom-hcom mappings
while (xmlStreamReader.hasNext())
{
int event = xmlStreamReader.next();
if (event == XMLStreamConstants.START_ELEMENT
&& xmlStreamReader.getLocalName().equals(ENTRY_ELEMENT))
{
String attrValueExpediaId = null;
String attrValueHcomId = null;
try
{
attrValueExpediaId = xmlStreamReader.getAttributeValue(null, EXPEDIA_ID_ATTRIBUTE);
attrValueHcomId = xmlStreamReader.getAttributeValue(null, HCOM_ID_ATTRIBUTE);
if(attrValueExpediaId != null && attrValueHcomId != null)
{
expediaId = Integer.parseInt(attrValueExpediaId);
hcomId = Integer.parseInt(attrValueHcomId);
newEToHComHotelIdMap.put(expediaId, hcomId);
newHToEComHotelIdMap.put(hcomId, expediaId);
}
else
{
EventLogEntry.logEvent(LOGGER, Level.ERROR,
EventLogIds.HOTELID_INVALID_MAPPING_ENTRIES_ID,
String.format(ErrorMessage.ERROR_INVALID_HOTEL_ID,
attrValueExpediaId, attrValueHcomId));
}
}
catch (NumberFormatException nfe)
{
EventLogEntry.logEvent(LOGGER, Level.ERROR,
EventLogIds.HOTELID_INVALID_MAPPING_ENTRIES_ID,
String.format(ErrorMessage.ERROR_INVALID_HOTEL_ID,
attrValueExpediaId, attrValueHcomId));
}
}
}
m_eComToHComHotelIdMap = newEToHComHotelIdMap;
m_hComToEComHotelIdMap = newHToEComHotelIdMap;
EventLogEntry.logEvent(LOGGER, Level.INFO, EventLogIds.HOTEL_ID_MAP_SUCCESS,
ErrorMessage.HOTEL_ID_MAPPING_SUCCESS);
}
finally
{
zipFile.close();
}
}
}
}