LectureUserView.java
5.77 KB
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
package fi.codecrew.moya.web.lecture;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.faces.model.ListDataModel;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.LectureBeanLocal;
import fi.codecrew.moya.enums.apps.LecturePermission;
import fi.codecrew.moya.model.Lecture;
import fi.codecrew.moya.model.LectureGroup;
import fi.codecrew.moya.utilities.I18n;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import fi.codecrew.moya.web.cdiview.user.UserView;
@Named
@ConversationScoped
public class LectureUserView extends GenericCDIView {
private static final long serialVersionUID = 1L;
@Inject
UserView userView;
@EJB
LectureBeanLocal lectureBean;
@EJB
EventBeanLocal eventBean;
ListDataModel<LectureGroup> lectureGroups = null;
ListDataModel<Lecture> lectures = null;
ListDataModel<Lecture> participatedLectures = null;
LectureGroup currentLectureGroup;
ScheduleModel coursesCalendar = null;
Date curDate = null;
public void initView() {
if (super.requirePermissions(LecturePermission.VIEW)) {
super.beginConversation();
}
}
public boolean isLectureGroupsVisible() {
if (lectureBean.getLectureGroups().size() <= 1)
return false;
return true;
}
public boolean isParticipateActive() {
return lectureBean.isUserCanParticipate(userView.getCurrentUser());
}
public ListDataModel<LectureGroup> getLectureGroups() {
lectureGroups = new ListDataModel<LectureGroup>(lectureBean.getLectureGroups());
return lectureGroups;
}
public void selectCurrentLectureGroup() {
if (lectureGroups != null && lectureGroups.isRowAvailable()) {
currentLectureGroup = lectureGroups.getRowData();
lectures = null;
}
}
public LectureGroup getCurrentLectureGroup() {
List<LectureGroup> groups = lectureBean.getLectureGroups();
if (currentLectureGroup == null && groups.size() > 0) {
currentLectureGroup = groups.get(0);
}
return currentLectureGroup;
}
public ListDataModel<Lecture> getLectures() {
if (currentLectureGroup == null)
return new ListDataModel<Lecture>();
if (lectures == null)
lectures = new ListDataModel<Lecture>(lectureBean.findAvailableLectures(getCurrentLectureGroup(), userView.getCurrentUser()));
return lectures;
}
public ListDataModel<Lecture> getParticipatedLectures() {
participatedLectures = new ListDataModel<Lecture>(lectureBean.getParticipatedLectures(userView.getCurrentUser()));
return participatedLectures;
}
public ScheduleModel getCoursesCalendar() {
if (coursesCalendar == null) {
coursesCalendar = new DefaultScheduleModel();
for (Lecture lecture : lectureBean.findAvailableLectures(getCurrentLectureGroup(), userView.getCurrentUser())) {
String altinfo = "\n" + lecture.getDescription() + "\n";
if (lecture.isFull())
altinfo += "\n" + I18n.get("lecture.full");
if (isCurrentGroupFull())
altinfo += "\n" + I18n.get("lecture.groupFull");
DefaultScheduleEvent event = new DefaultScheduleEvent(lecture.getName() + altinfo, lecture.getStartTime(), lecture.getEndTime(), lecture);
event.setDescription(lecture.getDescription());
if (lecture.isFull() || isCurrentGroupFull()) {
event.setStyleClass("lectureCalendarDisabled");
} else {
event.setStyleClass("lectureCalendar");
}
coursesCalendar.addEvent(event);
}
for (Lecture lecture : lectureBean.getParticipatedLectures(userView.getCurrentUser())) {
String altinfo = "\n" + lecture.getDescription() + "\n\n" + I18n.get("lecture.participating");
DefaultScheduleEvent event = new DefaultScheduleEvent(lecture.getName() + altinfo, lecture.getStartTime(), lecture.getEndTime(), lecture);
event.setDescription(lecture.getDescription());
event.setStyleClass("lectureCalendarParticipating");
coursesCalendar.addEvent(event);
}
}
return coursesCalendar;
}
public void onLecureCalendarEventSelect(SelectEvent selectEvent) {
if (!isParticipateActive()) {
return;
}
Lecture lecture = (Lecture) ((DefaultScheduleEvent) selectEvent.getObject()).getData();
lectureBean.participate(userView.getCurrentUser(), lecture);
// super.addFaceMessage("lecture.participated");
curDate = lecture.getStartTime();
this.lectures = null;
this.coursesCalendar = null;
}
public void onLecureCalendarDateSelect(SelectEvent selectEvent) {
}
public Date getScheduleInitDate() {
if (curDate != null)
return curDate;
Lecture lecture = lectureBean.getFirstLecture();
if (lecture == null)
return null;
return lecture.getStartTime();
}
public void participateCurrent() {
if (!isParticipateActive()) {
return;
}
if (lectures != null && lectures.isRowAvailable()) {
Lecture lecture = lectures.getRowData();
lectureBean.participate(userView.getCurrentUser(), lecture);
super.addFaceMessage("lecture.participated");
this.lectures = null;
this.coursesCalendar = null;
}
}
public void unParticipateCurrent() {
if (participatedLectures != null && participatedLectures.isRowAvailable()) {
Lecture lecture = participatedLectures.getRowData();
lectureBean.unparticipate(userView.getCurrentUser(), lecture);
super.addFaceMessage("lecture.unparticipated");
this.lectures = null;
this.coursesCalendar = null;
}
}
public boolean isCurrentGroupFull() {
return (lectureBean.userLectureSelectsLeft(getCurrentLectureGroup(), userView.getCurrentUser()) <= 0);
}
public int getCurrentGroupParticipateCount() {
return lectureBean.userLectureSelects(getCurrentLectureGroup(), userView.getCurrentUser());
}
}