Search in sources :

Example 6 with CommodityCode

use of org.kuali.kfs.vnd.businessobject.CommodityCode in project cu-kfs by CU-CommunityApps.

the class CommodityCodeUpdateServiceImpl method updateCommodityCodes.

/**
 * @param batchList
 * @return
 */
protected boolean updateCommodityCodes(List<CommodityCode> batchList) {
    // Retrieve complete list of codes to compare new data set against
    Collection<CommodityCode> codes = SpringContext.getBean(BusinessObjectService.class).findAll(CommodityCode.class);
    if (ObjectUtils.isNull(codes) || codes.size() < 1) {
        // Report error message and exit update process
        return false;
    }
    HashMap<String, CommodityCode> codesFromDatabase = buildMapOfCommodityCodes(codes);
    List<CommodityCode> newCodes = new ArrayList<CommodityCode>();
    List<CommodityCode> updatedCodes = new ArrayList<CommodityCode>();
    List<CommodityCode> inactivatedCodes = new ArrayList<CommodityCode>();
    for (CommodityCode codesFromFile : batchList) {
        CommodityCode ccRetrieved = codesFromDatabase.get(codesFromFile.getPurchasingCommodityCode());
        if (ObjectUtils.isNotNull(ccRetrieved)) {
            // Compare old and new to see if any changes are required
            if (!StringUtils.equalsIgnoreCase(codesFromFile.getCommodityDescription().trim(), ccRetrieved.getCommodityDescription().trim())) {
                // Update the code with the new values because descriptions don't match
                LOG.info("Updating commodity code description from '" + ccRetrieved.getPurchasingCommodityCode() + ": " + ccRetrieved.getCommodityDescription() + "' to '" + codesFromFile.getPurchasingCommodityCode() + ": " + codesFromFile.getCommodityDescription() + "'");
                ccRetrieved.setCommodityDescription(codesFromFile.getCommodityDescription());
                updatedCodes.add(ccRetrieved);
            } else if (!ccRetrieved.isActive()) {
                // Do something here if the code is in the file (thus active), but inactive in the DB
                ccRetrieved.setActive(true);
                updatedCodes.add(ccRetrieved);
                LOG.info("Reactivating commodity code: " + ccRetrieved.getPurchasingCommodityCode() + ": " + ccRetrieved.getCommodityDescription());
            }
        } else {
            // Add the new commodity code
            codesFromFile.setSalesTaxIndicator(false);
            codesFromFile.setRestrictedItemsIndicator(false);
            codesFromFile.setActive(true);
            newCodes.add(codesFromFile);
            // Report new code was added
            LOG.info("Adding new commodity code: " + codesFromFile.getPurchasingCommodityCode() + ": " + codesFromFile.getCommodityDescription());
        }
        // Remove from collection to keep collection representing only values not yet reviewed.
        codesFromDatabase.remove(codesFromFile.getPurchasingCommodityCode());
    }
    if (!codesFromDatabase.isEmpty()) {
        // Need to inactivate any remaining codes, as they are no longer in the file
        Collection<CommodityCode> dbCodesToInactivate = codesFromDatabase.values();
        LOG.info("Number of commodity codes to inactivate: " + dbCodesToInactivate.size());
        for (CommodityCode codeToInactivate : dbCodesToInactivate) {
            // Only bother inactivating if it's an active code, otherwise, ignore
            if (codeToInactivate.isActive()) {
                codeToInactivate.setActive(false);
                inactivatedCodes.add(codeToInactivate);
                LOG.info("Inactivating commodity code: " + codeToInactivate.getPurchasingCommodityCode() + ": " + codeToInactivate.getCommodityDescription());
            }
        }
        codesFromDatabase.clear();
    }
    Map<String, Integer> loadCounts = new HashMap<String, Integer>();
    Map<String, List<CommodityCode>> loadCodes = new HashMap<String, List<CommodityCode>>();
    // Save all new commodity codes
    LOG.info("NEW: Number of new commodity codes: " + newCodes.size());
    loadCounts.put(NEW_COUNTS, new Integer(newCodes.size()));
    loadCodes.put(NEW_CODES, newCodes);
    businessObjectService.save(newCodes);
    // Save all commodity code changes
    LOG.info("UPDATE: Number of commodity codes to update: " + updatedCodes.size());
    loadCounts.put(UPDATE_COUNTS, new Integer(updatedCodes.size()));
    loadCodes.put(UPDATE_CODES, updatedCodes);
    businessObjectService.save(updatedCodes);
    // Save all inactivated commodity code
    LOG.info("INACTIVE: Number of commodity codes to inactivate: " + inactivatedCodes.size());
    loadCounts.put(INACTIVE_COUNTS, new Integer(inactivatedCodes.size()));
    loadCodes.put(INACTIVE_CODES, inactivatedCodes);
    businessObjectService.save(inactivatedCodes);
    writeReports(getJobParameters(), loadCounts, loadCodes);
    return true;
}
Also used : HashMap(java.util.HashMap) CommodityCode(org.kuali.kfs.vnd.businessobject.CommodityCode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BusinessObjectService(org.kuali.kfs.krad.service.BusinessObjectService)

Example 7 with CommodityCode

use of org.kuali.kfs.vnd.businessobject.CommodityCode in project cu-kfs by CU-CommunityApps.

the class CommodityCodeInputFileType method parse.

/**
 * @see org.kuali.kfs.sys.batch.BatchInputFileType#parse(byte[])
 */
public Object parse(byte[] fileByteContent) throws ParseException {
    List<CommodityCode> batchList = new ArrayList<CommodityCode>();
    CommodityCode newCode = null;
    BufferedReader bufferedFileReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(fileByteContent)));
    String fileLine;
    String commodityCode;
    String commodityCodeDesc;
    int lineNumber = 0;
    try {
        while ((fileLine = bufferedFileReader.readLine()) != null) {
            lineNumber++;
            // Parse the tab delimited lines
            StringTokenizer st = new StringTokenizer(fileLine, "\t");
            commodityCode = st.nextToken();
            commodityCodeDesc = st.nextToken();
            if (st.hasMoreTokens()) {
            // Report too many values on the line
            }
            newCode = new CommodityCode();
            newCode.setPurchasingCommodityCode(commodityCode);
            newCode.setCommodityDescription(commodityCodeDesc);
            batchList.add(newCode);
        }
    } catch (IOException e) {
        // probably won't happen since we're reading from a byte array, but just in case
        LOG.error("Error encountered reading from file content", e);
        throw new ParseException("Error encountered reading from file content", e);
    }
    return batchList;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) CommodityCode(org.kuali.kfs.vnd.businessobject.CommodityCode) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ParseException(org.kuali.kfs.sys.exception.ParseException)

Aggregations

CommodityCode (org.kuali.kfs.vnd.businessobject.CommodityCode)7 ArrayList (java.util.ArrayList)4 List (java.util.List)3 BusinessObjectService (org.kuali.kfs.krad.service.BusinessObjectService)2 PurchasingItemBase (org.kuali.kfs.module.purap.businessobject.PurchasingItemBase)2 VendorCommodityCode (org.kuali.kfs.vnd.businessobject.VendorCommodityCode)2 VendorDetail (org.kuali.kfs.vnd.businessobject.VendorDetail)2 CuVendorSupplierDiversityExtension (edu.cornell.kfs.vnd.businessobject.CuVendorSupplierDiversityExtension)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 CapitalAssetSystem (org.kuali.kfs.integration.purap.CapitalAssetSystem)1 SequenceAccessorService (org.kuali.kfs.krad.service.SequenceAccessorService)1 PurApItem (org.kuali.kfs.module.purap.businessobject.PurApItem)1 PurchaseOrderCapitalAssetSystem (org.kuali.kfs.module.purap.businessobject.PurchaseOrderCapitalAssetSystem)1