001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.survey;
034:
035: import org.jfree.chart.ChartRenderingInfo;
036: import org.jfree.chart.ChartUtilities;
037: import org.jfree.chart.JFreeChart;
038: import org.jfree.chart.entity.StandardEntityCollection;
039:
040: import org.libresource.Libresource;
041:
042: import org.libresource.survey.SurveyConstants;
043: import org.libresource.survey.ejb.model.SurveyResourceValue;
044: import org.libresource.survey.interfaces.LibresourceSurveyService;
045:
046: import org.libresource.web.Controller;
047: import org.libresource.web.Helper;
048:
049: import java.io.ByteArrayOutputStream;
050: import java.io.PrintWriter;
051: import java.io.StringWriter;
052:
053: import java.net.URI;
054:
055: import javax.servlet.http.HttpServletRequest;
056: import javax.servlet.http.HttpServletResponse;
057:
058: public class SurveyController implements Controller {
059: public Object process(URI uri, HttpServletRequest request,
060: HttpServletResponse response) throws Exception {
061: LibresourceSurveyService surveyService = (LibresourceSurveyService) Libresource
062: .getService(SurveyConstants.SERVICE);
063: SurveyResourceValue survey = surveyService.getSurvey(uri);
064:
065: request.setAttribute("question", survey.getQuestion());
066: request.setAttribute("options", surveyService
067: .getOptionsInSurvey(uri));
068: request.setAttribute("state", survey.getState());
069: request.setAttribute("displayMode", survey.getDisplayMode());
070: request.setAttribute("lastVoteDate", survey.getLastVoteDate());
071:
072: int nbVoters = survey.getVoters().length;
073: request.setAttribute("results", surveyService.getResults(uri,
074: nbVoters));
075: request.setAttribute("nbVoters", new Integer(nbVoters));
076:
077: if (survey.getDisplayMode().compareTo("PIECHART") == 0) {
078: JFreeChart pieChart = DrawPieChartHelper.drawChart(uri,
079: DrawPieChartHelper.SURVEY_RESULTS);
080: ChartRenderingInfo pieInfo = new ChartRenderingInfo(
081: new StandardEntityCollection());
082: ByteArrayOutputStream pieBaos = new ByteArrayOutputStream();
083: ChartUtilities.writeChartAsPNG(pieBaos, pieChart, 500, 250,
084: pieInfo);
085:
086: StringWriter pieWriter = new StringWriter();
087: ChartUtilities.writeImageMap(new PrintWriter(pieWriter),
088: DrawPieChartHelper.SURVEY_RESULTS, pieInfo);
089: StatsCache
090: .putItem(pieBaos.toByteArray(), pieWriter
091: .toString(), uri,
092: DrawPieChartHelper.SURVEY_RESULTS);
093: request.setAttribute(DrawPieChartHelper.SURVEY_RESULTS,
094: StatsCache.getMap(uri,
095: DrawPieChartHelper.SURVEY_RESULTS));
096: }
097:
098: if (survey.getDisplayMode().compareTo("HISTOGRAM") == 0) {
099: JFreeChart chart = DrawHistogramHelper.drawChart(uri,
100: DrawHistogramHelper.SURVEY_RESULTS);
101: ChartRenderingInfo info = new ChartRenderingInfo(
102: new StandardEntityCollection());
103: ByteArrayOutputStream baos = new ByteArrayOutputStream();
104: ChartUtilities.writeChartAsPNG(baos, chart, 500, 250, info);
105:
106: StringWriter writer = new StringWriter();
107: ChartUtilities.writeImageMap(new PrintWriter(writer),
108: DrawHistogramHelper.SURVEY_RESULTS, info);
109: StatsCache.putItem(baos.toByteArray(), writer.toString(),
110: uri, DrawHistogramHelper.SURVEY_RESULTS);
111: request.setAttribute(DrawHistogramHelper.SURVEY_RESULTS,
112: StatsCache.getMap(uri,
113: DrawHistogramHelper.SURVEY_RESULTS));
114: }
115:
116: if (surveyService.isVoterInSurvey(uri)) {
117: request.setAttribute("voter", "true");
118: request.setAttribute("result", "true");
119: }
120:
121: if ((request.getParameter("view") != null)
122: || !Helper.checkSecurity(uri, "Survey:VOTE")) {
123: request.setAttribute("result", "true");
124: }
125:
126: request.setAttribute("color", new String[] { "#c5ceef",
127: "#ffffc5", "#3a63a5", "#dedede", "#9c8cb5" });
128:
129: return "/pages/modules/survey/viewSurvey.jsp";
130: }
131: }
|