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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.model.DasForeignKey;
  3. import com.intellij.database.model.DataType;
  4. import com.intellij.database.model.MultiRef;
  5. import com.intellij.database.psi.DbDataSource;
  6. import com.intellij.database.util.DasUtil;
  7. import com.intellij.openapi.project.Project;
  8. import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
  9. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  10. import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
  11. import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
  12. import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
  13. import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
  14. import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
  15. import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
  16. import org.json.JSONArray;
  17. import org.json.JSONObject;
  18. import org.jtwig.JtwigModel;
  19. import org.jtwig.JtwigTemplate;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Vector;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.StreamSupport;
  28. /**
  29. * Created by robin on 11/15/16.
  30. */
  31. public abstract class Generator {
  32. public static void saveOptions(GenerateOptions options) throws Exception
  33. {
  34. HashMap<String, Object> map = options.toMap();
  35. JSONObject obj = new JSONObject(map);
  36. FileOutputStream file = new FileOutputStream(options.getConfigAbsolutePath());
  37. file.write(obj.toString(4).getBytes());
  38. file.close();
  39. }
  40. public static GenerateOptions loadOptions(String configPath) throws Exception
  41. {
  42. String data = Helper.readFile(configPath);
  43. JSONObject obj = new JSONObject(data);
  44. String src = obj.getJSONObject("selection").getString("source");
  45. DbDataSource dataSource = Helper.findDataSource(src);
  46. if (dataSource == null) {
  47. throw new Exception("Data source " + src + " not found");
  48. }
  49. GenerateOptions options = new GenerateOptions(dataSource);
  50. options.setConfigAbsolutePath(configPath);
  51. options.fromJson(obj);
  52. return options;
  53. }
  54. public static ForeignKeyDbo convertForeignKey(DasForeignKey fk, DataSourceDbo source)
  55. {
  56. ForeignKeyDbo dbo = new ForeignKeyDbo();
  57. dbo.setName(fk.getName());
  58. dbo.setSourceForeignKeyName("fk_" + fk.getRefTable().getName());
  59. dbo.setSourceTable(source.getTables().stream().filter(t -> t.getName().equals(fk.getTable().getName())).findFirst().get());
  60. MultiRef columnsRef = fk.getColumnsRef();
  61. dbo.setSourceColumns(dbo.getSourceTable().getColumns().stream().filter(c -> StreamSupport.stream(columnsRef.names().spliterator(), false).anyMatch(cc -> cc.equals(c.getName()))).collect(Collectors.toList()));
  62. dbo.setTargetForeignKeyName(fk.getTable().getName() + "_fk");
  63. dbo.setTargetTable(source.getTables().stream().filter(t -> t.getName().equals(fk.getRefTable().getName())).findFirst().get());
  64. MultiRef refColumns = fk.getRefColumns();
  65. dbo.setTargetColumns(dbo.getTargetTable().getColumns().stream().filter(c -> StreamSupport.stream(refColumns.names().spliterator(), false).anyMatch(cc -> cc.equals(c.getName()))).collect(Collectors.toList()));
  66. dbo.getTargetTable().getTargetForeignKeys().add(dbo);
  67. return dbo;
  68. }
  69. public static String convertSqlType(DataType type, TypesCastOptions options)
  70. {
  71. boolean isArray = type.typeName.endsWith("[]");
  72. String sqlTypeName = isArray ? type.typeName.substring(0, type.typeName.length() - 2) : type.typeName;
  73. String typeName = type.typeName;
  74. HashMap<String, HashMap<String, String>> types = options.getTypes();
  75. if (types.containsKey(sqlTypeName)) {
  76. HashMap<String, String> subtype = types.get(sqlTypeName);
  77. if (subtype.containsKey(type.vagueArg)) {
  78. typeName = subtype.get(type.vagueArg);
  79. }
  80. else if (subtype.containsKey("*")) {
  81. typeName = subtype.get("*");
  82. }
  83. }
  84. if (isArray) {
  85. typeName = options.getArrayTemplate().replace("%t", typeName);
  86. }
  87. return typeName;
  88. }
  89. public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection, TypesCastOptions options)
  90. {
  91. ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
  92. dbo.setName(columnSelection.getColumn().getName());
  93. dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
  94. dbo.setSelected(columnSelection.isSelected());
  95. dbo.setNotNull(columnSelection.getColumn().isNotNull());
  96. dbo.setType(convertSqlType(columnSelection.getColumn().getDataType(), options));
  97. dbo.setTypeNotNull(options.getNonNullableTypes().contains(dbo.getType()));
  98. return dbo;
  99. }
  100. public static TableDataSourceDbo convertTable(TableSelection tableSelection, TypesCastOptions types)
  101. {
  102. TableDataSourceDbo dbo = new TableDataSourceDbo();
  103. dbo.setName(tableSelection.getTable().getName());
  104. dbo.setColumns(tableSelection.getColumns().stream().map(c -> convertColumn(c, types)).collect(Collectors.toList()));
  105. dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
  106. dbo.setHasAny(!tableSelection.hasNone());
  107. dbo.setTargetForeignKeys(new Vector<>());
  108. return dbo;
  109. }
  110. public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
  111. {
  112. DataSourceDbo dbo = new DataSourceDbo();
  113. dbo.setName(options.getSelection().getSource().getName());
  114. dbo.setTables(options.getSelection().getTables().stream().map(t -> convertTable(t, types)).collect(Collectors.toList()));
  115. List<TableDataSourceDbo> tables = dbo.getTables();
  116. List<TableSelection> tableSelections = options.getSelection().getTables();
  117. for (int i = 0; i < tables.size(); ++i) {
  118. TableDataSourceDbo table = tables.get(i);
  119. TableSelection tableSelection = tableSelections.get(i);
  120. table.setSourceForeignKeys(DasUtil.getForeignKeys(tableSelection.getTable()).toList().stream().map(t -> convertForeignKey(t, dbo)).collect(Collectors.toList()));
  121. }
  122. return dbo;
  123. }
  124. public static TypesCastOptions loadTypesCast(String file) throws IOException
  125. {
  126. TypesCastOptions dbo = new TypesCastOptions();
  127. String data = Helper.readFile(file);
  128. JSONObject obj = new JSONObject(data);
  129. JSONObject objTypes = obj.getJSONObject("types");
  130. HashMap<String, HashMap<String, String>> map = new HashMap<>();
  131. for (String key : objTypes.keySet()) {
  132. HashMap<String, String> typeMap = new HashMap<>();
  133. String type = objTypes.optString(key);
  134. JSONObject typeObject = objTypes.optJSONObject(key);
  135. if (typeObject != null) {
  136. for (String subtype : typeObject.keySet()) {
  137. typeMap.put(subtype, typeObject.getString(subtype));
  138. }
  139. }
  140. else if (type != null) {
  141. typeMap.put("*", type);
  142. }
  143. map.put(key, typeMap);
  144. }
  145. dbo.setTypes(map);
  146. List<String> nonNullableTypes = new Vector<>();
  147. JSONArray array = obj.getJSONArray("non-nullable-types");
  148. for (int i = 0; i < array.length(); ++i) {
  149. nonNullableTypes.add(array.getString(i));
  150. }
  151. dbo.setNonNullableTypes(nonNullableTypes);
  152. dbo.setArrayTemplate(obj.getString("arrayTemplate"));
  153. return dbo;
  154. }
  155. public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
  156. {
  157. String data = Helper.readFile(templatePath);
  158. FileOutputStream file = new FileOutputStream(outputFile);
  159. JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
  160. JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
  161. template.render(model, file);
  162. file.close();
  163. }
  164. public static void generate(GenerateOptions options, Project project) throws IOException
  165. {
  166. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  167. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  168. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  169. String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
  170. TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
  171. DataSourceDbo dbo = convertOptions(options, types);
  172. generateFile(dataSourceTemplateAbsolutePath, Helper.getAbsolutePath(project, options.getDataSourceRelativePath()), dbo, null);
  173. for (TableDataSourceDbo table : dbo.getTables()) {
  174. if (table.hasAny()) {
  175. generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  176. table.getName() + "." + options.getFilesExtension(), dbo, table);
  177. }
  178. }
  179. }
  180. }