001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.presentation.portlet.widgets.controller;
051:
052: import org.jaffa.presentation.portlet.widgets.model.GridModel;
053: import org.jaffa.presentation.portlet.widgets.model.GridModelRow;
054: import org.apache.log4j.Logger;
055: import java.util.*;
056: import java.io.*;
057: import org.jdom.input.*;
058: import org.jdom.*;
059: import org.jdom.output.*;
060: import org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException;
061: import org.jaffa.presentation.portlet.widgets.controller.EditBoxController;
062: import org.jaffa.presentation.portlet.widgets.model.EditBoxModel;
063: import org.jaffa.presentation.portlet.widgets.model.DateTimeModel;
064: import org.jaffa.presentation.portlet.widgets.model.CheckBoxModel;
065: import org.jaffa.presentation.portlet.widgets.model.DropDownModel;
066:
067: /** Controller for the Grid widget.
068: */
069: public class GridController {
070:
071: private static Logger log = Logger.getLogger(GridController.class);
072:
073: // These constants are used for creating a well-formed XML
074: private static final String XML_START = "<?xml version=\"1.0\"?><root>";
075: private static final String XML_END = "</root>";
076:
077: // These constants are used for accessing the XML elements
078: private static final String XML_WIDGET = "widget";
079: private static final String XML_ROW = "row";
080: private static final String XML_FIELD = "field";
081:
082: /** Updates the model with the input value.
083: * This will throw the XmlStructureRuntimeException, in case the input is not well-formed.
084: * @param value The new value for the model.
085: * @param model The model to be updated.
086: */
087: public static void updateModel(String value, GridModel model) {
088: // get the root element for the input XML
089: Element root = getRootElement(value);
090:
091: // iterate through each element
092: for (Iterator it = root.getChildren(XML_WIDGET).iterator(); it
093: .hasNext();) {
094: Element element = (Element) it.next();
095: int rowId = Integer.parseInt(element.getAttribute(XML_ROW)
096: .getValue());
097: String field = element.getAttribute(XML_FIELD).getValue();
098: String contents = getContents(element);
099: GridModelRow row = model.getRowById(rowId);
100: Object innerModel = row.getElement(field);
101: if (innerModel != null)
102: interpretAndUpdateModel(contents, innerModel);
103: }
104: }
105:
106: private static Element getRootElement(String xmlIn) {
107: // Convert to valid XML
108: String xml = XML_START + (xmlIn == null ? "" : xmlIn) + XML_END;
109:
110: // Load the input into JDOM
111: Document doc = null;
112: try {
113: SAXBuilder builder = new SAXBuilder();
114: doc = builder.build(new BufferedReader(
115: new StringReader(xml)));
116: } catch (org.jdom.JDOMException e) {
117: String str = "Invalid Packed Data From Widget. Can't Load into XML\n"
118: + "JDOM Reason : "
119: + e.getMessage()
120: + '\n'
121: + "XML Data was : " + xml;
122: log.error(str, e);
123: throw new XmlStructureRuntimeException(str, e);
124: }
125:
126: return doc.getRootElement();
127: }
128:
129: private static String getContents(Element element) {
130: String contents = null;
131:
132: // if this element has inner tags, then get the corresponding XML fragment
133: if (element.hasChildren()) {
134: try {
135: XMLOutputter xout = new XMLOutputter();
136: StringWriter sw = new StringWriter();
137: xout.outputElementContent(element, sw);
138: contents = sw.getBuffer().toString();
139: } catch (IOException e) {
140: String str = "IOException thrown while writing the XML to a String";
141: log.error(str, e);
142: throw new XmlStructureRuntimeException(str, e);
143: }
144: } else {
145: contents = element.getTextTrim();
146: }
147:
148: return contents;
149: }
150:
151: private static void interpretAndUpdateModel(String contents,
152: Object model) {
153: // @todo : factor in all the supported models
154: if (model instanceof EditBoxModel) {
155: EditBoxController.updateModel(contents,
156: (EditBoxModel) model);
157: } else if (model instanceof DateTimeModel) {
158: DateTimeController.updateModel(contents,
159: (DateTimeModel) model);
160: } else if (model instanceof CheckBoxModel) {
161: CheckBoxController.updateModel(contents,
162: (CheckBoxModel) model);
163: } else if (model instanceof DropDownModel) {
164: DropDownController.updateModel(contents,
165: (DropDownModel) model);
166: } else {
167: if (log.isDebugEnabled())
168: log
169: .debug("UnSupported model retrieved from a GridModelRow '"
170: + model.getClass()
171: + "'... Cannot be updated");
172: }
173: }
174:
175: }
|