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;
}
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;
}
Aggregations