1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package com.rthoni.intellij.codefromds.business;
-
- import com.intellij.database.psi.DbDataSource;
- import com.intellij.openapi.project.Project;
- import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
- import com.rthoni.intellij.codefromds.dbo.TableSelection;
- import org.json.JSONObject;
- import org.jtwig.JtwigModel;
- import org.jtwig.JtwigTemplate;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.HashMap;
-
- /**
- * Created by robin on 11/15/16.
- */
- public abstract class Generator {
-
- public static void saveOptions(GenerateOptions options) throws Exception
- {
- HashMap<String, Object> map = options.toMap();
- JSONObject obj = new JSONObject(map);
- FileOutputStream file = new FileOutputStream(options.getConfigAbsolutePath());
- file.write(obj.toString(4).getBytes());
- file.close();
- }
-
- public static GenerateOptions loadOptions(String configPath) throws Exception
- {
- String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
-
- JSONObject obj = new JSONObject(data);
- String src = obj.getJSONObject("selection").getString("source");
-
- DbDataSource dataSource = Helper.findDataSource(src);
- if (dataSource == null) {
- throw new Exception("Data source " + src + " not found");
- }
-
- GenerateOptions options = new GenerateOptions(dataSource);
- options.setConfigAbsolutePath(configPath);
- options.fromJson(obj);
- return options;
- }
-
- public static void generateFile(String templatePath, String outputFile, GenerateOptions options, TableSelection table) throws IOException
- {
- String data = Helper.readFile(templatePath);
-
- FileOutputStream file = new FileOutputStream(outputFile);
-
- JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
- JtwigModel model = JtwigModel.newModel().with("options", options).with("table", table);
-
- template.render(model, file);
-
- file.close();
- }
-
- public static void generate(GenerateOptions options, Project project) throws IOException
- {
- String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
- String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
- String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
-
- generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
- "Database." + options.getFilesExtension(), options, null);
- for (TableSelection table : options.getSelection().getTables()) {
- if (!table.hasNone()) {
- generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
- table.getTable().getName() + "." + options.getFilesExtension(), options, table);
- }
- }
- }
- }
|