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.controller.usergriddomain.UserGridSettings;
053: import org.jaffa.presentation.portlet.widgets.controller.usergriddomain.UserGridColumnSettings;
054: import org.jaffa.presentation.portlet.widgets.model.GridModel;
055: import org.jaffa.presentation.portlet.widgets.model.GridModelRow;
056: import org.apache.log4j.Logger;
057: import java.util.*;
058: import java.io.*;
059: import org.jdom.input.*;
060: import org.jdom.*;
061: import org.jdom.output.*;
062: import org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException;
063: import org.jaffa.presentation.portlet.widgets.controller.EditBoxController;
064: import org.jaffa.presentation.portlet.widgets.model.EditBoxModel;
065: import org.jaffa.presentation.portlet.widgets.model.DateTimeModel;
066: import org.jaffa.presentation.portlet.widgets.model.CheckBoxModel;
067: import org.jaffa.presentation.portlet.widgets.model.DropDownModel;
068: import org.jaffa.presentation.portlet.session.UserSession;
069: import javax.servlet.http.HttpServletRequest;
070: import org.jaffa.presentation.portlet.FormBase;
071: import org.jaffa.presentation.portlet.widgets.controller.usergriddomain.ObjectFactory;
072: import javax.xml.bind.JAXBException;
073:
074: /** Controller for the Grid widget.
075: */
076: public class UserGridController {
077:
078: private static Logger log = Logger
079: .getLogger(UserGridController.class);
080:
081: // These constants are used for creating a well-formed XML
082: private static final String XML_START = "<?xml version=\"1.0\"?><root>";
083: private static final String XML_END = "</root>";
084:
085: // These constants are used for accessing the XML elements
086: private static final String XML_WIDGET = "widget";
087: private static final String XML_ROW = "row";
088: private static final String XML_FIELD = "field";
089: private static final String XML_COLUMN = "column";
090: private static final String XML_NAME = "name";
091: private static final String XML_WIDTH = "width";
092: private static final String XML_USER = "user";
093: private static final String XML_GRIDID = "gridid";
094: private static final String XML_SETTINGS = "settings";
095: private static final String XML_TABLEWIDTH = "tableWidth";
096: private static final String XML_RESTORE = "restore";
097:
098: /** Updates the model with the input value.
099: * This will throw the XmlStructureRuntimeException, in case the input is not well-formed.
100: * @param value The new value for the model.
101: * @param model The model to be updated.
102: */
103: public static void updateModel(String value, GridModel model,
104: FormBase form) {
105: try {
106: String user = null;
107: String gridId = null;
108: String tableWidth = null;
109: List listSettings = new ArrayList();
110: // get the root element for the input XML
111: Element root = getRootElement(value);
112:
113: // iterate through each element in the Grid, and update there model
114: for (Iterator it = root.getChildren(XML_WIDGET).iterator(); it
115: .hasNext();) {
116: Element element = (Element) it.next();
117: int rowId = Integer.parseInt(element.getAttribute(
118: XML_ROW).getValue());
119: String field = element.getAttribute(XML_FIELD)
120: .getValue();
121: String contents = getContents(element);
122: if (log.isDebugEnabled())
123: log.debug(contents);
124: GridModelRow row = model.getRowById(rowId);
125: Object innerModel = row.getElement(field);
126: if (innerModel != null)
127: interpretAndUpdateModel(contents, innerModel);
128: }
129:
130: // Gets the Setting info posted from the JSP
131: // This is only posted when the Grid's Layout is being modified
132: Element element = root.getChild(XML_SETTINGS);
133: if (element != null) {
134: user = element.getChildText(XML_USER);
135: gridId = element.getChildText(XML_GRIDID);
136:
137: // See if the user selected to restore the settings
138: // This means delete there customized version
139: if (element.getChild(XML_RESTORE) != null) {
140: // Delete custom settings
141: model.setErrorInSavingUserSettings(!UserGridManager
142: .restore(user, gridId));
143:
144: } else {
145:
146: tableWidth = element.getChildText(XML_TABLEWIDTH);
147:
148: // create an ObjectFactory instance.
149: ObjectFactory objFactory = new ObjectFactory();
150: UserGridSettings thing = objFactory
151: .createUserGridSettings();
152: List cols = thing.getUserGridColumnSettings();
153:
154: //Iterates through all the column settings builds them into a List
155: //And passes that list to the UserGridManager setColSettings()
156: for (Iterator it = element.getChildren(XML_COLUMN)
157: .iterator(); it.hasNext();) {
158: Element column = (Element) it.next();
159: String name = column.getAttribute(XML_NAME)
160: .getValue();
161: String width = column.getAttribute(XML_WIDTH)
162: .getValue();
163: UserGridColumnSettings thing2 = objFactory
164: .createUserGridColumnSettings();
165: thing2.setName(name);
166: thing2.setWidth(width);
167: cols.add(thing2);
168: }
169: //Sets the value of the Outer Table Width
170: thing.setGridWidth(tableWidth);
171:
172: // Save settings to xml file
173: // Raise the error flag in the model, in case the 'save' fails
174: // The UserGridTag will raise an Error, in case the error-flag is set, and then reset the error-flag
175: model.setErrorInSavingUserSettings(!UserGridManager
176: .setColSettings(user, gridId, thing));
177: }
178: }
179: } catch (JAXBException e) {
180: log
181: .error(
182: "Failed to create Java content objects for marshalling into XML",
183: e);
184: model.setErrorInSavingUserSettings(true);
185: }
186:
187: }
188:
189: private static Element getRootElement(String xmlIn) {
190: // Convert to valid XML
191: String xml = XML_START + (xmlIn == null ? "" : xmlIn) + XML_END;
192:
193: // Load the input into JDOM
194: Document doc = null;
195: try {
196: SAXBuilder builder = new SAXBuilder();
197: doc = builder.build(new BufferedReader(
198: new StringReader(xml)));
199: } catch (org.jdom.JDOMException e) {
200: String str = "Invalid Packed Data From Widget. Can't Load into XML\n"
201: + "JDOM Reason : "
202: + e.getMessage()
203: + '\n'
204: + "XML Data was : " + xml;
205: log.error(str, e);
206: throw new XmlStructureRuntimeException(str, e);
207: }
208:
209: return doc.getRootElement();
210: }
211:
212: private static String getContents(Element element) {
213: String contents = null;
214:
215: // if this element has inner tags, then get the corresponding XML fragment
216: if (element.hasChildren()) {
217: try {
218: XMLOutputter xout = new XMLOutputter();
219: StringWriter sw = new StringWriter();
220: xout.outputElementContent(element, sw);
221: contents = sw.getBuffer().toString();
222: } catch (IOException e) {
223: String str = "IOException thrown while writing the XML to a String";
224: log.error(str, e);
225: throw new XmlStructureRuntimeException(str, e);
226: }
227: } else {
228: contents = element.getTextTrim();
229: }
230:
231: return contents;
232: }
233:
234: private static void interpretAndUpdateModel(String contents,
235: Object model) {
236: // @todo : factor in all the supported models
237: if (model instanceof EditBoxModel) {
238: EditBoxController.updateModel(contents,
239: (EditBoxModel) model);
240: } else if (model instanceof DateTimeModel) {
241: DateTimeController.updateModel(contents,
242: (DateTimeModel) model);
243: } else if (model instanceof CheckBoxModel) {
244: CheckBoxController.updateModel(contents,
245: (CheckBoxModel) model);
246: } else if (model instanceof DropDownModel) {
247: DropDownController.updateModel(contents,
248: (DropDownModel) model);
249: } else {
250: if (log.isDebugEnabled())
251: log
252: .debug("UnSupported model retrieved from a GridModelRow '"
253: + model.getClass()
254: + "'... Cannot be updated");
255: }
256: }
257:
258: }
|