DatasourceManager.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.yihu.hos.system.service;
  2. import com.yihu.hos.web.framework.model.ActionResult;
  3. import com.yihu.hos.web.framework.model.DataGridResult;
  4. import com.yihu.hos.system.dao.intf.IDatasourceDao;
  5. import com.yihu.hos.system.model.SystemDatasource;
  6. import com.yihu.hos.system.service.intf.IDatasourceManager;
  7. import com.yihu.hos.common.Services;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import javax.annotation.Resource;
  12. import java.util.List;
  13. import java.util.Map;
  14. /**
  15. * Created by hzp on 2016/1/11.
  16. */
  17. @Service(Services.Datasource)
  18. @Transactional
  19. public class DatasourceManager implements IDatasourceManager {
  20. @Autowired
  21. private IDatasourceDao datasourceDao;
  22. /**
  23. * 测试连接
  24. */
  25. private boolean testDB(String type, String config)
  26. {
  27. try {
  28. datasourceDao.testDatasource(type,config);
  29. return true;
  30. } catch (Exception e) {
  31. return false;
  32. }
  33. }
  34. /**
  35. * 根据ID获取数据源信息
  36. * @return
  37. */
  38. @Override
  39. public SystemDatasource getDatasourcegById(String id) throws Exception
  40. {
  41. return datasourceDao.getEntity(SystemDatasource.class, id);
  42. }
  43. /**
  44. * 获取数据源列表
  45. * @param page
  46. * @param rows
  47. * @return
  48. * @throws Exception
  49. */
  50. @Override
  51. public DataGridResult getDatasource(Map<String, Object> conditionMap, int page, int rows) throws Exception{
  52. return datasourceDao.getDatasource(conditionMap,page,rows);
  53. }
  54. /**
  55. * 根据组织ID获取据源列表
  56. */
  57. @Override
  58. public DataGridResult getDatasourceByOrg(String orgId) throws Exception{
  59. return datasourceDao.getDatasourceByOrg(orgId);
  60. }
  61. /**
  62. * 测试数据源
  63. * @param dataSource
  64. * @param config
  65. * @return
  66. */
  67. @Override
  68. public ActionResult testDatasource(String dataSource, String config) {
  69. try {
  70. boolean re = testDB(dataSource, config);
  71. if(re)
  72. return new ActionResult(true,"测试成功!");
  73. else
  74. return new ActionResult(false,"测试失败!");
  75. }
  76. catch (Exception e)
  77. {
  78. System.out.print(e.getMessage());
  79. return new ActionResult(false,"测试失败!");
  80. }
  81. }
  82. /**
  83. * 新增数据源
  84. * @param obj
  85. * @return
  86. */
  87. @Override
  88. @Transactional
  89. public ActionResult addDatasource(SystemDatasource obj) {
  90. try{
  91. datasourceDao.saveEntity(obj);
  92. return new ActionResult(true,"新增成功!");
  93. }
  94. catch (Exception ex)
  95. {
  96. return new ActionResult(false,"新增失败!");
  97. }
  98. }
  99. /**
  100. * 修改数据源
  101. * @param obj
  102. * @return
  103. */
  104. @Override
  105. @Transactional
  106. public ActionResult updateDatasource(SystemDatasource obj) {
  107. try{
  108. datasourceDao.updateEntity(obj);
  109. return new ActionResult(true,"修改成功!");
  110. }
  111. catch (Exception ex)
  112. {
  113. return new ActionResult(false,"修改失败!");
  114. }
  115. }
  116. /**
  117. * 删除数据源
  118. */
  119. @Override
  120. @Transactional
  121. public ActionResult deleteDatasource(String id)
  122. {
  123. try{
  124. datasourceDao.deleteEntity(SystemDatasource.class,id);
  125. return new ActionResult(true,"删除成功!");
  126. }
  127. catch (Exception ex)
  128. {
  129. return new ActionResult(false,"删除失败!");
  130. }
  131. }
  132. @Override
  133. public List<SystemDatasource> getDatasources(String orgId) {
  134. try {
  135. return datasourceDao.getDatasources(orgId);
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. return null;
  140. }
  141. }