1 package org
.cvut
.skischool
.beans
;
3 import java
.util
.ArrayList
;
6 import javax
.annotation
.security
.RolesAllowed
;
7 import javax
.ejb
.Stateless
;
8 import javax
.persistence
.EntityManager
;
9 import javax
.persistence
.PersistenceContext
;
10 import javax
.persistence
.Query
;
11 import javax
.persistence
.TemporalType
;
12 import org
.cvut
.skischool
.core
.DateTools
;
13 import org
.cvut
.skischool
.model
.Availability
;
14 import org
.cvut
.skischool
.model
.Instructor
;
15 import org
.cvut
.skischool
.model
.Lesson
;
16 import org
.cvut
.skischool
.model
.Student
;
23 public class LessonManagement
implements LessonManagementLocal
{
29 @RolesAllowed({"administrator"})
30 public void createLesson(Lesson lesson
) {
35 public void updateLesson(Lesson lesson
) {
40 @RolesAllowed({"administrator"})
41 public void deleteLesson(Lesson lesson
) {
42 if (!lesson
.isExecuted()) {
43 em
.remove(em
.merge(lesson
));
48 public Lesson
getLesson(Long id
) {
49 return em
.find(Lesson
.class, id
);
53 public List
<Instructor
> checkLessonAvailability(Date startTime
, Date endTime
, List
<Instructor
> instructors
) {
54 List
<Instructor
> occupiedInstructors
= new ArrayList
<Instructor
>();
55 Query q
= em
.createNamedQuery("Availability.getAvailabilityByTimeAndInstructor");
57 Date endTmp
= DateTools
.subtractMinute(endTime
);
59 for(Instructor instructor
: instructors
) {
60 List
<Availability
> availabilities
= (List
<Availability
>) q
61 .setParameter("startTime", startTime
, TemporalType
.TIMESTAMP
)
62 .setParameter("endTime", endTmp
, TemporalType
.TIMESTAMP
)
63 .setParameter("instructor", instructor
)
65 if (availabilities
.isEmpty()) {
66 occupiedInstructors
.add(instructor
);
70 return occupiedInstructors
;
74 public List
<Lesson
> checkLessonReservations(Date startTime
, Date endTime
, List
<Instructor
> instructors
) {
75 List
<Lesson
> conflictLessons
= new ArrayList
<Lesson
>();
76 Query q
= em
.createNamedQuery("Lesson.getLessonsByInstructorAndTime");
78 Date startTmp
= DateTools
.addMinute(startTime
);
79 Date endTmp
= DateTools
.subtractMinute(endTime
);
81 for (Instructor instructor
: instructors
) {
82 List
<Lesson
> lessons
= (List
<Lesson
>) q
83 .setParameter("startTime", startTmp
, TemporalType
.TIMESTAMP
)
84 .setParameter("endTime", endTmp
, TemporalType
.TIMESTAMP
)
85 .setParameter("instructor", instructor
)
87 conflictLessons
.addAll(lessons
);
90 return conflictLessons
;
94 public List
<Lesson
> getAllLessons() {
95 List
<Lesson
> lessons
= (List
<Lesson
>) em
.createNamedQuery("Lesson.getAllLessons").getResultList();
100 public List
<Lesson
> getLessonsByDate(Instructor instructor
, Date date
) {
101 Date dayBegin
= DateTools
.makeDateTime(date
, 0, 0, 0);
102 Date dayEnd
= DateTools
.addDay(dayBegin
);
104 Query q
= em
.createNamedQuery("Lesson.getLessonsByDate");
105 List
<Lesson
> lessons
= (List
<Lesson
>) q
106 .setParameter("instructor", instructor
)
107 .setParameter("dayBegin", dayBegin
, TemporalType
.TIMESTAMP
)
108 .setParameter("dayEnd", dayEnd
, TemporalType
.TIMESTAMP
)
115 public List
<Lesson
> getAllLessonsByType(String type
) {
116 Query q
= em
.createNamedQuery("Lesson.getAllLessonsByType");
117 List
<Lesson
> lessons
= (List
<Lesson
>) q
118 .setParameter("type",type
)
125 public long countLessonsLangBonus(Instructor instructor
, Date startDate
, Date endDate
) {
126 Query q
= em
.createNamedQuery("Lesson.countLessonsLangBonus");
127 Long lessonsCount
= (Long
) q
128 .setParameter("instructor", instructor
)
129 .setParameter("startDate", startDate
, TemporalType
.TIMESTAMP
)
130 .setParameter("endDate", endDate
, TemporalType
.TIMESTAMP
)
137 public List
<Lesson
> countLessons(Instructor instructor
, Date startDate
, Date endDate
, String type
) {
138 Query q
= em
.createNamedQuery("Lesson.countLessons");
139 List
<Lesson
> lessons
= (List
<Lesson
>) q
140 .setParameter("instructor", instructor
)
141 .setParameter("startDate", startDate
, TemporalType
.TIMESTAMP
)
142 .setParameter("endDate", endDate
, TemporalType
.TIMESTAMP
)
143 .setParameter("type", type
)
150 public List
<Lesson
> getLessonsByInstructorAndInterval(Instructor instructor
, Date startDate
, Date endDate
){
151 Query q
= em
.createNamedQuery("Lesson.getLessonsByInstructorAndInterval");
152 List
<Lesson
> lessons
= (List
<Lesson
>) q
153 .setParameter("instructor", instructor
)
154 .setParameter("startDate", startDate
, TemporalType
.TIMESTAMP
)
155 .setParameter("endDate", endDate
, TemporalType
.TIMESTAMP
)
162 public List
<Lesson
> getLessonsByStudentAndInterval(Student student
, Date startDate
, Date endDate
) {
163 Query q
= em
.createNamedQuery("Lesson.getLessonsByStudentAndInterval");
164 List
<Lesson
> lessons
= (List
<Lesson
>) q
165 .setParameter("student", student
)
166 .setParameter("startDate", startDate
, TemporalType
.TIMESTAMP
)
167 .setParameter("endDate", endDate
, TemporalType
.TIMESTAMP
)