cb76eeec by Ean Schuessler

Handle CSV input #4378

1 parent ef4b37ae
...@@ -55,6 +55,9 @@ import org.ofbiz.service.LocalDispatcher; ...@@ -55,6 +55,9 @@ import org.ofbiz.service.LocalDispatcher;
55 import org.ofbiz.service.ModelService; 55 import org.ofbiz.service.ModelService;
56 import org.ofbiz.service.ServiceContainer; 56 import org.ofbiz.service.ServiceContainer;
57 57
58 import org.apache.commons.csv.CSVFormat;
59 import org.apache.commons.csv.CSVRecord;
60
58 import groovy.lang.GroovyClassLoader; 61 import groovy.lang.GroovyClassLoader;
59 import groovy.lang.Script; 62 import groovy.lang.Script;
60 63
...@@ -139,11 +142,22 @@ public class DirectControlServlet extends HttpServlet { ...@@ -139,11 +142,22 @@ public class DirectControlServlet extends HttpServlet {
139 for (String key : items.keySet()) { 142 for (String key : items.keySet()) {
140 context.put(key, items.get(key)); 143 context.put(key, items.get(key));
141 } 144 }
145 } else if ("text/csv".equals(contentType)) {
146 Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(request.getReader());
147 List data = new ArrayList();
148 for (CSVRecord record : records) {
149 List row = new ArrayList();
150 String cell = null;
151 Iterator<String> i=record.iterator();
152 while (i.hasNext()) {
153 row.add(i.next());
154 }
155 data.add(row);
156 }
157 context.put("data", data);
142 } else { 158 } else {
143 // Check if the request is a backbone style "emulateJSON" request 159 // Check if the request is a backbone style "emulateJSON" request
144 if (contentType != null && 160 if (contentType != null && contentType.indexOf("x-www-form-urlencoded") != -1 && request.getParameter("model") != null) {
145 contentType.indexOf("x-www-form-urlencoded") != -1 &&
146 request.getParameter("model") != null) {
147 Debug.logInfo("MODEL: " + request.getParameter("model"), module); 161 Debug.logInfo("MODEL: " + request.getParameter("model"), module);
148 JSON json = new JSON(new StringReader(request.getParameter("model"))); 162 JSON json = new JSON(new StringReader(request.getParameter("model")));
149 Map<String,Object> items = json.JSONObject(); 163 Map<String,Object> items = json.JSONObject();
...@@ -254,7 +268,6 @@ public class DirectControlServlet extends HttpServlet { ...@@ -254,7 +268,6 @@ public class DirectControlServlet extends HttpServlet {
254 String jsonStr = json.toString(); 268 String jsonStr = json.toString();
255 response.setContentLength(jsonStr.getBytes("UTF8").length); 269 response.setContentLength(jsonStr.getBytes("UTF8").length);
256 writer.write(jsonStr); 270 writer.write(jsonStr);
257
258 writer.flush(); 271 writer.flush();
259 writer.close(); 272 writer.close();
260 } catch (Throwable t) { 273 } catch (Throwable t) {
......