view src/main/java/de/mpiwg/gazetteer/rest/SaveText.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children
line wrap: on
line source

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.icefaces.apache.commons.fileupload.servlet.ServletFileUpload;
import org.json.JSONObject;

import de.mpiwg.gazetteer.bo.LGBranch;
import de.mpiwg.gazetteer.bo.LGFile;
import de.mpiwg.gazetteer.utils.DataProvider;
import de.mpiwg.gazetteer.utils.JSONUtils;
import de.mpiwg.gazetteer.utils.exceptions.GazetteerException;
import de.mpiwg.gazetteer.utils.exceptions.VersioningException;

public class SaveText extends AbstractServletMethod{

	public static String name = "save";
	
	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("application/json");
		JSONObject json = new JSONObject();
		
		if (ServletFileUpload.isMultipartContent(request)) {
			String text = getRequestPart(request, "text");
			;//getRequestPart(request, "text");
			Long userId = getRequestLongPart(request, "userId");
			Long branchId = getRequestLongPart(request, "branchId");
			Long userPreviousFileId = getRequestLongPart(request, "userPreviousFileId");

			if(StringUtils.isNotEmpty(text) && userId != null && branchId != null && userPreviousFileId != null){
				//text = new String(text.getBytes("iso-8859-1"), "UTF-8");
				try {
					LGFile file = DataProvider.getInstance().saveFile(branchId, text, userId, userPreviousFileId);
					LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId());
					
					JSONObject jsonBranch = JSONUtils.branch2JSON(branch);
					JSONObject jsonFile = JSONUtils.file2JSON(file, true);
					
					json.put("branch", jsonBranch);
					json.put("file", jsonFile);
					json.put("status", "ok");
				} catch (GazetteerException e){
					json.put("status", "error");
					json.put("message", e.getMessage());
					json.put("code", e.getCode());
					if(e instanceof VersioningException){
						
						JSONObject curentFile = JSONUtils.file2JSON(((VersioningException) e).getCurrentFile(), false);
						JSONObject userFile = JSONUtils.file2JSON(((VersioningException) e).getUserFile(), false);
						
						json.put("currentFile", curentFile);	
						json.put("userFile", userFile);	
					}
				} catch (Exception e) {
					e.printStackTrace();
					json.put("status", "error");
					json.put("message", e.getMessage());
					json.put("code", GazetteerException.CODE);
				}
				
				
			}else{
				json.put("status", "error");
				json.put("message", "Following parameters are mandatory: text, userId, branchId, userPreviousFileId.");
			}
			
		} else {
			json.put("status", "error");
			json.put("message", "Content-type wrong. It should be: multipart/form-data");
		}
		
		PrintWriter out = response.getWriter();
		out.print(json.toString());
		out.flush();
	}
	
}