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

import gov.va.med.esr.common.model.financials.SpouseFinancials;
import gov.va.med.esr.common.model.person.Spouse;
import gov.va.med.fw.util.builder.Builder;
import gov.va.med.fw.util.builder.BuilderException;

/**
 * 
 * @author Rajiv Patnaik
 * Created on Sep 13, 2005
 * @version 1.0
 * 
 * Copyright © 2005 VHA. All rights reserved
 */
public class SpouseFinancialsBuilder extends RelationFinancialsBuilder
{
    /**
	 * An instance of serialVersionUID
	 */
	private static final long serialVersionUID = -7175547061040435950L;
	private Builder spouseBuilder;

    public SpouseFinancialsBuilder()
    {
        super();
    }

    public Builder getSpouseBuilder()
    {
        return this.spouseBuilder;
    }

    public void setSpouseBuilder(Builder spouseBuilder)
    {
        this.spouseBuilder = spouseBuilder;
    }

    public SpouseFinancials build(SpouseFinancialsMetaData metaData)
            throws BuilderException
    {
        SpouseFinancials input = metaData.getEntity();
        SpouseFinancials output = (input == null) ? new SpouseFinancials()
                : input;

        this.transfer(output, metaData);

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

    private Spouse buildSpouse(Spouse input, SpouseMetaData metaData)
            throws BuilderException
    {
        if (metaData == null)
        {
            return null;
        }
        metaData.setEntity(input);
        return (Spouse) this.spouseBuilder.build(metaData);

    }

    private boolean shouldKeep(SpouseFinancials obj)
    {
        return true;
    }

    private void transfer(SpouseFinancials input,
            SpouseFinancialsMetaData metaData) throws BuilderException
    {
        super.transfer(input, metaData);

        if (this.shouldKeep(input))
        {
            Spouse reportedOn = this.buildSpouse(input.getReportedOn(),
                    metaData.getReportedOnSpouse());
            input.setReportedOn(reportedOn);
        }
    }

}
