PageRenderTime 249ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/BGCX261/zonales-svn-to-git
Java | 72 lines | 57 code | 6 blank | 9 comment | 4 complexity | cb8db839d8b0d05aba6b0aa6d7278906 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.Date;
  12. import java.util.Properties;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import org.zonales.ZGram.ZGram;
  19. import org.zonales.ZGram.daos.ZGramDao;
  20. import org.zonales.crawlConfig.objets.State;
  21. import org.zonales.errors.ZMessages;
  22. import org.zonales.errors.ZMessage;
  23. /**
  24. *
  25. * @author nacho
  26. */
  27. public class SetZGram extends BaseService {
  28. @Override
  29. public void serve(HttpServletRequest request, HttpServletResponse response, Properties props) throws ServletException, IOException, Exception {
  30. response.setCharacterEncoding("UTF-8");
  31. response.setContentType("text/html");
  32. PrintWriter out = response.getWriter();
  33. int cod = Integer.valueOf(request.getParameter("cod"));
  34. String msg = request.getParameter("msg");
  35. String metadataJson = request.getParameter("metadata");
  36. String verbatim = request.getParameter("verbatim");
  37. String state = request.getParameter("state");
  38. String creadoPor = request.getParameter("creadoPor");
  39. ZGram zgram = new ZGram();
  40. Gson metadataGson = new Gson();
  41. ZMessage zMessage = new ZMessage(cod, msg);
  42. //Mapeo en un objeto ZCrawling la metadata que vienen en formato JSON en el request
  43. zgram = metadataGson.fromJson(metadataJson.replace("\\\"", "\"").replace("\\'", "\""), ZGram.class);
  44. zgram.setVerbatim(verbatim);
  45. zgram.setEstado(state != null && state.length() > 0 ? state : State.GENERATED);
  46. zgram.setCreado((new Date()).getTime());
  47. zgram.setZmessage(zMessage);
  48. zgram.setPeriodicidad(20); //TODO: correfir, por ahora está duro por defecto
  49. if (creadoPor != null) {
  50. zgram.setCreadoPor(creadoPor);
  51. }
  52. try {
  53. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Se intentará guardar: {0}", new Object[]{zgram});
  54. ZGramDao zGramDao = new ZGramDao(props.getProperty("db_host"), Integer.valueOf(props.getProperty("db_port")), props.getProperty("db_name"));
  55. String id = zGramDao.save(zgram);
  56. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Extracción guardada {0}", new Object[]{zgram});
  57. out.print(ZMessages.SUCCESS.toString().replace("}", "") + ", \"id\": \"" + id + "\"}");
  58. } catch (MongoException ex) {
  59. Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Error guardando extracción {0}: {1}", new Object[]{zgram,ex.getMessage()});
  60. out.print(ZMessages.SAVE_FAILED);
  61. } catch (Exception ex) {
  62. Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Base de datos no disponible {0}", new Object[]{ex.getMessage()});
  63. out.print(ZMessages.NO_DB_FAILED);
  64. }
  65. }
  66. }