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.

Helper.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.model.DasRoutine;
  3. import com.intellij.database.psi.DbDataSource;
  4. import com.intellij.database.psi.DbPsiFacade;
  5. import com.intellij.database.psi.DbRoutine;
  6. import com.intellij.openapi.project.Project;
  7. import com.intellij.openapi.project.ProjectManager;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10. import javax.sql.DataSource;
  11. import java.io.IOException;
  12. import java.nio.charset.StandardCharsets;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import java.nio.file.Paths;
  16. import java.util.Arrays;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import java.util.Vector;
  20. import java.util.stream.Collectors;
  21. /**
  22. * Created by robin on 11/15/16.
  23. */
  24. public abstract class Helper {
  25. public static List<DbRoutine> getRoutines(DbDataSource dataSource)
  26. {
  27. List<DbRoutine> routines = new Vector<>();
  28. dataSource.getModel().traverser().forEach(dasObject ->
  29. {
  30. if (dasObject instanceof DbRoutine) {
  31. routines.add((DbRoutine) dasObject);
  32. }
  33. });
  34. return routines;
  35. }
  36. public static List<DbDataSource> getDataSources()
  37. {
  38. ProjectManager pm = ProjectManager.getInstance();
  39. Project[] projects = pm.getOpenProjects();
  40. return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
  41. .flatMap(Collection::stream).collect(Collectors.toList());
  42. }
  43. public static DbDataSource findDataSource(String name)
  44. {
  45. return getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst().orElse(null);
  46. }
  47. public static JSONObject findInJson(JSONArray tables, String key, String name)
  48. {
  49. for (int i = 0; i < tables.length(); ++i) {
  50. if (tables.getJSONObject(i).getString(key).equals(name)) {
  51. return tables.getJSONObject(i);
  52. }
  53. }
  54. return null;
  55. }
  56. // public static JSONObject findColumnInJson(JSONArray tables, String name)
  57. // {
  58. // for (int i = 0; i < tables.length(); ++i) {
  59. // if (tables.getJSONObject(i).getString("column").equals(name)) {
  60. // return tables.getJSONObject(i);
  61. // }
  62. // }
  63. // return null;
  64. // }
  65. public static String readFile(String path) throws IOException {
  66. String data = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
  67. .stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  68. if (data.startsWith("\uFEFF")) {
  69. return data.substring(1);
  70. }
  71. return data;
  72. }
  73. public static String getJsonString(JSONObject obj, String key, String defaultValue)
  74. {
  75. try {
  76. return obj.getString(key);
  77. }
  78. catch (Exception ignored)
  79. {
  80. return defaultValue;
  81. }
  82. }
  83. public static String getJsonString(JSONObject obj, String key)
  84. {
  85. return getJsonString(obj, key, "");
  86. }
  87. public static String getRelativePath(String base, String absolute)
  88. {
  89. if (absolute == null || absolute.isEmpty()) {
  90. return "";
  91. }
  92. Path basePath = Paths.get(base);
  93. Path absolutePath = Paths.get(absolute);
  94. return basePath.relativize(absolutePath).toString();
  95. }
  96. public static String getRelativePath(Project project, String absolute)
  97. {
  98. return getRelativePath(project.getBasePath(), absolute);
  99. }
  100. public static String getAbsolutePath(String base, String relative)
  101. {
  102. return Paths.get(base, relative).toString();
  103. }
  104. public static String getAbsolutePath(Project project, String relative)
  105. {
  106. return getAbsolutePath(project.getBasePath(), relative);
  107. }
  108. public static String pluralize(String str)
  109. {
  110. if (str.endsWith("y")) {
  111. return str.substring(1, str.length() - 1) + "ies";
  112. }
  113. return str + "s";
  114. }
  115. }