/griffin-core/src/main/java/com/ebay/oss/griffin/repo/DqJobRepoImpl.java

https://github.com/eBay/griffin · Java · 102 lines · 72 code · 15 blank · 15 comment · 7 complexity · 6c88d9456561ad4d1660620a879b5c1b MD5 · raw file

  1. /*
  2. Copyright (c) 2016 eBay Software Foundation.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.ebay.oss.griffin.repo;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.springframework.stereotype.Repository;
  17. import com.ebay.oss.griffin.common.NumberUtils;
  18. import com.ebay.oss.griffin.domain.DqJob;
  19. import com.google.gson.Gson;
  20. import com.mongodb.BasicDBObject;
  21. import com.mongodb.DBObject;
  22. import com.mongodb.util.JSON;
  23. // FIXME why DqJob use a string id
  24. @Repository
  25. public class DqJobRepoImpl extends BaseRepo<DqJob> implements DqJobRepo {
  26. public DqJobRepoImpl() {
  27. super("dq_job", DqJob.class);
  28. }
  29. @Override
  30. public void update(DqJob job) {
  31. DBObject dbo = dboOf(job);
  32. if (dbo != null)
  33. dbCollection.remove(dbo);
  34. save(job);
  35. }
  36. DBObject dboOf(DqJob job) {
  37. return dbCollection.findOne(new BasicDBObject("_id", job.getId()));
  38. }
  39. @Override
  40. public int newJob(DqJob job) {
  41. try {
  42. DBObject temp = dboOf(job);
  43. if (temp != null)
  44. return 0;
  45. Gson gson = new Gson();
  46. DBObject t1 = (DBObject) JSON.parse(gson.toJson(job));
  47. dbCollection.save(t1);
  48. return 1;
  49. } catch (Exception e) {
  50. logger.warn("===========insert new job error==============="
  51. + e.getMessage());
  52. return 0;
  53. }
  54. }
  55. @Override
  56. public DqJob getById(String jobId) {
  57. DBObject dbo = dbCollection.findOne(new BasicDBObject("_id", jobId));
  58. if (dbo == null) {
  59. return null;
  60. }
  61. return toEntity(dbo);
  62. }
  63. @Override
  64. protected DqJob toEntity(DBObject o) {
  65. DqJob entity = new DqJob();
  66. entity.setId((String)o.get("_id"));
  67. entity.setModelList((String)o.get("modelList"));
  68. entity.setJobType(NumberUtils.parseInt( o.get("jobType")));
  69. entity.setStatus(NumberUtils.parseInt( o.get("status")));
  70. entity.setStarttime(NumberUtils.parseLong(o.get("starttime")));
  71. entity.setContent((String) o.get("content"));
  72. entity.setEndtime(NumberUtils.parseLong( o.get("endtime")));
  73. entity.setValue(NumberUtils.parseLong(o.get("value")));
  74. return entity;
  75. }
  76. @Override
  77. public List<DqJob> getByStatus(int status) {
  78. List<DqJob> list = new ArrayList<>();
  79. List<DBObject> dboList = dbCollection.find(new BasicDBObject("status", status)).toArray();
  80. for (DBObject dbo : dboList) {
  81. list.add(toEntity(dbo));
  82. }
  83. return list;
  84. }
  85. }