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.FormConstants;
034: import org.libresource.form.FormField;
035: import org.libresource.form.ejb.model.FormResourceValue;
036: import org.libresource.form.interfaces.LibresourceFormService;
037:
038: import org.libresource.kernel.KernelConstants;
039: import org.libresource.kernel.LibresourceSecurityException;
040: import org.libresource.kernel.interfaces.KernelService;
041:
042: import org.libresource.web.Controller;
043: import org.libresource.wiki.WikiConstants;
044:
045: import java.net.URI;
046:
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.Vector;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053:
054: public class EditFormController implements Controller {
055: private LibresourceFormService libresourceFormService;
056:
057: public Object process(URI uri, HttpServletRequest request,
058: HttpServletResponse response) throws Exception {
059: libresourceFormService = (LibresourceFormService) Libresource
060: .getService(FormConstants.SERVICE);
061:
062: FormResourceValue form = libresourceFormService.getForm(uri);
063: request.setAttribute("selectPage", Boolean.valueOf(form
064: .getResourceTypeToCreate().equals(
065: WikiConstants.RESOURCE)));
066:
067: if (request.getParameter("cancel") != null) {
068: request.setAttribute("form", form);
069: request.setAttribute("description", form.getDescription());
070:
071: return uri;
072: }
073:
074: if (request.getParameter("name") == null) {
075: // TO prevent a deny later
076: KernelService kernelService = (KernelService) Libresource
077: .getService(KernelConstants.SERVICE);
078:
079: if (!kernelService.checkSecurity(uri,
080: KernelConstants.SECURITY_UPDATE)) {
081: throw new LibresourceSecurityException(uri,
082: KernelConstants.SECURITY_UPDATE);
083: }
084:
085: request.setAttribute("form", form);
086:
087: return "/pages/modules/form/editForm.jsp";
088: }
089:
090: // What kind of action
091: if (request.getParameter("save") != null) {
092: if (saveData(uri, request, response)) {
093: form = libresourceFormService.getForm(uri);
094: request.setAttribute("form", form);
095: request.setAttribute("description", form
096: .getDescription());
097: } else {
098: form = libresourceFormService.getForm(uri);
099: form.setFormFields(getFields(request, response));
100: request.setAttribute("form", form);
101: request.setAttribute("description", form
102: .getDescription());
103: request
104: .setAttribute("error",
105: "Error: Field Id and field Label can't be empty. Field Id must be unique.");
106:
107: return "/pages/modules/form/editForm.jsp";
108: }
109: }
110:
111: if (request.getParameter("addField") != null) {
112: if (saveData(uri, request, response)) {
113: libresourceFormService.addFormField(uri, new FormField(
114: ""));
115:
116: form = libresourceFormService.getForm(uri);
117: request.setAttribute("form", form);
118:
119: return "/pages/modules/form/editForm.jsp";
120: } else {
121: form = libresourceFormService.getForm(uri);
122: form.setFormFields(getFields(request, response));
123: request.setAttribute("form", form);
124: request.setAttribute("description", form
125: .getDescription());
126: request
127: .setAttribute("error",
128: "Error: Field Id and field Label can't be empty. Field Id must be unique.");
129:
130: return "/pages/modules/form/editForm.jsp";
131: }
132: }
133:
134: // Check field moves/delete
135: int nbFields = (form.getFormFields() == null) ? 0 : form
136: .getFormFields().size();
137:
138: for (int i = 0; i < nbFields; i++) {
139: if (request.getParameter("up_field_" + i) != null) {
140: if (saveData(uri, request, response)) {
141: form = libresourceFormService.getForm(uri);
142: libresourceFormService.moveFormFieldUp(uri,
143: ((FormField) form.getFormFields().get(i))
144: .getFieldId());
145: form = libresourceFormService.getForm(uri);
146: request.setAttribute("form", form);
147:
148: return "/pages/modules/form/editForm.jsp";
149: } else {
150: form = libresourceFormService.getForm(uri);
151: form.setFormFields(getFields(request, response));
152: request.setAttribute("form", form);
153: request.setAttribute("description", form
154: .getDescription());
155: request
156: .setAttribute("error",
157: "Error: Field Id and field Label can't be empty. Field Id must be unique.");
158:
159: return "/pages/modules/form/editForm.jsp";
160: }
161: }
162:
163: if (request.getParameter("down_field_" + i) != null) {
164: if (saveData(uri, request, response)) {
165: form = libresourceFormService.getForm(uri);
166: libresourceFormService.moveFormFieldDown(uri,
167: ((FormField) form.getFormFields().get(i))
168: .getFieldId());
169: form = libresourceFormService.getForm(uri);
170: request.setAttribute("form", form);
171:
172: return "/pages/modules/form/editForm.jsp";
173: } else {
174: form = libresourceFormService.getForm(uri);
175: form.setFormFields(getFields(request, response));
176: request.setAttribute("form", form);
177: request.setAttribute("description", form
178: .getDescription());
179: request
180: .setAttribute("error",
181: "Error: Field Id and field Label can't be empty. Field Id must be unique.");
182:
183: return "/pages/modules/form/editForm.jsp";
184: }
185: }
186:
187: if (request.getParameter("addField_" + i) != null) {
188: if (saveData(uri, request, response)) {
189: libresourceFormService.addFormField(uri, i + 1,
190: new FormField(""));
191:
192: form = libresourceFormService.getForm(uri);
193: request.setAttribute("form", form);
194:
195: return "/pages/modules/form/editForm.jsp";
196: } else {
197: form = libresourceFormService.getForm(uri);
198: form.setFormFields(getFields(request, response));
199: request.setAttribute("form", form);
200: request.setAttribute("description", form
201: .getDescription());
202: request
203: .setAttribute("error",
204: "Error: Field Id and field Label can't be empty. Field Id must be unique.");
205:
206: return "/pages/modules/form/editForm.jsp";
207: }
208: }
209:
210: if (request.getParameter("delete_field_" + i) != null) {
211: saveData(uri, request, response);
212: form = libresourceFormService.getForm(uri);
213: libresourceFormService.removeFormField(uri,
214: ((FormField) form.getFormFields().get(i))
215: .getFieldId());
216: form = libresourceFormService.getForm(uri);
217: request.setAttribute("form", form);
218:
219: return "/pages/modules/form/editForm.jsp";
220: }
221: }
222:
223: return uri;
224: }
225:
226: private boolean saveData(URI uri, HttpServletRequest request,
227: HttpServletResponse response) throws Exception {
228: libresourceFormService.editForm(uri, request
229: .getParameter("name"), request
230: .getParameter("description"), request
231: .getParameter("destination"), request
232: .getParameter("typeCreation"), request
233: .getParameter("redirect"));
234:
235: // Get forms fields
236: Vector formFields = getFields(request, response);
237:
238: // Check fields ID
239: ArrayList fieldsIds = new ArrayList();
240: Iterator fieldIterator = formFields.iterator();
241: FormField currentField = null;
242: boolean ok = true;
243:
244: while (fieldIterator.hasNext()) {
245: currentField = (FormField) fieldIterator.next();
246:
247: if (fieldsIds.contains(currentField.getFieldId())
248: || (currentField.getFieldId().trim().length() == 0)
249: || (currentField.getFieldLabel().trim().length() == 0)) {
250: // Error
251: ok = false;
252: } else {
253: fieldsIds.add(currentField.getFieldId());
254: }
255: }
256:
257: if (ok) {
258: libresourceFormService.setFormFields(uri, formFields);
259: }
260:
261: request.setAttribute("selectPage", Boolean.valueOf(request
262: .getParameter("typeCreation").equals(
263: WikiConstants.RESOURCE)));
264:
265: return ok;
266: }
267:
268: private Vector getFields(HttpServletRequest request,
269: HttpServletResponse response) throws Exception {
270: // Get forms fields
271: int formIndex = 0;
272: Vector formFields = new Vector();
273: FormField currentField = null;
274:
275: while (request.getParameter("fieldID_" + formIndex) != null) {
276: currentField = new FormField(
277: request.getParameter("fieldID_" + formIndex),
278: request.getParameter("fieldLABEL_" + formIndex),
279: request.getParameter("fieldREQUIRED_" + formIndex) != null,
280: Integer.parseInt(request.getParameter("fieldTYPE_"
281: + formIndex)));
282: currentField.setOptions(request
283: .getParameter("fieldOPTIONS_" + formIndex));
284: formFields.add(currentField);
285: formIndex++;
286:
287: // Check fields ID
288: }
289:
290: return formFields;
291: }
292: }
|