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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.rthoni.intellij.codefromds.dbo.options;
  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.JSONObject;
  7. import java.io.File;
  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 GenerateOptions {
  16. private DataSourceSelection _selection;
  17. private String _modelsRelativePath;
  18. private String _dataSourceTemplateRelativePath;
  19. private String _modelsTemplateRelativePath;
  20. private String _configAbsolutePath;
  21. private String _filesExtension;
  22. public GenerateOptions(DbDataSource source) {
  23. _modelsRelativePath = "Models";
  24. _dataSourceTemplateRelativePath = "Templates" + File.separator + "DataSource.twig";
  25. _modelsTemplateRelativePath = "Templates" + File.separator + "Models.twig";
  26. _configAbsolutePath = source.getProject().getBasePath() + File.separator + "code-from-ds.json";
  27. _filesExtension = "cs";
  28. _selection = new DataSourceSelection(source);
  29. _selection.setSource(source);
  30. final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
  31. List<TableSelection> tableSelections = new Vector<>();
  32. for (DasTable table : tables) {
  33. TableSelection tableSelection = new TableSelection();
  34. tableSelection.setTable(table);
  35. tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(c -> {
  36. ColumnSelection selection = new ColumnSelection();
  37. selection.setColumn(c);
  38. selection.setSelected(true);
  39. return selection;
  40. }).collect(Collectors.toList()));
  41. tableSelections.add(tableSelection);
  42. }
  43. _selection.setTables(tableSelections);
  44. }
  45. public HashMap<String, Object> toMap()
  46. {
  47. HashMap<String, Object> map = new HashMap<>();
  48. map.put("modelsRelativePath", _modelsRelativePath);
  49. map.put("dataSourceTemplateRelativePath", _dataSourceTemplateRelativePath);
  50. map.put("modelsTemplateRelativePath", _modelsTemplateRelativePath);
  51. map.put("filesExtension", _filesExtension);
  52. map.put("selection", _selection == null ? null : new JSONObject(_selection.toMap()));
  53. return map;
  54. }
  55. public void fromJson(JSONObject json)
  56. {
  57. _modelsRelativePath = Helper.getJsonString(json, "modelsRelativePath");
  58. _dataSourceTemplateRelativePath = Helper.getJsonString(json, "dataSourceTemplateRelativePath");
  59. _modelsTemplateRelativePath = Helper.getJsonString(json, "modelsTemplateRelativePath");
  60. _filesExtension = Helper.getJsonString(json, "filesExtension");
  61. _selection.fromJson(json.getJSONObject("selection"));
  62. }
  63. public DataSourceSelection getSelection() {
  64. return _selection;
  65. }
  66. public void setSelection(DataSourceSelection selection) {
  67. _selection = selection;
  68. }
  69. public String getModelsRelativePath() {
  70. return _modelsRelativePath;
  71. }
  72. public void setModelsRelativePath(String modelsRelativePath) {
  73. _modelsRelativePath = modelsRelativePath;
  74. }
  75. public String getDataSourceTemplateRelativePath() {
  76. return _dataSourceTemplateRelativePath;
  77. }
  78. public void setDataSourceTemplateRelativePath(String dataSourceTemplateRelativePath) {
  79. _dataSourceTemplateRelativePath = dataSourceTemplateRelativePath;
  80. }
  81. public String getModelsTemplateRelativePath() {
  82. return _modelsTemplateRelativePath;
  83. }
  84. public void setModelsTemplateRelativePath(String modelsTemplateRelativePath) {
  85. _modelsTemplateRelativePath = modelsTemplateRelativePath;
  86. }
  87. public String getConfigAbsolutePath() {
  88. return _configAbsolutePath;
  89. }
  90. public void setConfigAbsolutePath(String configAbsolutePath) {
  91. _configAbsolutePath = configAbsolutePath;
  92. }
  93. public String getFilesExtension() {
  94. return _filesExtension;
  95. }
  96. public void setFilesExtension(String filesExtension) {
  97. _filesExtension = filesExtension;
  98. }
  99. }