Search in sources :

Example 1 with AttributedImportPurchasingAccountsPayableItemEvent

use of org.kuali.kfs.module.purap.document.validation.event.AttributedImportPurchasingAccountsPayableItemEvent in project cu-kfs by CU-CommunityApps.

the class PurchasingActionBase method importItems.

/**
 * Import items to the document from a spreadsheet.
 *
 * @param mapping An ActionMapping
 * @param form An ActionForm
 * @param request The HttpServletRequest
 * @param response The HttpServletResponse
 * @return An ActionForward
 * @throws Exception
 */
public ActionForward importItems(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    LOG.info("Importing item lines");
    PurchasingFormBase purchasingForm = (PurchasingFormBase) form;
    PurchasingDocument purDocument = (PurchasingDocument) purchasingForm.getDocument();
    String documentNumber = purDocument.getDocumentNumber();
    FormFile itemFile = purchasingForm.getItemImportFile();
    Class itemClass = purDocument.getItemClass();
    List<PurApItem> importedItems = null;
    String errorPath = PurapConstants.ITEM_TAB_ERRORS;
    ItemParser itemParser = purDocument.getItemParser();
    // starting position of the imported items, equals the # of
    int itemLinePosition = purDocument.getItemLinePosition();
    try {
        importedItems = itemParser.importItems(itemFile, itemClass, documentNumber);
        // validate imported items
        boolean allPassed = true;
        int itemLineNumber = 0;
        for (PurApItem item : importedItems) {
            // Before the validation, set the item line number to the same as the line number in the import file (starting from
            // 1)
            // so that the error message will use the correct line number if there're errors for the current item line.
            item.setItemLineNumber(++itemLineNumber);
            allPassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedImportPurchasingAccountsPayableItemEvent("", purDocument, item));
            // After the validation, set the item line number to the correct value as if it's added to the item list.
            item.setItemLineNumber(itemLineNumber + itemLinePosition);
        }
        if (allPassed) {
            updateBOReferenceforNewItems(importedItems, (PurchasingDocumentBase) purDocument);
            purDocument.getItems().addAll(itemLinePosition, importedItems);
        }
    } catch (ItemParserException e) {
        GlobalVariables.getMessageMap().putError(errorPath, e.getErrorKey(), e.getErrorParameters());
    }
    return mapping.findForward(KFSConstants.MAPPING_BASIC);
}
Also used : ItemParser(org.kuali.kfs.module.purap.util.ItemParser) PurApItem(org.kuali.kfs.module.purap.businessobject.PurApItem) PurchasingDocument(org.kuali.kfs.module.purap.document.PurchasingDocument) AttributedImportPurchasingAccountsPayableItemEvent(org.kuali.kfs.module.purap.document.validation.event.AttributedImportPurchasingAccountsPayableItemEvent) FormFile(org.apache.struts.upload.FormFile) ItemParserException(org.kuali.kfs.module.purap.exception.ItemParserException)

Aggregations

FormFile (org.apache.struts.upload.FormFile)1 PurApItem (org.kuali.kfs.module.purap.businessobject.PurApItem)1 PurchasingDocument (org.kuali.kfs.module.purap.document.PurchasingDocument)1 AttributedImportPurchasingAccountsPayableItemEvent (org.kuali.kfs.module.purap.document.validation.event.AttributedImportPurchasingAccountsPayableItemEvent)1 ItemParserException (org.kuali.kfs.module.purap.exception.ItemParserException)1 ItemParser (org.kuali.kfs.module.purap.util.ItemParser)1