PollView.java
4.47 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
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
private Poll poll;
private static final Logger logger = LoggerFactory.getLogger(PollView.class);
public void initPollList() {
List<Poll> pollList = pollBean.findPolls();
setPolls(new ListDataModel<Poll>(pollList));
logger.debug("Found {} polls from bean", pollList.size());
}
public String nextPage() {
++thisPage;
return null;
}
public List<QuestionWrapper> getCurrentPage() {
return pages.get(thisPage);
}
public String beginPoll() {
thisPage = 1;
setPoll(polls.getRowData());
pages = new HashMap<Integer, List<QuestionWrapper>>();
for (PollQuestion q : getPoll().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());
}
pages = null;
polls = null;
setPoll(null);
return "/poll/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;
// TODO: VALIDOI POLLIVASTAUKSET!
return ret;
}
public void setPoll(Poll poll) {
this.poll = poll;
}
public Poll getPoll() {
return poll;
}
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 boolean isFreeText() {
return question.isFreeText();
}
public PollAnswer getTextAnswer() {
if (!isFreeText()) {
throw new RuntimeException("IS not freetext!! !!BUG!!BUG!!");
}
return answers.get(0);
}
public void setOneSelected(PollAnswer ans) {
if (ans != null) {
for (PollAnswer possibleAns : answers) {
if (possibleAns.getChoice().equals(ans.getChoice())) {
possibleAns.setAnswerBoolean(true);
} else {
possibleAns.setAnswerBoolean(false);
}
}
}
}
public PollAnswer getOneSelected() {
for (PollAnswer possibleAns : answers) {
if (possibleAns.getAnswerBoolean()) {
return possibleAns;
}
}
return null;
}
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;
}
}
}