Search in sources :

Example 1 with ObjectFactory

use of org.jaffa.presentation.portlet.widgets.controller.usergriddomain.ObjectFactory 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);
    }
}
Also used : XmlStructureRuntimeException(org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException) ObjectFactory(org.jaffa.presentation.portlet.widgets.controller.usergriddomain.ObjectFactory) UserGridSettings(org.jaffa.presentation.portlet.widgets.controller.usergriddomain.UserGridSettings) GridModelRow(org.jaffa.presentation.portlet.widgets.model.GridModelRow) UserGridColumnSettings(org.jaffa.presentation.portlet.widgets.controller.usergriddomain.UserGridColumnSettings)

Aggregations

XmlStructureRuntimeException (org.jaffa.presentation.portlet.widgets.controller.exceptions.XmlStructureRuntimeException)1 ObjectFactory (org.jaffa.presentation.portlet.widgets.controller.usergriddomain.ObjectFactory)1 UserGridColumnSettings (org.jaffa.presentation.portlet.widgets.controller.usergriddomain.UserGridColumnSettings)1 UserGridSettings (org.jaffa.presentation.portlet.widgets.controller.usergriddomain.UserGridSettings)1 GridModelRow (org.jaffa.presentation.portlet.widgets.model.GridModelRow)1