/**
   * Uses a <code>{@link Unit}</code> obtained from the <code>{@link Unit}</code> lookup to create a
   * <code>{@link ProposalPersonUnit}</code> instance.
   *
   * @param unitId
   * @return ProposalPersonUnit
   */
  public ProposalPersonUnit createProposalPersonUnit(String unitId, ProposalPerson person) {
    ProposalPersonUnit retval = new ProposalPersonUnit();
    Map valueMap = new HashMap();
    valueMap.put("unitNumber", unitId);
    Collection<Unit> units = getBusinessObjectService().findMatching(Unit.class, valueMap);

    for (Unit found : units) {
      retval.setUnitNumber(found.getUnitNumber());
      retval.setUnit(found);
    }

    for (InvestigatorCreditType creditType : getInvestigatorCreditTypes()) {
      ProposalUnitCreditSplit creditSplit = new ProposalUnitCreditSplit();
      creditSplit.setProposalPersonUnit(retval);
      creditSplit.setInvCreditTypeCode(creditType.getCode());
      creditSplit.setCredit(new ScaleTwoDecimal(0));
      retval.getCreditSplits().add(creditSplit);
    }

    return retval;
  }
 /**
  * It populates the Active credit type in the proposalpersoncreditsplit and unitcreditsplit
  *
  * @param document
  * @return true or false
  */
 public void populateActiveCredittypesPerson(ProposalDevelopmentDocument document) {
   Collection<InvestigatorCreditType> invcrdttype = getInvestigatorCreditTypes();
   for (ProposalPerson person : document.getDevelopmentProposal().getInvestigators()) {
     for (InvestigatorCreditType invcredtype : invcrdttype) {
       boolean creditTypeFound = false;
       for (ProposalPersonCreditSplit proposalpersoncrdt : person.getCreditSplits()) {
         if ((invcredtype.getCode().equals(proposalpersoncrdt.getInvCreditTypeCode()))) {
           creditTypeFound = true;
           break;
         }
       }
       if (!creditTypeFound) {
         ProposalPersonCreditSplit creditSplit = new ProposalPersonCreditSplit();
         creditSplit.setProposalPerson(person);
         creditSplit.setInvCreditTypeCode(invcredtype.getCode());
         creditSplit.setCredit(new ScaleTwoDecimal(0));
         person.getCreditSplits().add(creditSplit);
       }
     }
     for (ProposalPersonUnit unitsplit : person.getUnits()) {
       for (InvestigatorCreditType invcrdtype : invcrdttype) {
         boolean creditTypeFound = false;
         for (ProposalUnitCreditSplit unitcreditsplit : unitsplit.getCreditSplits()) {
           if ((invcrdtype.getCode().equals(unitcreditsplit.getInvCreditTypeCode()))) {
             creditTypeFound = true;
             break;
           }
         }
         if (!creditTypeFound) {
           ProposalUnitCreditSplit creditSplit = new ProposalUnitCreditSplit();
           creditSplit.setProposalPersonUnit(unitsplit);
           creditSplit.setInvCreditTypeCode(invcrdtype.getCode());
           creditSplit.setCredit(new ScaleTwoDecimal(0));
           unitsplit.getCreditSplits().add(creditSplit);
         }
       }
     }
   }
 }
  @Override
  public List<ProposalCreditSplitListDto> createCreditSplitListItems(
      List<ProposalPerson> investigators) {
    List<ProposalCreditSplitListDto> creditSplitListItems =
        new ArrayList<ProposalCreditSplitListDto>();
    Map<String, ProposalPersonCreditSplit> totalInvestigatorSplits =
        new HashMap<String, ProposalPersonCreditSplit>();
    for (ProposalPerson investigator : investigators) {
      ProposalCreditSplitListDto investigatorLine = new ProposalCreditSplitListDto();
      investigatorLine.setDescription(investigator.getFullName());
      investigatorLine.setLineType("investigator");
      investigatorLine.getCreditSplits().addAll(investigator.getCreditSplits());
      creditSplitListItems.add(investigatorLine);
      for (ProposalPersonCreditSplit invesitgatorCreditSplit : investigator.getCreditSplits()) {
        if (totalInvestigatorSplits.containsKey(invesitgatorCreditSplit.getInvCreditTypeCode())) {
          ProposalPersonCreditSplit creditSplit =
              totalInvestigatorSplits.get(invesitgatorCreditSplit.getInvCreditTypeCode());
          creditSplit.setCredit(creditSplit.getCredit().add(invesitgatorCreditSplit.getCredit()));
        } else {
          ProposalPersonCreditSplit creditSplit = new ProposalPersonCreditSplit();
          creditSplit.setCredit(invesitgatorCreditSplit.getCredit());
          totalInvestigatorSplits.put(invesitgatorCreditSplit.getInvCreditTypeCode(), creditSplit);
        }
      }

      Map<String, ProposalUnitCreditSplit> totalUnitSplits =
          new HashMap<String, ProposalUnitCreditSplit>();
      for (ProposalPersonUnit unit : investigator.getUnits()) {
        ProposalCreditSplitListDto unitLine = new ProposalCreditSplitListDto();
        unitLine.setDescription(unit.getUnitNumber() + " - " + unit.getUnit().getUnitName());
        unitLine.getCreditSplits().addAll(unit.getCreditSplits());
        unitLine.setLineType("unit");
        creditSplitListItems.add(unitLine);
        for (ProposalUnitCreditSplit unitCreditSplit : unit.getCreditSplits()) {
          if (totalUnitSplits.containsKey(unitCreditSplit.getInvCreditTypeCode())) {
            ProposalUnitCreditSplit creditSplit =
                totalUnitSplits.get(unitCreditSplit.getInvCreditTypeCode());
            creditSplit.setCredit(creditSplit.getCredit().add(unitCreditSplit.getCredit()));
          } else {
            ProposalUnitCreditSplit creditSplit = new ProposalUnitCreditSplit();
            creditSplit.setCredit(unitCreditSplit.getCredit());
            totalUnitSplits.put(unitCreditSplit.getInvCreditTypeCode(), creditSplit);
          }
        }
      }

      ProposalCreditSplitListDto unitTotalLine = new ProposalCreditSplitListDto();
      unitTotalLine.setDescription("Unit Total:");
      unitTotalLine.setLineType("unit-total");
      for (Map.Entry<String, ProposalUnitCreditSplit> entry : totalUnitSplits.entrySet()) {
        unitTotalLine.getCreditSplits().add(0, entry.getValue());
      }
      creditSplitListItems.add(unitTotalLine);
    }

    ProposalCreditSplitListDto investigatorTotalLine = new ProposalCreditSplitListDto();
    investigatorTotalLine.setDescription("Investigator Total:");
    investigatorTotalLine.setLineType("investigator-total");
    for (Map.Entry<String, ProposalPersonCreditSplit> entry : totalInvestigatorSplits.entrySet()) {
      investigatorTotalLine.getCreditSplits().add(0, entry.getValue());
    }
    creditSplitListItems.add(investigatorTotalLine);

    return creditSplitListItems;
  }