123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package com.rthoni.intellij.codefromds.business;
-
- import com.intellij.database.psi.DbDataSource;
- import com.intellij.openapi.project.Project;
- import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
- import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
- import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
- import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
- import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
- import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
- import com.rthoni.intellij.codefromds.dbo.template.SqlTypeDbo;
- import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
- import org.json.JSONArray;
- 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.util.HashMap;
- import java.util.List;
- import java.util.Vector;
-
- /**
- * 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 = Helper.readFile(configPath);
-
- 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 String convertSqlType(SqlTypeDbo type, TypesCastOptions options)
- {
- boolean isArray = type.getType().endsWith("[]");
- String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
- String typeName = type.getType();
- HashMap<String, HashMap<String, String>> types = options.getTypes();
-
- if (types.containsKey(sqlTypeName)) {
- HashMap<String, String> subtype = types.get(sqlTypeName);
- if (subtype.containsKey(type.getVagueArg())) {
- typeName = subtype.get(type.getVagueArg());
- }
- else if (subtype.containsKey("*")) {
- typeName = subtype.get("*");
- }
- }
- if (isArray) {
- typeName = options.getArrayTemplate().replace("%t", typeName);
- }
- return typeName;
- }
- //
- // public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
- // {
- // List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
- // for (ColumnDataSourceDbo col : part2) {
- // if (!list1.contains(col)) {
- // list1.add(col);
- // }
- // }
- // if (list1.size() != list2.size()) {
- // return false;
- // }
- // list1.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
- // list2.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
- // return list1.equals(list2);
- // }
-
- public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
- {
- String dataSourceName = options.getSelection().getSource().getName();
- DataSourceDbo dataSourceDbo = DataSourcesBusiness.getDataSourceDbo(dataSourceName);
-
- for (TableDataSourceDbo table : dataSourceDbo.getTables())
- {
- TableSelection tableSelection = options.getSelection().getTables().stream()
- .filter(t -> t.getTable().getName().equals(table.getName()))
- .findFirst().orElse(null);
- for (ColumnDataSourceDbo column : table.getColumns())
- {
- ColumnSelection columnSelection = tableSelection.getColumns().stream()
- .filter(c -> c.getColumn().getName().equals(column.getName()))
- .findFirst().orElse(null);
- column.setType(convertSqlType(column.getSqlType(), types));
- column.setTypeNotNull(types.getNonNullableTypes().contains(column.getType()));
- column.setSelected(columnSelection.isSelected());
- }
- }
-
- return dataSourceDbo;
- }
-
- public static TypesCastOptions loadTypesCast(String file) throws IOException
- {
- TypesCastOptions dbo = new TypesCastOptions();
- String data = Helper.readFile(file);
-
- JSONObject obj = new JSONObject(data);
- JSONObject objTypes = obj.getJSONObject("types");
- HashMap<String, HashMap<String, String>> map = new HashMap<>();
-
- for (String key : objTypes.keySet()) {
- HashMap<String, String> typeMap = new HashMap<>();
- String type = objTypes.optString(key);
- JSONObject typeObject = objTypes.optJSONObject(key);
- if (typeObject != null) {
- for (String subtype : typeObject.keySet()) {
- typeMap.put(subtype, typeObject.getString(subtype));
- }
- }
- else if (type != null) {
- typeMap.put("*", type);
- }
- map.put(key, typeMap);
- }
- dbo.setTypes(map);
-
- List<String> nonNullableTypes = new Vector<>();
- JSONArray array = obj.getJSONArray("non-nullable-types");
- for (int i = 0; i < array.length(); ++i) {
- nonNullableTypes.add(array.getString(i));
- }
- dbo.setNonNullableTypes(nonNullableTypes);
-
- dbo.setArrayTemplate(obj.getString("arrayTemplate"));
-
- return dbo;
- }
-
- public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
- {
- String data = Helper.readFile(templatePath);
-
- FileOutputStream file = new FileOutputStream(outputFile);
-
- JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
- JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
-
- template.render(model, file);
-
- file.close();
- }
-
- public static void generate(GenerateOptions options, Project project) throws IOException
- {
- DataSourcesBusiness.getDataSourcesDbo();
- String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
- String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
- String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
- String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
- String dataSourceAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceRelativePath());
-
- TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
-
- DataSourceDbo dbo = convertOptions(options, types);
-
- generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null);
- for (TableDataSourceDbo table : dbo.getTables()) {
- if (table.hasAny()) {
- String filename = modelsAbsolutePath + File.separator + table.getName() + "." + options.getFilesExtension();
- generateFile(modelsTemplateAbsolutePath, filename, dbo, table);
- }
- }
- }
- }
|