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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.model.DasForeignKey;
  3. import com.intellij.database.model.DataType;
  4. import com.intellij.database.model.MultiRef;
  5. import com.intellij.database.psi.DbDataSource;
  6. import com.intellij.database.util.DasUtil;
  7. import com.intellij.openapi.project.Project;
  8. import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
  9. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  10. import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
  11. import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
  12. import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
  13. import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
  14. import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
  15. import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
  16. import org.json.JSONObject;
  17. import org.jtwig.JtwigModel;
  18. import org.jtwig.JtwigTemplate;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Vector;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.StreamSupport;
  27. /**
  28. * Created by robin on 11/15/16.
  29. */
  30. public abstract class Generator {
  31. public static void saveOptions(GenerateOptions options) throws Exception
  32. {
  33. HashMap<String, Object> map = options.toMap();
  34. JSONObject obj = new JSONObject(map);
  35. FileOutputStream file = new FileOutputStream(options.getConfigAbsolutePath());
  36. file.write(obj.toString(4).getBytes());
  37. file.close();
  38. }
  39. public static GenerateOptions loadOptions(String configPath) throws Exception
  40. {
  41. String data = Helper.readFile(configPath);
  42. JSONObject obj = new JSONObject(data);
  43. String src = obj.getJSONObject("selection").getString("source");
  44. DbDataSource dataSource = Helper.findDataSource(src);
  45. if (dataSource == null) {
  46. throw new Exception("Data source " + src + " not found");
  47. }
  48. GenerateOptions options = new GenerateOptions(dataSource);
  49. options.setConfigAbsolutePath(configPath);
  50. options.fromJson(obj);
  51. return options;
  52. }
  53. public static ForeignKeyDbo convertForeignKey(DasForeignKey fk, DataSourceDbo source)
  54. {
  55. ForeignKeyDbo dbo = new ForeignKeyDbo();
  56. dbo.setName(fk.getName());
  57. dbo.setSourceForeignKeyName("fk_" + fk.getRefTable().getName());
  58. dbo.setSourceTable(source.getTables().stream().filter(t -> t.getName().equals(fk.getTable().getName())).findFirst().get());
  59. MultiRef columnsRef = fk.getColumnsRef();
  60. dbo.setSourceColumns(dbo.getSourceTable().getColumns().stream().filter(c -> StreamSupport.stream(columnsRef.names().spliterator(), false).anyMatch(cc -> cc.equals(c.getName()))).collect(Collectors.toList()));
  61. dbo.setTargetForeignKeyName(fk.getTable().getName() + "_fk");
  62. dbo.setTargetTable(source.getTables().stream().filter(t -> t.getName().equals(fk.getRefTable().getName())).findFirst().get());
  63. MultiRef refColumns = fk.getRefColumns();
  64. dbo.setTargetColumns(dbo.getTargetTable().getColumns().stream().filter(c -> StreamSupport.stream(refColumns.names().spliterator(), false).anyMatch(cc -> cc.equals(c.getName()))).collect(Collectors.toList()));
  65. dbo.getTargetTable().getTargetForeignKeys().add(dbo);
  66. return dbo;
  67. }
  68. public static String convertSqlType(DataType type, TypesCastOptions options)
  69. {
  70. boolean isArray = type.typeName.endsWith("[]");
  71. String sqlTypeName = isArray ? type.typeName.substring(0, type.typeName.length() - 2) : type.typeName;
  72. String typeName = type.typeName;
  73. HashMap<String, HashMap<String, String>> types = options.getTypes();
  74. if (types.containsKey(sqlTypeName)) {
  75. HashMap<String, String> subtype = types.get(sqlTypeName);
  76. if (subtype.containsKey(type.vagueArg)) {
  77. typeName = subtype.get(type.vagueArg);
  78. }
  79. else if (subtype.containsKey("*")) {
  80. typeName = subtype.get("*");
  81. }
  82. }
  83. if (isArray) {
  84. typeName = options.getArrayTemplate().replace("%t", typeName);
  85. }
  86. return typeName;
  87. }
  88. public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection, TypesCastOptions options)
  89. {
  90. ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
  91. dbo.setName(columnSelection.getColumn().getName());
  92. dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
  93. dbo.setSelected(columnSelection.isSelected());
  94. dbo.setType(convertSqlType(columnSelection.getColumn().getDataType(), options));
  95. return dbo;
  96. }
  97. public static TableDataSourceDbo convertTable(TableSelection tableSelection, TypesCastOptions types)
  98. {
  99. TableDataSourceDbo dbo = new TableDataSourceDbo();
  100. dbo.setName(tableSelection.getTable().getName());
  101. dbo.setColumns(tableSelection.getColumns().stream().map(c -> convertColumn(c, types)).collect(Collectors.toList()));
  102. dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
  103. dbo.setHasAny(!tableSelection.hasNone());
  104. dbo.setTargetForeignKeys(new Vector<>());
  105. return dbo;
  106. }
  107. public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
  108. {
  109. DataSourceDbo dbo = new DataSourceDbo();
  110. dbo.setName(options.getSelection().getSource().getName());
  111. dbo.setTables(options.getSelection().getTables().stream().map(t -> convertTable(t, types)).collect(Collectors.toList()));
  112. List<TableDataSourceDbo> tables = dbo.getTables();
  113. List<TableSelection> tableSelections = options.getSelection().getTables();
  114. for (int i = 0; i < tables.size(); ++i) {
  115. TableDataSourceDbo table = tables.get(i);
  116. TableSelection tableSelection = tableSelections.get(i);
  117. table.setSourceForeignKeys(DasUtil.getForeignKeys(tableSelection.getTable()).toList().stream().map(t -> convertForeignKey(t, dbo)).collect(Collectors.toList()));
  118. }
  119. return dbo;
  120. }
  121. public static TypesCastOptions loadTypesCast(String file) throws IOException
  122. {
  123. TypesCastOptions dbo = new TypesCastOptions();
  124. String data = Helper.readFile(file);
  125. JSONObject obj = new JSONObject(data);
  126. JSONObject objTypes = obj.getJSONObject("types");
  127. HashMap<String, HashMap<String, String>> map = new HashMap<>();
  128. for (String key : objTypes.keySet()) {
  129. HashMap<String, String> typeMap = new HashMap<>();
  130. String type = objTypes.optString(key);
  131. JSONObject typeObject = objTypes.optJSONObject(key);
  132. if (typeObject != null) {
  133. for (String subtype : typeObject.keySet()) {
  134. typeMap.put(subtype, typeObject.getString(subtype));
  135. }
  136. }
  137. else if (type != null) {
  138. typeMap.put("*", type);
  139. }
  140. map.put(key, typeMap);
  141. }
  142. dbo.setTypes(map);
  143. dbo.setArrayTemplate(obj.getString("arrayTemplate"));
  144. return dbo;
  145. }
  146. public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
  147. {
  148. String data = Helper.readFile(templatePath);
  149. FileOutputStream file = new FileOutputStream(outputFile);
  150. JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
  151. JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
  152. template.render(model, file);
  153. file.close();
  154. }
  155. public static void generate(GenerateOptions options, Project project) throws IOException
  156. {
  157. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  158. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  159. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  160. String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
  161. TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
  162. DataSourceDbo dbo = convertOptions(options, types);
  163. generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  164. "Database." + options.getFilesExtension(), dbo, null);
  165. for (TableDataSourceDbo table : dbo.getTables()) {
  166. if (table.hasAny()) {
  167. generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
  168. table.getName() + "." + options.getFilesExtension(), dbo, table);
  169. }
  170. }
  171. }
  172. }