Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Generator.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.options.ColumnSelection;
  5. import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
  6. import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
  7. import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
  8. import com.rthoni.intellij.codefromds.dbo.template.*;
  9. import groovy.json.StringEscapeUtils;
  10. import org.json.JSONArray;
  11. import org.json.JSONObject;
  12. import org.jtwig.JtwigModel;
  13. import org.jtwig.JtwigTemplate;
  14. import org.jtwig.environment.EnvironmentConfiguration;
  15. import org.jtwig.environment.EnvironmentConfigurationBuilder;
  16. import org.jtwig.functions.FunctionRequest;
  17. import org.jtwig.functions.SimpleJtwigFunction;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Vector;
  24. import java.util.function.Consumer;
  25. /**
  26. * Created by robin on 11/15/16.
  27. */
  28. public abstract class Generator {
  29. public static void saveOptions(GenerateOptions options) throws Exception
  30. {
  31. HashMap<String, Object> map = options.toMap();
  32. JSONObject obj = new JSONObject(map);
  33. FileOutputStream file = new FileOutputStream(options.getConfigAbsolutePath());
  34. file.write(obj.toString(4).getBytes());
  35. file.close();
  36. }
  37. public static GenerateOptions loadOptions(String configPath) throws Exception
  38. {
  39. String data = Helper.readFile(configPath);
  40. JSONObject obj = new JSONObject(data);
  41. String src = obj.getJSONObject("selection").getString("source");
  42. DbDataSource dataSource = Helper.findDataSource(src);
  43. if (dataSource == null) {
  44. throw new Exception("Data source " + src + " not found");
  45. }
  46. GenerateOptions options = new GenerateOptions(dataSource);
  47. options.setConfigAbsolutePath(configPath);
  48. options.fromJson(obj);
  49. return options;
  50. }
  51. public static String escapeReservedWords(String id, TypesCastOptions options)
  52. {
  53. if (options.getReservedWords().contains(id)) {
  54. return "_" + id;
  55. }
  56. return id;
  57. }
  58. public static String escapeString(String str)
  59. {
  60. return StringEscapeUtils.escapeJava(str);
  61. }
  62. public static void convertSqlType(SqlTypeDbo type, TypesCastOptions options)
  63. {
  64. if (type == null) {
  65. return;
  66. }
  67. boolean isArray = type.getType().endsWith("[]");
  68. String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
  69. String typeName = null;
  70. HashMap<String, HashMap<String, String>> types = options.getTypes();
  71. if (types.containsKey(sqlTypeName)) {
  72. HashMap<String, String> subtype = types.get(sqlTypeName);
  73. if (subtype.containsKey(type.getVagueArg())) {
  74. typeName = subtype.get(type.getVagueArg());
  75. }
  76. else if (subtype.containsKey("*")) {
  77. typeName = subtype.get("*");
  78. }
  79. }
  80. if (typeName == null) {
  81. typeName = types.get("*").get("*");
  82. }
  83. if (isArray) {
  84. typeName = options.getArrayTemplate().replace("%t", typeName);
  85. }
  86. type.setLanguageType(typeName);
  87. type.setLanguageTypeNotNull(options.getNonNullableTypes().contains(typeName));
  88. }
  89. // public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
  90. // {
  91. // List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
  92. // for (ColumnDataSourceDbo col : part2) {
  93. // if (!list1.contains(col)) {
  94. // list1.add(col);
  95. // }
  96. // }
  97. // if (list1.size() != list2.size()) {
  98. // return false;
  99. // }
  100. // list1.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
  101. // list2.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
  102. // return list1.equals(list2);
  103. // }
  104. public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
  105. {
  106. String dataSourceName = options.getSelection().getSource().getName();
  107. DataSourceDbo dataSourceDbo = DataSourcesBusiness.getDataSourceDbo(dataSourceName);
  108. for (TableDataSourceDbo table : dataSourceDbo.getTables())
  109. {
  110. TableSelection tableSelection = options.getSelection().getTables().stream()
  111. .filter(t -> t.getTable().getName().equals(table.getName()))
  112. .findFirst().orElse(null);
  113. for (ColumnDataSourceDbo column : table.getColumns())
  114. {
  115. ColumnSelection columnSelection = tableSelection.getColumns().stream()
  116. .filter(c -> c.getColumn().getName().equals(column.getName()))
  117. .findFirst().orElse(null);
  118. convertSqlType(column.getSqlType(), types);
  119. column.setSelected(columnSelection.isSelected());
  120. }
  121. }
  122. for (StoredProcedureDbo spDbo : dataSourceDbo.getStoredProcedures())
  123. {
  124. convertSqlType(spDbo.getSqlType(), types);
  125. for (StoredProcedureArgDbo arg : spDbo.getArguments()) {
  126. convertSqlType(arg.getSqlType(), types);
  127. }
  128. }
  129. return dataSourceDbo;
  130. }
  131. public static TypesCastOptions loadTypesCast(String file) throws IOException
  132. {
  133. TypesCastOptions dbo = new TypesCastOptions();
  134. String data = Helper.readFile(file);
  135. JSONObject obj = new JSONObject(data);
  136. JSONObject objTypes = obj.getJSONObject("types");
  137. HashMap<String, HashMap<String, String>> map = new HashMap<>();
  138. for (String key : objTypes.keySet()) {
  139. HashMap<String, String> typeMap = new HashMap<>();
  140. String type = objTypes.optString(key);
  141. JSONObject typeObject = objTypes.optJSONObject(key);
  142. if (typeObject != null) {
  143. for (String subtype : typeObject.keySet()) {
  144. typeMap.put(subtype, typeObject.getString(subtype));
  145. }
  146. }
  147. else if (type != null) {
  148. typeMap.put("*", type);
  149. }
  150. map.put(key, typeMap);
  151. }
  152. dbo.setTypes(map);
  153. List<String> nonNullableTypes = new Vector<>();
  154. JSONArray array = obj.getJSONArray("non-nullable-types");
  155. for (int i = 0; i < array.length(); ++i) {
  156. nonNullableTypes.add(array.getString(i));
  157. }
  158. dbo.setNonNullableTypes(nonNullableTypes);
  159. List<String> reservedWords = new Vector<>();
  160. array = obj.getJSONArray("reserved-words");
  161. for (int i = 0; i < array.length(); ++i) {
  162. reservedWords.add(array.getString(i));
  163. }
  164. dbo.setReservedWords(reservedWords);
  165. dbo.setArrayTemplate(obj.getString("arrayTemplate"));
  166. return dbo;
  167. }
  168. public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource,
  169. TableDataSourceDbo table, TypesCastOptions options) throws IOException
  170. {
  171. String data = Helper.readFile(templatePath);
  172. FileOutputStream file = new FileOutputStream(outputFile);
  173. final SimpleJtwigFunction escapeReservedWords = new SimpleJtwigFunction() {
  174. @Override
  175. public String name() {
  176. return "escapeReservedWords";
  177. }
  178. @Override
  179. public Object execute(FunctionRequest functionRequest) {
  180. String arg = (String) functionRequest.get(0);
  181. String v = escapeReservedWords(arg, options);
  182. return v;
  183. }
  184. };
  185. final SimpleJtwigFunction escapeString = new SimpleJtwigFunction() {
  186. @Override
  187. public String name() {
  188. return "escapeString";
  189. }
  190. @Override
  191. public Object execute(FunctionRequest functionRequest) {
  192. Object obj = functionRequest.get(0);
  193. if (obj == null)
  194. {
  195. return null;
  196. }
  197. String arg = (String) obj;
  198. String v = escapeString(arg);
  199. return v;
  200. }
  201. };
  202. final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder
  203. .configuration()
  204. .functions()
  205. .add(escapeReservedWords)
  206. .add(escapeString)
  207. .and()
  208. .build();
  209. JtwigTemplate template = JtwigTemplate.inlineTemplate(data, configuration);
  210. JtwigModel model = JtwigModel.newModel()
  211. .with("dataSource", dataSource)
  212. .with("table", table);
  213. template.render(model, file);
  214. file.close();
  215. }
  216. public static void generate(GenerateOptions options, Project project) throws IOException
  217. {
  218. DataSourcesBusiness.getDataSourcesDbo();
  219. String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
  220. String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
  221. String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
  222. String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
  223. String dataSourceAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceRelativePath());
  224. TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
  225. DataSourceDbo dbo = convertOptions(options, types);
  226. generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null, types);
  227. for (TableDataSourceDbo table : dbo.getTables()) {
  228. if (table.hasAny()) {
  229. String filename = modelsAbsolutePath + File.separator + table.getName() + "." + options.getFilesExtension();
  230. generateFile(modelsTemplateAbsolutePath, filename, dbo, table, types);
  231. }
  232. }
  233. }
  234. }