/*
 * Created on Sep 29, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.service;

import gov.va.med.esr.common.model.person.Person;
import gov.va.med.esr.common.model.person.id.PersonIdEntityKeyImpl;
import gov.va.med.esr.common.persistent.person.PersonDAO;
import gov.va.med.esr.common.util.RuleAbstractTestCase;
import gov.va.med.esr.service.trigger.BulletinTrigger;
import gov.va.med.esr.service.trigger.BulletinTriggerEvent;
import gov.va.med.esr.service.trigger.LetterTriggerEvent;
import gov.va.med.fw.persistent.DAOException;
import gov.va.med.fw.service.ServiceException;
import gov.va.med.fw.service.trigger.TriggerEvent;
import gov.va.med.fw.service.trigger.TriggerRouter;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author DNS   TSAIG
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TestCommsConsumer extends RuleAbstractTestCase{

	private TriggerRouter triggerRouter = null;
	private Person person = null;
	
    public TestCommsConsumer(String testName) {
        super(testName);

    }
	public void testTriggerBulletin() 
	{
	    try{
        	person = getPersonById(new BigDecimal(71159));
	    } catch (Exception dex)
	    {
	        fail("#### Fail to get a person with id: 71159 ####", dex);
	    }
	    
	    try{
		    if (triggerRouter == null)
		        triggerRouter = (TriggerRouter)applicationContext.getBean("common.triggerEventRouter");
		    
		    //personID: 71941- 71945, 71947, 71948 
		    TriggerEvent triggerEvent = null;
	
	        BulletinTriggerEvent bulletinTriggerEvent =
	            new BulletinTriggerEvent(BulletinTrigger.DataType.HEC_NOTIFY_PURPLE_HEART_NO_DECISION);
	        bulletinTriggerEvent.addField(BulletinTriggerEvent.HecNotifyPurplseHeartNoDecision.EFFECTIVE_DATE, new Date());
	        bulletinTriggerEvent.addField(BulletinTriggerEvent.HecNotifyPurplseHeartNoDecision.REJECTED_REMARK,
	            "Incomplete information on file.");
	        bulletinTriggerEvent.setPersonId(person.getPersonEntityKey());
	        triggerEvent = bulletinTriggerEvent;
	
			// Process the event
			triggerRouter.processTriggerEvent(triggerEvent);
	    } catch (ServiceException ex)
	    {
	        fail("#### Fail to process a trigger bulletin event ####", ex);	        
	    }
	}
	
	public void triggerLetter()
	{
	    try{
	        	person = getPersonById(new BigDecimal(71159));
	    } catch (Exception dex)
	    {
	        fail("#### Fail to get a person with id: 71159 ####", dex);
	    }
		    
		    
		try{
		    if (triggerRouter == null)
		        triggerRouter = (TriggerRouter)applicationContext.getBean("common.triggerEventRouter");
	
		    //personID: 71941- 71945, 71947, 71948 
		    TriggerEvent triggerEvent = null;
	
	        LetterTriggerEvent letterTriggerEvent = new LetterTriggerEvent(getLookupService().getComLetterTemplateTypeByCode("742-600C"));
	        letterTriggerEvent.setPersonId(person.getPersonEntityKey());
	        triggerEvent = letterTriggerEvent;
	
			// Process the event
			triggerRouter.processTriggerEvent(triggerEvent);
		} catch (ServiceException ex)
		{
		    fail("#### Fail to process a trigger letter event ####", ex);	        
		}
	}
	
	private Person getPersonById(BigDecimal personId) throws DAOException, ServiceException
	{
	    if (personId == null)
	        return null;
        //get a person
        PersonDAO personDao = (PersonDAO)applicationContext.getBean("personDAO");
        return (Person) personDao.getByKey(
                new PersonIdEntityKeyImpl(personId));
        
	}

}
