Mercurial > hg > IndexMetaContextualizer
comparison src/main/java/de/mpiwg/indexmeta/web/servlet/methods/JSONGetCtxFromDB.java @ 7:bc57f2660b0f
implementation of web service
| author | Jorge Urzua <jurzua@mpiwg-berlin.mpg.de> |
|---|---|
| date | Fri, 12 Apr 2013 17:48:42 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 5:7d231e4e86e5 | 7:bc57f2660b0f |
|---|---|
| 1 package de.mpiwg.indexmeta.web.servlet.methods; | |
| 2 | |
| 3 import java.io.IOException; | |
| 4 import java.io.PrintWriter; | |
| 5 import java.util.ArrayList; | |
| 6 import java.util.List; | |
| 7 | |
| 8 import javax.servlet.ServletException; | |
| 9 import javax.servlet.http.HttpServletRequest; | |
| 10 import javax.servlet.http.HttpServletResponse; | |
| 11 | |
| 12 import org.apache.commons.lang.StringUtils; | |
| 13 import org.json.JSONArray; | |
| 14 import org.json.JSONException; | |
| 15 import org.json.JSONObject; | |
| 16 | |
| 17 import de.mpiwg.indexmeta.bo.Contextualization; | |
| 18 import de.mpiwg.indexmeta.services.DataProvider; | |
| 19 import de.mpiwg.indexmeta.web.utils.JSONUtils; | |
| 20 | |
| 21 public class JSONGetCtxFromDB extends AbstractServletMethod { | |
| 22 | |
| 23 public static void execute(DataProvider dp, | |
| 24 HttpServletRequest request, HttpServletResponse response) | |
| 25 throws ServletException, IOException, JSONException { | |
| 26 | |
| 27 JSONObject json = new JSONObject(); | |
| 28 | |
| 29 long startExecution = System.currentTimeMillis(); | |
| 30 | |
| 31 String indexMetaId = getString(request, p_indexMetaId); | |
| 32 String type = getString(request, p_type); | |
| 33 String elementId = getString(request, p_elementId); | |
| 34 String remoteId = getString(request, p_remoteId); | |
| 35 | |
| 36 List<Contextualization> rs = null; | |
| 37 | |
| 38 if (StringUtils.isNotEmpty(indexMetaId)) { | |
| 39 | |
| 40 rs = dp.getCtxByIndexMetaId(indexMetaId); | |
| 41 rs = filterByType(rs, type); | |
| 42 rs = filterByElementId(rs, elementId); | |
| 43 rs = filterByRemoteId(rs, remoteId); | |
| 44 | |
| 45 } else if (StringUtils.isNotEmpty(type)) { | |
| 46 | |
| 47 rs = dp.getCtxByType(type); | |
| 48 rs = filterByElementId(rs, elementId); | |
| 49 rs = filterByRemoteId(rs, remoteId); | |
| 50 | |
| 51 } else { | |
| 52 response.sendError(HttpServletResponse.SC_BAD_REQUEST, | |
| 53 "Parameters 'indexMetaId' and 'type' are null. " | |
| 54 + "At least one of them must be not null."); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 JSONArray rsArray = new JSONArray(); | |
| 59 for (Contextualization ctx : rs) { | |
| 60 rsArray.put(JSONUtils.ctx2JSON(ctx)); | |
| 61 } | |
| 62 | |
| 63 json.put("rs", rsArray); | |
| 64 json.put(RUNTIME, (System.currentTimeMillis() - startExecution)); | |
| 65 | |
| 66 PrintWriter out = response.getWriter(); | |
| 67 out.print(json.toString()); | |
| 68 | |
| 69 } | |
| 70 | |
| 71 } |
