-
Notifications
You must be signed in to change notification settings - Fork 276
/
ProgrammingExercise.java
552 lines (466 loc) · 21.3 KB
/
ProgrammingExercise.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package de.tum.in.www1.artemis.domain;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.ZonedDateTime;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.persistence.*;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.tum.in.www1.artemis.domain.enumeration.AssessmentType;
import de.tum.in.www1.artemis.domain.enumeration.ProgrammingLanguage;
import de.tum.in.www1.artemis.domain.enumeration.RepositoryType;
import de.tum.in.www1.artemis.domain.enumeration.SubmissionType;
import de.tum.in.www1.artemis.domain.participation.Participation;
import de.tum.in.www1.artemis.domain.participation.SolutionProgrammingExerciseParticipation;
import de.tum.in.www1.artemis.domain.participation.TemplateProgrammingExerciseParticipation;
/**
* A ProgrammingExercise.
*/
@Entity
@DiscriminatorValue(value = "P")
@SecondaryTable(name = "programming_exercise_details")
public class ProgrammingExercise extends Exercise {
private static final Logger log = LoggerFactory.getLogger(ProgrammingExercise.class);
@Column(name = "test_repository_url")
private String testRepositoryUrl;
@Column(name = "publish_build_plan_url")
private Boolean publishBuildPlanUrl;
@Column(name = "allow_online_editor", table = "programming_exercise_details")
private Boolean allowOnlineEditor;
@Column(name = "allow_offline_ide", table = "programming_exercise_details")
private Boolean allowOfflineIde;
@Column(name = "static_code_analysis_enabled", table = "programming_exercise_details")
private Boolean staticCodeAnalysisEnabled;
@Column(name = "max_static_code_analysis_penalty", table = "programming_exercise_details")
private Integer maxStaticCodeAnalysisPenalty;
@Enumerated(EnumType.STRING)
@Column(name = "programming_language")
private ProgrammingLanguage programmingLanguage;
@Column(name = "package_name")
private String packageName;
@Column(name = "sequential_test_runs")
private Boolean sequentialTestRuns;
@Nullable
@Column(name = "build_and_test_student_submissions_after_due_date", table = "programming_exercise_details")
private ZonedDateTime buildAndTestStudentSubmissionsAfterDueDate;
@Nullable
@Column(name = "test_cases_changed", table = "programming_exercise_details")
private Boolean testCasesChanged = false; // default value
@Column(name = "project_key", table = "programming_exercise_details", nullable = false)
private String projectKey;
@OneToOne(cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(unique = true, name = "template_participation_id")
@JsonIgnoreProperties("programmingExercise")
private TemplateProgrammingExerciseParticipation templateParticipation;
@OneToOne(cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(unique = true, name = "solution_participation_id")
@JsonIgnoreProperties("programmingExercise")
private SolutionProgrammingExerciseParticipation solutionParticipation;
@OneToMany(mappedBy = "exercise", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnoreProperties("exercise")
private Set<ProgrammingExerciseTestCase> testCases = new HashSet<>();
@OneToMany(mappedBy = "exercise", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
@JsonIgnoreProperties("exercise")
private Set<StaticCodeAnalysisCategory> staticCodeAnalysisCategories = new HashSet<>();
@Transient
private boolean isLocalSimulationTransient;
/**
* Convenience getter. The actual URL is stored in the {@link TemplateProgrammingExerciseParticipation}
*
* @return The URL of the template repository as a String
*/
// jhipster-needle-entity-add-field - Jhipster will add fields here, do not remove
@JsonIgnore
public String getTemplateRepositoryUrl() {
if (templateParticipation != null && Hibernate.isInitialized(templateParticipation)) {
return templateParticipation.getRepositoryUrl();
}
return null;
}
private void setTemplateRepositoryUrl(String templateRepositoryUrl) {
if (templateParticipation != null && Hibernate.isInitialized(templateParticipation)) {
this.templateParticipation.setRepositoryUrl(templateRepositoryUrl);
}
}
@JsonIgnore
public String getTemplateRepositoryName() {
return getRepositoryNameFor(getTemplateRepositoryUrl(), RepositoryType.TEMPLATE);
}
/**
* Convenience getter. The actual URL is stored in the {@link SolutionProgrammingExerciseParticipation}
*
* @return The URL of the solution repository as a String
*/
@JsonIgnore
public String getSolutionRepositoryUrl() {
if (solutionParticipation != null && Hibernate.isInitialized(solutionParticipation)) {
return solutionParticipation.getRepositoryUrl();
}
return null;
}
private void setSolutionRepositoryUrl(String solutionRepositoryUrl) {
if (solutionParticipation != null && Hibernate.isInitialized(solutionParticipation)) {
this.solutionParticipation.setRepositoryUrl(solutionRepositoryUrl);
}
}
@JsonIgnore
public String getSolutionRepositoryName() {
return getRepositoryNameFor(getSolutionRepositoryUrl(), RepositoryType.SOLUTION);
}
public void setTestRepositoryUrl(String testRepositoryUrl) {
this.testRepositoryUrl = testRepositoryUrl;
}
public String getTestRepositoryUrl() {
return testRepositoryUrl;
}
/**
* Returns the test repository name of the exercise. Test test repository name is extracted from the test repository url.
*
* @return the test repository name if a valid test repository url is set. Otherwise returns null!
*/
public String getTestRepositoryName() {
return getRepositoryNameFor(getTestRepositoryUrl(), RepositoryType.TESTS);
}
/**
* Get the repository name for any stored repository, i.e. the slug of the repository.
*
* @param repoUrl The full URL of the repository
* @param repoType The repository type, meaning one of the base repositories (template, solution, test)
* @return The full repository slug for the given URL
*/
private String getRepositoryNameFor(final String repoUrl, final RepositoryType repoType) {
if (repoUrl == null) {
return null;
}
Pattern p = Pattern.compile(".*/(.*-" + repoType.getName() + ")\\.git");
Matcher m = p.matcher(repoUrl);
if (!m.matches() || m.groupCount() != 1)
return null;
return m.group(1);
}
@JsonIgnore // we now store it in templateParticipation --> this is just a convenience getter
public String getTemplateBuildPlanId() {
if (templateParticipation != null && Hibernate.isInitialized(templateParticipation)) {
return templateParticipation.getBuildPlanId();
}
return null;
}
private void setTemplateBuildPlanId(String templateBuildPlanId) {
if (templateParticipation != null && Hibernate.isInitialized(templateParticipation)) {
this.templateParticipation.setBuildPlanId(templateBuildPlanId);
}
}
@JsonIgnore // we now store it in solutionParticipation --> this is just a convenience getter
public String getSolutionBuildPlanId() {
if (solutionParticipation != null && Hibernate.isInitialized(solutionParticipation)) {
return solutionParticipation.getBuildPlanId();
}
return null;
}
private void setSolutionBuildPlanId(String solutionBuildPlanId) {
if (solutionParticipation != null && Hibernate.isInitialized(solutionParticipation)) {
this.solutionParticipation.setBuildPlanId(solutionBuildPlanId);
}
}
public Boolean isPublishBuildPlanUrl() {
return publishBuildPlanUrl;
}
public void setPublishBuildPlanUrl(Boolean publishBuildPlanUrl) {
this.publishBuildPlanUrl = publishBuildPlanUrl;
}
public Boolean isAllowOnlineEditor() {
return allowOnlineEditor;
}
public void setAllowOnlineEditor(Boolean allowOnlineEditor) {
this.allowOnlineEditor = allowOnlineEditor;
}
public Boolean isAllowOfflineIde() {
return allowOfflineIde;
}
public void setAllowOfflineIde(Boolean allowOfflineIde) {
this.allowOfflineIde = allowOfflineIde;
}
public Boolean isStaticCodeAnalysisEnabled() {
return this.staticCodeAnalysisEnabled;
}
public void setStaticCodeAnalysisEnabled(Boolean staticCodeAnalysisEnabled) {
this.staticCodeAnalysisEnabled = staticCodeAnalysisEnabled;
}
public Integer getMaxStaticCodeAnalysisPenalty() {
return maxStaticCodeAnalysisPenalty;
}
public void setMaxStaticCodeAnalysisPenalty(Integer maxStaticCodeAnalysisPenalty) {
this.maxStaticCodeAnalysisPenalty = maxStaticCodeAnalysisPenalty;
}
public String getProjectKey() {
return this.projectKey;
}
/**
* Generates a unique project key based on the course short name and the exercise short name. This should only be used
* for instantiating a new exercise
*
* The key concatenates the course short name and the exercise short name (in upper case letters), e.g.: <br>
* Course: <code>crs</code> <br>
* Exercise: <code>exc</code> <br>
* Project key: <code>CRSEXC</code>
*/
public void generateAndSetProjectKey() {
// Don't set the project key, if it has already been set
if (this.projectKey != null) {
return;
}
// Get course over exerciseGroup for exam programming exercises
Course course = getCourseViaExerciseGroupOrCourseMember();
this.projectKey = (course.getShortName() + this.getShortName()).toUpperCase().replaceAll("\\s+", "");
}
/**
* Get the latest (potentially) graded submission for a programming exercise.
* Programming submissions work differently in this regard as a submission without a result does not mean it is not rated/assessed, but that e.g. the CI system failed to deliver the build results.
*
* @param submissions Submissions for the given student.
* @return the latest graded submission.
*/
@Nullable
@Override
public Submission findAppropriateSubmissionByResults(Set<Submission> submissions) {
return submissions.stream().filter(submission -> {
if (submission.getResult() != null) {
return (submission.getResult().isRated() && !submission.getResult().getAssessmentType().equals(AssessmentType.MANUAL))
|| submission.getResult().getAssessmentType().equals(AssessmentType.MANUAL)
&& (this.getAssessmentDueDate() == null || this.getAssessmentDueDate().isBefore(ZonedDateTime.now()));
}
return this.getDueDate() == null || submission.getType().equals(SubmissionType.INSTRUCTOR) || submission.getType().equals(SubmissionType.TEST)
|| submission.getSubmissionDate().isBefore(this.getDueDate());
}).max(Comparator.comparing(Submission::getSubmissionDate)).orElse(null);
}
public ProgrammingLanguage getProgrammingLanguage() {
return programmingLanguage;
}
public ProgrammingExercise programmingLanguage(ProgrammingLanguage programmingLanguage) {
this.programmingLanguage = programmingLanguage;
return this;
}
public void setProgrammingLanguage(ProgrammingLanguage programmingLanguage) {
this.programmingLanguage = programmingLanguage;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public TemplateProgrammingExerciseParticipation getTemplateParticipation() {
return templateParticipation;
}
public void setTemplateParticipation(TemplateProgrammingExerciseParticipation templateParticipation) {
this.templateParticipation = templateParticipation;
if (this.templateParticipation != null) {
this.templateParticipation.setProgrammingExercise(this);
}
}
public SolutionProgrammingExerciseParticipation getSolutionParticipation() {
return solutionParticipation;
}
public void setSolutionParticipation(SolutionProgrammingExerciseParticipation solutionParticipation) {
this.solutionParticipation = solutionParticipation;
if (this.solutionParticipation != null) {
this.solutionParticipation.setProgrammingExercise(this);
}
}
// jhipster-needle-entity-add-getters-setters - Jhipster will add getters and setters here, do not remove
/**
* Gets a URL of the templateRepositoryUrl if there is one
*
* @return a URL object of the templateRepositoryUrl or null if there is no templateRepositoryUrl
*/
@JsonIgnore
public URL getTemplateRepositoryUrlAsUrl() {
String templateRepositoryUrl = getTemplateRepositoryUrl();
if (templateRepositoryUrl == null || templateRepositoryUrl.isEmpty()) {
return null;
}
try {
return new URL(templateRepositoryUrl);
}
catch (MalformedURLException e) {
log.warn("Cannot create URL for templateRepositoryUrl: " + templateRepositoryUrl + " due to the following error: " + e.getMessage());
}
return null;
}
/**
* Gets a URL of the solutionRepositoryUrl if there is one
*
* @return a URL object of the solutionRepositoryUrl or null if there is no solutionRepositoryUrl
*/
@JsonIgnore
public URL getSolutionRepositoryUrlAsUrl() {
String solutionRepositoryUrl = getSolutionRepositoryUrl();
if (solutionRepositoryUrl == null || solutionRepositoryUrl.isEmpty()) {
return null;
}
try {
return new URL(solutionRepositoryUrl);
}
catch (MalformedURLException e) {
log.warn("Cannot create URL for solutionRepositoryUrl: " + solutionRepositoryUrl + " due to the following error: " + e.getMessage());
}
return null;
}
/**
* Gets a URL of the testRepositoryURL if there is one
*
* @return a URL object of the testRepositoryURl or null if there is no testRepositoryUrl
*/
@JsonIgnore
public URL getTestRepositoryUrlAsUrl() {
if (testRepositoryUrl == null || testRepositoryUrl.isEmpty()) {
return null;
}
try {
return new URL(testRepositoryUrl);
}
catch (MalformedURLException e) {
log.warn("Cannot create URL for testRepositoryUrl: " + testRepositoryUrl + " due to the following error: " + e.getMessage());
}
return null;
}
/**
* Returns the project name by concatenating the course short name with the exercise title.
*
* @return project name of the programming exercise
*/
@JsonIgnore
public String getProjectName() {
// this is the name used for VC service and CI service
return getCourseViaExerciseGroupOrCourseMember().getShortName() + " " + this.getTitle();
}
@JsonIgnore
public String getPackageFolderName() {
return getPackageName().replace(".", "/");
}
public Set<ProgrammingExerciseTestCase> getTestCases() {
return testCases;
}
public void setTestCases(Set<ProgrammingExerciseTestCase> testCases) {
this.testCases = testCases;
}
public Set<StaticCodeAnalysisCategory> getStaticCodeAnalysisCategories() {
return staticCodeAnalysisCategories;
}
public void setStaticCodeAnalysisCategories(Set<StaticCodeAnalysisCategory> staticCodeAnalysisCategories) {
this.staticCodeAnalysisCategories = staticCodeAnalysisCategories;
}
@JsonProperty("sequentialTestRuns")
public boolean hasSequentialTestRuns() {
if (sequentialTestRuns == null) {
return false;
}
return sequentialTestRuns;
}
public void setSequentialTestRuns(Boolean sequentialTestRuns) {
this.sequentialTestRuns = sequentialTestRuns;
}
@Nullable
public ZonedDateTime getBuildAndTestStudentSubmissionsAfterDueDate() {
return buildAndTestStudentSubmissionsAfterDueDate;
}
public void setBuildAndTestStudentSubmissionsAfterDueDate(@Nullable ZonedDateTime buildAndTestStudentSubmissionsAfterDueDate) {
this.buildAndTestStudentSubmissionsAfterDueDate = buildAndTestStudentSubmissionsAfterDueDate;
}
public boolean getTestCasesChanged() {
if (testCasesChanged == null) {
return false;
}
return testCasesChanged;
}
public void setTestCasesChanged(boolean testCasesChanged) {
this.testCasesChanged = testCasesChanged;
}
@Override
public AssessmentType getAssessmentType() {
if (super.getAssessmentType() == null) {
return AssessmentType.AUTOMATIC;
}
return super.getAssessmentType();
}
/**
* set all sensitive information to null, so no info with respect to the solution gets leaked to students through json
*/
@Override
public void filterSensitiveInformation() {
setTemplateRepositoryUrl(null);
setSolutionRepositoryUrl(null);
setTestRepositoryUrl(null);
setTemplateBuildPlanId(null);
setSolutionBuildPlanId(null);
super.filterSensitiveInformation();
}
@Override
public Set<Result> findResultsFilteredForStudents(Participation participation) {
boolean isAssessmentOver = getAssessmentDueDate() == null || getAssessmentDueDate().isBefore(ZonedDateTime.now());
return participation.getResults().stream()
.filter(result -> (result.getAssessmentType().equals(AssessmentType.MANUAL) && isAssessmentOver) || result.getAssessmentType().equals(AssessmentType.AUTOMATIC))
.collect(Collectors.toSet());
}
@Override
@Nullable
public Submission findLatestSubmissionWithRatedResultWithCompletionDate(Participation participation, Boolean ignoreAssessmentDueDate) {
// for most types of exercises => return latest result (all results are relevant)
Submission latestSubmission = null;
// we get the results over the submissions
if (participation.getSubmissions() == null || participation.getSubmissions().isEmpty()) {
return null;
}
for (var submission : participation.getSubmissions()) {
var result = submission.getResult();
if (result == null) {
continue;
}
// NOTE: for the dashboard we only use rated results with completion date or automatic result
boolean isAssessmentOver = ignoreAssessmentDueDate || getAssessmentDueDate() == null || getAssessmentDueDate().isBefore(ZonedDateTime.now());
if ((result.getAssessmentType().equals(AssessmentType.MANUAL) && isAssessmentOver) || result.getAssessmentType().equals(AssessmentType.AUTOMATIC)) {
// take the first found result that fulfills the above requirements
if (latestSubmission == null) {
latestSubmission = submission;
}
// take newer results and thus disregard older ones
else if (latestSubmission.getResult().getCompletionDate().isBefore(result.getCompletionDate())) {
latestSubmission = submission;
}
}
}
return latestSubmission;
}
/**
* Check if manual results are allowed for the exercise
* @return true if manual results are allowed, false otherwise
*/
public boolean areManualResultsAllowed() {
// Only allow manual results for programming exercises if option was enabled and due dates have passed;
final var relevantDueDate = getBuildAndTestStudentSubmissionsAfterDueDate() != null ? getBuildAndTestStudentSubmissionsAfterDueDate() : getDueDate();
return getAssessmentType() == AssessmentType.SEMI_AUTOMATIC && (relevantDueDate == null || relevantDueDate.isBefore(ZonedDateTime.now()));
}
@Override
public String toString() {
return "ProgrammingExercise{" + "id=" + getId() + ", templateRepositoryUrl='" + getTemplateRepositoryUrl() + "'" + ", solutionRepositoryUrl='" + getSolutionRepositoryUrl()
+ "'" + ", templateBuildPlanId='" + getTemplateBuildPlanId() + "'" + ", solutionBuildPlanId='" + getSolutionBuildPlanId() + "'" + ", publishBuildPlanUrl='"
+ isPublishBuildPlanUrl() + "'" + ", allowOnlineEditor='" + isAllowOnlineEditor() + "'" + ", programmingLanguage='" + getProgrammingLanguage() + "'"
+ ", packageName='" + getPackageName() + "'" + ", testCasesChanged='" + testCasesChanged + "'" + "}";
}
public boolean getIsLocalSimulation() {
return this.isLocalSimulationTransient;
}
public void setIsLocalSimulation(Boolean isLocalSimulationTransient) {
this.isLocalSimulationTransient = isLocalSimulationTransient;
}
}