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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.GenerateOptions;
  5. import com.rthoni.intellij.codefromds.dbo.TableSelection;
  6. import org.json.JSONObject;
  7. import org.jtwig.JtwigModel;
  8. import org.jtwig.JtwigTemplate;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.nio.charset.StandardCharsets;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.util.HashMap;
  16. /**
  17. * Created by robin on 11/15/16.
  18. */
  19. public abstract class Generator {
  20. public static void saveOptions(GenerateOptions options) throws Exception
  21. {
  22. HashMap<String, Object> map = options.toMap();
  23. JSONObject obj = new JSONObject(map);
  24. FileOutputStream file = new FileOutputStream(options.getConfigAbsolutePath());
  25. file.write(obj.toString(4).getBytes());
  26. file.close();
  27. }
  28. public static GenerateOptions loadOptions(String configPath) throws Exception
  29. {
  30. String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  31. JSONObject obj = new JSONObject(data);
  32. String src = obj.getJSONObject("selection").getString("source");
  33. DbDataSource dataSource = Helper.findDataSource(src);
  34. if (dataSource == null) {
  35. throw new Exception("Data source " + src + " not found");
  36. }
  37. GenerateOptions options = new GenerateOptions(dataSource);
  38. options.setConfigAbsolutePath(configPath);
  39. options.fromJson(obj);
  40. return options;
  41. }
  42. public static void generateFile(String templatePath, String outputFile, GenerateOptions options, TableSelection table) throws IOException
  43. {
  44. String data = Helper.readFile(templatePath);
  45. FileOutputStream file = new FileOutputStream(outputFile);
  46. JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
  47. JtwigModel model = JtwigModel.newModel().with("options", options).with("table", table);
  48. template.render(model, file);
  49. file.close();
  50. }
  51. public static void generate(GenerateOptions options, Project project) throws IOException
  52. {
  53. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  54. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  55. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  56. generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  57. "Database." + options.getFilesExtension(), options, null);
  58. for (TableSelection table : options.getSelection().getTables()) {
  59. if (!table.hasNone()) {
  60. generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  61. table.getTable().getName() + "." + options.getFilesExtension(), options, table);
  62. }
  63. }
  64. }
  65. }