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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.intellij.database.psi.DbPsiFacade;
  4. import com.intellij.openapi.project.Project;
  5. import com.intellij.openapi.project.ProjectManager;
  6. import com.rthoni.intellij.codefromds.dbo.TableSelection;
  7. import org.json.JSONArray;
  8. import org.json.JSONObject;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.nio.charset.StandardCharsets;
  12. import java.nio.file.Files;
  13. import java.nio.file.LinkOption;
  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.Optional;
  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<DbDataSource> getDataSources()
  26. {
  27. ProjectManager pm = ProjectManager.getInstance();
  28. Project[] projects = pm.getOpenProjects();
  29. return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
  30. .flatMap(Collection::stream).collect(Collectors.toList());
  31. }
  32. public static DbDataSource findDataSource(String name)
  33. {
  34. Optional<DbDataSource> opt = getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst();
  35. if (opt.isPresent()) {
  36. return opt.get();
  37. }
  38. return null;
  39. }
  40. public static JSONObject findTableInJson(JSONArray tables, String name)
  41. {
  42. for (int i = 0; i < tables.length(); ++i) {
  43. if (tables.getJSONObject(i).getString("table").equals(name)) {
  44. return tables.getJSONObject(i);
  45. }
  46. }
  47. return null;
  48. }
  49. public static JSONObject findColumnInJson(JSONArray tables, String name)
  50. {
  51. for (int i = 0; i < tables.length(); ++i) {
  52. if (tables.getJSONObject(i).getString("column").equals(name)) {
  53. return tables.getJSONObject(i);
  54. }
  55. }
  56. return null;
  57. }
  58. public static String readFile(String path) throws IOException {
  59. return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
  60. .stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  61. }
  62. public static String getJsonString(JSONObject obj, String key, String defaultValue)
  63. {
  64. try {
  65. return obj.getString(key);
  66. }
  67. catch (Exception ignored)
  68. {
  69. return defaultValue;
  70. }
  71. }
  72. public static String getJsonString(JSONObject obj, String key)
  73. {
  74. return getJsonString(obj, key, "");
  75. }
  76. public static String getRelativePath(String base, String absolute)
  77. {
  78. if (absolute == null || absolute.isEmpty()) {
  79. return "";
  80. }
  81. Path basePath = Paths.get(base);
  82. Path absolutePath = Paths.get(absolute);
  83. return basePath.relativize(absolutePath).toString();
  84. }
  85. public static String getRelativePath(Project project, String absolute)
  86. {
  87. return getRelativePath(project.getBasePath(), absolute);
  88. }
  89. public static String getAbsolutePath(String base, String relative)
  90. {
  91. return Paths.get(base, relative).toString();
  92. }
  93. public static String getAbsolutePath(Project project, String relative)
  94. {
  95. return getAbsolutePath(project.getBasePath(), relative);
  96. }
  97. }