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.

Generator.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.intellij.openapi.project.Project;
  4. import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
  5. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  6. import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
  7. import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
  8. import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
  9. import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
  10. import com.rthoni.intellij.codefromds.dbo.template.SqlTypeDbo;
  11. import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
  12. import org.json.JSONArray;
  13. import org.json.JSONObject;
  14. import org.jtwig.JtwigModel;
  15. import org.jtwig.JtwigTemplate;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Vector;
  22. /**
  23. * Created by robin on 11/15/16.
  24. */
  25. public abstract class Generator {
  26. public static void saveOptions(GenerateOptions options) throws Exception
  27. {
  28. HashMap<String, Object> map = options.toMap();
  29. JSONObject obj = new JSONObject(map);
  30. FileOutputStream file = new FileOutputStream(options.getConfigAbsolutePath());
  31. file.write(obj.toString(4).getBytes());
  32. file.close();
  33. }
  34. public static GenerateOptions loadOptions(String configPath) throws Exception
  35. {
  36. String data = Helper.readFile(configPath);
  37. JSONObject obj = new JSONObject(data);
  38. String src = obj.getJSONObject("selection").getString("source");
  39. DbDataSource dataSource = Helper.findDataSource(src);
  40. if (dataSource == null) {
  41. throw new Exception("Data source " + src + " not found");
  42. }
  43. GenerateOptions options = new GenerateOptions(dataSource);
  44. options.setConfigAbsolutePath(configPath);
  45. options.fromJson(obj);
  46. return options;
  47. }
  48. public static String convertSqlType(SqlTypeDbo type, TypesCastOptions options)
  49. {
  50. boolean isArray = type.getType().endsWith("[]");
  51. String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
  52. String typeName = type.getType();
  53. HashMap<String, HashMap<String, String>> types = options.getTypes();
  54. if (types.containsKey(sqlTypeName)) {
  55. HashMap<String, String> subtype = types.get(sqlTypeName);
  56. if (subtype.containsKey(type.getVagueArg())) {
  57. typeName = subtype.get(type.getVagueArg());
  58. }
  59. else if (subtype.containsKey("*")) {
  60. typeName = subtype.get("*");
  61. }
  62. }
  63. if (isArray) {
  64. typeName = options.getArrayTemplate().replace("%t", typeName);
  65. }
  66. return typeName;
  67. }
  68. //
  69. // public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
  70. // {
  71. // List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
  72. // for (ColumnDataSourceDbo col : part2) {
  73. // if (!list1.contains(col)) {
  74. // list1.add(col);
  75. // }
  76. // }
  77. // if (list1.size() != list2.size()) {
  78. // return false;
  79. // }
  80. // list1.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
  81. // list2.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
  82. // return list1.equals(list2);
  83. // }
  84. public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
  85. {
  86. String dataSourceName = options.getSelection().getSource().getName();
  87. DataSourceDbo dataSourceDbo = DataSourcesBusiness.getDataSourceDbo(dataSourceName);
  88. for (TableDataSourceDbo table : dataSourceDbo.getTables())
  89. {
  90. TableSelection tableSelection = options.getSelection().getTables().stream()
  91. .filter(t -> t.getTable().getName().equals(table.getName()))
  92. .findFirst().orElse(null);
  93. for (ColumnDataSourceDbo column : table.getColumns())
  94. {
  95. ColumnSelection columnSelection = tableSelection.getColumns().stream()
  96. .filter(c -> c.getColumn().getName().equals(column.getName()))
  97. .findFirst().orElse(null);
  98. column.setType(convertSqlType(column.getSqlType(), types));
  99. column.setTypeNotNull(types.getNonNullableTypes().contains(column.getType()));
  100. column.setSelected(columnSelection.isSelected());
  101. }
  102. }
  103. return dataSourceDbo;
  104. }
  105. public static TypesCastOptions loadTypesCast(String file) throws IOException
  106. {
  107. TypesCastOptions dbo = new TypesCastOptions();
  108. String data = Helper.readFile(file);
  109. JSONObject obj = new JSONObject(data);
  110. JSONObject objTypes = obj.getJSONObject("types");
  111. HashMap<String, HashMap<String, String>> map = new HashMap<>();
  112. for (String key : objTypes.keySet()) {
  113. HashMap<String, String> typeMap = new HashMap<>();
  114. String type = objTypes.optString(key);
  115. JSONObject typeObject = objTypes.optJSONObject(key);
  116. if (typeObject != null) {
  117. for (String subtype : typeObject.keySet()) {
  118. typeMap.put(subtype, typeObject.getString(subtype));
  119. }
  120. }
  121. else if (type != null) {
  122. typeMap.put("*", type);
  123. }
  124. map.put(key, typeMap);
  125. }
  126. dbo.setTypes(map);
  127. List<String> nonNullableTypes = new Vector<>();
  128. JSONArray array = obj.getJSONArray("non-nullable-types");
  129. for (int i = 0; i < array.length(); ++i) {
  130. nonNullableTypes.add(array.getString(i));
  131. }
  132. dbo.setNonNullableTypes(nonNullableTypes);
  133. dbo.setArrayTemplate(obj.getString("arrayTemplate"));
  134. return dbo;
  135. }
  136. public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
  137. {
  138. String data = Helper.readFile(templatePath);
  139. FileOutputStream file = new FileOutputStream(outputFile);
  140. JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
  141. JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
  142. template.render(model, file);
  143. file.close();
  144. }
  145. public static void generate(GenerateOptions options, Project project) throws IOException
  146. {
  147. DataSourcesBusiness.getDataSourcesDbo();
  148. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  149. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  150. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  151. String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
  152. String dataSourceAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceRelativePath());
  153. TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
  154. DataSourceDbo dbo = convertOptions(options, types);
  155. generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null);
  156. for (TableDataSourceDbo table : dbo.getTables()) {
  157. if (table.hasAny()) {
  158. String filename = modelsAbsolutePath + File.separator + table.getName() + "." + options.getFilesExtension();
  159. generateFile(modelsTemplateAbsolutePath, filename, dbo, table);
  160. }
  161. }
  162. }
  163. }