You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataSourceSelection.java 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.rthoni.intellij.codefromds.dbo.options;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.intellij.database.util.DasUtil;
  4. import com.rthoni.intellij.codefromds.business.Helper;
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Vector;
  10. import java.util.stream.Collectors;
  11. /**
  12. * Created by robin on 11/15/16.
  13. */
  14. public class DataSourceSelection {
  15. private DbDataSource _source;
  16. private List<TableSelection> _tables;
  17. public DataSourceSelection(DbDataSource source) {
  18. _source = source;
  19. _tables = DasUtil.getTables(source).toList().stream().map(TableSelection::new).collect(Collectors.toList());
  20. }
  21. public HashMap<String, Object> toMap()
  22. {
  23. HashMap<String, Object> map = new HashMap<>();
  24. map.put("source", _source == null ? null : _source.getName());
  25. List<Object> tables = new Vector<>();
  26. for (TableSelection table : _tables) {
  27. tables.add(table.toMap());
  28. }
  29. map.put("tables", tables);
  30. return map;
  31. }
  32. public void fromJson(JSONObject json)
  33. {
  34. JSONArray array = json.getJSONArray("tables");
  35. for (TableSelection table : _tables) {
  36. JSONObject obj = Helper.findTableInJson(array, table.getTable().getName());
  37. if (obj != null) {
  38. table.fromJson(obj);
  39. } else {
  40. for (ColumnSelection column : table.getColumns()) {
  41. column.setSelected(false);
  42. }
  43. }
  44. }
  45. }
  46. public DbDataSource getSource() {
  47. return _source;
  48. }
  49. public void setSource(DbDataSource source) {
  50. _source = source;
  51. }
  52. public List<TableSelection> getTables() {
  53. return _tables;
  54. }
  55. public void setTables(List<TableSelection> tables) {
  56. _tables = tables;
  57. }
  58. }