Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Generator.java 10KB

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