Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GenerateOptions.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.rthoni.intellij.codefromds.dbo.options;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.rthoni.intellij.codefromds.business.DataSourcesBusiness;
  4. import com.rthoni.intellij.codefromds.business.Helper;
  5. import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
  6. import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
  7. import com.rthoni.intellij.codefromds.dbo.template.StoredProcedureDbo;
  8. import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11. import java.io.File;
  12. import java.util.HashMap;
  13. /**
  14. * Created by robin on 11/15/16.
  15. */
  16. public class GenerateOptions {
  17. private DataSourceDbo _dataSource;
  18. private String _dataSourceRelativePath;
  19. private String _modelsRelativePath;
  20. private String _dataSourceTemplateRelativePath;
  21. private String _modelsTemplateRelativePath;
  22. private String _configAbsolutePath;
  23. private String _filesExtension;
  24. private String _castFileRelativePath;
  25. public GenerateOptions(DataSourceDbo dataSource, String configAbsolutePath) {
  26. _filesExtension = "cs";
  27. _dataSourceRelativePath = "DataAccess/Database." + _filesExtension;
  28. _modelsRelativePath = "Models";
  29. _dataSourceTemplateRelativePath = "code-from-ds" + File.separator + "DataSource.twig";
  30. _modelsTemplateRelativePath = "code-from-ds" + File.separator + "Models.twig";
  31. _configAbsolutePath = configAbsolutePath;
  32. _castFileRelativePath = "code-from-ds" + File.separator + "types-cast.json";
  33. _dataSource = dataSource;
  34. }
  35. public HashMap<String, Object> toMap()
  36. {
  37. HashMap<String, Object> map = new HashMap<>();
  38. map.put("dataSourceRelativePath", _dataSourceRelativePath);
  39. map.put("modelsRelativePath", _modelsRelativePath);
  40. map.put("dataSourceTemplateRelativePath", _dataSourceTemplateRelativePath);
  41. map.put("modelsTemplateRelativePath", _modelsTemplateRelativePath);
  42. map.put("filesExtension", _filesExtension);
  43. map.put("castFileRelativePath", _castFileRelativePath);
  44. JSONObject selectionObj = new JSONObject();
  45. selectionObj.put("source", _dataSource.getName());
  46. JSONArray tablesObj = new JSONArray();
  47. selectionObj.put("tables", tablesObj);
  48. for (TableDataSourceDbo table : _dataSource.getTables()) {
  49. JSONObject tableObj = new JSONObject();
  50. tablesObj.put(tableObj);
  51. tableObj.put("table", table.getName());
  52. JSONArray columnsObj = new JSONArray();
  53. tableObj.put("columns", columnsObj);
  54. for (ColumnDataSourceDbo column : table.getColumns()) {
  55. JSONObject columnObj = new JSONObject();
  56. columnsObj.put(columnObj);
  57. columnObj.put("column", column.getName());
  58. columnObj.put("selected", column.isSelected());
  59. }
  60. }
  61. JSONArray spsObj = new JSONArray();
  62. selectionObj.put("storedProcedures", spsObj);
  63. for (StoredProcedureDbo sp : _dataSource.getStoredProcedures()) {
  64. JSONObject spObj = new JSONObject();
  65. spsObj.put(spObj);
  66. spObj.put("storedProcedure", sp.getFullName());
  67. spObj.put("selected", sp.isSelected());
  68. }
  69. map.put("selection", selectionObj);
  70. return map;
  71. }
  72. public void fromJson(JSONObject json)
  73. {
  74. _dataSourceRelativePath = Helper.getJsonString(json, "dataSourceRelativePath");
  75. _modelsRelativePath = Helper.getJsonString(json, "modelsRelativePath");
  76. _dataSourceTemplateRelativePath = Helper.getJsonString(json, "dataSourceTemplateRelativePath");
  77. _modelsTemplateRelativePath = Helper.getJsonString(json, "modelsTemplateRelativePath");
  78. _filesExtension = Helper.getJsonString(json, "filesExtension");
  79. _castFileRelativePath = Helper.getJsonString(json, "castFileRelativePath");
  80. JSONObject selectionObj = json.optJSONObject("selection");
  81. if (selectionObj != null) {
  82. JSONArray tablesObj = selectionObj.optJSONArray("tables");
  83. if (tablesObj != null) {
  84. for (int i = 0; i < tablesObj.length(); ++i) {
  85. JSONObject tableObj = tablesObj.optJSONObject(i);
  86. if (tableObj != null) {
  87. TableDataSourceDbo table = _dataSource.findTable(tableObj.getString("table"));
  88. if (table != null) {
  89. JSONArray columnsObj = tableObj.optJSONArray("columns");
  90. if (columnsObj != null) {
  91. for (int j = 0; j < columnsObj.length(); ++j) {
  92. JSONObject columnObj = columnsObj.optJSONObject(j);
  93. if (columnObj != null) {
  94. ColumnDataSourceDbo column = table.findColumn(columnObj.getString("column"));
  95. if (column != null) {
  96. column.setSelected(columnObj.getBoolean("selected"));
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. JSONArray spsObj = selectionObj.optJSONArray("storedProcedures");
  106. if (spsObj != null) {
  107. for (int i = 0; i < spsObj.length(); ++i) {
  108. JSONObject spObj = spsObj.optJSONObject(i);
  109. if (spObj != null) {
  110. StoredProcedureDbo sp = _dataSource.findStoredProcedure(spObj.getString("storedProcedure"));
  111. if (sp != null) {
  112. sp.setSelected(spObj.getBoolean("selected"));
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. public DataSourceDbo getDataSource() {
  120. return _dataSource;
  121. }
  122. public String getModelsRelativePath() {
  123. return _modelsRelativePath;
  124. }
  125. public void setModelsRelativePath(String modelsRelativePath) {
  126. _modelsRelativePath = modelsRelativePath;
  127. }
  128. public String getDataSourceTemplateRelativePath() {
  129. return _dataSourceTemplateRelativePath;
  130. }
  131. public void setDataSourceTemplateRelativePath(String dataSourceTemplateRelativePath) {
  132. _dataSourceTemplateRelativePath = dataSourceTemplateRelativePath;
  133. }
  134. public String getModelsTemplateRelativePath() {
  135. return _modelsTemplateRelativePath;
  136. }
  137. public void setModelsTemplateRelativePath(String modelsTemplateRelativePath) {
  138. _modelsTemplateRelativePath = modelsTemplateRelativePath;
  139. }
  140. public String getConfigAbsolutePath() {
  141. return _configAbsolutePath;
  142. }
  143. public void setConfigAbsolutePath(String configAbsolutePath) {
  144. _configAbsolutePath = configAbsolutePath;
  145. }
  146. public String getFilesExtension() {
  147. return _filesExtension;
  148. }
  149. public void setFilesExtension(String filesExtension) {
  150. _filesExtension = filesExtension;
  151. }
  152. public String getCastFileRelativePath() {
  153. return _castFileRelativePath;
  154. }
  155. public void setCastFileRelativePath(String castFileRelativePath) {
  156. _castFileRelativePath = castFileRelativePath;
  157. }
  158. public String getDataSourceRelativePath() {
  159. return _dataSourceRelativePath;
  160. }
  161. public void setDataSourceRelativePath(String dataSourceRelativePath) {
  162. _dataSourceRelativePath = dataSourceRelativePath;
  163. }
  164. }