cb76eeec by Ean Schuessler

Handle CSV input #4378

1 parent ef4b37ae
......@@ -55,6 +55,9 @@ import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceContainer;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
......@@ -139,11 +142,22 @@ public class DirectControlServlet extends HttpServlet {
for (String key : items.keySet()) {
context.put(key, items.get(key));
}
} else if ("text/csv".equals(contentType)) {
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(request.getReader());
List data = new ArrayList();
for (CSVRecord record : records) {
List row = new ArrayList();
String cell = null;
Iterator<String> i=record.iterator();
while (i.hasNext()) {
row.add(i.next());
}
data.add(row);
}
context.put("data", data);
} else {
// Check if the request is a backbone style "emulateJSON" request
if (contentType != null &&
contentType.indexOf("x-www-form-urlencoded") != -1 &&
request.getParameter("model") != null) {
if (contentType != null && contentType.indexOf("x-www-form-urlencoded") != -1 && request.getParameter("model") != null) {
Debug.logInfo("MODEL: " + request.getParameter("model"), module);
JSON json = new JSON(new StringReader(request.getParameter("model")));
Map<String,Object> items = json.JSONObject();
......@@ -254,7 +268,6 @@ public class DirectControlServlet extends HttpServlet {
String jsonStr = json.toString();
response.setContentLength(jsonStr.getBytes("UTF8").length);
writer.write(jsonStr);
writer.flush();
writer.close();
} catch (Throwable t) {
......