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.

GenerateOptions.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 org.json.JSONObject;
  6. import java.io.File;
  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 GenerateOptions {
  15. public GenerateOptions(DbDataSource source) {
  16. _modelsPath = source.getProject().getBasePath() + File.separator + "Models";
  17. _dataSourceTemplatePath = source.getProject().getBasePath() + File.separator + "Templates" + File.separator + "DataSource.twig";
  18. _modelsTemplatePath = source.getProject().getBasePath() + File.separator + "Templates" + File.separator + "Models.twig";
  19. _configPath = source.getProject().getBasePath() + File.separator + "code-from-ds.json";
  20. _selection = new DataSourceSelection(source);
  21. _selection.setSource(source);
  22. final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
  23. List<TableSelection> tableSelections = new Vector<>();
  24. for (DasTable table : tables) {
  25. TableSelection tableSelection = new TableSelection();
  26. tableSelection.setTable(table);
  27. tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(c -> {
  28. ColumnSelection selection = new ColumnSelection();
  29. selection.setColumn(c);
  30. selection.setSelected(true);
  31. return selection;
  32. }).collect(Collectors.toList()));
  33. tableSelections.add(tableSelection);
  34. }
  35. _selection.setTables(tableSelections);
  36. }
  37. private DataSourceSelection _selection;
  38. private String _modelsPath;
  39. private String _dataSourceTemplatePath;
  40. private String _modelsTemplatePath;
  41. private String _configPath;
  42. public HashMap<String, Object> toMap()
  43. {
  44. HashMap<String, Object> map = new HashMap<>();
  45. map.put("modelsPath", _modelsPath);
  46. map.put("dataSourceTemplatePath", _dataSourceTemplatePath);
  47. map.put("modelsTemplatePath", _modelsTemplatePath);
  48. map.put("selection", _selection == null ? null : new JSONObject(_selection.toMap()));
  49. return map;
  50. }
  51. public void fromJson(JSONObject json)
  52. {
  53. _modelsPath = json.getString("modelsPath");
  54. _dataSourceTemplatePath = json.getString("dataSourceTemplatePath");
  55. _modelsTemplatePath = json.getString("modelsTemplatePath");
  56. _selection.fromJson(json.getJSONObject("selection"));
  57. }
  58. public DataSourceSelection getSelection() {
  59. return _selection;
  60. }
  61. public void setSelection(DataSourceSelection selection) {
  62. _selection = selection;
  63. }
  64. public String getModelsPath() {
  65. return _modelsPath;
  66. }
  67. public void setModelsPath(String modelsPath) {
  68. _modelsPath = modelsPath;
  69. }
  70. public String getDataSourceTemplatePath() {
  71. return _dataSourceTemplatePath;
  72. }
  73. public void setDataSourceTemplatePath(String dataSourceTemplatePath) {
  74. _dataSourceTemplatePath = dataSourceTemplatePath;
  75. }
  76. public String getModelsTemplatePath() {
  77. return _modelsTemplatePath;
  78. }
  79. public void setModelsTemplatePath(String modelsTemplatePath) {
  80. _modelsTemplatePath = modelsTemplatePath;
  81. }
  82. public String getConfigPath() {
  83. return _configPath;
  84. }
  85. public void setConfigPath(String configPath) {
  86. _configPath = configPath;
  87. }
  88. }