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

import gov.va.med.esr.common.model.financials.DependentFinancials;
import gov.va.med.esr.common.model.person.Dependent;
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 DependentFinancialsBuilder extends RelationFinancialsBuilder
{
    /**
	 * An instance of serialVersionUID
	 */
	private static final long serialVersionUID = 9149938727122944380L;
	private Builder dependentBuilder;

    public DependentFinancialsBuilder()
    {
        super();
    }

    public Builder getDependentBuilder()
    {
        return this.dependentBuilder;
    }

    public void setDependentBuilder(Builder dependentBuilder)
    {
        this.dependentBuilder = dependentBuilder;
    }

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

        this.transfer(output, metaData);

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

    private boolean shouldKeep(DependentFinancials obj)
    {
        return (obj != null && obj.getReportedOn() != null 
                && obj.getReportedOn().getRelationship() != null);
    }

    private void transfer(DependentFinancials input,
            DependentFinancialsMetaData metaData) throws BuilderException
    {
        super.transfer(input, metaData);

        input.setAttendedSchool(super.build(input.getAttendedSchool(), metaData
                .getAttendedSchool()));
        input
                .setContributedToSupport(super.build(input
                        .getContributedToSupport(), metaData
                        .getContributedToSupport()));
        input.setAmountContributedToSupport(super.build(input
                .getAmountContributedToSupport(), metaData
                .getAmountContributedToSupport()));
        input.setHasIncome(super.build(input.getHasIncome(), metaData
                .getHasIncome()));
        input.setIncapableOfSelfSupport(super.build(input
                .getIncapableOfSelfSupport(), metaData
                .getIncapableOfSelfSupport()));
        input.setIncomeAvailableToPatient(super.build(input
                .getIncomeAvailableToPatient(), metaData
                .getIncomeAvailableToPatient()));
        input.setLivedWithPatient(super.build(input.getLivedWithPatient(),
                metaData.getLivedWithPatient()));
        
        Dependent reportedOn = this.buildDependent(input.getReportedOn(),
                metaData.getReportedOnDependent());
        input.setReportedOn(reportedOn);
    }

    private Dependent buildDependent(Dependent input, DependentMetaData metaData)
            throws BuilderException
    {
        if (metaData == null)
        {
            return null;
        }
        metaData.setEntity(input);
        return (Dependent) dependentBuilder.build(metaData);
    }

}