Mercurial > hg > LGServices
diff src/main/java/de/mpiwg/gazetteer/rest/SaveText.java @ 0:3e62083dbcbf
First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Thu, 23 Apr 2015 15:46:01 +0200 |
parents | |
children | f106f2487ac1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/gazetteer/rest/SaveText.java Thu Apr 23 15:46:01 2015 +0200 @@ -0,0 +1,83 @@ +package de.mpiwg.gazetteer.rest; + +import java.io.PrintWriter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.lang.StringUtils; +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"); + + System.out.println("%%%%% SaveText [branchId=" + branchId + ", userId=" + userId + "]"); + + 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(); + } + +}