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

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