diff src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java @ 58:b8ad346e39a0

new: modify based on doc 'Improving LGServices interface.docx': user workflow improvement, functions like adding task into a topic and search suggestions.
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 02 May 2016 12:03:30 +0200
parents
children a025dd907626
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/gazetteer/rest/DownloadTabDelimited4File.java	Mon May 02 12:03:30 2016 +0200
@@ -0,0 +1,68 @@
+package de.mpiwg.gazetteer.rest;
+
+import java.io.PrintWriter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang.StringUtils;
+import org.json.JSONObject;
+
+import de.mpiwg.gazetteer.bo.LGFile;
+import de.mpiwg.gazetteer.utils.DataProvider;
+import de.mpiwg.gazetteer.utils.FileManager;
+import de.mpiwg.gazetteer.utils.HTTPUtils;
+
+public class DownloadTabDelimited4File extends AbstractServletMethod {
+	public static String name = "downloadTabDelimited4File";
+	
+	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
+		
+		Long fileId = getQueryLongParam(request, "fileId");
+		
+		if(fileId != null){
+			LGFile file = DataProvider.getInstance().getFile(fileId);
+			if(file != null){
+				
+				String text = HTTPUtils.getTabDelimitedOfFile(fileId);	// get text as tab-delimited file
+					
+				if(StringUtils.isNotEmpty(text)){
+				
+					String filename = file.getFileName(); // "112_360452_360453_2015.09.04_12.43.08.924_11.txt";   
+					
+					//filename extension with csv
+					filename = filename.substring(0, filename.length()-3);
+					filename += "csv";
+					
+					response.setContentType("text/csv; charset=UTF-8"); 
+					response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   
+				
+					
+					PrintWriter out = response.getWriter();
+					out.print(text);
+					out.flush();
+					
+				}	
+							
+				
+			} else{
+				response.setContentType("application/json");
+				JSONObject json = new JSONObject();
+				json.put("status", "error");
+				json.put("message", "File no found (" + fileId + ")");
+				PrintWriter out = response.getWriter();
+				out.print(json.toString());
+				out.flush();
+			}
+		}else{
+			response.setContentType("application/json");
+			JSONObject json = new JSONObject();
+			json.put("status", "error");
+			json.put("message", "Following parameters are mandatory: fileId.");
+			PrintWriter out = response.getWriter();
+			out.print(json.toString());
+			out.flush();
+		}
+	}
+	
+}