/**
   * This method will remove all PO related transactions from display on GL results
   *
   * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
   */
  @Override
  public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
    // update status code from user input value to DB value.
    updateStatusCodeCriteria(fieldValues);

    List<? extends BusinessObject> searchResults = super.getSearchResults(fieldValues);
    if (searchResults == null || searchResults.isEmpty()) {
      return searchResults;
    }
    Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(GeneralLedgerEntry.class);
    Long matchingResultsCount = null;
    List<GeneralLedgerEntry> newList = new ArrayList<GeneralLedgerEntry>();
    for (BusinessObject businessObject : searchResults) {
      GeneralLedgerEntry entry = (GeneralLedgerEntry) businessObject;
      if (!CabConstants.PREQ.equals(entry.getFinancialDocumentTypeCode())) {
        if (!CabConstants.CM.equals(entry.getFinancialDocumentTypeCode())) {
          newList.add(entry);
        } else if (CabConstants.CM.equals(entry.getFinancialDocumentTypeCode())) {
          Map<String, String> cmKeys = new HashMap<String, String>();
          cmKeys.put(
              CabPropertyConstants.PurchasingAccountsPayableDocument.DOCUMENT_NUMBER,
              entry.getDocumentNumber());
          // check if CAB PO document exists, if not included
          Collection<PurchasingAccountsPayableDocument> matchingCreditMemos =
              businessObjectService.findMatching(PurchasingAccountsPayableDocument.class, cmKeys);
          if (matchingCreditMemos == null || matchingCreditMemos.isEmpty()) {
            newList.add(entry);
          }
        }
      }
    }
    matchingResultsCount = Long.valueOf(newList.size());
    if (matchingResultsCount.intValue() <= searchResultsLimit.intValue()) {
      matchingResultsCount = new Long(0);
    }
    return new CollectionIncomplete(newList, matchingResultsCount);
  }
  /**
   * Set CAB line item information from PurAp PaymentRequestItem or CreditMemoItem.
   *
   * @param purchasingAccountsPayableItemAsset
   * @param docTypeCode
   */
  public void setAccountsPayableItemsFromPurAp(
      PurchasingAccountsPayableItemAsset purchasingAccountsPayableItemAsset, String docTypeCode) {
    Map<String, Object> pKeys = new HashMap<String, Object>();
    pKeys.put(
        PurapPropertyConstants.ITEM_IDENTIFIER,
        purchasingAccountsPayableItemAsset.getAccountsPayableLineItemIdentifier());

    // Access PurAp data based on item type(PREQ or CM).
    if (CabConstants.PREQ.equalsIgnoreCase(docTypeCode)) {
      // for PREQ document
      PaymentRequestItem item =
          (PaymentRequestItem)
              businessObjectService.findByPrimaryKey(PaymentRequestItem.class, pKeys);
      if (ObjectUtils.isNull(item)) {
        throw new PurApDocumentUnavailableException(
            "PaymentRequestItem with id = "
                + purchasingAccountsPayableItemAsset.getAccountsPayableLineItemIdentifier()
                + " doesn't exist in table.");
      }

      purchasingAccountsPayableItemAsset.setItemLineNumber(item.getItemLineNumber());
      if (item.getItemType() != null) {
        purchasingAccountsPayableItemAsset.setAdditionalChargeNonTradeInIndicator(
            item.getItemType().isAdditionalChargeIndicator()
                & !CabConstants.TRADE_IN_TYPE_CODE.equalsIgnoreCase(item.getItemTypeCode()));
        purchasingAccountsPayableItemAsset.setTradeInAllowance(
            item.getItemType().isAdditionalChargeIndicator()
                & CabConstants.TRADE_IN_TYPE_CODE.equalsIgnoreCase(item.getItemTypeCode()));
        purchasingAccountsPayableItemAsset.setItemTypeCode(item.getItemTypeCode());
      }
      purchasingAccountsPayableItemAsset.setItemAssignedToTradeInIndicator(
          item.getItemAssignedToTradeInIndicator());
      // PurchaseOrderItemIdentifier will be used when populate PurAp asset data
      PurchaseOrderItem poi = item.getPurchaseOrderItem();
      if (poi != null) {
        purchasingAccountsPayableItemAsset.setPurchaseOrderItemIdentifier(poi.getItemIdentifier());
      }
    } else {
      // for CM document
      CreditMemoItem item =
          (CreditMemoItem) businessObjectService.findByPrimaryKey(CreditMemoItem.class, pKeys);
      if (ObjectUtils.isNull(item)) {
        throw new PurApDocumentUnavailableException(
            "CreditMemoItem with id = "
                + purchasingAccountsPayableItemAsset.getAccountsPayableLineItemIdentifier()
                + " doesn't exist in table.");
      }

      purchasingAccountsPayableItemAsset.setItemLineNumber(item.getItemLineNumber());
      if (item.getItemType() != null) {
        purchasingAccountsPayableItemAsset.setAdditionalChargeNonTradeInIndicator(
            item.getItemType().isAdditionalChargeIndicator()
                & !CabConstants.TRADE_IN_TYPE_CODE.equalsIgnoreCase(item.getItemTypeCode()));
        purchasingAccountsPayableItemAsset.setTradeInAllowance(
            item.getItemType().isAdditionalChargeIndicator()
                & CabConstants.TRADE_IN_TYPE_CODE.equalsIgnoreCase(item.getItemTypeCode()));
        purchasingAccountsPayableItemAsset.setItemTypeCode(item.getItemTypeCode());
      }
      purchasingAccountsPayableItemAsset.setItemAssignedToTradeInIndicator(
          item.getItemAssignedToTradeInIndicator());
      // PurchaseOrderItemIdentifier will be used when populate PurAp asset data
      PurchaseOrderItem poi = getPurchaseOrderItemfromCreditMemoItem(item);
      if (poi != null) {
        purchasingAccountsPayableItemAsset.setPurchaseOrderItemIdentifier(poi.getItemIdentifier());
      }
      // if PREQ Credit Memo document
      VendorCreditMemoDocument cmDoc = (VendorCreditMemoDocument) item.getPurapDocument();
      if (ObjectUtils.isNotNull(cmDoc) && cmDoc.isSourceDocumentPaymentRequest()) {
        purchasingAccountsPayableItemAsset.setPaymentRequestIdentifier(
            cmDoc.getPaymentRequestIdentifier());
      }
    }
  }