use of org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException in project jaffa-framework by jaffa-projects.
the class GridController method updateModel.
/**
* Updates the model with the input value.
* This will throw the XmlStructureRuntimeException, in case the input is not well-formed.
* @param value The new value for the model.
* @param model The model to be updated.
*/
public static void updateModel(String value, GridModel model) {
try {
String user = null;
String gridId = null;
String tableWidth = null;
List listSettings = new ArrayList();
// get the root element for the input XML
Element root = getRootElement(value);
// iterate through each element in the Grid, and update there model
for (Iterator it = root.getChildren(XML_WIDGET).iterator(); it.hasNext(); ) {
Element element = (Element) it.next();
int rowId = Integer.parseInt(element.getAttribute(XML_ROW).getValue());
String field = element.getAttribute(XML_FIELD).getValue();
String contents = getContents(element);
GridModelRow row = model.getRowById(rowId);
Object innerModel = row.get(field);
if (innerModel != null) {
interpretAndUpdateModel(contents, innerModel);
}
}
// Gets the Setting info posted from the JSP
// This is only posted when the Grid's Layout is being modified
Element element = root.getChild(XML_SETTINGS);
if (element != null) {
// no longer expect the user to be passed as this could be spoofed, get it from the security manager
if (SecurityManager.getPrincipal() != null) {
user = SecurityManager.getPrincipal().getName();
} else {
// Error can't save settings if no user is set
log.error(" Can't save user settings: User unknown or not set by SecurityManager.");
model.setErrorInSavingUserSettings(true);
return;
}
gridId = element.getChildText(XML_GRIDID);
log.debug("Grid id=" + gridId + " for user=" + user + " is being modified");
// This means delete there customized version
if (element.getChild(XML_RESTORE) != null) {
// Delete custom settings
model.setErrorInSavingUserSettings(!UserGridManager.restore(user, gridId));
log.debug("Grid id=" + gridId + " for user=" + user + " has been reset to the default");
} else {
tableWidth = element.getChildText(XML_TABLEWIDTH);
// create an ObjectFactory instance.
ObjectFactory objFactory = new ObjectFactory();
UserGridSettings thing = objFactory.createUserGridSettings();
List cols = thing.getUserGridColumnSettings();
// And passes that list to the UserGridManager setColSettings()
if (log.isDebugEnabled()) {
log.debug("Saving Grid Column Listing for User=" + user + ", GridId=" + gridId);
}
for (Iterator it = element.getChildren(XML_COLUMN).iterator(); it.hasNext(); ) {
Element column = (Element) it.next();
String name = column.getAttribute(XML_NAME).getValue();
String width = column.getAttribute(XML_WIDTH).getValue();
UserGridColumnSettings thing2 = objFactory.createUserGridColumnSettings();
thing2.setName(name);
thing2.setWidth(width);
cols.add(thing2);
if (log.isDebugEnabled()) {
log.debug(" show column = " + name + ", width=" + width);
}
}
// Sets the value of the Outer Table Width
thing.setGridWidth(tableWidth);
// Save settings to xml file
// Raise the error flag in the model, in case the 'save' fails
// The UserGridTag will raise an Error, in case the error-flag is set, and then reset the error-flag
model.setErrorInSavingUserSettings(!UserGridManager.setColSettings(user, gridId, thing));
}
// See if the user whats to toggle the popup hints.
String hints = element.getChildText(XML_HINTS);
if (hints != null) {
try {
log.debug("Current Rule " + GridTag.RULE_USERGRID_POPUP + " = " + ContextManagerFactory.instance().getProperty(GridTag.RULE_USERGRID_POPUP));
ContextManagerFactory.instance().setUserPreference(GridTag.RULE_USERGRID_POPUP, hints);
log.debug("New Rule " + GridTag.RULE_USERGRID_POPUP + " = " + ContextManagerFactory.instance().getProperty(GridTag.RULE_USERGRID_POPUP));
} catch (IOException e) {
log.error("Failed to changed user preferences for " + GridTag.RULE_USERGRID_POPUP, e);
}
}
}
// Process any state related to the Tree Widget
for (Iterator it = root.getChildren(XML_DISPLAY).iterator(); it.hasNext(); ) {
Element data = (Element) it.next();
int rowId = Integer.parseInt(data.getAttribute(XML_ROW).getValue());
Boolean expanded = new Boolean(data.getAttribute(XML_EXPANDED).getValue());
String contents = getContents(data);
GridModelRow row = model.getRow(rowId);
row.addElement("isDisplayed", new Boolean(contents));
row.addElement("isExpanded", expanded);
}
} catch (Exception e) {
log.error("Failed to create Java content objects for marshalling into XML", e);
model.setErrorInSavingUserSettings(true);
}
}
use of org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException in project jaffa-framework by jaffa-projects.
the class GridController method getContents.
private static String getContents(Element element) {
String contents = null;
// if this element has inner tags, then get the corresponding XML fragment
if (!element.getChildren().isEmpty()) {
try {
XMLOutputter xout = new XMLOutputter();
StringWriter sw = new StringWriter();
xout.outputElementContent(element, sw);
contents = sw.getBuffer().toString();
} catch (IOException e) {
String str = "IOException thrown while writing the XML to a String";
log.error(str, e);
throw new XmlStructureRuntimeException(str, e);
}
} else {
contents = element.getTextTrim();
}
// header.js converts # to %23 when packaging the data. Lets revert that change
return contents != null ? contents.replaceAll("%23", "#") : null;
}
use of org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException in project jaffa-framework by jaffa-projects.
the class GridController method getRootElement.
private static Element getRootElement(String xmlIn) {
// Convert to valid XML
String xml = XML_START + (xmlIn == null ? "" : xmlIn) + XML_END;
// Load the input into JDOM
Document doc = null;
try {
SAXBuilder builder = new SAXBuilder();
doc = builder.build(new BufferedReader(new StringReader(xml)));
} catch (org.jdom.JDOMException e) {
String str = "Invalid Packed Data From Widget. Can't Load into XML\n" + "JDOM Reason : " + e.getMessage() + '\n' + "XML Data was : " + xml;
log.error(str, e);
throw new XmlStructureRuntimeException(str, e);
} catch (IOException e) {
String str = "IOException thrown while writing the XML to a String";
log.error(str, e);
throw new XmlStructureRuntimeException(str, e);
}
return doc.getRootElement();
}
Aggregations