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.wiki.macros;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.URINotExistException;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.libresource.survey.SurveyConstants;
042: import org.libresource.survey.ejb.model.SurveyResourceValue;
043: import org.libresource.survey.interfaces.LibresourceSurveyService;
044:
045: import org.libresource.web.wiki.LibresourceRenderContext;
046:
047: import org.radeox.macro.BaseMacro;
048: import org.radeox.macro.parameter.MacroParameter;
049:
050: import java.io.IOException;
051: import java.io.Writer;
052:
053: import java.net.URI;
054:
055: import java.util.Iterator;
056: import java.util.Vector;
057:
058: public class SurveyMacro extends BaseMacro {
059: private String scheme;
060: private String host;
061: private LibresourceSurveyService surveyService;
062: private KernelService kernelService;
063:
064: public SurveyMacro() throws Exception {
065: surveyService = (LibresourceSurveyService) Libresource
066: .getService(SurveyConstants.SERVICE);
067: kernelService = (KernelService) Libresource
068: .getService(KernelConstants.SERVICE);
069: scheme = kernelService.getUriScheme();
070: host = kernelService.getUriHost();
071: }
072:
073: public String getName() {
074: return "survey";
075: }
076:
077: public void execute(Writer writer, MacroParameter params)
078: throws IllegalArgumentException, IOException {
079: try {
080: if (params.getLength() != 1) {
081: throw new IllegalArgumentException(
082: "(one param required : surveyUri)");
083: }
084:
085: String LibresourceLinkPattern = "([^\"'=]|^)("
086: + scheme
087: + "://(%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[-_.!~*';/?:@#&=+$,\\p{Alnum}])+)";
088: URI currentUri = ((LibresourceRenderContext) params
089: .getContext()).getUri();
090: String uri = params.get(0);
091: URI resolvedUri = null;
092: String surveyUrl = null;
093:
094: if (uri.matches(LibresourceLinkPattern)) {
095: resolvedUri = kernelService.normalizeURI(new URI(uri));
096: } else {
097: URI relativeLink;
098:
099: if (uri.startsWith("/")) {
100: relativeLink = new URI(scheme + "://" + host + uri);
101: } else {
102: relativeLink = new URI(uri);
103: }
104:
105: resolvedUri = kernelService.normalizeURI(currentUri
106: .resolve(relativeLink));
107: }
108:
109: surveyUrl = ""
110: + kernelService.normalizeURI(resolvedUri).getPath()
111: .substring(1);
112:
113: try {
114: SurveyResourceValue survey = surveyService
115: .getSurvey(resolvedUri);
116:
117: String question = survey.getQuestion();
118: int nbVoters = survey.getVoters().length;
119: Vector results = surveyService.getResults(resolvedUri,
120: nbVoters);
121: Object[] tab = new Object[4];
122: writer.write("<div>");
123: writer.write("<a href=\"" + surveyUrl + "\"><b>"
124: + question + "</b></a><br/>");
125: writer.write("<table>");
126:
127: for (Iterator i = results.iterator(); i.hasNext();) {
128: tab = (Object[]) i.next();
129: writer.write("<tr><td><i>" + tab[1].toString()
130: + "</i></td>");
131: writer.write("<td align=\"center\"><div style=\"");
132:
133: if (Integer.parseInt(tab[3].toString()) == 0) {
134: writer.write("color:Black;");
135: } else {
136: writer.write("color:white;");
137: }
138:
139: writer
140: .write("background-color:blue; height:100%; width:"
141: + tab[3].toString()
142: + "px\">"
143: + tab[3].toString()
144: + "%</div></td>");
145: writer.write("<td>" + tab[2].toString()
146: + " vote</td></tr>");
147: }
148:
149: writer.write("</table>");
150: writer.write(nbVoters + " votes");
151: writer.write("</div>");
152: } catch (URINotExistException e) {
153: throw new IllegalArgumentException(
154: "(survey not found at uri : " + resolvedUri);
155: }
156: } catch (IOException e) {
157: throw e;
158: } catch (IllegalArgumentException e) {
159: throw e;
160: } catch (Exception e) {
161: throw new IllegalArgumentException("error in survey : "
162: + e.getMessage());
163: }
164: }
165:
166: public String getDescription() {
167: return "Display results of a survey.";
168: }
169:
170: public String[] getParamDescription() {
171: return new String[] { "the survey URI" };
172: }
173: }
|