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:
033: import org.libresource.form.Form;
034: import org.libresource.form.FormConstants;
035: import org.libresource.form.FormField;
036: import org.libresource.form.FormFieldRequiredException;
037: import org.libresource.form.ejb.model.FormResourceValue;
038: import org.libresource.form.interfaces.LibresourceFormService;
039:
040: import org.libresource.kernel.KernelConstants;
041: import org.libresource.kernel.interfaces.KernelService;
042:
043: import org.libresource.web.Controller;
044:
045: import java.net.URI;
046:
047: import java.util.ArrayList;
048: import java.util.Iterator;
049:
050: import javax.servlet.http.HttpServletRequest;
051: import javax.servlet.http.HttpServletResponse;
052:
053: public class SubmitFormController implements Controller {
054: private LibresourceFormService libresourceFormService;
055: private KernelService kernelService;
056:
057: public Object process(URI uri, HttpServletRequest request,
058: HttpServletResponse response) throws Exception {
059: // Generic op
060: if (kernelService == null) {
061: kernelService = (KernelService) Libresource
062: .getService(KernelConstants.SERVICE);
063: }
064:
065: if (libresourceFormService == null) {
066: libresourceFormService = (LibresourceFormService) Libresource
067: .getService(FormConstants.SERVICE);
068: }
069:
070: FormResourceValue form = libresourceFormService.getForm(uri);
071:
072: // ok
073: if (request.getParameter("ok") != null) {
074: // retreive meta-data
075: Form formValue = new Form();
076: FormField currentField;
077:
078: ArrayList<String> missingFields = new ArrayList<String>(5);
079:
080: for (Iterator i = form.getFormFields().iterator(); i
081: .hasNext();) {
082: currentField = (FormField) ((FormField) i.next())
083: .clone();
084:
085: try {
086: currentField.updateValueFromRequest(request);
087: } catch (FormFieldRequiredException e) {
088: missingFields.add(currentField.getFieldLabel());
089: }
090:
091: formValue.addField(currentField);
092: }
093:
094: if (missingFields.size() > 0) {
095: request.setAttribute("fieldMissing", missingFields
096: .toString());
097: request.setAttribute("form", formValue);
098: request.setAttribute("description", form
099: .getDescription());
100:
101: return "/pages/modules/form/viewForm.jsp";
102: }
103:
104: // Submit
105: libresourceFormService.submitForm(uri, formValue);
106:
107: // Redirect
108: if ((form.getRedirectionURI() != null)
109: && (form.getRedirectionURI().trim().length() > 0)) {
110: if (form.getRedirectionURI().startsWith("http://")
111: || form.getRedirectionURI()
112: .startsWith("ftp://")) {
113: return new URI(form.getRedirectionURI());
114: } else {
115: return Libresource.getAbsoluteURI(uri, form
116: .getRedirectionURI()
117: + "?successSubmit");
118: }
119: }
120: }
121:
122: // cancel
123: request.setAttribute("form", form);
124: request.setAttribute("description", form.getDescription());
125:
126: return "/pages/modules/form/viewForm.jsp";
127: }
128: }
|