Rev 1306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
671 | jmachado | 1 | package pt.estgp.estgweb.domain; |
2 | |||
3 | import jomm.dao.impl.AbstractDao; |
||
4 | import org.jboss.util.Objects; |
||
5 | import org.jfree.chart.ChartFactory; |
||
6 | import org.jfree.chart.ChartUtilities; |
||
7 | import org.jfree.chart.JFreeChart; |
||
8 | import org.jfree.chart.plot.PlotOrientation; |
||
9 | import org.jfree.data.category.CategoryDataset; |
||
10 | import org.jfree.data.category.DefaultCategoryDataset; |
||
11 | import org.jfree.data.general.Dataset; |
||
12 | import pt.estgp.estgweb.Globals; |
||
13 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
14 | import pt.estgp.estgweb.domain.dao.impl.*; |
||
15 | |||
16 | import java.io.File; |
||
17 | import java.io.IOException; |
||
18 | import java.util.*; |
||
19 | |||
20 | /** |
||
21 | * @author: Duarte Santos |
||
22 | * @date: 09-05-2011 |
||
23 | * @time: 23:53 |
||
24 | * @email: a12564 [at] estgp [dot] pt |
||
25 | */ |
||
26 | |||
27 | public class Main |
||
28 | { |
||
29 | public static Survey createSurvey(User user) |
||
30 | { |
||
31 | Survey survey = DomainObjectFactory.createSurveyImpl(); |
||
32 | survey.setStartDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
33 | survey.setSemester("S2"); |
||
34 | survey.setYear("2010"); |
||
35 | survey.setOwner(user); |
||
36 | SurveyDao.getInstance().save(survey); |
||
37 | return survey; |
||
38 | } |
||
39 | |||
40 | public static List<SurveyStructuralNode> createStructuralNodes(Survey survey) |
||
41 | { |
||
42 | List<SurveyStructuralNode> nodes = new ArrayList<SurveyStructuralNode>(); |
||
43 | List<String> nodeTexts = new ArrayList<String>(); |
||
44 | |||
45 | nodeTexts.add("201011"); |
||
46 | nodeTexts.add("Engenharia Informática"); |
||
47 | nodeTexts.add("Semestre1"); |
||
48 | nodeTexts.add("Semestre2"); |
||
49 | nodeTexts.add("Teoria da Computação"); |
||
50 | nodeTexts.add("Redes de computadores I"); |
||
51 | nodeTexts.add("Introdução à Programação"); |
||
52 | nodeTexts.add("Análise de Matemática I"); |
||
53 | |||
54 | int numNodes = nodeTexts.size(); |
||
55 | int i = 0; |
||
56 | while(numNodes > 0) |
||
57 | { |
||
58 | nodes.add(DomainObjectFactory.createSurveyStructuralNodeImpl()); |
||
59 | nodes.get(i).setSurvey(survey); |
||
60 | nodes.get(i).setNodes(new HashSet<SurveyStructuralNode>(0)); |
||
61 | i++; |
||
62 | numNodes--; |
||
63 | } |
||
64 | |||
65 | nodes.get(0).setMsg(nodeTexts.get(0)); |
||
66 | nodes.get(1).setMsg(nodeTexts.get(1)); |
||
67 | nodes.get(2).setMsg(nodeTexts.get(2)); |
||
68 | nodes.get(3).setMsg(nodeTexts.get(3)); |
||
69 | nodes.get(4).setMsg(nodeTexts.get(4)); |
||
70 | nodes.get(5).setMsg(nodeTexts.get(5)); |
||
71 | nodes.get(6).setMsg(nodeTexts.get(6)); |
||
72 | nodes.get(7).setMsg(nodeTexts.get(7)); |
||
73 | |||
74 | nodes.get(0).getNodes().add(nodes.get(1)); |
||
75 | nodes.get(1).getNodes().add(nodes.get(2)); |
||
76 | nodes.get(1).getNodes().add(nodes.get(3)); |
||
77 | nodes.get(2).getNodes().add(nodes.get(4)); |
||
78 | nodes.get(3).getNodes().add(nodes.get(5)); |
||
79 | nodes.get(3).getNodes().add(nodes.get(6)); |
||
80 | nodes.get(3).getNodes().add(nodes.get(7)); |
||
81 | |||
82 | SurveyStructuralNodeDao.getInstance().save(nodes.get(0)); |
||
83 | SurveyStructuralNodeDao.getInstance().save(nodes.get(1)); |
||
84 | SurveyStructuralNodeDao.getInstance().save(nodes.get(2)); |
||
85 | SurveyStructuralNodeDao.getInstance().save(nodes.get(3)); |
||
86 | SurveyStructuralNodeDao.getInstance().save(nodes.get(4)); |
||
87 | SurveyStructuralNodeDao.getInstance().save(nodes.get(5)); |
||
88 | SurveyStructuralNodeDao.getInstance().save(nodes.get(6)); |
||
89 | SurveyStructuralNodeDao.getInstance().save(nodes.get(7)); |
||
90 | |||
91 | return nodes; |
||
92 | } |
||
93 | |||
94 | public static SurveyResponse createSurveyResponses(User user, SurveyStructuralNode node) |
||
95 | { |
||
96 | SurveyResponse response = DomainObjectFactory.createSurveyResponseImpl(); |
||
97 | response.setStructuralNode(node); |
||
98 | response.setAnswered(true); |
||
99 | response.setOwner(user); |
||
100 | SurveyResponseDao.getInstance().save(response); |
||
101 | |||
102 | return response; |
||
103 | } |
||
104 | |||
105 | public static List<SurveyQuestion> createSurveyQuestions(Survey survey) |
||
106 | { |
||
107 | List<SurveyQuestion> questions = new ArrayList<SurveyQuestion>(); |
||
108 | List<String> questionTexts = new ArrayList<String>(); |
||
109 | |||
110 | questionTexts.add("Pergunta numero 1"); |
||
111 | questionTexts.add("Pergunta numero 2"); |
||
112 | questionTexts.add("Pergunta numero 3"); |
||
113 | |||
114 | int numQuestions = questionTexts.size(); |
||
115 | int i = 0; |
||
116 | while(numQuestions > 0) |
||
117 | { |
||
118 | questions.add(DomainObjectFactory.createSurveyQuestionImpl()); |
||
119 | questions.get(i).setSurvey(survey); |
||
120 | questions.get(i).setLabelpt(questionTexts.get(i)); |
||
121 | SurveyQuestionDao.getInstance().save(questions.get(i)); |
||
122 | i++; |
||
123 | numQuestions--; |
||
124 | } |
||
125 | |||
126 | return questions; |
||
127 | } |
||
128 | |||
129 | public static List<SurveyQuestionItem> createSurveyQuestionItems(List<SurveyQuestion> questions) |
||
130 | { |
||
131 | List<SurveyQuestionItem> items = new ArrayList<SurveyQuestionItem>(); |
||
132 | List<String> itemsText = new ArrayList<String>(); |
||
133 | |||
134 | itemsText.add("Item numero 1-1"); |
||
135 | itemsText.add("Item numero 1-2"); |
||
136 | itemsText.add("Item numero 1-3"); |
||
137 | itemsText.add(""); |
||
138 | itemsText.add(""); |
||
139 | |||
140 | int numItems = itemsText.size(); |
||
141 | int i = 0; |
||
142 | while(numItems > 0) |
||
143 | { |
||
144 | items.add(DomainObjectFactory.createSurveyQuestionItemImpl()); |
||
145 | items.get(i).setLabelpt(itemsText.get(i)); |
||
146 | i++; |
||
147 | numItems--; |
||
148 | } |
||
149 | |||
150 | items.get(0).setQuestion(questions.get(0)); |
||
151 | items.get(1).setQuestion(questions.get(0)); |
||
152 | items.get(2).setQuestion(questions.get(0)); |
||
153 | items.get(3).setQuestion(questions.get(1)); |
||
154 | items.get(4).setQuestion(questions.get(2)); |
||
155 | |||
156 | SurveyQuestionItemDao.getInstance().save(items.get(0)); |
||
157 | SurveyQuestionItemDao.getInstance().save(items.get(1)); |
||
158 | SurveyQuestionItemDao.getInstance().save(items.get(2)); |
||
159 | SurveyQuestionItemDao.getInstance().save(items.get(3)); |
||
160 | SurveyQuestionItemDao.getInstance().save(items.get(4)); |
||
161 | |||
162 | return items; |
||
163 | } |
||
164 | |||
165 | public static List<SurveyQuestionCandidateAnswer> createSurveyQuestionCandidateAnswers(List<SurveyQuestionItem> items) |
||
166 | { |
||
167 | List<SurveyQuestionCandidateAnswer> candidates = new ArrayList<SurveyQuestionCandidateAnswer>(); |
||
168 | List<String> candidatesText = new ArrayList<String>(); |
||
169 | |||
170 | candidatesText.add("Candidate numero 1-1"); |
||
171 | candidatesText.add("Candidate numero 1-2"); |
||
172 | candidatesText.add("Candidate numero 1-3"); |
||
173 | candidatesText.add("Candidate numero 2-1"); |
||
174 | candidatesText.add("Candidate numero 2-2"); |
||
175 | candidatesText.add("Candidate numero 2-3"); |
||
176 | candidatesText.add("Candidate numero 3-1"); |
||
177 | candidatesText.add("Candidate numero 3-2"); |
||
178 | candidatesText.add("Candidate numero 3-3"); |
||
179 | |||
180 | int numCandidates = candidatesText.size(); |
||
181 | int i = 0; |
||
182 | while(numCandidates > 0) |
||
183 | { |
||
184 | candidates.add(DomainObjectFactory.createSurveyQuestionCandidateAnswerImpl()); |
||
185 | candidates.get(i).setLabelpt(candidatesText.get(i)); |
||
186 | i++; |
||
187 | numCandidates--; |
||
188 | } |
||
189 | |||
190 | candidates.get(0).setQuestionItem(items.get(0)); |
||
191 | candidates.get(0).setQuestionItem(items.get(1)); |
||
192 | candidates.get(0).setQuestionItem(items.get(2)); |
||
193 | candidates.get(0).setType("radio"); |
||
194 | candidates.get(1).setQuestionItem(items.get(0)); |
||
195 | candidates.get(1).setQuestionItem(items.get(1)); |
||
196 | candidates.get(1).setQuestionItem(items.get(2)); |
||
197 | candidates.get(1).setType("radio"); |
||
198 | candidates.get(2).setQuestionItem(items.get(0)); |
||
199 | candidates.get(2).setQuestionItem(items.get(1)); |
||
200 | candidates.get(2).setQuestionItem(items.get(2)); |
||
201 | candidates.get(2).setType("radio"); |
||
202 | candidates.get(3).setQuestionItem(items.get(3)); |
||
203 | candidates.get(3).setType("radio"); |
||
204 | candidates.get(4).setQuestionItem(items.get(3)); |
||
205 | candidates.get(4).setType("radio"); |
||
206 | candidates.get(5).setQuestionItem(items.get(3)); |
||
207 | candidates.get(5).setType("radio"); |
||
208 | candidates.get(6).setQuestionItem(items.get(4)); |
||
209 | candidates.get(6).setType("checkbox"); |
||
210 | candidates.get(7).setQuestionItem(items.get(4)); |
||
211 | candidates.get(7).setType("checkbox"); |
||
212 | candidates.get(8).setQuestionItem(items.get(4)); |
||
213 | candidates.get(8).setType("checkbox"); |
||
214 | |||
215 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(0)); |
||
216 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(1)); |
||
217 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(2)); |
||
218 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(3)); |
||
219 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(4)); |
||
220 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(5)); |
||
221 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(6)); |
||
222 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(7)); |
||
223 | SurveyQuestionCandidateAnswerDao.getInstance().save(candidates.get(8)); |
||
224 | |||
225 | return candidates; |
||
226 | } |
||
227 | |||
228 | public static List<SurveyQuestionAnswer> createSurveyQuestionAnswers(SurveyResponse response, List<SurveyQuestionCandidateAnswer> candidates) |
||
229 | { |
||
230 | List<SurveyQuestionAnswer> answers = new ArrayList<SurveyQuestionAnswer>(); |
||
231 | |||
232 | int numAnswers = 6; |
||
233 | int i = 0; |
||
234 | while(numAnswers > 0) |
||
235 | { |
||
236 | answers.add(DomainObjectFactory.createSurveyQuestionAnswerImpl()); |
||
237 | answers.get(i).setResponse(response); |
||
238 | i++; |
||
239 | numAnswers--; |
||
240 | } |
||
241 | |||
242 | answers.get(0).setCandidateAnswer(candidates.get(0)); |
||
243 | answers.get(1).setCandidateAnswer(candidates.get(1)); |
||
244 | answers.get(2).setCandidateAnswer(candidates.get(1)); |
||
245 | answers.get(3).setCandidateAnswer(candidates.get(4)); |
||
246 | answers.get(4).setCandidateAnswer(candidates.get(7)); |
||
247 | answers.get(5).setCandidateAnswer(candidates.get(8)); |
||
248 | |||
249 | SurveyQuestionAnswerDao.getInstance().save(answers.get(0)); |
||
250 | SurveyQuestionAnswerDao.getInstance().save(answers.get(1)); |
||
251 | SurveyQuestionAnswerDao.getInstance().save(answers.get(2)); |
||
252 | SurveyQuestionAnswerDao.getInstance().save(answers.get(3)); |
||
253 | SurveyQuestionAnswerDao.getInstance().save(answers.get(4)); |
||
254 | SurveyQuestionAnswerDao.getInstance().save(answers.get(5)); |
||
255 | |||
256 | return answers; |
||
257 | } |
||
258 | |||
259 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) |
||
260 | { |
||
261 | AbstractDao.getCurrentSession().beginTransaction(); |
||
262 | |||
263 | User user = DaoFactory.getUserDaoImpl().loadByUsername("super"); |
||
264 | |||
265 | List<Survey> surveys = DaoFactory.getSurveyDaoImpl().loadAllSurveys(); |
||
266 | |||
267 | List<SurveyResponse> responses = DaoFactory.getSurveyResponseDaoImpl() |
||
268 | .loadResponsesByParentStructuralNodeId(surveys.get(0).getStructuralNode().getId()); |
||
269 | |||
270 | if(responses.size() > 0) |
||
271 | { |
||
272 | 1.5.0/docs/api/java/lang/System.html">System.out.println("--->"+responses.get(0).getAnswers().size()); |
||
273 | } |
||
274 | |||
275 | // List ll = DaoFactory.getSurveyQuestionAnswerDaoImpl().teste(); |
||
276 | |||
277 | 5+0%2Fdocs%2Fapi+List">List ll = new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList(); |
||
278 | |||
279 | 1.5.0/docs/api/java/lang/String.html">String[] strs = new 1.5.0/docs/api/java/lang/String.html">String[ll.size()]; |
||
280 | int[] ints = new int[ll.size()]; |
||
281 | 1.5.0/docs/api/java/lang/String.html">String question = ""; |
||
282 | |||
283 | 1.5.0/docs/api/java/util/Iterator.html">Iterator iter = ll.iterator(); |
||
284 | |||
285 | int i = 0; |
||
286 | while (iter.hasNext()) |
||
287 | { |
||
288 | 5+0%2Fdocs%2Fapi+Object">Object[] objs = (5+0%2Fdocs%2Fapi+Object">Object[]) iter.next(); |
||
289 | |||
290 | if(question == "") |
||
291 | { |
||
292 | SurveyQuestion quest = DaoFactory.getSurveyQuestionDaoImpl().loadById(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(objs[0].toString())); |
||
293 | question = quest.getLabelpt(); |
||
294 | } |
||
295 | |||
296 | SurveyQuestionCandidateAnswer cand = DaoFactory.getSurveyQuestionCandidateAnswerDaoImpl().loadById(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(objs[2].toString())); |
||
297 | strs[i] = cand.getLabelpt(); |
||
298 | ints[i] = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(objs[3].toString()); |
||
299 | i++; |
||
300 | } |
||
301 | |||
302 | createBarChart(question, "", "Número de Respostas", strs, ints); |
||
303 | |||
304 | // // ----------------- POPULAR |
||
305 | // Survey survey = createSurvey(user); |
||
306 | // List<SurveyStructuralNode> nodes = createStructuralNodes(survey); |
||
307 | // SurveyResponse responses = createSurveyResponses(user, nodes.get(0)); |
||
308 | // List<SurveyQuestion> questions = createSurveyQuestions(survey); |
||
309 | // List<SurveyQuestionItem> items = createSurveyQuestionItems(questions); |
||
310 | // List<SurveyQuestionCandidateAnswer> candidates = createSurveyQuestionCandidateAnswers(items); |
||
311 | // List<SurveyQuestionAnswer> answers = createSurveyQuestionAnswers(responses, candidates); |
||
312 | |||
313 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
314 | } |
||
315 | |||
316 | public static void printSurveyStructuralNodesOrganized(SurveyStructuralNode node) |
||
317 | { |
||
318 | if(node == null) return; |
||
319 | |||
320 | for(SurveyStructuralNode n : node.getNodes()) |
||
321 | { |
||
322 | 1.5.0/docs/api/java/lang/System.html">System.out.println(+n.getId()+" - "+n.getMsg()); |
||
323 | printSurveyStructuralNodesOrganized(n); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | public static void createBarChart(1.5.0/docs/api/java/lang/String.html">String mText, 1.5.0/docs/api/java/lang/String.html">String hText, 1.5.0/docs/api/java/lang/String.html">String vText, 1.5.0/docs/api/java/lang/String.html">String[] series, int[] numbers) |
||
328 | { |
||
329 | 1.5.0/docs/api/java/io/File.html">File img = new 1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + "/teste.png"); |
||
330 | |||
331 | DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
||
332 | |||
333 | if(series.length == numbers.length) |
||
334 | { |
||
335 | for(int i=0; i<series.length; i++) |
||
336 | { |
||
337 | dataset.addValue(numbers[i], series[i], ""); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | CategoryDataset datasett = dataset; |
||
342 | |||
343 | JFreeChart chart = ChartFactory.createBarChart(mText,hText,vText,datasett,PlotOrientation.VERTICAL,true,true,false); |
||
344 | |||
345 | try { |
||
346 | ChartUtilities.saveChartAsPNG(img, chart, 400, 300); |
||
347 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
348 | e.printStackTrace(); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | public static void displayObjectList(5+0%2Fdocs%2Fapi+List">List list) |
||
353 | { |
||
354 | 1.5.0/docs/api/java/util/Iterator.html">Iterator iter = list.iterator(); |
||
355 | if (!iter.hasNext()) |
||
356 | { |
||
357 | 1.5.0/docs/api/java/lang/System.html">System.out.println("No objects to display."); |
||
358 | return; |
||
359 | } |
||
360 | while (iter.hasNext()) |
||
361 | { |
||
362 | 5+0%2Fdocs%2Fapi+Object">Object obj = iter.next(); |
||
363 | |||
364 | 1.5.0/docs/api/java/lang/System.html">System.out.println(obj); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | public static void displayObjectListArray(5+0%2Fdocs%2Fapi+List">List list) |
||
369 | { |
||
370 | 1.5.0/docs/api/java/util/Iterator.html">Iterator iter = list.iterator(); |
||
371 | if (!iter.hasNext()) |
||
372 | { |
||
373 | 1.5.0/docs/api/java/lang/System.html">System.out.println("No objects to display."); |
||
374 | return; |
||
375 | } |
||
376 | while (iter.hasNext()) |
||
377 | { |
||
378 | 5+0%2Fdocs%2Fapi+Object">Object[] objs = (5+0%2Fdocs%2Fapi+Object">Object[]) iter.next(); |
||
379 | |||
380 | 1.5.0/docs/api/java/lang/System.html">System.out.print(objs[0]); |
||
381 | 1.5.0/docs/api/java/lang/System.html">System.out.print(" "); |
||
382 | 1.5.0/docs/api/java/lang/System.html">System.out.print(objs[1]); |
||
383 | 1.5.0/docs/api/java/lang/System.html">System.out.print(" "); |
||
384 | 1.5.0/docs/api/java/lang/System.html">System.out.println(objs[2]); |
||
385 | } |
||
386 | } |
||
387 | } |