/*
 * Created on Dec 27, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.signature.action;

import gov.va.med.esr.ui.common.service.LookupCacheService;

import gov.va.med.esr.common.model.lookup.SignatureIndicator;
import gov.va.med.esr.common.model.person.SignatureImage;
import gov.va.med.esr.ui.conversion.UIConversionServiceImpl;
import gov.va.med.fw.conversion.ConversionServiceException;

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

	/**
	 * 
	 */
	public SignatureConversionService() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	protected void convertBean(Object source, Object target)
	throws ConversionServiceException
	{
		log.trace("SignatureConversionService postProcessConvert() is called.");
        LookupCacheService lookupCacheService = getLookupCacheService();
		//PersonHelperService helper = getPersonHelperService();
		
		//convert the Signature onto SignatureEditForm
		if (source instanceof SignatureImage)
		{
			if (target instanceof SignatureEditForm)
			{
				SignatureImage signatureImage = (SignatureImage)source;
				SignatureEditForm formBean = (SignatureEditForm)target;
				formBean.setImageId(signatureImage.getImageIdNumber());
				formBean.setSubmittingFacility(signatureImage.getSiteSubmittingImage());
				formBean.setDateIndexed(signatureImage.getDateIndex());
				formBean.setIndexedBy(signatureImage.getUser());
				if (signatureImage.getSignatureIndicator() != null)
					formBean.setSignatureonFile(signatureImage.getSignatureIndicator().getCode());
				formBean.setLastUpdated(signatureImage.getModifiedOn());
			}
			else
			{
				throw new ConversionServiceException("Type mismatch: Expecting SignatureImage, SignatureEditForm Received " +
						source.getClass().getName() + " and " + target.getClass().getName());
			}
		}
		else if (target instanceof SignatureImage)
		{
			if (source instanceof SignatureEditForm)
			{
	            try
	            {
					SignatureImage image = (SignatureImage)target;
					SignatureEditForm formBean = (SignatureEditForm)source;
					image.setSignatureIndicator((SignatureIndicator)lookupCacheService.getByCodeFromCache(
			        		SignatureIndicator.class, formBean.getSignatureonFile()));
	            }
	            catch (Exception ex)
	            {
	                throw new ConversionServiceException("Conversion Failed", ex);
	            }
			}
			else
			{
				throw new ConversionServiceException("Type mismatch: Expecting SignatureImage and SignatureEditForm Received " +
						target.getClass().getName() + " and " + source.getClass().getName());
			}
		}
		
	}
}