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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.intellij.database.util.DasUtil;
  4. import com.intellij.openapi.project.Project;
  5. import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
  6. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  7. import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
  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.TableDataSourceDbo;
  11. import org.json.JSONObject;
  12. import org.jtwig.JtwigModel;
  13. import org.jtwig.JtwigTemplate;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.nio.charset.StandardCharsets;
  18. import java.nio.file.Files;
  19. import java.nio.file.Paths;
  20. import java.util.HashMap;
  21. import java.util.stream.Collectors;
  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 = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  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 ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection)
  49. {
  50. ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
  51. dbo.setName(columnSelection.getColumn().getName());
  52. dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
  53. dbo.setSelected(columnSelection.isSelected());
  54. return dbo;
  55. }
  56. public static TableDataSourceDbo convertTable(TableSelection tableSelection)
  57. {
  58. TableDataSourceDbo dbo = new TableDataSourceDbo();
  59. dbo.setName(tableSelection.getTable().getName());
  60. dbo.setColumns(tableSelection.getColumns().stream().map(Generator::convertColumn).collect(Collectors.toList()));
  61. dbo.setPrimaryKeys(tableSelection.getColumns().stream().filter(c -> DasUtil.isPrimary(c.getColumn())).map(Generator::convertColumn).collect(Collectors.toList()));
  62. dbo.setHasAny(!tableSelection.hasNone());
  63. return dbo;
  64. }
  65. public static DataSourceDbo convertOptions(GenerateOptions options)
  66. {
  67. DataSourceDbo dbo = new DataSourceDbo();
  68. dbo.setName(options.getSelection().getSource().getName());
  69. dbo.setTables(options.getSelection().getTables().stream().map(Generator::convertTable).collect(Collectors.toList()));
  70. return dbo;
  71. }
  72. public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
  73. {
  74. String data = Helper.readFile(templatePath);
  75. FileOutputStream file = new FileOutputStream(outputFile);
  76. JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
  77. JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
  78. template.render(model, file);
  79. file.close();
  80. }
  81. public static void generate(GenerateOptions options, Project project) throws IOException
  82. {
  83. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  84. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  85. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  86. DataSourceDbo dbo = convertOptions(options);
  87. generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  88. "Database." + options.getFilesExtension(), dbo, null);
  89. for (TableDataSourceDbo table : dbo.getTables()) {
  90. if (table.hasAny()) {
  91. generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  92. table.getName() + "." + options.getFilesExtension(), dbo, table);
  93. }
  94. }
  95. }
  96. }