NetletAdminServiceViewBean.java :  » Portal » Open-Portal » com » sun » portal » netlet » admin » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » netlet » admin » NetletAdminServiceViewBean.java
/** 
 * Copyright 2002 Sun Microsystems, Inc. All 
 * rights reserved. Use of this product is subject 
 * to license terms. Federal Acquisitions: 
 * Commercial Software -- Government Users 
 * Subject to Standard License Terms and 
 * Conditions. 
 * 
 * Sun, Sun Microsystems, the Sun logo, and Sun ONE 
 * are trademarks or registered trademarks of Sun Microsystems, 
 * Inc. in the United States and other countries. 
 *
 * @ Author Bhavanishankar
 */

package com.sun.portal.netlet.admin;

// JDK classes

import java.io.IOException;
import java.io.Serializable;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iplanet.am.console.base.AMMessageViewBean;
import com.iplanet.am.console.base.AMViewBeanBase;
import com.iplanet.am.console.base.model.AMAdminConstants;
import com.iplanet.am.console.base.model.AMConsoleException;
import com.iplanet.am.console.components.view.html.DynamicGUI;
import com.iplanet.am.console.components.view.html.IPlanetButton;
import com.iplanet.am.console.components.view.html.MessageBox;
import com.iplanet.am.console.user.UMCreateSvcTemplateViewBean;
import com.iplanet.jato.model.ModelControlException;
import com.iplanet.jato.view.View;
import com.iplanet.jato.view.ViewBean;
import com.iplanet.jato.view.event.ChildDisplayEvent;
import com.iplanet.jato.view.event.DisplayEvent;
import com.iplanet.jato.view.event.RequestInvocationEvent;
import com.iplanet.jato.view.html.ComboBox;
import com.iplanet.jato.view.html.HiddenField;
import com.iplanet.jato.view.html.OptionList;
import com.iplanet.jato.view.html.StaticTextField;
import com.iplanet.sso.SSOException;
import com.sun.portal.netlet.admin.model.NetletAdminServiceModel;
import com.sun.portal.netlet.admin.model.NetletAdminServiceModelImpl;

public class NetletAdminServiceViewBean extends AMViewBeanBase implements NetletAdminService, AMAdminConstants {
    public static final String PAGE_DESCRIPTION = "serviceDescription";

    public static final String PAGE_NAME = "NetletAdminService";

    public static final String DEFAULT_DISPLAY_URL = "/ps/netletadmin/NetletAdminService.jsp";

    public static final String GLOBAL_LABEL = "lblGlobal";

    public static final String ORG_LABEL = "lblOrg";

    public static final String DYNAMIC_LABEL = "lblDynamic";

    public static final String GLOBAL_DATA_VIEW = "GlobalDataView";

    public static final String ORG_DATA_VIEW = "OrgDataView";

    public static final String DYNAMIC_DATA_VIEW = "DynamicDataView";

    public static final String CHILD_CC_MSGBOX = "ccMessageBox";

    public static final String SUBMIT_BTN = "SubmitButton";

    public static final String RESET_BTN = "ResetButton";

    public static final String DELETE_BUTTON = "DeleteButton";

    public static final String PRIORITY_COMBOBOX = "Priority";

    public static final String PRIORITY_LABEL = "lblPriority";

    public static final String TITLE_HTML_PAGE = "titleHtmlPage";

    public static final String CHILD_TILEDVIEW = "NetletRulesTiledView";

    public static final String CHILD_NAME_LABEL = "nameLabel";

    public static final String CHILD_ACTION_LABEL = "actionLabel";

    public static final String NETLET_RULES_LABEL = "netletRulesLabel";

    public static final String ADD_NETLETRULE_BTN = "addruleButton";

    public static final String DELETE_NETLETRULES_BTN = "delrulesButton";

    public static final String NETLET_RULES_COUNT = "netletRulesCount";

    public static final String NO_RULE_SELECTION = "noRuleSelection";

    public static final String CONFIRM_RULE_DELETE = "confirmRuleDelete";

    // public static final String PAGE_ENCODING = "gx_charset";

    private NetletAdminServiceModel model = null;

    private NetletAdminModelManager modelManager = null;

    private String serviceName = null;

    private String template = null;

    private boolean templateLevel = false;

    public static final String SVC_NAME_ATTR = "com.iplanet.am.console.service.svcName";

    public static final String TEMPLATE_ATTR = "com.iplanet.am.console.service.templateLevel";

    public static final String LOCATION_DN = "com-iplanet-am-console-location-dn";

    public NetletAdminServiceViewBean(String pageName) {
        super(pageName);
        registerChildren();
    }

    public NetletAdminServiceViewBean() {
        super(PAGE_NAME);
        setDefaultDisplayURL(DEFAULT_DISPLAY_URL);
        registerChildren();
    }

    protected void registerChildren() {
        registerChild(PAGE_DESCRIPTION, StaticTextField.class);
        registerChild(GLOBAL_LABEL, StaticTextField.class);
        registerChild(ORG_LABEL, StaticTextField.class);
        registerChild(DYNAMIC_LABEL, StaticTextField.class);
        registerChild(GLOBAL_DATA_VIEW, NetletAdminServiceView.class);
        registerChild(ORG_DATA_VIEW, NetletAdminServiceView.class);
        registerChild(DYNAMIC_DATA_VIEW, NetletAdminServiceView.class);
        registerChild(CHILD_CC_MSGBOX, MessageBox.class);
        registerChild(SUBMIT_BTN, IPlanetButton.class);
        registerChild(RESET_BTN, IPlanetButton.class);
        registerChild(DELETE_BUTTON, IPlanetButton.class);
        registerChild(TITLE_HTML_PAGE, StaticTextField.class);
        registerChild(PRIORITY_COMBOBOX, ComboBox.class);
        registerChild(PRIORITY_LABEL, StaticTextField.class);
        registerChild(CHILD_TILEDVIEW, NetletRulesTiledView.class);
        registerChild(CHILD_NAME_LABEL, StaticTextField.class);
        registerChild(CHILD_ACTION_LABEL, StaticTextField.class);
        registerChild(NETLET_RULES_LABEL, StaticTextField.class);
        registerChild(ADD_NETLETRULE_BTN, IPlanetButton.class);
        registerChild(DELETE_NETLETRULES_BTN, IPlanetButton.class);
        registerChild(NETLET_RULES_COUNT, HiddenField.class);
        registerChild(NO_RULE_SELECTION, StaticTextField.class);
        registerChild(CONFIRM_RULE_DELETE, StaticTextField.class);
        // registerChild(PAGE_ENCODING, StaticTextField.class);
    }

    /**
     * creates child view/component
     * 
     * @param name
     *            of view/component
     * @return child view/component
     */
    protected View createChild(String name) {
        /* get the model manager and model ready */
        getNetletModelMgr();
        View child = null;
        if (name.equals(PAGE_DESCRIPTION)) {
            child = new StaticTextField(this, PAGE_DESCRIPTION, "");
        } else if (name.equals(GLOBAL_LABEL)) {
            child = new StaticTextField(this, GLOBAL_LABEL, "");
        } else if (name.equals(ORG_LABEL)) {
            child = new StaticTextField(this, ORG_LABEL, "");
        } else if (name.equals(DYNAMIC_LABEL)) {
            child = new StaticTextField(this, DYNAMIC_LABEL, "");
        } else if (name.equals(GLOBAL_DATA_VIEW)) {
            child = new NetletAdminServiceView(this, GLOBAL_DATA_VIEW, NetletAdminServiceModel.GLOBAL_TYPE);
        } else if (name.equals(ORG_DATA_VIEW)) {
            child = new NetletAdminServiceView(this, ORG_DATA_VIEW, NetletAdminServiceModel.ORG_TYPE);
        } else if (name.equals(DYNAMIC_DATA_VIEW)) {
            child = new NetletAdminServiceView(this, DYNAMIC_DATA_VIEW, NetletAdminServiceModel.DYNAMIC_TYPE);
        } else if (name.equals(CHILD_TILEDVIEW)) {
            return new NetletRulesTiledView(this, CHILD_TILEDVIEW);
        } else if (name.equals(CHILD_NAME_LABEL)) {
            return new StaticTextField(this, CHILD_NAME_LABEL, modelManager.getString("name.label"));
        } else if (name.equals(CHILD_ACTION_LABEL)) {
            return new StaticTextField(this, CHILD_ACTION_LABEL, modelManager.getString("action.label"));
        } else if (name.equals(NETLET_RULES_LABEL)) {
            int netletrulesIndex = model.getNetletRulesIndex();
            model.setCurrentRow(NetletAdminServiceModel.DYNAMIC_TYPE, netletrulesIndex);
            return new StaticTextField(this, NETLET_RULES_LABEL, model.getDynamicGUI().getLabel() + ":");
        } else if (name.equals(CHILD_CC_MSGBOX)) {
            return new MessageBox(this, CHILD_CC_MSGBOX, "");
        } else if (name.equals(ADD_NETLETRULE_BTN)) {
            child = new IPlanetButton(this, ADD_NETLETRULE_BTN, modelManager.getString("add.button"));
        } else if (name.equals(DELETE_NETLETRULES_BTN)) {
            child = new IPlanetButton(this, DELETE_NETLETRULES_BTN, modelManager.getString("delete.button"));
        } else if (name.equals(SUBMIT_BTN)) {
            child = new IPlanetButton(this, SUBMIT_BTN, "");
        } else if (name.equals(RESET_BTN)) {
            child = new IPlanetButton(this, RESET_BTN, "");
        } else if (name.equals(DELETE_BUTTON)) {
            child = new IPlanetButton(this, DELETE_BUTTON, "");
        } else if (name.equals(PRIORITY_COMBOBOX)) {
            child = new ComboBox(this, PRIORITY_COMBOBOX, "");
        } else if (name.equals(PRIORITY_LABEL)) {
            child = new ComboBox(this, PRIORITY_LABEL, "");
        } else if (name.equals(TITLE_HTML_PAGE)) {
            StaticTextField txtField = new StaticTextField(this, TITLE_HTML_PAGE, modelManager
                    .getString("netlet.admin.title"));
            child = txtField;
        } else if (name.equals(NETLET_RULES_COUNT)) {
            return new HiddenField(this, NETLET_RULES_COUNT, "");
        } else if (name.equals(NO_RULE_SELECTION)) {
            return new StaticTextField(this, NO_RULE_SELECTION, modelManager.getString("noruleselection"));
        } else if (name.equals(CONFIRM_RULE_DELETE)) {
            return new StaticTextField(this, CONFIRM_RULE_DELETE, modelManager.getString("confirmruledelete"));
            /*
             * } else if (name.equals(PAGE_ENCODING)) { child = new
             * StaticTextField(this, PAGE_ENCODING, "");
             */
        } else {
            child = super.createChild(name);
        }
        return child;
    }

    public void beginDisplay(DisplayEvent event) throws ModelControlException {
        getModel();
        setChildValues(model);
        model.process();
        setDisplayFieldValue(PAGE_DESCRIPTION, model.getServiceDisplayName());
        setDisplayFieldValue(GLOBAL_LABEL, modelManager.getString("global"));
        setDisplayFieldValue(ORG_LABEL, modelManager.getString("organization"));
        setDisplayFieldValue(DYNAMIC_LABEL, modelManager.getString("dynamic"));
        setDisplayFieldValue(SUBMIT_BTN, modelManager.getString("save.button"));
        setDisplayFieldValue(RESET_BTN, modelManager.getString("reset.button"));
        setDisplayFieldValue(DELETE_BUTTON, modelManager.getString("delete.button"));
        setDisplayFieldValue(NETLET_RULES_COUNT, model.getNetletRulesCount());
        setDisplayFieldValue(HELP_DOC_URL, modelManager.getString(NetletAdminService.SRA_BASE_HLP_URL));
        setDisplayFieldValue(HELP_ANCHOR_TAG, model.getHelpAnchorTag(NetletAdminService.SRA_NL_HLP_URL));

        // setPageEncoding();
    }

    public void passPgSessionMap(ViewBean other) {
        Map attributes = getPageSessionAttributes();
        if ((attributes != null) && (attributes.size() > 0)) {
            Iterator iter = attributes.keySet().iterator();
            while (iter.hasNext()) {
                String key = (String) iter.next();
                other.setPageSessionAttribute(key, (Serializable) attributes.get(key));
            }
        }
    }

    public NetletRulesTiledView getNetletRulesTiledView() {
        return (NetletRulesTiledView) getChild(CHILD_TILEDVIEW);
    }

    public boolean beginGlobalBlockDisplay(ChildDisplayEvent event) {
        return (getModel().getSize(NetletAdminServiceModel.GLOBAL_TYPE) != 0);
    }

    public boolean beginOrgBlockDisplay(ChildDisplayEvent event) {
        return (getModel().getSize(NetletAdminServiceModel.ORG_TYPE) != 0);
    }

    public boolean beginTopLevelBlockDisplay(ChildDisplayEvent event) {
        return (!templateLevel && getModel().getSize(NetletAdminServiceModel.DYNAMIC_TYPE) != 0);
    }

    public boolean beginTemplateLevelBlockDisplay(ChildDisplayEvent event) {
        if (!templateLevel) {
            return false;
        } else {
            NetletAdminServiceModel m = getModel();
            boolean visible = (m.getSize(NetletAdminServiceModel.DYNAMIC_TYPE) != 0);
            if (visible) {
                setDisplayFieldValue(PRIORITY_LABEL, modelManager.getString("priority.label"));
                String[][] options = m.getPriorityOptions();
                OptionList optionList = new OptionList(options[0], options[1]);
                ComboBox cb = (ComboBox) getDisplayField(PRIORITY_COMBOBOX);
                cb.setOptions(optionList);
                cb.setValue(m.getPriority());
            }
            return visible;
        }
    }

    public boolean beginDeleteButtonBlockDisplay(ChildDisplayEvent event) {
        return templateLevel;
    }

    public void handleSubmitButtonRequest(RequestInvocationEvent event) throws ModelControlException, SSOException {
        getModel();
        NetletAdminServiceView view = null;
        view = (NetletAdminServiceView) getChild(GLOBAL_DATA_VIEW);
        if (view != null) {
            store(view, NetletAdminServiceModel.GLOBAL_TYPE);
        }
        view = (NetletAdminServiceView) getChild(ORG_DATA_VIEW);
        if (view != null) {
            store(view, NetletAdminServiceModel.ORG_TYPE);
        }
        view = (NetletAdminServiceView) getChild(DYNAMIC_DATA_VIEW);
        if (view != null) {
            store(view, NetletAdminServiceModel.DYNAMIC_TYPE);
        }
        MessageBox msgBox = (MessageBox) getDisplayField(CHILD_CC_MSGBOX);
        msgBox.setTitle(modelManager.getString("servicepropssaved.msg"));
        msgBox.setType(MessageBox.TYPE_INFORMATION);
        msgBox.setVisible(true);
        forwardTo();
    }

    /**
     * Called when Reset button is clicked. Does nothing and refreshes the page.
     */
    public void handleResetButtonRequest(RequestInvocationEvent event) throws ModelControlException {
        redirectToNASURL();
    }

    public void handleDeleteButtonRequest(RequestInvocationEvent event) throws ModelControlException,
            AMConsoleException {
        NetletAdminServiceModel m = getModel();
        if (templateLevel) {
            m.deleteTemplate();
            UMCreateSvcTemplateViewBean vb = (UMCreateSvcTemplateViewBean) getViewBean(UMCreateSvcTemplateViewBean.class);
            passPgSessionMap(vb);
            vb.setServiceName(m.getServiceName());
            vb.forwardTo(getRequestContext());
        } else {
            forwardTo();
        }
    }

    public void handleAddruleButtonRequest(RequestInvocationEvent event) throws ModelControlException {
        modelManager.setCurrentNetletRulesRow(-1);
        EditNetletRuleViewBean vb = (EditNetletRuleViewBean) getViewBean(EditNetletRuleViewBean.class);
        passPgSessionMap(vb);
        vb.forwardTo(getRequestContext());
    }

    public void handleDelrulesButtonRequest(RequestInvocationEvent event) throws ModelControlException {

        NetletRulesTiledView tileView = getNetletRulesTiledView();
        tileView.getPrimaryModel().setSize(tileView.getNetletRulesCount());
        List selections = new LinkedList();
        while (tileView.nextTile()) {
            String value = (String) tileView.getDisplayFieldValue(NetletRulesTiledView.CHILD_CB_SELECT_RULE);
            if (value.equals("true")) {
                value = (String) tileView.getDisplayFieldValue(NetletRulesTiledView.CHILD_HREF_TEXT);
                selections.add(value);
            }
        }

        if (selections.size() == 0) {
            getNetletModelMgr();
            MessageBox msgBox = (MessageBox) getDisplayField(CHILD_CC_MSGBOX);
            msgBox.setTitle(modelManager.getString("noselectionmsgbox.title"));
            msgBox.setMessage(modelManager.getString("noruleselection"));
            msgBox.setType(MessageBox.TYPE_ERROR);
            msgBox.setVisible(true);
            /* Redisplay the page with the error */
            forwardTo();
            return;
        } else if (selections.size() > 0) {
            Vector netletRules = tileView.getNetletRules();
            int nrSize = netletRules.size();
            if (netletRules != null) {
                for (int i = 0; i < selections.size(); i++) {
                    for (int j = 0; j < netletRules.size(); j++) {
                        StringTokenizer st1 = new StringTokenizer((String) netletRules.get(j),
                                EditNetletRuleViewBean.RULE_DELIM);
                        if (st1.hasMoreTokens())
                            st1 = new StringTokenizer(st1.nextToken(), EditNetletRuleViewBean.ALGO_DELIM);
                        if (st1.hasMoreTokens()) {
                            String rulename = st1.nextToken();
                            if (rulename.equals(selections.get(i))) {
                                netletRules.remove(j);
                                --j;
                            }
                        }
                    }
                }
            }
            Set newRuleSet = new HashSet(netletRules);
            Map newMap = new HashMap();
            newMap.put("sunPortalNetletRules", newRuleSet);
            getModel();
            model.store(NetletAdminServiceModel.DYNAMIC_TYPE, newMap);
            redirectToNASURL();
        }
    }

    private void store(NetletAdminServiceView view, int type) throws SSOException {
        NetletAdminServiceModel m = getModel();
        m.process();
        Map map = new HashMap(10);
        List dynComps = view.getDynamicCompList(m.getNetletRulesIndex());
        if (dynComps != null && !dynComps.isEmpty()) {
            Iterator it = dynComps.iterator();
            while (it.hasNext()) {
                DynamicGUI dGui = (DynamicGUI) it.next();
                if (dGui != null) {
                    Set dgValues = dGui.getValues();
                    if (dGui.getSyntax() == DynamicGUI.SYNTAX_DATE) {
                        try {
                            dgValues = m.getDateInDefaultLocale(dgValues);
                        } catch (AMConsoleException ace) {
                            NetletAdminModelManager.debugError("Error in converting value to default locale : " + ace);
                            // showMessage(MessageBox.TYPE_ERROR,
                            // m.getErrorTitle(),
                            // ace.getMessage());
                            continue;
                        }
                    }
                    // map.put(dGui.getName(), dGui.getValues());

                    map.put(dGui.getName(), dgValues);

                }
            }
        }
        if (type == NetletAdminServiceModel.DYNAMIC_TYPE) {
            String priority = (String) getDisplayFieldValue(PRIORITY_COMBOBOX);
            if (!"".equals(priority))
                map.put("priority", priority);
        }

        if (!map.isEmpty()) {
            // TOFIX: enclose in a try-catch and handle errors
            m.store(type, map);
        }
    }

    public NetletAdminModelManager getNetletModelMgr() {
        if (modelManager == null) {
            modelManager = (NetletAdminModelManager) getRequestContext().getModelManager();
        }
        return modelManager;
    }

    public String getServiceName() {
        if (serviceName == null) {
            HttpServletRequest req = getRequestContext().getRequest();
            serviceName = req.getParameter("ServiceName");
            if (serviceName == null) {
                serviceName = (String) getPageSessionAttribute(SVC_NAME_ATTR);
            } else {
                setPageSessionAttribute(SVC_NAME_ATTR, serviceName);
            }
        }
        return serviceName;
    }

    public boolean isTemplate() {

        if (template == null) {
            HttpServletRequest req = getRequestContext().getRequest();
            template = req.getParameter("Template");
            if (template == null) {
                template = (String) getPageSessionAttribute(TEMPLATE_ATTR);
            } else {
                setPageSessionAttribute(TEMPLATE_ATTR, template);
            }
            if (template != null && template.equals("true")) {
                templateLevel = true;
            } else {
                template = "false";
                templateLevel = false;
            }
        }
        return templateLevel;
    }

    public String getLocationDN() {
        String locationDN = null;
        HttpServletRequest req = getRequestContext().getRequest();
        String encodedDN = req.getParameter("Location");
        if (encodedDN == null) {
            locationDN = (String) getPageSessionAttribute(LOCATION_DN);
        } else {
            try {
                locationDN = URLDecoder.decode(encodedDN);
                setPageSessionAttribute(LOCATION_DN, locationDN);
            } catch (Exception e) {
                // Too bad
            }
        }
        return locationDN;
    }

    public NetletAdminServiceModel getModel() {

        if (model == null) {
            HttpServletRequest req = getRequestContext().getRequest();
            req.setAttribute(NetletAdminModelManager.SRAP_NETLET_MODEL_MGR_KEY, getNetletModelMgr());
            /*
             * Get all request parameters and set them as pageSessionAttributes
             */
            String locationDN = getLocationDN();
            String svcName = getServiceName();
            boolean isTemplateLevel = isTemplate();

            model = new NetletAdminServiceModelImpl(req, "srapnetletadminmsgs", getPageSessionAttributes(), svcName,
                    isTemplateLevel);

            /**
             * Chech whether the template exists or not. If not forward the
             * request to UMCreateSvcTemplateViewBean
             */
            model.process();

            if (!model.hasSchema()) {
                AMMessageViewBean vb = (AMMessageViewBean) getViewBean(AMMessageViewBean.class);
                vb.setMessageType(MessageBox.TYPE_INFORMATION);
                // vb.setTitle(model.getNoAttrsMessage());
                vb.setTitle(modelManager.getString("noSchema.message"));
                vb.setMessage("");
                vb.addButton(model.getLocalizedString("ok.button"), "../console/html/blank.html");
                vb.forwardTo(getRequestContext());
            } else if (templateLevel && !model.hasTemplate()) {
                UMCreateSvcTemplateViewBean vb = (UMCreateSvcTemplateViewBean) getViewBean(UMCreateSvcTemplateViewBean.class);
                passPgSessionMap(vb);
                vb.setServiceName(svcName);
                vb.forwardTo(getRequestContext());
            }
        }
        return model;
    }

    public String getNetletAdminServiceURL() {
        String nasURL = null;
        getModel();
        if (model != null) {
            nasURL = model.getPropsUrl();
            String queryParam = "?ServiceName=" + getPageSessionAttribute(NetletAdminServiceViewBean.SVC_NAME_ATTR)
                    + "&Location="
                    + URLEncoder.encode((String) getPageSessionAttribute(NetletAdminServiceViewBean.LOCATION_DN))
                    + "&Template=" + getPageSessionAttribute(NetletAdminServiceViewBean.TEMPLATE_ATTR);
            nasURL += queryParam;
        }
        return nasURL;
    }

    public void redirectToNASURL() {
        String nasURL = getNetletAdminServiceURL();
        HttpServletResponse response = getRequestContext().getResponse();
        try {
            response.sendRedirect(nasURL);
        } catch (IOException ioe) {
            forwardTo();
        }
    }

    /*
     * private void setPageEncoding () { getModel(); HttpServletRequest req =
     * getRequestContext().getRequest(); HttpServletResponse res =
     * getRequestContext().getResponse(); Locale locale = model.getUserLocale();
     * String agentType = model.getClientType(); String contentType =
     * model.getContentType(agentType); String charset =
     * model.getCharset(agentType,locale); res.setContentType(contentType +
     * ";charset=" + charset); String jCharset =
     * BrowserEncoding.mapHttp2JavaCharset (charset);
     * setDisplayFieldValue(PAGE_ENCODING, jCharset); }
     */

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.