/**
  * Sets all origin entries' entry IDs to be sequential starting from 0 in the collection
  *
  * @param originEntries collection of origin entries
  */
 public static void setSequentialEntryIds(Collection<OriginEntryFull> originEntries) {
   int index = 0;
   for (OriginEntryFull entry : originEntries) {
     entry.setEntryId(new Integer(index));
     index++;
   }
 }
 /**
  * Given an instance of statistics, it adds information from the passed in entry to the statistics
  *
  * @param entry origin entry
  * @param statistics adds statistics from the passed in origin entry to the passed in statistics
  */
 public static void updateStatisticsWithEntry(
     OriginEntryFull entry, OriginEntryStatistics statistics) {
   statistics.incrementCount();
   if (isDebit(entry)) {
     statistics.addDebit(entry.getTransactionLedgerEntryAmount());
   } else if (isCredit(entry)) {
     statistics.addCredit(entry.getTransactionLedgerEntryAmount());
   } else {
     statistics.addBudget(entry.getTransactionLedgerEntryAmount());
   }
 }
  /**
   * Applies a list of change criteria groups to an origin entry. Note that the returned value, if
   * not null, is a reference to the same instance as the origin entry passed in (i.e. intentional
   * side effect)
   *
   * @param entry origin entry
   * @param matchCriteriaOnly if true and no criteria match, then this method will return null
   * @param changeCriteriaGroups list of change criteria groups to apply
   * @return the passed in entry instance, or null (see above)
   */
  public static OriginEntryFull applyCriteriaToEntry(
      OriginEntryFull entry,
      boolean matchCriteriaOnly,
      List<CorrectionChangeGroup> changeCriteriaGroups) {
    if (matchCriteriaOnly && !doesEntryMatchAnyCriteriaGroups(entry, changeCriteriaGroups)) {
      return null;
    }

    for (CorrectionChangeGroup ccg : changeCriteriaGroups) {
      int matches = 0;
      for (CorrectionCriteria cc : ccg.getCorrectionCriteria()) {
        if (entryMatchesCriteria(cc, entry)) {
          matches++;
        }
      }

      // If they all match, change it
      if (matches == ccg.getCorrectionCriteria().size()) {
        for (CorrectionChange change : ccg.getCorrectionChange()) {
          // Change the row
          entry.setFieldValue(change.getCorrectionFieldName(), change.getCorrectionFieldValue());
        }
      }
    }
    return entry;
  }
 /** This method is for refreshing References of Origin Entry */
 protected void refreshOriginEntryReferences(OriginEntryFull originEntry) {
   Map<String, Class> referenceClasses =
       persistenceStructureService.listReferenceObjectFields(originEntry.getClass());
   for (String reference : referenceClasses.keySet()) {
     if (KFSPropertyConstants.PROJECT.equals(reference)) {
       if (KFSConstants.getDashProjectCode().equals(originEntry.getProjectCode())) {
         originEntry.setProject(null);
       } else {
         persistenceService.retrieveReferenceObject(originEntry, reference);
       }
     } else if (KFSPropertyConstants.FINANCIAL_SUB_OBJECT.equals(reference)) {
       if (KFSConstants.getDashFinancialSubObjectCode()
           .equals(originEntry.getFinancialSubObjectCode())) {
         originEntry.setFinancialSubObject(null);
       } else {
         persistenceService.retrieveReferenceObject(originEntry, reference);
       }
     } else if (KFSPropertyConstants.SUB_ACCOUNT.equals(reference)) {
       if (KFSConstants.getDashSubAccountNumber().equals(originEntry.getSubAccountNumber())) {
         originEntry.setSubAccount(null);
       } else {
         persistenceService.retrieveReferenceObject(originEntry, reference);
       }
     } else {
       persistenceService.retrieveReferenceObject(originEntry, reference);
     }
   }
 }
  /**
   * Returns whether an origin entry matches the passed in criteria. If both the criteria and actual
   * value are both String types and are empty, null, or whitespace only, then they will match.
   *
   * @param cc correction criteria to test against origin entry
   * @param oe origin entry to test
   * @return true if origin entry matches the passed in criteria
   */
  public static boolean entryMatchesCriteria(CorrectionCriteria cc, OriginEntryFull oe) {
    OriginEntryFieldFinder oeff = new OriginEntryFieldFinder();
    Object fieldActualValue = oe.getFieldValue(cc.getCorrectionFieldName());
    String fieldTestValue =
        StringUtils.isBlank(cc.getCorrectionFieldValue()) ? "" : cc.getCorrectionFieldValue();
    String fieldType = oeff.getFieldType(cc.getCorrectionFieldName());

    String fieldActualValueString = convertToString(fieldActualValue, fieldType);

    if ("String".equals(fieldType)
        || "sw".equals(cc.getCorrectionOperatorCode())
        || "ew".equals(cc.getCorrectionOperatorCode())
        || "ct".equals(cc.getCorrectionOperatorCode())) {
      return compareStringData(cc, fieldTestValue, fieldActualValueString);
    }
    int compareTo = 0;
    try {
      if (fieldActualValue == null) {
        return false;
      }
      if ("Integer".equals(fieldType)) {
        compareTo = ((Integer) fieldActualValue).compareTo(Integer.parseInt(fieldTestValue));
      }
      if ("KualiDecimal".equals(fieldType)) {
        compareTo =
            ((KualiDecimal) fieldActualValue)
                .compareTo(new KualiDecimal(Double.parseDouble(fieldTestValue)));
      }
      if ("BigDecimal".equals(fieldType)) {
        compareTo =
            ((BigDecimal) fieldActualValue)
                .compareTo(new BigDecimal(Double.parseDouble(fieldTestValue)));
      }
      if ("Date".equals(fieldType)) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        compareTo = ((Date) fieldActualValue).compareTo(df.parse(fieldTestValue));
      }
    } catch (Exception e) {
      // any exception while parsing data return false
      return false;
    }
    return compareTo(compareTo, cc.getCorrectionOperatorCode());
  }
 /**
  * Returns whether the origin entry represents a credit
  *
  * @param oe origin entry
  * @return true if origin entry represents a credit
  */
 public static boolean isCredit(OriginEntryFull oe) {
   return KFSConstants.GL_CREDIT_CODE.equals(oe.getTransactionDebitCreditCode());
 }
 /**
  * Sets all origin entries' entry IDs to null within the collection.
  *
  * @param originEntries collection of origin entries
  */
 public static void setAllEntryIdsToNull(Collection<OriginEntryFull> originEntries) {
   for (OriginEntryFull entry : originEntries) {
     entry.setEntryId(null);
   }
 }