diff src/main/java/de/intevation/lada/rest/LProbeService.java @ 321:5844d7457dde

Completed importer for LAF format. Ort objects still need some attention.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 23 Aug 2013 11:35:24 +0200
parents f0e1caad6027
children 30883ab746a5
line wrap: on
line diff
--- a/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri Aug 23 11:32:21 2013 +0200
+++ b/src/main/java/de/intevation/lada/rest/LProbeService.java	Fri Aug 23 11:35:24 2013 +0200
@@ -1,6 +1,10 @@
 package de.intevation.lada.rest;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
@@ -34,6 +38,7 @@
 import de.intevation.lada.data.LProbeRepository;
 import de.intevation.lada.data.QueryBuilder;
 import de.intevation.lada.data.Repository;
+import de.intevation.lada.data.importer.Importer;
 import de.intevation.lada.model.LProbe;
 import de.intevation.lada.model.LProbeInfo;
 import de.intevation.lada.utils.QueryTools;
@@ -70,6 +75,10 @@
     @Named("dataauthorization")
     private Authorization authorization;
 
+    @Inject
+    @Named("lafimporter")
+    private Importer importer;
+
     /**
      * The logger for this class.
      */
@@ -261,6 +270,45 @@
     public Response upload(MultipartFormDataInput input, @Context HttpHeaders header) {
         try {
             AuthenticationResponse auth = authentication.authorizedGroups(header);
+            if (!authentication.isAuthorizedUser(header)) {
+                return new Response(false, 698, null);
+            }
+
+            String name = "";
+            String content = "";
+            Map<String, List<InputPart>> data = input.getFormDataMap();
+            try {
+                List<InputPart> parts = input.getParts();
+                for (InputPart part: parts) {
+                    InputStream inStream = part.getBody(InputStream.class, null);
+                    MultivaluedMap<String, String> headers = part.getHeaders();
+                    String[] cDisp = headers.getFirst("content-disposition").split(";");
+                    for (String fName : cDisp) {
+                        if (fName.trim().startsWith("filename")) {
+                            String[] fileName = fName.split("=");
+                            name = fileName[1].trim().replace("\"", "");
+                        }
+                    }
+                    content = IOUtils.toString(inStream);
+                }
+            }
+            catch (IOException e) {
+                return new Response(false, 603, null);
+            }
+
+            boolean success = importer.importData(content, auth);
+            List<Object> respData = new LinkedList<Object>();
+            respData.add(importer.getErrors());
+            respData.add(importer.getWarnings());
+            Map<String, String> fileData = new HashMap<String, String>();
+            fileData.put("filename", name);
+            respData.add(fileData);
+            int code = 200;
+            if (!success) {
+                code = 660;
+            }
+            Response response = new Response(success, code, respData);
+            return response;
             // TODO: Check Authorisation. How should we check the
             // authorisation while importing? I think we must differ between
             // updating already existing proben and creating new proben. (ti)
@@ -272,7 +320,6 @@
             //}
             // TODO: Response must contain a "file" attribute with the name of
             // the uploaded file.(ti) <2013-08-13 16:23> 
-            return new Response(true, 200, null);
             //return new Response(false, 698, null);
         }
         catch(AuthenticationException ae) {
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)