package gov.va.med.esr.common.builder.entity;

import gov.va.med.esr.common.model.ee.HealthBenefitPlan;
import gov.va.med.esr.common.model.lookup.DataChangeSource;
import gov.va.med.esr.common.model.lookup.HealthBenefitPlanType;
import gov.va.med.fw.util.builder.Builder;
import gov.va.med.fw.util.builder.BuilderException;

/**
 * Builds a Health Benefit Plan from Health Benefit Plan metadata.
 *  
 *  CCR12064 
 * @author DNS   zhangs Created on Feburary, 2013
 * @version 1.0
 * 
 * Copyright © 2013 VHA. All rights reserved
 */
public class HealthBenefitPlanBuilder extends EntityBuilder
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8317331907031635156L;

    private Builder typeBuilder;
    private Builder changeSourceBuilder;

    public HealthBenefitPlanBuilder()
    {
        super();
    }

    public Builder getTypeBuilder()
    {
        return this.typeBuilder;
    }

    public void setTypeBuilder(Builder typeBuilder)
    {
        this.typeBuilder = typeBuilder;
    }

    
    
    
    public HealthBenefitPlan build(HealthBenefitPlanMetaData metaData) throws BuilderException
    {
    	HealthBenefitPlan input = metaData.getEntity();
    	HealthBenefitPlan output = (input == null) ? new HealthBenefitPlan() : input;
        this.transfer(output, metaData);

        return this.shouldKeep(output) ? output : null;
    }

    private HealthBenefitPlanType buildType(HealthBenefitPlanType input, String code)
            throws BuilderException
    {
        return (HealthBenefitPlanType) super.build(this.typeBuilder, input, code);
    }
    
    private DataChangeSource buildChangeSource(DataChangeSource input, String source)
    throws BuilderException
   {
            return (DataChangeSource) super.build(this.changeSourceBuilder, input, source);
   }

    private boolean shouldKeep(HealthBenefitPlan obj)
    {
        return (obj.getPlanType() != null);
    }

    private void transfer(HealthBenefitPlan input, HealthBenefitPlanMetaData metaData)
            throws BuilderException
    {
        input.setPlanType(this.buildType(input.getPlanType(),metaData.getHBPCode()));
               
        input.setChangeDate(super.build(input.getChangeDate(), metaData.getHBPLastUpdatedDate()));
        
        input.setChangeSite(super.build(input.getChangeSite(), metaData.getHBPLastUpdatedBySite()));
        input.setChangeSource(this.buildChangeSource(input.getChangeSource(), metaData.getHBPLastUpdatedBySource()));  
           
        
    }

	public Builder getChangeSourceBuilder() {
		return changeSourceBuilder;
	}

	public void setChangeSourceBuilder(Builder changeSourceBuilder) {
		this.changeSourceBuilder = changeSourceBuilder;
	}
}

