|
1
|
|
|
package unicon.matthews.oneroster.service; |
|
2
|
|
|
|
|
3
|
|
|
import java.util.Collection; |
|
4
|
|
|
|
|
5
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
6
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
7
|
|
|
import org.springframework.stereotype.Service; |
|
8
|
|
|
|
|
9
|
|
|
import unicon.matthews.oneroster.Class; |
|
10
|
|
|
import unicon.matthews.oneroster.Course; |
|
11
|
|
|
import unicon.matthews.oneroster.service.repository.MongoCourse; |
|
12
|
|
|
import unicon.matthews.oneroster.service.repository.MongoCourseRepository; |
|
13
|
|
|
|
|
14
|
|
|
@Service |
|
15
|
|
|
public class CourseService { |
|
16
|
|
|
|
|
17
|
|
|
private MongoCourseRepository mongoCourseRepository; |
|
18
|
|
|
private ClassService classService; |
|
19
|
|
|
|
|
20
|
|
|
@Autowired |
|
21
|
|
|
public CourseService(MongoCourseRepository mongoCourseRepository, |
|
22
|
|
|
ClassService classService) { |
|
23
|
|
|
this.mongoCourseRepository = mongoCourseRepository; |
|
24
|
|
|
this.classService = classService; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public Course findBySourcedId(final String tenantId, final String orgId, final String courseSourcedId) { |
|
28
|
|
|
MongoCourse mongoCourse |
|
29
|
|
|
= mongoCourseRepository |
|
30
|
|
|
.findByTenantIdAndOrgIdAndCourseSourcedId(tenantId, orgId, courseSourcedId); |
|
31
|
|
|
|
|
32
|
|
|
if (mongoCourse != null) { |
|
33
|
|
|
return mongoCourse.getCourse(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public Course save(final String tenantId, final String orgId, Course course) { |
|
40
|
|
|
if (StringUtils.isBlank(tenantId) |
|
41
|
|
|
|| StringUtils.isBlank(orgId) |
|
42
|
|
|
|| course == null |
|
43
|
|
|
|| StringUtils.isBlank(course.getSourcedId())) { |
|
44
|
|
|
throw new IllegalArgumentException(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
MongoCourse mongoCourse |
|
48
|
|
|
= mongoCourseRepository |
|
49
|
|
|
.findByTenantIdAndOrgIdAndCourseSourcedId(tenantId, orgId, course.getSourcedId()); |
|
50
|
|
|
|
|
51
|
|
|
if (mongoCourse == null) { |
|
52
|
|
|
mongoCourse |
|
53
|
|
|
= new MongoCourse.Builder() |
|
54
|
|
|
.withCourseSourcedId(course.getSourcedId()) |
|
55
|
|
|
.withOrgId(orgId) |
|
56
|
|
|
.withTenantId(tenantId) |
|
57
|
|
|
.withCourse(course) |
|
58
|
|
|
.build(); |
|
59
|
|
|
} |
|
60
|
|
|
else { |
|
61
|
|
|
mongoCourse |
|
62
|
|
|
= new MongoCourse.Builder() |
|
63
|
|
|
.withId(mongoCourse.getId()) |
|
64
|
|
|
.withCourseSourcedId(mongoCourse.getCourseSourcedId()) |
|
65
|
|
|
.withOrgId(mongoCourse.getOrgId()) |
|
66
|
|
|
.withTenantId(mongoCourse.getTenantId()) |
|
67
|
|
|
.withCourse(course) |
|
68
|
|
|
.build(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
MongoCourse saved = mongoCourseRepository.save(mongoCourse); |
|
72
|
|
|
|
|
73
|
|
|
Collection<Class> classes = classService.findClassesForCourse(tenantId, orgId, saved.getCourseSourcedId()); |
|
74
|
|
|
|
|
75
|
|
|
if (classes != null) { |
|
76
|
|
|
for (Class cls : classes) { |
|
77
|
|
|
Class updatedClass |
|
78
|
|
|
= new Class.Builder() |
|
79
|
|
|
.withCourse(saved.getCourse()) |
|
80
|
|
|
.withMetadata(cls.getMetadata()) |
|
81
|
|
|
.withSourcedId(cls.getSourcedId()) |
|
82
|
|
|
.withStatus(cls.getStatus()) |
|
83
|
|
|
.withTitle(cls.getTitle()) |
|
84
|
|
|
.build(); |
|
85
|
|
|
|
|
86
|
|
|
classService.save(tenantId, orgId, updatedClass); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return saved.getCourse(); |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|