001: /**
002: * LibreSource Community
003: * Copyright (C) 2004-2007 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This software is not a free software; you can modify it under the
007: * LibreSource Enterprise user license but you can't redistribute it.
008: * See licenses details in LSE-user-license.txt
009: *
010: * Initial authors :
011: *
012: * Guillaume Bort / INRIA
013: * Francois Charoy / Universite Nancy 2
014: * Julien Forest / Artenum
015: * Claude Godart / Universite Henry Poincare
016: * Florent Jouille / INRIA
017: * Sebastien Jourdain / INRIA / Artenum
018: * Yves Lerumeur / Artenum
019: * Pascal Molli / Universite Henry Poincare
020: * Gerald Oster / INRIA
021: * Mariarosa Penzi / Artenum
022: * Gerard Sookahet / Artenum
023: * Raphael Tani / INRIA
024: *
025: * Contributors :
026: *
027: * Stephane Bagnier / Artenum
028: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
029: */package org.libresource.web.wiki.macros;
030:
031: import java.io.IOException;
032: import java.io.Writer;
033: import java.net.URI;
034: import java.util.ArrayList;
035:
036: import javax.servlet.jsp.jstl.fmt.LocalizationContext;
037:
038: import org.libresource.Libresource;
039: import org.libresource.form.Form;
040: import org.libresource.form.FormResultSet;
041: import org.libresource.form.IdLabelBean;
042: import org.libresource.membership.MembershipConstants;
043: import org.libresource.membership.interfaces.MembershipService;
044: import org.libresource.web.controllers.form.ResultFormExporterController;
045: import org.libresource.web.servlets.BaseServlet;
046: import org.libresource.web.wiki.LibresourceRenderContext;
047: import org.radeox.macro.BaseMacro;
048: import org.radeox.macro.parameter.MacroParameter;
049:
050: public class TableFormFieldMacro extends BaseMacro {
051: private final static String DOJO_TABLE_SCRIPT = "<script type=\"text/javascript\">dojo.require('dojo.widget.FilteringTable');dojo.hostenv.writeIncludes();</script>";
052:
053: public TableFormFieldMacro() throws Exception {
054: }
055:
056: public String getName() {
057: return "tableForm";
058: }
059:
060: public void execute(Writer writer, MacroParameter params)
061: throws IllegalArgumentException, IOException {
062: try {
063: MembershipService membershipService = (MembershipService) Libresource
064: .getService(MembershipConstants.SERVICE);
065: String lang = (String) membershipService.getProfile()
066: .getInfos().get("libresource-web.lang");
067: LocalizationContext local = BaseServlet
068: .getLocalisation(lang == null ? "en" : lang);
069:
070: URI uriToGetField = null;
071:
072: // Get params
073: URI currentUri = ((LibresourceRenderContext) params
074: .getContext()).getUri();
075: String uri = (String) params.getParams().get("uri");
076: boolean printGo = true;
077: boolean csvExport = true;
078: // Set the right URI
079: if (uri == null) {
080: uriToGetField = Libresource.getAbsoluteURI(currentUri,
081: ".");
082: } else {
083: uriToGetField = Libresource.getAbsoluteURI(currentUri,
084: uri);
085: }
086: // Get the forms
087: FormResultSet formList = ResultFormExporterController
088: .getFormList(uriToGetField);
089:
090: // Manage the print param
091: if (params.getParams().get("print") != null) {
092: printGo = ((String) params.getParams().get("print"))
093: .trim().toLowerCase().indexOf("golink") != -1;
094: csvExport = ((String) params.getParams().get("print"))
095: .trim().toLowerCase().indexOf("csvexport") != -1;
096: }
097: // Manage the field param
098: if (params.getParams().get("field") != null) {
099: ArrayList<String> fieldIds = new ArrayList<String>();
100: String[] fieldsList = ((String) params.getParams().get(
101: "field")).trim().toLowerCase().split(",");
102: for (int i = 0; i < fieldsList.length; i++) {
103: fieldIds.add(fieldsList[i].trim());
104: }
105: if (fieldIds.size() > 0) {
106: // Filter ids and labels
107: formList.filter(fieldIds);
108: }
109: }
110:
111: if (formList.size() > 0) {
112: // Dojo javascript
113: writer.write(DOJO_TABLE_SCRIPT);
114:
115: // Print the forms label
116: writer
117: .write("<table dojoType=\"filteringTable\" id=\"formTable\" multiple=\"true\" alternateRows=\"true\" maxSortable=\"1\" class=\"lsTable\"><thead><tr>");
118:
119: for (IdLabelBean idLabel : formList.getIdsLabels()) {
120: writer.write("<th field=\"" + idLabel.getId()
121: + "\" dataType=\"String\" valign=\"top\">");
122: writer.write(idLabel.getLabel());
123: writer.write("</th>");
124: }
125:
126: if (printGo) {
127: writer.write("<th></th>");
128: }
129:
130: writer.write("</tr></thead>");
131:
132: // Print form
133: writer.write("<tbody>");
134:
135: int indexValue = 1;
136: for (Form form : formList) {
137: writer.write("<tr value=\"");
138: writer.write(Integer.toString(indexValue++));
139: writer.write("\">");
140:
141: for (String id : formList.getIds()) {
142: writer.write("<td>");
143: if (form.getField(id) != null)
144: writer.write(form.getField(id).getValue());
145: writer.write("</td>");
146: }
147:
148: if (printGo) {
149: writer.write("<td><a href=\"");
150: writer.write(form.getLocalURI().getPath());
151: writer.write("\">");
152: writer.write(local.getResourceBundle()
153: .getString("form.table.go.link"));
154: writer.write("</a></td>");
155: }
156:
157: writer.write("</tr>");
158: }
159:
160: writer.write("</tbody></table>");
161:
162: // Export CSV
163: if (csvExport) {
164: writer.write("<a href=\"");
165: writer.write(uriToGetField.getPath());
166: writer
167: .write("?action=childrenFormData&format=csv&nodecorator\">");
168: writer.write(local.getResourceBundle().getString(
169: "form.table.export.csv"));
170: writer.write("</a><br/>");
171: }
172:
173: // flush
174: writer.flush();
175: }
176: } catch (IOException e) {
177: throw e;
178: } catch (IllegalArgumentException e) {
179: throw e;
180: } catch (Exception e) {
181: throw new IllegalArgumentException(e.getMessage());
182: }
183: }
184:
185: public String getDescription() {
186: // ex: (tableForm) or (tableForm:uri=../form I print=name, mail, goLink)
187: // I as pipe
188: return "Display a table listing form results.";
189: }
190:
191: public String[] getParamDescription() {
192: return new String[] {
193: "1. (uri=) The uri on which you want to get the form value (Optional, by default use the local path)",
194: "2. (print=) Print additinal features such as a link to the data (goLink) or a link to get a CSV file (csvExport) (Optional, by default print all)",
195: "3. (field=) The list of field to show (Optional by default all)" };
196: }
197: }
|