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:
035: import org.libresource.Libresource;
036: import org.libresource.form.Form;
037: import org.libresource.form.FormConstants;
038: import org.libresource.form.FormField;
039: import org.libresource.kernel.KernelConstants;
040: import org.libresource.kernel.interfaces.KernelService;
041: import org.libresource.web.wiki.LibresourceRenderContext;
042: import org.radeox.macro.BaseMacro;
043: import org.radeox.macro.parameter.MacroParameter;
044:
045: public class PrintFormFieldMacro extends BaseMacro {
046: private KernelService kernelService = null;
047:
048: public PrintFormFieldMacro() throws Exception {
049: kernelService = (KernelService) Libresource
050: .getService(KernelConstants.SERVICE);
051: }
052:
053: public String getName() {
054: return "printField";
055: }
056:
057: public void execute(Writer writer, MacroParameter params)
058: throws IllegalArgumentException, IOException {
059: try {
060: URI uriToGetField = null;
061:
062: // Get params
063: URI currentUri = ((LibresourceRenderContext) params
064: .getContext()).getUri();
065: String uri = ((String) params.getParams().get("uri"));
066: String fieldId = (String) params.getParams().get("field");
067: boolean printLabel = true;
068: boolean printValue = true;
069: boolean printEditLink = true;
070:
071: if (params.getParams().get("print") != null) {
072: String value = ((String) params.getParams()
073: .get("print")).trim().toLowerCase();
074: printLabel = value.indexOf("label") != -1;
075: printValue = value.indexOf("value") != -1;
076: printEditLink = value.indexOf("edit") != -1;
077: }
078:
079: // Set the right URI
080: if (uri == null) {
081: uriToGetField = Libresource.getAbsoluteURI(currentUri,
082: ".");
083: } else {
084: uriToGetField = Libresource.getAbsoluteURI(currentUri,
085: uri);
086: }
087:
088: // Get the form
089: Form form = (Form) kernelService.getPropertyObject(
090: uriToGetField, FormConstants.NODE_PROPERTY_FORM);
091:
092: // Print the fields
093: if (fieldId == null) {
094: for (FormField currentField : form.getFields()) {
095: writer.write(currentField.getHtmlValue(printLabel,
096: printValue));
097: writer.write("\n<br/>\n");
098: }
099: } else {
100: if (fieldId.indexOf(",") != -1) {
101: // several fields to print
102: String[] fieldIdArray = fieldId.split(",");
103: String currentFieldId = null;
104:
105: for (int i = 0; i < fieldIdArray.length; i++) {
106: currentFieldId = fieldIdArray[i].trim();
107:
108: if ((currentFieldId.length() > 0)
109: && (form.getField(currentFieldId) != null)) {
110: writer.write(form.getField(currentFieldId)
111: .getHtmlValue(printLabel,
112: printValue));
113: writer.write("\n<br/>\n");
114: }
115: }
116: } else {
117: // only one field to print
118: writer.write(form.getField(fieldId.trim())
119: .getHtmlValue(printLabel, printValue));
120: }
121: }
122:
123: if (printEditLink) {
124: writer.write("\n<br/><a href=\""
125: + uriToGetField.getPath()
126: + "?action=editFormInPlace\">Edit</a>");
127: }
128:
129: writer.flush();
130: } catch (IOException e) {
131: throw e;
132: } catch (IllegalArgumentException e) {
133: throw e;
134: } catch (Exception e) {
135: throw new IllegalArgumentException(e.getMessage());
136: }
137: }
138:
139: public String getDescription() {
140: // ex: {printFormField:uri=.|field=name,mail,description|print=label,value,edit}
141: return "Display a node forms fields values.";
142: }
143:
144: public String[] getParamDescription() {
145: return new String[] {
146: "1. (uri=) The uri on which you want to get the form value (Optional, by default=.)",
147: "2. (print=) Print or not the field label and the field value (Optional, by default=label, value, edit)",
148: "3. (field=) Print or not the specified field (Optional, by default will print all)" };
149: }
150: }
|