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 2.2KB

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