/*
  * This method initializes the awardDocument ,award and awardAamountInfo
  * reference variables
  */
 private void initialize(Award award) {
   this.awardDocument = award.getAwardDocument();
   this.award = award;
   List<AwardAmountInfo> awardAmountInfos = award.getAwardAmountInfos();
   if (awardAmountInfos != null && !awardAmountInfos.isEmpty()) {
     awardAmountInfo = awardAmountInfos.get(0);
   }
 }
  public AwardAmountInfo fetchLastAwardAmountInfoForDocNum(Award award, String docNum) {
    List<AwardAmountInfo> validAwardAmountInfos = new ArrayList<AwardAmountInfo>();
    int docNumIntValue = Integer.parseInt(docNum.trim());

    for (AwardAmountInfo aai : award.getAwardAmountInfos()) {
      if (aai.getTimeAndMoneyDocumentNumber() == null
          || (aai.getAwardNumber().endsWith("-00001")
              && award.getAwardAmountInfos().indexOf(aai) == 1)) {
        validAwardAmountInfos.add(aai);
      } else {
        if (Integer.parseInt(aai.getTimeAndMoneyDocumentNumber().trim()) > docNumIntValue) {
          break;
        } else {
          validAwardAmountInfos.add(aai);
        }
      }
    }
    return validAwardAmountInfos.get(validAwardAmountInfos.size() - 1);
  }
  public boolean processSaveAwardDetailsAndDates(
      AwardDetailsAndDatesSaveEvent awardDetailsAndDatesSaveEvent) {
    boolean valid = true;
    Award award = awardDetailsAndDatesSaveEvent.getAward();
    if (award.getAnticipatedTotal().isLessThan(award.getObligatedTotal())) {
      valid = false;
      reportError(ANTICIPATED_AMOUNT_PROPERTY_NAME, KeyConstants.ERROR_ANTICIPATED_AMOUNT);
    }
    if (award.getObligatedTotal().isLessThan(KualiDecimal.ZERO)) {
      valid = false;
      reportError(OBLIGATED_AMOUNT_PROPERTY_NAME, KeyConstants.ERROR_OBLIGATED_AMOUNT_NEGATIVE);
    }
    if (award.getAnticipatedTotal().isLessThan(KualiDecimal.ZERO)) {
      valid = false;
      reportError(ANTICIPATED_AMOUNT_PROPERTY_NAME, KeyConstants.ERROR_ANTICIPATED_AMOUNT_NEGATIVE);
    }
    if (award.getObligatedTotal().isGreaterThan(new KualiDecimal(0))
        &&
        // award.getAwardEffectiveDate() == null) {
        award
                .getAwardAmountInfos()
                .get(award.getAwardAmountInfos().size() - 1)
                .getCurrentFundEffectiveDate()
            == null) {
      valid = false;
      reportError(AWARD_EFFECTIVE_DATE_PROPERTY_NAME, KeyConstants.ERROR_AWARD_EFFECTIVE_DATE);
    }
    if (award.getObligatedTotal().isGreaterThan(new KualiDecimal(0))
        &&
        // award.getObligationExpirationDate() == null) {
        award
                .getAwardAmountInfos()
                .get(award.getAwardAmountInfos().size() - 1)
                .getObligationExpirationDate()
            == null) {
      valid = false;
      reportError(
          OBLIGATION_EXPIRATION_DATE_PROPERTY_NAME, KeyConstants.ERROR_OBLIGATION_EXPIRATION_DATE);
    }

    return valid;
  }
  /**
   * This method fetches the Award Amount Info object that is to be displayed in UI for Award. The
   * rules are: 1)the Award Amount Infos awardNumber and versionNumber must match the Award BO 2)The
   * Award Amount Infos timeAndMoneyDocumentNumber must be null or from a T&M document that has been
   * finalized Users don't want this data to apply to Award until the T&M document has been
   * approved.
   *
   * @param award
   * @return
   * @throws WorkflowException
   * @throws WorkflowException
   */
  @SuppressWarnings("unchecked")
  public AwardAmountInfo fetchLastAwardAmountInfoForAwardVersionAndFinalizedTandMDocumentNumber(
      Award award) {

    List<AwardAmountInfo> validAwardAmountInfos = new ArrayList<AwardAmountInfo>();

    for (AwardAmountInfo aai : award.getAwardAmountInfos()) {
      // the aai needs to be added if it is created on initialization, or in the case of a root node
      // we add a new one for initial money transaction.
      // if an award has been versioned, the initial transaction will be the first index in list.
      if (aai.getTimeAndMoneyDocumentNumber() == null
          || (aai.getAwardNumber().endsWith("-00001")
              && award.getAwardAmountInfos().indexOf(aai) == 1)) {
        validAwardAmountInfos.add(aai);
      } else {
        Map<String, Object> fieldValues1 = new HashMap<String, Object>();
        fieldValues1.put("documentNumber", aai.getTimeAndMoneyDocumentNumber());
        List<TimeAndMoneyDocument> timeAndMoneyDocuments =
            (List<TimeAndMoneyDocument>)
                getBusinessObjectService().findMatching(TimeAndMoneyDocument.class, fieldValues1);
        try {
          TimeAndMoneyDocument timeAndMoneyDocument =
              (TimeAndMoneyDocument)
                  getDocumentService()
                      .getByDocumentHeaderId(
                          timeAndMoneyDocuments.get(0).getDocumentHeader().getDocumentNumber());
          if (timeAndMoneyDocument.getDocumentHeader().hasWorkflowDocument()) {
            if (timeAndMoneyDocument.getDocumentHeader().getWorkflowDocument().stateIsFinal()) {
              validAwardAmountInfos.add(aai);
            }
          }
        } catch (WorkflowException e) {
          LOG.error(e.getMessage(), e);
        }
      }
    }
    return validAwardAmountInfos.get(validAwardAmountInfos.size() - 1);
  }
 /**
  * This method will set the values to award transaction info xml object attributes .
  *
  * @return AwardTransactionInfo xml object
  */
 protected AwardTransactionInfo getAwardTransactiontInfo(Award award) {
   List<AwardTransactionType> awardTransactionTypeList = new ArrayList<AwardTransactionType>();
   AwardTransactionInfo awardTransactionInfo = AwardTransactionInfo.Factory.newInstance();
   AwardTransactionType awardTransactionType = null;
   for (org.kuali.kra.award.home.AwardAmountInfo awardAmount : award.getAwardAmountInfos()) {
     AwardAmountTransaction awardAmountTransaction =
         getAwardAmountTransaction(awardAmount.getTimeAndMoneyDocumentNumber());
     if (awardAmountTransaction != null) {
       awardTransactionType = AwardTransactionType.Factory.newInstance();
       setAwardAmountTransaction(awardTransactionType, awardAmountTransaction);
       awardTransactionTypeList.add(awardTransactionType);
     }
   }
   awardTransactionInfo.setTransactionInfoArray(
       awardTransactionTypeList.toArray(new AwardTransactionType[0]));
   return awardTransactionInfo;
 }