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.controllers.form;
030:
031: import org.libresource.Libresource;
032: import org.libresource.LibresourceResourceValue;
033:
034: import org.libresource.form.Form;
035: import org.libresource.form.FormConstants;
036: import org.libresource.form.FormResultSet;
037:
038: import org.libresource.kernel.KernelConstants;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.libresource.web.Controller;
042:
043: import java.net.URI;
044:
045: import java.util.Collection;
046:
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049:
050: public class ResultFormExporterController implements Controller {
051: private static KernelService kernel;
052:
053: public Object process(URI uri, HttpServletRequest request,
054: HttpServletResponse response) throws Exception {
055: Collection formList = getFormList(uri);
056: request.setAttribute("formList", formList);
057: request.setAttribute("labels", ((Form) formList.iterator()
058: .next()).getFieldsLabel());
059:
060: String exportFormat = (request.getParameter("format") == null) ? "html"
061: : request.getParameter("format");
062:
063: if (exportFormat.equals("csv")) {
064: request.setAttribute("nodecorator", "");
065: response.addHeader("Content-Disposition",
066: "attachment; filename="
067: + getKernel().getResource(uri)
068: .getShortResourceName().replaceAll(
069: " ", "_") + ".csv");
070:
071: return "/pages/modules/form/viewFormResultCSV.jsp";
072: } else if (exportFormat.equals("json")) {
073: return "/pages/modules/form/viewFormResultJSON.jsp";
074: }
075:
076: return "/pages/modules/form/viewFormResultHTML.jsp";
077: }
078:
079: public static KernelService getKernel() throws Exception {
080: if (kernel == null) {
081: kernel = (KernelService) Libresource
082: .getService(KernelConstants.SERVICE);
083: }
084:
085: return kernel;
086: }
087:
088: public static FormResultSet getFormList(URI uri) throws Exception {
089: FormResultSet formList = new FormResultSet();
090: LibresourceResourceValue[] resourceValues = getKernel()
091: .listResourcesAt(uri);
092: Form currentForm = null;
093:
094: for (int i = 0; i < resourceValues.length; i++) {
095: currentForm = (Form) getKernel().getPropertyObject(
096: resourceValues[i].getUri(),
097: FormConstants.NODE_PROPERTY_FORM);
098:
099: if (currentForm != null) {
100: currentForm.setLocalURI(resourceValues[i].getUri());
101: formList.add(currentForm);
102: }
103: }
104:
105: return formList;
106: }
107: }
|