gt: 		if (pdDoc.getDevelopmentProposal().getOwnedByUnit() != null) {
context: /*
   * Kuali Coeus, a comprehensive research administration system for higher education.
   * 
   * Copyright 2005-2016 Kuali, Inc.
   * 
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   * 
   * You should have received a copy of the GNU Affero General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package org.kuali.coeus.s2sgen.impl.generate.support;
  
  import gov.grants.apply.forms.rrSF42412V12.*;
  import gov.grants.apply.forms.rrSF42412V12.ApplicationTypeCodeDataType.Enum;
  import gov.grants.apply.forms.rrSF42412V12.RRSF42412Document.RRSF42412;
  import gov.grants.apply.forms.rrSF42412V12.RRSF42412Document.RRSF42412.*;
  import gov.grants.apply.forms.rrSF42412V12.RRSF42412Document.RRSF42412.ApplicantInfo.ContactPersonInfo;
  import gov.grants.apply.forms.rrSF42412V12.RRSF42412Document.RRSF42412.ApplicantType.SmallBusinessOrganizationType;
  import gov.grants.apply.forms.rrSF42412V12.RRSF42412Document.RRSF42412.ApplicantType.SmallBusinessOrganizationType.IsSociallyEconomicallyDisadvantaged;
  import gov.grants.apply.forms.rrSF42412V12.RRSF42412Document.RRSF42412.ApplicantType.SmallBusinessOrganizationType.IsWomenOwned;
  import gov.grants.apply.system.attachmentsV10.AttachedFileDataType;
  import gov.grants.apply.system.globalLibraryV20.AddressDataType;
  import gov.grants.apply.system.globalLibraryV20.ApplicantTypeCodeDataType;
  import gov.grants.apply.system.globalLibraryV20.OrganizationDataType;
  import gov.grants.apply.system.globalLibraryV20.YesNoDataType;
  import gov.grants.apply.system.universalCodesV20.CountryCodeDataType;
  import org.apache.commons.lang3.StringUtils;
  import org.apache.xmlbeans.XmlObject;
  import org.kuali.coeus.common.api.org.OrganizationContract;
  import org.kuali.coeus.common.api.ynq.YnqConstant;
  import org.kuali.coeus.common.questionnaire.api.answer.AnswerHeaderContract;
  import org.kuali.coeus.common.budget.api.core.BudgetContract;
  import org.kuali.coeus.common.budget.api.income.BudgetProjectIncomeContract;
  import org.kuali.coeus.common.budget.api.nonpersonnel.BudgetLineItemCalculatedAmountContract;
  import org.kuali.coeus.common.budget.api.nonpersonnel.BudgetLineItemContract;
  import org.kuali.coeus.common.budget.api.period.BudgetPeriodContract;
  import org.kuali.coeus.common.api.rolodex.RolodexContract;
  import org.kuali.coeus.common.api.sponsor.SponsorContract;
  import org.kuali.coeus.propdev.api.budget.ProposalDevelopmentBudgetExtContract;
  import org.kuali.coeus.propdev.api.budget.modular.BudgetModularIdcContract;
  import org.kuali.coeus.propdev.api.core.DevelopmentProposalContract;
  import org.kuali.coeus.propdev.api.location.ProposalSiteContract;
  import org.kuali.coeus.propdev.api.person.ProposalPersonContract;
  import org.kuali.coeus.propdev.api.s2s.S2sOpportunityContract;
  import org.kuali.coeus.s2sgen.impl.generate.FormVersion;
  import org.kuali.coeus.s2sgen.impl.validate.S2SErrorHandlerService;
  import org.kuali.coeus.propdev.api.core.ProposalDevelopmentDocumentContract;
  
  import org.kuali.coeus.sys.api.model.ScaleTwoDecimal;
  import org.kuali.coeus.s2sgen.api.core.ConfigurationConstants;
  import org.kuali.coeus.propdev.api.attachment.NarrativeContract;
  import org.kuali.coeus.s2sgen.impl.generate.FormGenerator;
  import org.kuali.coeus.s2sgen.impl.person.DepartmentalPersonDto;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.core.io.Resource;
  
  import java.math.BigDecimal;
  import java.util.*;
  
  /**
   * Class for generating the XML object for grants.gov RRSF424V1_0. Form is
   * generated using XMLBean classes and is based on RRSF424V1_2 schema.
   * 
   * @author Kuali Research Administration Team (kualidev@oncourse.iu.edu)
   */
  @FormGenerator("RRSF424V1_2Generator")
  public class RRSF424V1_2Generator extends RRSF424BaseGenerator {
  
      private static final String MODULAR_BUDGET_REQUIRED = "s2s.budget.modularbudget.required";
  
      @Value("http://apply.grants.gov/forms/RR_SF424_1_2-V1.2")
      private String namespace;
  
      @Value("RR_SF424_1_2-V1.2")
      private String formName;
  
      @Value("classpath:org/kuali/coeus/s2sgen/impl/generate/support/stylesheet/RR_SF424-V1.2.xsl")
      private Resource stylesheet;
  
      @Value("gov.grants.apply.forms.rrSF42412V12")
      private String packageName;
  
      @Value("120")
      private int sortIndex;
  
      @Autowired
      @Qualifier("s2SErrorHandlerService")
      protected S2SErrorHandlerService s2SErrorHandlerService;
  
      private DepartmentalPersonDto departmentalPerson;
      private List<? extends AnswerHeaderContract> answerHeaders;
  	/**
  	 * 
  	 * This method gives information of applications that are used in RRSF424
  	 * 
  	 * @return rrSF424Document {@link XmlObject} of type RRSF424Document.
  	 */
  	private RRSF42412Document getRRSF424() {
          answerHeaders = getPropDevQuestionAnswerService().getQuestionnaireAnswerHeaders(pdDoc.getDevelopmentProposal().getProposalNumber());
          RRSF42412Document rrSF424Document = RRSF42412Document.Factory
  				.newInstance();
  		RRSF42412 rrsf42412 = RRSF42412.Factory.newInstance();
  		rrsf42412.setFormVersion(FormVersion.v1_2.getVersion());
  		rrsf42412.setSubmittedDate(Calendar.getInstance());
  		if(getSubmissionTypeCode() != null){
  			rrsf42412.setSubmissionTypeCode(SubmissionTypeDataType.Enum.forInt(Integer.parseInt(getSubmissionTypeCode())));
  		}
  		rrsf42412.setStateID(getRolodexState());
  		rrsf42412.setApplicantInfo(getApplicationInfo());
  		rrsf42412.setEmployerID(getEmployerId());
  		rrsf42412.setApplicantType(getApplicantType());
  		if(getAgencyRoutingNumber()!=null){
  	        rrsf42412.setAgencyRoutingNumber(getAgencyRoutingNumber());
  	    }
  		rrsf42412.setApplicationType(getApplicationType());
  		rrsf42412.setApplicantID(pdDoc.getDevelopmentProposal()
  				.getProposalNumber());
  		rrsf42412.setFederalAgencyName(getFederalAgencyName());
  		rrsf42412.setProjectTitle(getProjectTitle());
  		rrsf42412.setProposedProjectPeriod(getProjectPeriod());
  		rrsf42412.setCongressionalDistrict(getCongDistrict());
  		if(pdDoc.getDevelopmentProposal().getCfdaNumber()!=null){
  		    rrsf42412.setCFDANumber(pdDoc.getDevelopmentProposal().getCfdaNumber());
  		}
  		rrsf42412.setActivityTitle(getActivityTitle());
  		setFederalId(rrsf42412);
  		rrsf42412.setPDPIContactInfo(getPDPI());
  		rrsf42412.setEstimatedProjectFunding(getProjectFunding());
  		rrsf42412.setTrustAgree(YesNoDataType.Y_YES);// Value is hardcoded
  		rrsf42412.setStateReview(getStateReview());
  		rrsf42412.setAORInfo(getAORInfoType());
  		rrsf42412.setAORSignature(getAORSignature());
  		rrsf42412.setAORSignedDate(Calendar.getInstance());
  		setPreApplicationAttachment(rrsf42412);
  		setSFLLLAttachment(rrsf42412);
  		rrSF424Document.setRRSF42412(rrsf42412);
  		return rrSF424Document;
  	}
  
  	/**
  	 * 
  	 * This method is to get estimated project funds for RRSF424
  	 * 
  	 * @return EstimatedProjectFunding estimated total cost for the project.
  	 */
  	private EstimatedProjectFunding getProjectFunding() {
  		EstimatedProjectFunding funding = EstimatedProjectFunding.Factory
  				.newInstance();
  		funding.setTotalEstimatedAmount(BigDecimal.ZERO);
  		funding.setTotalNonfedrequested(BigDecimal.ZERO);
  		funding.setTotalfedNonfedrequested(BigDecimal.ZERO);
  		funding.setEstimatedProgramIncome(BigDecimal.ZERO);
  		boolean hasBudgetLineItem = false;
          ProposalDevelopmentBudgetExtContract budget = s2SCommonBudgetService.getBudget(pdDoc.getDevelopmentProposal());
  
  		if (budget != null) {
  
              ScaleTwoDecimal totalCost = ScaleTwoDecimal.ZERO;
              if (budget.getModularBudgetFlag()) {
                  ScaleTwoDecimal fundsRequested = ScaleTwoDecimal.ZERO;
                  ScaleTwoDecimal totalDirectCost = ScaleTwoDecimal.ZERO;
  
  				// get modular budget amounts instead of budget detail amounts
  				for (BudgetPeriodContract budgetPeriod : budget.getBudgetPeriods()) {
  	                if(budgetPeriod.getBudgetModular()==null){
  	                    getAuditErrors().add(s2SErrorHandlerService.getError(MODULAR_BUDGET_REQUIRED, getFormName()));
  	                    break;
  	                }else{
  					totalDirectCost = totalDirectCost.add(budgetPeriod
  							.getBudgetModular().getTotalDirectCost());
  					for (BudgetModularIdcContract budgetModularIdc : budgetPeriod
  							.getBudgetModular().getBudgetModularIdcs()) {
  						fundsRequested = fundsRequested.add(budgetModularIdc
  								.getFundsRequested());
  					}
  				}
  				}
  				totalCost = totalCost.add(totalDirectCost);
  				totalCost = totalCost.add(fundsRequested);
  			} else {
              	 totalCost=budget.getTotalCost();
              }
              ScaleTwoDecimal fedNonFedCost = totalCost;
              ScaleTwoDecimal costSharingAmount = ScaleTwoDecimal.ZERO;
  
              for (BudgetPeriodContract budgetPeriod : budget.getBudgetPeriods()) {
                  for (BudgetLineItemContract lineItem : budgetPeriod.getBudgetLineItems()) {
                      hasBudgetLineItem = true;
                      if (budget.getSubmitCostSharingFlag() && lineItem.getSubmitCostSharingFlag()) {
                          costSharingAmount =  costSharingAmount.add(lineItem.getCostSharingAmount());
                          List<? extends BudgetLineItemCalculatedAmountContract> calculatedAmounts = lineItem.getBudgetLineItemCalculatedAmounts();
                          for (BudgetLineItemCalculatedAmountContract budgetLineItemCalculatedAmount : calculatedAmounts) {
                               costSharingAmount =  costSharingAmount.add(budgetLineItemCalculatedAmount.getCalculatedCostSharing());
                          }
  
                      }
                  }
              }
              if(!hasBudgetLineItem && budget.getSubmitCostSharingFlag()){
                  costSharingAmount = budget.getCostSharingAmount();
              }
              fedNonFedCost = fedNonFedCost.add(costSharingAmount);
              funding = EstimatedProjectFunding.Factory.newInstance();
              funding.setTotalEstimatedAmount(totalCost
                      .bigDecimalValue());
              funding.setTotalNonfedrequested(costSharingAmount.bigDecimalValue());
              funding.setTotalfedNonfedrequested(fedNonFedCost.bigDecimalValue());
              funding.setEstimatedProgramIncome(getTotalProjectIncome(budget));
  		}
  		return funding;
  	}
  
  	/*
  	 * This method computes total project income for the given budget
  	 */
  	private BigDecimal getTotalProjectIncome(BudgetContract budget) {
  		BigDecimal totalProjectIncome = BigDecimal.ZERO;
  		for (BudgetProjectIncomeContract budgetProjectIncome : budget
  				.getBudgetProjectIncomes()) {
  			totalProjectIncome = totalProjectIncome.add(budgetProjectIncome
  					.getProjectIncome().bigDecimalValue());
  		}
  		return totalProjectIncome;
  	}
  
  	/**
  	 * 
  	 * This method gives the information for an application which consists of
  	 * personal details
  	 * 
  	 * @return appInfo(ApplicantInfo) applicant details.
  	 */
  	private ApplicantInfo getApplicationInfo() {
  		ApplicantInfo appInfo = ApplicantInfo.Factory.newInstance();
  		appInfo.setContactPersonInfo(getContactPersonInfo());
  		appInfo.setOrganizationInfo(getOrganizationDataType());
  		return appInfo;
  	}
  
  	private ContactPersonInfo getContactPersonInfo() {
  		String contactType = getContactType();
  		ContactPersonInfo contactInfo = ContactPersonInfo.Factory.newInstance();
  		if (contactType.equals(CONTACT_TYPE_I)) {
  			// use organization rolodex contact
  			if (pdDoc.getDevelopmentProposal().getApplicantOrganization() != null) {
  				contactInfo = getContactInfo(pdDoc.getDevelopmentProposal()
  						.getApplicantOrganization().getRolodex());
  			}
  		} else {
  			// contact will come from unit or unit_administrators
  			DepartmentalPersonDto depPerson = getContactPerson(pdDoc);
  			if (depPerson != null) {
  				contactInfo.setName(globLibV20Generator
  						.getHumanNameDataType(depPerson));
  				contactInfo.setPhone(depPerson.getOfficePhone());
  				if (StringUtils.isNotEmpty(depPerson.getFaxNumber())) {
  					contactInfo.setFax(depPerson.getFaxNumber());
  				}
  				if (depPerson.getEmailAddress() != null) {
  					contactInfo.setEmail(depPerson.getEmailAddress());
  				}
  			}
  		}
  		return contactInfo;
  	}
  
  	private OrganizationDataType getOrganizationDataType() {
  		OrganizationDataType orgType = OrganizationDataType.Factory
  				.newInstance();
          RolodexContract rolodex = pdDoc.getDevelopmentProposal()
  				.getApplicantOrganization().getRolodex();
  		orgType.setAddress(globLibV20Generator.getAddressDataType(rolodex));
  
  		OrganizationContract organization = pdDoc.getDevelopmentProposal()
  				.getApplicantOrganization().getOrganization();
  		if (organization != null) {
  			orgType.setOrganizationName(organization.getOrganizationName());
  			orgType.setDUNSID(organization.getDunsNumber());
  		}
  