actionmapping.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,000 行 · 第 1/2 页

JAVA
1,000
字号
                break;
            attValue = elem.getAttributeValue(attName);
            if (attValue != null) {
                factory.setValue (instance, attName, attValue);
            }
        }
        
        return (instance);
    }
 */
    
    private Object deleteActionMapping (ActionMappingForm form) {
        JDOMAdapter adapter = new JDOMAdapter ();
        JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
        JtMessage msg1 = new JtMessage (JDOMAdapter.SAVE_FILE);
        JtFactory factory = new JtFactory ();
        Element elem;
        List children;
        int i;
        String tmp;
        String actionPath;
                
        if (form == null)
            return null;
        
        actionPath = form.getPath();
        
        if (actionPath == null)
            return null;
        
        
        config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
        
        if (config == null) {
            handleError ("Unable to create configuration class");
            return (null);
        }
        
        factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));

        
        adapter.setPath(config.getConfigPath());  
        
        msg.setMsgContent("struts-config/action-mappings");
        //adapter.setPath("c:/tmp/test.xml");
               
        factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
        
        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            return (null);
        } 
        
        children = (List) factory.sendMessage (adapter, msg);
        if (children == null)
            return (null);
        
        
        for (i = 0; i < children.size(); i++) {
            elem = (Element) children.get(i);
            
            tmp = elem.getAttributeValue("path");
            
            if (actionPath.equals(tmp)) {

                elem.getParentElement();
                elem.detach ();
                factory.sendMessage (adapter, msg1);
                return (null);               
                
            }
        }        
       
        return (null);
    }
    
    private Object createActionMapping (ActionMappingForm form) {
        JDOMAdapter adapter = new JDOMAdapter ();
        JtMessage msg1 = new JtMessage (JDOMAdapter.SAVE_FILE);
        JtMessage msg2 = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        JtFactory factory = new JtFactory ();
        Element newElem;
        //Object obj;
        String actionPath;
                
        if (form == null)
            return null;
        
        actionPath = form.getPath();
        
        if (actionPath == null)
            return null;
        
        
        
        if (findActionMapping (actionPath) != null) {
            handleUIError ("Action Mapping already exists");
            return null;
        }       
                

        newElem = (Element) buildActionMapping (form);

        if (newElem == null) {
            handleError ("Unable to create the Action Mapping.");
            return (null);
        }
        
        config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
        
        if (config == null) {
            handleError ("Unable to create configuration class");
            return (null);
        }
        
        factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
       
        adapter.setPath(config.getConfigPath());       
        
        //adapter.setPath("c:/tmp/test.xml");
        factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
        
        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            return (null);
        } 
        
        msg2.setMsgData("struts-config/action-mappings");
        msg2.setMsgContent(newElem);
        newElem.detach ();
        factory.sendMessage (adapter, msg2);

        factory.sendMessage (adapter, msg1);
        return (null);               

    }   
    
 
    private Object updateActionMapping (ActionMappingForm form) {
        JDOMAdapter adapter = new JDOMAdapter ();
        JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
        JtMessage msg1 = new JtMessage (JDOMAdapter.SAVE_FILE);
        JtMessage msg2 = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        JtFactory factory = new JtFactory ();
        Element elem, newElem;
        //ActionMappingForm actionMappingForm = new ActionMappingForm ();
        //Object obj;
        List children;
        int i;
        String tmp;
        String actionPath;
                
        if (form == null)
            return null;
        
        actionPath = form.getPath();
        
        if (actionPath == null)
            return null;
        
        //msg.setMsgContent("struts-config/action-mappings");
        config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
        
        if (config == null) {
            handleError ("Unable to create configuration class");
            return (null);
        }
        
        factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));
        
        msg.setMsgContent("struts-config/action-mappings");
        //adapter.setPath("c:/tmp/test.xml");
        adapter.setPath(config.getConfigPath());
               
        factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
        
        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            return (null);
        } 
        
        children = (List) factory.sendMessage (adapter, msg);
        if (children == null)
            return (null);
        
        
        for (i = 0; i < children.size(); i++) {
            elem = (Element) children.get(i);
            
            tmp = elem.getAttributeValue("path");
            
            if (actionPath.equals(tmp)) {

                newElem = (Element) buildActionMapping (form);
                
                if (newElem == null) {
                    handleError ("Unable to create the Action Mapping.");
                    return (null);
                }
                msg2.setMsgData("struts-config/action-mappings");
                msg2.setMsgContent(newElem);
                newElem.detach ();
                factory.sendMessage (adapter, msg2);
                
                elem.getParentElement();
                elem.detach ();
                
                
                factory.sendMessage (adapter, msg1);
                return (null);               
                
            }
        }        
       
        return (null);
    } 
    
    
    private void updateForm (ActionMappingForm form, Element elem) {
        List children; 
        int i;
        Element elem1;
        String name;
        
        if (form == null || elem == null)
            return;
        
        if (form.getCancellable() == null)
            form.setCancellable("false");
        
        if (form.getValidate() == null)
            form.setValidate("true");
        
        children = elem.getChildren();
        
        if (children == null)
            return;
        
        for (i = 0; i < children.size(); i++) {
            elem1 = (Element) children.get(i);
            
            if ("forward".equals (elem1.getName())) {
                name = elem1.getAttributeValue("name");
                
                if ("success".equals(name)) {
                    form.setForwardSuccess(elem1.getAttributeValue("path"));
                } else 
                    if ("failure".equals(name)) {
                        form.setForwardFailure(elem1.getAttributeValue("path"));
                    } else
                        if ("cancel".equals(name)) {
                            form.setForwardCancel(elem1.getAttributeValue("path"));
                        }
                
            }
            
        }
        
    }
 
    
    private void updateBean (ActionMapping actionMapping, Element elem) {
        List children; 
        int i;
        Element elem1;
        String name;
        
        if (actionMapping == null || elem == null)
            return;
        
        children = elem.getChildren();
        
        if (children == null)
            return;
        
        for (i = 0; i < children.size(); i++) {
            elem1 = (Element) children.get(i);
            
            if ("forward".equals (elem1.getName())) {
                name = elem1.getAttributeValue("name");
                
                if ("success".equals(name)) {
                    actionMapping.setForwardSuccess(elem1.getAttributeValue("path"));
                } else 
                    if ("failure".equals(name)) {
                        actionMapping.setForwardFailure(elem1.getAttributeValue("path"));
                    } else
                        if ("cancel".equals(name)) {
                            actionMapping.setForwardCancel(elem1.getAttributeValue("path"));
                        }
                
                
            }
            
        }
        
    }
    
    private Object getActionMapping (String actionPath) {
        JDOMAdapter adapter = new JDOMAdapter ();
        JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
        JtMessage msg1 = new JtMessage (JDOMAdapter.ELEMENT_TO_BEAN);
        JtFactory factory = new JtFactory ();
        Element elem;
        ActionMappingForm actionMappingForm = new ActionMappingForm ();
        Object obj;
        List children;
        int i;
        String tmp;
        
        
        if (actionPath == null)
            return null;
        
        config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
        
        if (config == null) {
            handleError ("Unable to create configuration class");
            return (null);
        }
        factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));

        msg.setMsgContent("struts-config/action-mappings");
        //adapter.setPath("c:/tmp/test.xml");
        adapter.setPath(config.getConfigPath());
               
        factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
        
        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            return (null);
        } 
        
        children = (List) factory.sendMessage (adapter, msg);
        if (children == null)
            return (null);
        
        
        for (i = 0; i < children.size(); i++) {
            elem = (Element) children.get(i);
            
            tmp = elem.getAttributeValue("path");
            
            if (actionPath.equals(tmp)) {

                msg1.setMsgContent(elem);
                msg1.setMsgData(actionMappingForm);
                obj = adapter.processMessage(msg1);
                
                updateForm ((ActionMappingForm) obj, elem);
                
                return (obj);
            }
        }        
       
        return (null);
    }
    
    
    private Object getActionMappings () {
        JDOMAdapter adapter = new JDOMAdapter ();
        JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
        JtMessage msg1 = new JtMessage (JDOMAdapter.ELEMENT_TO_BEAN);
        JtFactory factory = new JtFactory ();
        List children;
        Element elem;
        ActionMapping actionMapping = new ActionMapping ();
        List list = new LinkedList ();
        Object obj;
        
        
        int i;
        
        msg.setMsgContent("struts-config/action-mappings");
        
        config = (WizardConfig) factory.createObject(WizardConfig.JtCLASS_NAME);
        
        if (config == null) {
            handleError ("Unable to create configuration class");
            return (null);
        }
        
        factory.sendMessage(config, new JtMessage (JtObject.JtINITIALIZE));

        //adapter.setPath("c:/tmp/test.xml");
        
        adapter.setPath(config.getConfigPath());
        
        factory.sendMessage (adapter, new JtMessage (JDOMAdapter.READ_FILE));
        
        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            return (null);
        }    
        
        children = (List) factory.sendMessage (adapter, msg);
        if (children == null)
            return (null);
        
        
        for (i = 0; i < children.size(); i++) {
            elem = (Element) children.get(i);
            
            msg1.setMsgContent(elem);
            msg1.setMsgData(actionMapping);
            obj = adapter.processMessage(msg1);
            updateBean ((ActionMapping) obj, elem);
            if (obj != null)
                list.add(obj);
        }
        
        return (list);
    }
    
    
    /**
     * Process object messages.
     * <ul>
     * <li> JtACTIVATE
     * </ul>
     * @param message message
     */

    public Object processMessage (Object message) {
        Object data;
        String content;
        JtContext context;
        ActionForm form = null;

        JtMessage e = (JtMessage) message;

        if (e == null ||  (e.getMsgId() == null))
            return (null);
        
        
        context = (JtContext) e.getMsgContext();
        if (context != null)
            form = (ActionForm) context.getActionForm();  


        if (e.getMsgId().equals(JtObject.JtACTIVATE)) {

            // pass the form information   
            data = e.getMsgData ();
            return ( getActionMappings ());
        }
        
        
        if (e.getMsgId().equals(ActionMapping.READ)) {
 
            content = (String) e.getMsgContent ();
            return ( getActionMapping (content));
        }
        
        if (e.getMsgId().equals(ActionMapping.DELETE)) {
            
            //content = (String) e.getMsgContent ();
            //data = e.getMsgData ();
            return ( deleteActionMapping ((ActionMappingForm) form));
        }
        
        if (e.getMsgId().equals(ActionMapping.UPDATE)) {
            
            //content = (String) e.getMsgContent ();
            //data = e.getMsgData ();
            return ( updateActionMapping ((ActionMappingForm) form));
        }
        
        
        if (e.getMsgId().equals(ActionMapping.CREATE)) {
            
            //content = (String) e.getMsgContent ();
            //data = e.getMsgData ();
            return ( createActionMapping ((ActionMappingForm) form));
        }

        return (super.processMessage(message));

    }
    /**
     * Unit tests the messages processed by this class
     */

    public static void main(String[] args) {

        JtObject factory = new JtFactory ();
        ActionMapping actionMapping = new ActionMapping ();
        JtMessage msg;
        List list;


        msg = new JtMessage (JtObject.JtACTIVATE);          
        
        list = (List) factory.sendMessage (actionMapping, msg);
        System.out.println ("List=" + list);


    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?