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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.model.DasForeignKey;
  3. import com.intellij.database.model.MultiRef;
  4. import com.intellij.database.psi.DbDataSource;
  5. import com.intellij.database.util.DasUtil;
  6. import com.intellij.openapi.project.Project;
  7. import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
  8. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  9. import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
  10. import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
  11. import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
  12. import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
  13. import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
  14. import org.json.JSONObject;
  15. import org.jtwig.JtwigModel;
  16. import org.jtwig.JtwigTemplate;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.nio.charset.StandardCharsets;
  21. import java.nio.file.Files;
  22. import java.nio.file.Paths;
  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 = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  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 ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection)
  70. {
  71. ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
  72. dbo.setName(columnSelection.getColumn().getName());
  73. dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
  74. dbo.setSelected(columnSelection.isSelected());
  75. return dbo;
  76. }
  77. public static TableDataSourceDbo convertTable(TableSelection tableSelection)
  78. {
  79. TableDataSourceDbo dbo = new TableDataSourceDbo();
  80. dbo.setName(tableSelection.getTable().getName());
  81. dbo.setColumns(tableSelection.getColumns().stream().map(Generator::convertColumn).collect(Collectors.toList()));
  82. dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
  83. dbo.setHasAny(!tableSelection.hasNone());
  84. dbo.setTargetForeignKeys(new Vector<>());
  85. return dbo;
  86. }
  87. public static DataSourceDbo convertOptions(GenerateOptions options)
  88. {
  89. DataSourceDbo dbo = new DataSourceDbo();
  90. dbo.setName(options.getSelection().getSource().getName());
  91. dbo.setTables(options.getSelection().getTables().stream().map(Generator::convertTable).collect(Collectors.toList()));
  92. List<TableDataSourceDbo> tables = dbo.getTables();
  93. List<TableSelection> tableSelections = options.getSelection().getTables();
  94. for (int i = 0; i < tables.size(); ++i) {
  95. TableDataSourceDbo table = tables.get(i);
  96. TableSelection tableSelection = tableSelections.get(i);
  97. table.setSourceForeignKeys(DasUtil.getForeignKeys(tableSelection.getTable()).toList().stream().map(t -> convertForeignKey(t, dbo)).collect(Collectors.toList()));
  98. }
  99. return dbo;
  100. }
  101. public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
  102. {
  103. String data = Helper.readFile(templatePath);
  104. FileOutputStream file = new FileOutputStream(outputFile);
  105. JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
  106. JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
  107. template.render(model, file);
  108. file.close();
  109. }
  110. public static void generate(GenerateOptions options, Project project) throws IOException
  111. {
  112. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  113. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  114. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  115. DataSourceDbo dbo = convertOptions(options);
  116. generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  117. "Database." + options.getFilesExtension(), dbo, null);
  118. for (TableDataSourceDbo table : dbo.getTables()) {
  119. if (table.hasAny()) {
  120. generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  121. table.getName() + "." + options.getFilesExtension(), dbo, table);
  122. }
  123. }
  124. }
  125. }