Hi Everyone,
I am having difficulty with how to keep parsed/validated information from an
uploaded CSV file around so I can display it for review to the customer
before it is put into the db.
I am using the importAddressMatchMap java service(called from request map)
as a reference to parse and validate the user uploaded CSV file. I don't
want to put the information into the db after parsing/validation, but want
to allow the user to verify the information first before the order is
submitted.
Where can I store the information(records or map) so I can call it from a
.ftl in the next step or two?
Any guidance would be greatly appreciated!
-CJ
I have included the OOTB java method below for reference.
public static Map<String, Object>
importAddressMatchMapCsv(DispatchContext dctx, Map<String, ? extends Object>
context) {
GenericDelegator delegator = dctx.getDelegator();
ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
String encoding = System.getProperty("file.encoding");
String csvFile =
Charset.forName(encoding).decode(fileBytes).toString();
csvFile = csvFile.replaceAll("\\r", "");
String[] records = csvFile.split("\\n");
for (int i = 0; i < records.length; i++) {
if (records[i] != null) {
String str = records[i].trim();
String[] map = str.split(",");
if (map.length != 2 && map.length != 3) {
return ServiceUtil.returnError("Invalid format for CSV
(key,value,sequence)");
} else {
GenericValue addrMap =
delegator.makeValue("AddressMatchMap");
addrMap.put("mapKey", map[0].trim().toUpperCase());
addrMap.put("mapValue", map[1].trim().toUpperCase());
int seq = i + 1;
if (map.length == 3) {
char[] chars = map[2].toCharArray();
boolean isNumber = true;
for (char c: chars) {
if (!Character.isDigit(c)) {
isNumber = false;
}
}
if (isNumber) {
try {
seq = Integer.parseInt(map[2]);
} catch (Throwable t) {
Debug.logWarning(t, "Unable to parse
number", module);
}
}
}
addrMap.put("sequenceNum", Long.valueOf(seq));
Debug.log("Creating map entry: " + addrMap, module);
try {
delegator.create(addrMap);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
} else {
return ServiceUtil.returnError("No records found in file");
}
}
return ServiceUtil.returnSuccess();
}
--
View this message in context:
http://www.nabble.com/Need-some-guidance-on-storing-parsed-info-from-CSV-files-tp21693280p21693280.html
Sent from the OFBiz - User mailing list archive at Nabble.com.