PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/ZCrawl/ZCrawlSources/src/org/zonales/ZGram/services/UpdateZGram.java

https://gitlab.com/BGCX261/zonales-svn-to-git
Java | 115 lines | 83 code | 17 blank | 15 comment | 20 complexity | f8b1bfe186fe662286629d04324f1b60 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.zonales.ZGram.services;
  6. import com.google.gson.Gson;
  7. import org.zonales.BaseService;
  8. import com.mongodb.MongoException;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.util.Properties;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import org.zonales.ZGram.ZGram;
  18. import org.zonales.ZGram.daos.ZGramDao;
  19. import org.zonales.errors.ZMessages;
  20. /**
  21. *
  22. * @author nacho
  23. */
  24. public class UpdateZGram extends BaseService {
  25. @Override
  26. public void serve(HttpServletRequest request, HttpServletResponse response, Properties props) throws ServletException, IOException, Exception {
  27. response.setCharacterEncoding("UTF-8");
  28. response.setContentType("text/html");
  29. PrintWriter out = response.getWriter();
  30. String id = request.getParameter("id");
  31. String codStr = request.getParameter("newcod"); // != null ? Integer.valueOf(request.getParameter("newcod")) : null;
  32. String msg = request.getParameter("newmsg");
  33. String metadataJson = request.getParameter("newmetadata");
  34. String verbatim = request.getParameter("newverbatim");
  35. String state = request.getParameter("newstate");
  36. String modificadoPor = request.getParameter("modificadoPor");
  37. Long ultimaExtraccionConDatos = request.getParameter("ultimaExtraccionConDatos") != null ? Long.valueOf(request.getParameter("ultimaExtraccionConDatos")) : null;
  38. Long ultimoHitDeExtraccion = request.getParameter("ultimoHitDeExtraccion") != null ? Long.valueOf(request.getParameter("ultimoHitDeExtraccion")) : null;
  39. ZGramDao zGramDao = new ZGramDao(props.getProperty("db_host"), Integer.valueOf(props.getProperty("db_port")), props.getProperty("db_name"));
  40. Gson metadataGson = new Gson();
  41. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Verbatim {0}", new Object[]{verbatim});
  42. //Mapeo en un objeto ZCrawling la metadata que vienen en formato JSON en el request
  43. ZGram zgram;
  44. if (metadataJson != null) {
  45. zgram = metadataGson.fromJson(metadataJson.replace("\\\"", "\""), ZGram.class);
  46. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Metadata {0}", new Object[]{metadataJson});
  47. } else {
  48. String zgramJson = zGramDao.retrieveJson(id);
  49. zgram = metadataGson.fromJson(zgramJson, ZGram.class);
  50. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "=====>> ZGRAM: {0}", new Object[]{metadataJson});
  51. /*zgram = new ZGram();
  52. zgram.setIncluyeComentarios(null);
  53. zgram.setTagsFuente(null);
  54. zgram.setSiosi(null);
  55. zgram.setNocriterio(null);*/
  56. }
  57. if (codStr != null) {
  58. Integer cod = Integer.valueOf(request.getParameter("newcod"));
  59. zgram.setCod(cod);
  60. }
  61. if (msg != null) {
  62. zgram.setMsg(msg);
  63. }
  64. if (verbatim != null) {
  65. zgram.setVerbatim(verbatim);
  66. }
  67. if (state != null) {
  68. zgram.setEstado(state);
  69. }
  70. if (ultimaExtraccionConDatos != null) {
  71. zgram.setUltimaExtraccionConDatos(ultimaExtraccionConDatos);
  72. }
  73. if (ultimoHitDeExtraccion != null) {
  74. zgram.setUltimoHitDeExtraccion(ultimoHitDeExtraccion);
  75. }
  76. if (modificadoPor != null) {
  77. zgram.setModificadoPor(modificadoPor);
  78. }
  79. //zgram.setPeriodicidad(20); //TODO: correfir, por ahora está duro por defecto, siempre que actualizo la seteo en 20
  80. try {
  81. zGramDao.update(id, zgram);
  82. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Extracción actualizada {0}", new Object[]{zgram});
  83. out.print(ZMessages.SUCCESS.toString().replace("}", "") + ", \"id\": \"" + id + "\"}");
  84. } catch (MongoException ex) {
  85. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Error actualizando extracción {0}: {1}", new Object[]{zgram, ex.getMessage()});
  86. out.print(ZMessages.SAVE_FAILED);
  87. } catch (Exception ex) {
  88. StringBuilder stacktrace = new StringBuilder();
  89. for (StackTraceElement line : ex.getStackTrace()) {
  90. stacktrace.append(line.toString());
  91. stacktrace.append("\n");
  92. }
  93. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,
  94. "EXCEPCION: {0}\nTRACE: {1}", new Object[]{ex, stacktrace.toString()});
  95. out.print(ZMessages.NO_DB_FAILED);
  96. }
  97. }
  98. }