formbean.java

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

JAVA
2,094
字号
    }
    
    
    private List lookup () {
        
        JtMessage msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
        JtFactory factory = new JtFactory ();
        List children;
        Element elem;
        String tmp;
        FormBeanForm bean, bean1;
        int i, j;
        LinkedList col = new LinkedList ();
        
        
        //ValidatorlConfigFile confFile = new ValidatorlConfigFile ();
        
        handleTrace ("lookup", 0);
        
        
        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));

        
        jdomAdapter.setPath(config.getConfigPath());
        
        msg.setMsgContent("struts-config/form-beans");
        //adapter.setPath("c:/tmp/test.xml");
               
        factory.sendMessage (jdomAdapter, new JtMessage (JDOMAdapter.READ_FILE));
        
        if (jdomAdapter.getObjException() != null) {
            this.setObjException(jdomAdapter.getObjException());
            return (null);
        } 
        
        
        children = (List) factory.sendMessage (jdomAdapter, msg);
        if (children == null)
            return (null);      
        
        for (i = 0; i < children.size(); i++) {
            
            elem = (Element) children.get(i);
            
            tmp = elem.getAttributeValue("name");
            
            if (tmp == null)
                continue;
            
            bean = new FormBeanForm ();
            bean.setName(tmp);
            
            if (col.isEmpty()) {
                col.add(bean);
                continue;
            }
            for (j = 0; j < col.size(); j++) {
                bean1 = (FormBeanForm) col.get(j);
                if (bean.getName().compareTo(bean1.getName()) <= 0)
                    break;
            }
            
            col.add(j, bean);
        }    
        
        return (col);        
    }
    
    private Object getFormBean (String name) {
        //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, property;
        FormBeanForm actionMappingForm = new FormBeanForm ();
        DynaMapping mapping = new DynaMapping ();        
        DynaMapping mappings[];
        Object obj;
        List children;
        List properties;
        Iterator iterator;
        int i, index;
        String tmp;
        int count;
        String type;
        String path;
        ValidatorlConfigFile confFile = new ValidatorlConfigFile ();
        
        handleTrace ("getFormBean", 0);
        
        if (name == 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));

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

                msg1.setMsgContent(elem);
                msg1.setMsgData(actionMappingForm);
                obj = jdomAdapter.processMessage(msg1);
                
                if (obj == null)
                    return (null);
                
                type = ((FormBeanForm) obj).getType();
                
                if ("org.apache.struts.action.DynaActionForm".equals(type)) {
                    ((FormBeanForm) obj).setDynaActionForm(true);
                } else
                    ((FormBeanForm) obj).setDynaActionForm(false);
                
                
                if (((FormBeanForm) obj).isDynaActionForm()) {

                    properties = elem.getChildren();

                    if (properties == null || (properties.size() == 0))
                        return (obj);

                    iterator = properties.iterator();


                    if (properties.size() > FormBeanForm.MAX_ENTRIES) {  // chech
                        count = properties.size();
                        mappings = new DynaMapping[properties.size()];
                        ((FormBeanForm) obj).setMappings(mappings);
                    } 
                    //else {
                    //    count = FormBeanForm.MAX_ENTRIES;
                    //    mappings = new DynaMapping[FormBeanForm.MAX_ENTRIES];                    
                    //}

                    //for (i = 0; i < count; i++)
                    //    mappings[i] = new DynaMapping ();

                    mappings = ((FormBeanForm) obj).getMappings();
                    for (i = 0; i < FormBeanForm.INITIAL_ENTRIES; i++) {
                        if (mappings[i] != null)
                            mappings[i].setName("");
                    }

                    index = 0;
                    while (iterator.hasNext()) {

                        property = (Element) iterator.next();


                        //handleTrace ("getName:" + property.getName());

                        if (!"form-property".equals(property.getName())) 
                            continue;

                        msg1.setMsgContent(property);
                        msg1.setMsgData(new DynaMapping ());

                        //mappings = ((FormBeanForm) obj).getMappings ();

                        mapping = (DynaMapping) jdomAdapter.processMessage(msg1);

                        mapping.setFieldType(mapToDisplayType (mapping.getType()));

                        if (mapping != null) {
                            mappings[index] = mapping;
                            //mappings[index].setName(mapping.getName());
                            //mappings[index].setType(mapping.getType());
                            //mappings[index].setInitial(mapping.getInitial());
                            index++; // check
                        }
                        //handleTrace ("form-property:"); //check
                    }

                    //if (index > 0) {
                    //((FormBeanForm) obj).setMappings(mappings);
                    //}
                    //updateForm ((ActionMappingForm) obj, elem);
                } else {
                    mappings = ((FormBeanForm) obj).getMappings();
                    for (i = 0; i < FormBeanForm.INITIAL_ENTRIES; i++) {
                        if (mappings[i] != null)
                            mappings[i].setName("");
                    }
                }
                
                if (path == null)
                    return (null);
                
                path = concatenatePath (config.getWebAppPath(), path);
                handleTrace ("getFormBean (validator config path):" + path, 0);
                
                if (path == null)
                    return (null);
                
                confFile.setPath(path);
            
                
                readValidatorForm (confFile, (FormBeanForm) obj);
                
                return (obj);
            }
        }        
       
        return (null);
    }
    
    
    private Object getFormBeans () {
        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;
        FormBean formBean = new FormBean ();
        List list = new LinkedList ();
        Object obj;
        String type;
        
        int i;
        
        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/form-beans");
        //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);
            
            type = elem.getAttributeValue("type");
            //if ("org.apache.struts.action.DynaActionForm".equals(type))
            //    continue;
            
            msg1.setMsgContent(elem);
            msg1.setMsgData(formBean);
            obj = adapter.processMessage(msg1);
            //updateBean ((FormBean) obj, elem);
            if (obj != null)
                list.add(obj);
        }
        
        return (list);
    }
    
    private FormBeanForm newFormBean() {
        FormBeanForm form = new FormBeanForm ();
        //DynaMapping mappings[] = new DynaMapping[FormBeanForm.MAX_ENTRIES];
        DynaMapping mappings[];
        int i;
        
        form.setName("");
        
        mappings = form.getMappings (); // check
        // Display 6 entries or so
        
        for (i = 0; i < FormBeanForm.INITIAL_ENTRIES; i++) {
            //mappings[i] = new DynaMapping();
            mappings[i].setName("");            
        }
        
        //form.setMappings(mappings);
        
        
        return (form);
                
    }   
    
    
    /**
     * 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 ( getFormBeans ());
        }
        
        if (e.getMsgId().equals(FormBean.NEW)) {
            
            return ( newFormBean ());
        }
        
        if (e.getMsgId().equals(FormBean.READ) || e.getMsgId().equals(JtObject.JtREAD)) {
 
            content = (String) e.getMsgContent ();
            return ( getFormBean (content));
        }
        
        if (e.getMsgId().equals(FormBean.DELETE)) {
            
            //content = (String) e.getMsgContent ();
            //data = e.getMsgData ();
            return ( deleteFormBean ((FormBeanForm) form));
        }
        
        if (e.getMsgId().equals(FormBean.UPDATE)) {
            
            //content = (String) e.getMsgContent ();
            //data = e.getMsgData ();
            return ( updateFormBean ((FormBeanForm) form));
        }
        
        if (e.getMsgId().equals(FormBean.JtLOOKUP)) {
            
            return ( lookup ());
        }
        
        
        if (e.getMsgId().equals(FormBean.CREATE)) {
            
            //content = (String) e.getMsgContent ();
            //data = e.getMsgData ();
            return ( createFormBean ((FormBeanForm) form));
        }

        return (super.processMessage(message));

    }
    /**
     * Demonstrates the messages processed by this class
     */

    public static void main(String[] args) {

        JtObject factory = new JtFactory ();
        FormBean actionMapping = new FormBean ();
        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 + -
显示快捷键?