PollView.java
2.99 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
package fi.insomnia.bortal.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ListDataModel;
import fi.insomnia.bortal.beans.PollBeanLocal;
import fi.insomnia.bortal.model.Poll;
import fi.insomnia.bortal.model.PollAnswer;
import fi.insomnia.bortal.model.PollQuestion;
import fi.insomnia.bortal.model.PossibleAnswer;
@ManagedBean(name = "pollView")
@SessionScoped
public class PollView extends GenericView {
@EJB
private PollBeanLocal pollBean;
private ListDataModel<Poll> polls;
private HashMap<Integer, List<QuestionWrapper>> pages;
private Integer thisPage = 1;
public void initPollList() {
setPolls(new ListDataModel<Poll>(pollBean.findPolls()));
}
public String nextPage() {
++thisPage;
return null;
}
public List<QuestionWrapper> getCurrentPage() {
return pages.get(thisPage);
}
public String beginPoll() {
thisPage = 1;
Poll poll = polls.getRowData();
pages = new HashMap<Integer, List<QuestionWrapper>>();
for (PollQuestion q : poll.getQuestions()) {
if (!pages.containsKey(q.getPage())) {
pages.put(q.getPage(), new LinkedList<QuestionWrapper>());
}
pages.get(q.getPage()).add(new QuestionWrapper(q));
}
return "/poll/answerToPoll";
}
public String savePoll() {
if (validate(this)) {
pollBean.createAnswers(createAnswers());
}
return "thankYou";
}
public void setPolls(ListDataModel<Poll> polls) {
this.polls = polls;
}
public ListDataModel<Poll> getPolls() {
return polls;
}
private List<PollAnswer> createAnswers() {
ArrayList<PollAnswer> ret = new ArrayList<PollAnswer>();
for (List<QuestionWrapper> qw : pages.values()) {
for (QuestionWrapper wrapper : qw) {
ret.addAll(wrapper.getAnswers());
}
}
return ret;
}
public boolean validate(PollView pollView) {
boolean ret = true;
return ret;
}
public class QuestionWrapper {
private PollQuestion question;
private ArrayList<PollAnswer> answers;
public QuestionWrapper(PollQuestion q) {
question = q;
answers = new ArrayList<PollAnswer>();
for (PossibleAnswer possible : q.getAnswers()) {
answers.add(new PollAnswer(possible));
}
}
public PollQuestion getQuestion() {
return question;
}
public void setQuestion(PollQuestion question) {
this.question = question;
}
public void setAnswers(ArrayList<PollAnswer> answers) {
this.answers = answers;
}
public ArrayList<PollAnswer> getAnswers() {
return answers;
}
}
}