/branches/ZCrawl/ZCrawlMetaServlet/src/org/zonales/crawlConfig/daos/ServiceDao.java

https://gitlab.com/BGCX261/zonales-svn-to-git · Java · 157 lines · 111 code · 36 blank · 10 comment · 22 complexity · 7d7495f532fe6267d0397e24e5253748 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.zonales.crawlConfig.daos;
  6. import com.mongodb.BasicDBObject;
  7. import com.mongodb.DBCollection;
  8. import com.mongodb.DBCursor;
  9. import com.mongodb.DBObject;
  10. import com.mongodb.MongoException;
  11. import java.util.ArrayList;
  12. import java.util.StringTokenizer;
  13. import org.zonales.crawlConfig.objets.Param;
  14. import org.zonales.crawlConfig.objets.Service;
  15. /**
  16. *
  17. * @author nacho
  18. */
  19. public class ServiceDao extends BaseDao {
  20. private DBCollection services;
  21. public ServiceDao(String db_host, Integer db_port, String db_name) {
  22. super(db_host, db_port, db_name);
  23. this.services = this.db.getCollection("services");
  24. this.services.ensureIndex(new BasicDBObject("name", 1), "uniqueName", true);
  25. }
  26. public void save(Service service) throws MongoException {
  27. BasicDBObject serviceDoc = new BasicDBObject();
  28. ArrayList<Param> params = service.getParams();
  29. ArrayList paramsToDoc = new ArrayList();
  30. for (Param param: params) {
  31. paramsToDoc.add(new BasicDBObject(param.getName(), param.getRequired()));
  32. }
  33. serviceDoc.put("name", service.getName());
  34. serviceDoc.put("uri", service.getUri());
  35. serviceDoc.put("pluginName", service.getPluginName());
  36. serviceDoc.put("params", paramsToDoc);
  37. System.out.println(serviceDoc.toString());
  38. this.services.insert(serviceDoc);
  39. }
  40. public void update(String name, Service newService) throws MongoException {
  41. BasicDBObject query = new BasicDBObject("name", name);
  42. DBObject resp;
  43. DBCursor cur;
  44. cur = this.services.find(query);
  45. resp = cur.next();
  46. if (resp != null) {
  47. BasicDBObject serviceDoc = new BasicDBObject();
  48. if (newService.getName() != null) {
  49. serviceDoc.put("name", newService.getName());
  50. } else {
  51. serviceDoc.put("name", (String)resp.get("name"));
  52. }
  53. if (newService.getUri() != null) {
  54. serviceDoc.put("uri", newService.getUri());
  55. } else {
  56. serviceDoc.put("uri", (String)resp.get("uri"));
  57. }
  58. if (newService.getPluginName() != null) {
  59. serviceDoc.put("pluginName", newService.getPluginName());
  60. } else {
  61. serviceDoc.put("pluginName", (String)resp.get("pluginName"));
  62. }
  63. ArrayList<Param> params = newService.getParams();
  64. if (params != null) {
  65. ArrayList paramsToDoc = new ArrayList();
  66. for (Param param: params) {
  67. paramsToDoc.add(new BasicDBObject(param.getName(), param.getRequired()));
  68. }
  69. serviceDoc.put("params", paramsToDoc);
  70. } else {
  71. serviceDoc.put("params", resp.get("params"));
  72. }
  73. this.services.update(new BasicDBObject().append("name", name), serviceDoc);
  74. }
  75. }
  76. public String retrieveJson(String name) {
  77. BasicDBObject query = new BasicDBObject("name", name);
  78. DBObject resp;
  79. DBCursor cur;
  80. cur = this.services.find(query);
  81. resp = cur.next();
  82. resp.removeField("_id");
  83. //System.out.println(resp);
  84. return resp.toString();
  85. }
  86. public Service retrieve(String name) {
  87. BasicDBObject query = new BasicDBObject("name", name);
  88. DBObject resp;
  89. DBCursor cur;
  90. Service service = new Service();
  91. ArrayList<BasicDBObject> paramsJson = new ArrayList<BasicDBObject>();
  92. String paramName = "";
  93. String token;
  94. Boolean paramRequired = false;
  95. StringTokenizer paramToken;
  96. int tokenCount = 0;
  97. cur = this.services.find(query);
  98. resp = cur.next();
  99. resp.removeField("_id");
  100. service.setName((String)resp.get("name"));
  101. service.setUri((String)resp.get("uri"));
  102. service.setPluginName((String)resp.get("pluginName"));
  103. paramsJson = (ArrayList<BasicDBObject>)resp.get("params");
  104. for (BasicDBObject paramJson: paramsJson) {
  105. paramToken = new StringTokenizer(paramJson.toString(), "\" }");
  106. while (paramToken.hasMoreTokens()) {
  107. token = paramToken.nextToken();
  108. tokenCount++;
  109. if (tokenCount == 2) {
  110. paramName = token;
  111. }
  112. if (tokenCount == 4) {
  113. paramRequired = Boolean.valueOf(token);
  114. }
  115. }
  116. service.addParam(paramName, paramRequired);
  117. }
  118. //System.out.println(service);
  119. return service;
  120. }
  121. }