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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. private List<StoredProcedureSelection> _storedProcedures;
  18. public DataSourceSelection(DbDataSource source) {
  19. _source = source;
  20. _tables = DasUtil.getTables(source).toList().stream().map(TableSelection::new).collect(Collectors.toList());
  21. _storedProcedures = Helper.getRoutines(source).stream().map(StoredProcedureSelection::new).collect(Collectors.toList());
  22. }
  23. public HashMap<String, Object> toMap()
  24. {
  25. HashMap<String, Object> map = new HashMap<>();
  26. map.put("source", _source == null ? null : _source.getName());
  27. List<Object> tables = new Vector<>();
  28. for (TableSelection table : _tables) {
  29. tables.add(table.toMap());
  30. }
  31. map.put("tables", tables);
  32. List<Object> sps = new Vector<>();
  33. for (StoredProcedureSelection sp : _storedProcedures) {
  34. sps.add(sp.toMap());
  35. }
  36. map.put("storedProcedures", sps);
  37. return map;
  38. }
  39. public void fromJson(JSONObject json)
  40. {
  41. JSONArray array = json.getJSONArray("tables");
  42. for (TableSelection table : _tables) {
  43. JSONObject obj = Helper.findInJson(array, "table", table.getTable().getName());
  44. if (obj != null) {
  45. table.fromJson(obj);
  46. } else {
  47. for (ColumnSelection column : table.getColumns()) {
  48. column.setSelected(false);
  49. }
  50. }
  51. }
  52. array = json.getJSONArray("storedProcedures");
  53. for (StoredProcedureSelection sp : _storedProcedures) {
  54. JSONObject obj = Helper.findInJson(array, "name", sp.getStoredProcedure().getText());
  55. if (obj != null) {
  56. sp.fromJson(obj);
  57. }
  58. }
  59. }
  60. public DbDataSource getSource() {
  61. return _source;
  62. }
  63. public void setSource(DbDataSource source) {
  64. _source = source;
  65. }
  66. public List<TableSelection> getTables() {
  67. return _tables;
  68. }
  69. public void setTables(List<TableSelection> tables) {
  70. _tables = tables;
  71. }
  72. public List<StoredProcedureSelection> getStoredProcedures() {
  73. return _storedProcedures;
  74. }
  75. public void setStoredProcedures(List<StoredProcedureSelection> storedProcedures) {
  76. _storedProcedures = storedProcedures;
  77. }
  78. }