-
Notifications
You must be signed in to change notification settings - Fork 14
/
ObjectFinder.java
227 lines (194 loc) · 8.91 KB
/
ObjectFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package fi.helsinki.cs.tmc.intellij.services;
import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
import fi.helsinki.cs.tmc.core.exceptions.ShowToUserException;
import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException;
import fi.helsinki.cs.tmc.intellij.holders.TmcCoreHolder;
import fi.helsinki.cs.tmc.intellij.holders.TmcSettingsManager;
import fi.helsinki.cs.tmc.intellij.services.errors.ErrorMessageService;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** Finds various exercises and courses from the disk or by asking the TMCServer. */
public class ObjectFinder {
private static final Logger logger = LoggerFactory.getLogger(ObjectFinder.class);
@Nullable
public Course findCourse(String searchTerm, String titleOrName) {
TmcCore core = TmcCoreHolder.get();
List<Course> courses = getCourses(core);
if (courses != null) {
for (Course c : courses) {
if ((titleOrName.equals("name") && c.getName().equals(searchTerm))
|| (titleOrName.equals("title") && c.getTitle().equals(searchTerm))) {
try {
logger.info("Trying to get course details from TmcCore. @ObjectFinder", c);
return core.getCourseDetails(ProgressObserver.NULL_OBSERVER, c).call();
} catch (TmcCoreException exception) {
logger.warn(
"Could not find course. @ObjectFinder",
exception,
exception.getStackTrace());
new ErrorMessageService().showHumanReadableErrorMessage(exception, false);
} catch (Exception e) {
logger.warn("Could not find course. @ObjectFinder", e, e.getStackTrace());
new ErrorMessageService()
.showErrorMessageWithExceptionDetails(
e, "Could not find course.", true);
}
}
}
}
return null;
}
@TestOnly
public Course findCourseForTesting(String searchTerm, String titleOrName, TmcCore core) {
List<Course> courses = getCourses(core);
for (Course c : courses) {
if ((titleOrName.equals("name") && c.getName().equals(searchTerm))
|| (titleOrName.equals("title") && c.getTitle().equals(searchTerm))) {
try {
return core.getCourseDetails(ProgressObserver.NULL_OBSERVER, c).call();
} catch (TmcCoreException exception) {
new ErrorMessageService().showHumanReadableErrorMessage(exception, false);
} catch (Exception e) {
new ErrorMessageService()
.showErrorMessageWithExceptionDetails(
e, "Could not find course.", true);
}
}
}
return null;
}
private List<Course> getCourses(TmcCore core) {
logger.info("Processing getCourses @ObjectFinder");
List<Course> courses = null;
try {
courses = core.listCourses(ProgressObserver.NULL_OBSERVER).call();
} catch (ShowToUserException exception) {
logger.warn(
"Failed to fetch courses from TmcCore. @ObjectFinder",
exception,
exception.getStackTrace());
ErrorMessageService error = new ErrorMessageService();
error.showErrorMessagePopup(
"Failed to fetch courses from the server.\nPlease check your internet connection.");
} catch (TmcCoreException e) {
logger.warn("Getting courses failed @ObjectFinder", e, e.getStackTrace());
new ErrorMessageService().showHumanReadableErrorMessage(e, false);
} catch (Exception e) {
logger.warn("Getting courses failed @ObjectFinder", e, e.getStackTrace());
new ErrorMessageService()
.showErrorMessageWithExceptionDetails(
e, "Something went wrong while trying to get the course list", true);
}
return courses;
}
public List<String> listAllDownloadedCourses() {
logger.info("Processing listAllDownloadedCourses. @ObjectFinder");
List<String> courseTitles = new ArrayList<>();
if (getCourses(TmcCoreHolder.get())
== null) { // this because we don't want to go through all courses if the connection
// to the server hasn't been established
return null;
}
for (String name :
getListOfDirectoriesInPath(TmcSettingsManager.get().getProjectBasePath())) {
Course course = findCourse(name, "name");
if (course == null) {
continue;
}
courseTitles.add(course.getTitle());
}
return courseTitles;
}
public List<String> listAllDownloadedExercises(String courseTitle) {
logger.info(
"Processing listAllDownloadedExercises from course {}. @ObjectFinder", courseTitle);
if (findCourse(courseTitle, "title") == null) {
return new ArrayList<>();
}
return getListOfDirectoriesInPath(
TmcSettingsManager.get().getProjectBasePath()
+ File.separator
+ findCourse(courseTitle, "title").getName());
}
private List<String> getListOfDirectoriesInPath(String folderPath) {
logger.info("Processing getListOfDirectoriesInPath. @ObjectFinder");
List<String> fileNames = new ArrayList<>();
try (DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(Paths.get(folderPath))) {
logger.info("Getting list of directories in path. @ObjectFinder");
addExercisesToList(fileNames, directoryStream);
} catch (Exception ex) {
logger.warn(
"Could not get list of directories in path. @ObjectFinder",
ex,
ex.getStackTrace());
ex.printStackTrace();
}
Collections.sort(fileNames);
return fileNames;
}
private void addExercisesToList(List<String> fileNames, DirectoryStream<Path> directoryStream) {
for (Path path : directoryStream) {
if (!Files.isDirectory(path)) {
continue;
}
// courseAndExerciseName is an array where the second to last element is course name and
// last element is exercise name
String[] courseAndExerciseNameArray = PathResolver.getCourseAndExerciseName(path);
if (courseAndExerciseNameArray == null
|| getExerciseName(courseAndExerciseNameArray).charAt(0) == '.') {
logger.info("exerciseCourse variable = null. @ObjectFinder");
continue;
}
logger.info(
"Adding exercise to list. @ObjectFinder",
getExerciseName(courseAndExerciseNameArray));
fileNames.add(getExerciseName(courseAndExerciseNameArray));
}
}
private String getExerciseName(String[] courseAndExerciseName) {
logger.info("Trying to getExerciseName. @ObjectFinder");
return courseAndExerciseName[courseAndExerciseName.length - 1];
}
public Project findCurrentProject() {
logger.info("Trying to findCurrentProject. @ObjectFinder");
DataContext dataContext = DataManager.getInstance().getDataContextFromFocus().getResult();
if (dataContext == null) {
Project[] projects = ProjectManager.getInstance().getOpenProjects();
if (projects.length > 0) {
return projects[projects.length - 1];
}
return null;
}
return DataKeys.PROJECT.getData(dataContext);
}
public Course findCourseNoDetails(String courseName, TmcCore core) {
try {
List<Course> list = core.listCourses(ProgressObserver.NULL_OBSERVER).call();
for (Course cor : list) {
if (cor.getName().equals(courseName)) {
return cor;
}
}
} catch (Exception e) {
}
return null;
}
}