formbean.java

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

JAVA
2,094
字号
package Jt.wizard.struts;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import Jt.JtContext;
import Jt.JtFactory;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.wizard.Resource;
import Jt.wizard.ResourceFile;
import Jt.wizard.WizardConfig;
import Jt.wizard.ValidatorlConfigFile;
import Jt.xml.JDOMAdapter;

import org.apache.struts.action.ActionForm;
import org.jdom.Element;

/**
 * Handles Form Bean Descriptors (Internal JtWizard Class).
 */

public class FormBean extends JtObject {
    private static final long serialVersionUID = 1L;
    public static final String JtCLASS_NAME = FormBean.class.getName();
    public static final String READ = "READ";
    public static final String DELETE = "DELETE";
    public static final String UPDATE = "UPDATE";
    public static final String CREATE = "CREATE";
    public static final String NEW = "NEW";
    private JtFactory factory = new JtFactory ();
    private WizardConfig config = null;    
    private JDOMAdapter jdomAdapter = new JDOMAdapter ();
    private ResourceFile resourceFile = new ResourceFile ();
    
    private String type;
    private String name;
    private String delete;
    private String configFile;
    private boolean dynaActionForm;

    
    
    
    public String getConfigFile() {
        return configFile;
    }
    public void setConfigFile(String configFile) {
        this.configFile = configFile;
    }

    public String getDelete() {
        return "delete";
    }

    public void setDelete(String delete) {
        this.delete = this.delete; // null
    }

    public boolean isDynaActionForm() {
        return dynaActionForm;
    }
    public void setDynaActionForm(boolean dynaActionForm) {
        this.dynaActionForm = dynaActionForm;
    }
    public String getName() {
        return (name);
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    private void handleUIError (String error) {
        Collection col;

        if (error == null)
            return;
        col = (Collection) this.getObjErrors();

        if (col == null) {
            col = new LinkedList ();
            setObjErrors (col);
        }
        col.add(error);


    }

    // Propagate the exception 

    private Exception propagateException (Exception ex)
    {
      if (ex != null)
        this.setObjException(ex);  

      return (ex);
    }  
    
/*    
    public Object generateFormBean (FormBeanForm form) {

        JDOMAdapter adapter;
        Element element;
        JtMessage msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);
        //JtFactory factory = new JtFactory ();

        if (form == null) {
            return (null);            
        } 

        type = (String) form.getType ();      
        if (type == null || type.equals ("")) {
            handleUIError ("Please enter a type.");
            //return (null);
        }    

        name = (String) form.getName ();      
        if (name == null || name.equals ("")) {
            handleUIError ("Please enter the name of the form bean.");
            //return (null);
        } 


        if (findFormBean (name) != null) {
            handleUIError ("Form Bean Mapping already exists (same path).");           
        }       
        
        if (getObjErrors() != null) {
            return (null);
        }


            

        adapter = (JDOMAdapter) factory.createObject (JDOMAdapter.JtCLASS_NAME);

        element = new Element ("form-bean");

        element.setAttribute("name", name);
        element.setAttribute("type", type);
        
        msg.setMsgContent (element);      
        factory.sendMessage(adapter, msg);

        element = new Element ("forward");
        element.setAttribute("name", "success");    
        //element.setAttribute("path", forwardSuccess);   
        msg.setMsgContent (element);   

        msg.setMsgData ("action");
        factory.sendMessage(adapter, msg);      


        element = new Element ("forward");
        element.setAttribute("name", "failure");    
        //element.setAttribute("path", forwardFailure);   
        msg.setMsgContent (element);   

        msg.setMsgData ("action");
        factory.sendMessage(adapter, msg);   

        return (adapter.getContent());
    }
*/    
    
    
    
    private void updateResourceFile (String key, String value, String operation) {
        JtMessage msg;
        Resource res, res1;
        
        
        if (key == null || value == null)
            return;
        
        if (resourceFile == null)
            return;
        
        res = new Resource ();
        
        res.setName(key);
        res.setValue(value);       
        
        
        if (JtObject.JtCREATE.equals(operation)) {
            msg = new JtMessage (JtObject.JtCREATE);
            msg.setMsgContent(res);

            // The Resource File won't be saved to disk until
            // the JtSAVE message is sent.
            factory.sendMessage(resourceFile, msg);
            //factory.sendMessage(resourceFile, new JtMessage (JtObject.JtSAVE));
        } else if (JtObject.JtUPDATE.equals(operation)) {
            
            msg = new JtMessage (JtObject.JtREAD); 
            msg.setMsgContent(key);

            res1 = (Resource) factory.sendMessage(resourceFile, msg);
            
            if (res1 == null) {
                msg = new JtMessage (JtObject.JtCREATE);
                msg.setMsgContent(res);

                factory.sendMessage(resourceFile, msg);
                return;
            }
            
            msg = new JtMessage (JtObject.JtUPDATE);
            msg.setMsgContent(res);

            // The Resource File won't be saved to disk until
            // the JtSAVE message is sent.
            factory.sendMessage(resourceFile, msg);
            //factory.sendMessage(resourceFile, new JtMessage (JtObject.JtSAVE));
        }

        
    }
    
    private String capFirstLetter (String str) {
        if (str == null)
            return (null);

        if (str.equals(""))
            return (str);

        if (str.length() == 1)
            return str.toUpperCase();

        return (str.substring(0, 1).toUpperCase() + str.substring(1));
    }
    
    private Element buildFieldElement (String formName, DynaMapping mapping, String operation) {
        StringBuffer buffer = new StringBuffer ();
        Element fieldElement = new Element ("field");
        Element arg0Elem = null;
        Element argElem;
        Element elem, tmp, msgElem;
        String fieldName;
        String type;
        String mask;
       
        if (mapping == null || formName == null) 
            return (null);
        
        
        fieldName = mapping.getName();
        type = mapping.getFieldType();
        
        if (fieldName == null || fieldName.equals(""))
            return (null);
        
        fieldElement.setAttribute("property", fieldName);
        
        buffer.append("");
        
        mask = mapping.getMask();
        
        if (mask != null && !mask.equals("")) {
            msgElem = new Element ("msg");
            msgElem.setAttribute ("name", "mask");
            msgElem.setAttribute("key", "Jt." + formName + "." + fieldName + ".mask");
            updateResourceFile ("Jt." + formName + "." + fieldName + ".mask", 
                    "{0} must match the field mask.", operation);            
            fieldElement.addContent (msgElem);
        }        
        
        if (mapping.isRequired()) {
            buffer.append("required");  
            
            arg0Elem = new Element ("arg");
            arg0Elem.setAttribute("key", "Jt." + formName + "." + fieldName);
            arg0Elem.setAttribute("position", "0");
            updateResourceFile ("Jt." + formName + "." + fieldName, capFirstLetter (fieldName), operation);
            fieldElement.addContent(arg0Elem);
        }    
        
        
        if (mapping.getMinlength() > 0) {  // bug
            if (buffer.length() > 0) {
                buffer.append(",");
            }
            buffer.append("minlength");
            
            if (arg0Elem == null) {
                arg0Elem = new Element ("arg");
                arg0Elem.setAttribute("key", "Jt." + formName + "." + fieldName);
                arg0Elem.setAttribute("position", "0");
                updateResourceFile ("Jt." + formName + "." + fieldName, capFirstLetter (fieldName),
                        operation);
                fieldElement.addContent(arg0Elem);
            }
            argElem = new Element ("arg");
            argElem.setAttribute("key", "${var:minlength}");
            argElem.setAttribute("name", "minlength");
            argElem.setAttribute("resource", "false");
            argElem.setAttribute("position", "1");
            fieldElement.addContent(argElem);
        }    
 
        
        if (mapping.getMaxlength() > 0) {
            if (buffer.length() > 0) {
                buffer.append(",");
            }
            buffer.append("maxlength");
            
            if (arg0Elem == null) {
                arg0Elem = new Element ("arg");
                arg0Elem.setAttribute("key", "Jt." + formName + "." + fieldName);
                arg0Elem.setAttribute("position", "0");
                updateResourceFile ("Jt." + formName + "." + fieldName, capFirstLetter (fieldName), 
                        operation);
                fieldElement.addContent(arg0Elem);
            }
            argElem = new Element ("arg");
            argElem.setAttribute("key", "${var:maxlength}");
            argElem.setAttribute("name", "maxlength");
            argElem.setAttribute("resource", "false");
            argElem.setAttribute("position", "1");
            fieldElement.addContent(argElem);
        }   
        
        
        if ("date".equals(type)) {
            if (buffer.length() > 0) {
                buffer.append(",");
            }
            buffer.append(type);
            
            if (arg0Elem == null) {
                arg0Elem = new Element ("arg");
                arg0Elem.setAttribute("key", "Jt." + formName + "." + fieldName);
                arg0Elem.setAttribute("position", "0");
                updateResourceFile ("Jt." + formName + "." + fieldName, capFirstLetter (fieldName),
                        operation);
                fieldElement.addContent(arg0Elem);
            }           
        }
        
        if ("email".equals(type) || "creditCard".equals(type) ||
                "byte".equals(type) || "short".equals(type) || "integer".equals(type) ||
                "long".equals(type) || "float".equals(type) || "double".equals(type)) {
            if (buffer.length() > 0) {
                buffer.append(",");
            }
            buffer.append(type);
            
            if (arg0Elem == null) {
                arg0Elem = new Element ("arg");
                arg0Elem.setAttribute("key", "Jt." + formName + "." + fieldName);
                arg0Elem.setAttribute("position", "0");
                updateResourceFile ("Jt." + formName + "." + fieldName, capFirstLetter (fieldName),
                        operation);
                fieldElement.addContent(arg0Elem);
            }           
        }
        
        if ("String".equals(type) || "boolean".equals(type)) // check
        {
            if (arg0Elem == null) {
                arg0Elem = new Element ("arg");
                arg0Elem.setAttribute("key", "Jt." + formName + "." + fieldName);
                arg0Elem.setAttribute("position", "0");
                updateResourceFile ("Jt." + formName + "." + fieldName, capFirstLetter (fieldName),
                        operation);
                fieldElement.addContent(arg0Elem);
            }             
        }
        
        if (mapping.getMinlength() > 0) {
            elem = new Element ("var");
            tmp = new Element ("var-name");
            tmp.setText ("minlength");
            elem.addContent(tmp);
            
            tmp = new Element ("var-value");
            tmp.setText ("" + mapping.getMinlength());
            elem.addContent(tmp);
            fieldElement.addContent(elem);           
        }
        
        if (mapping.getMaxlength() > 0) {
            elem = new Element ("var");
            tmp = new Element ("var-name");
            tmp.setText ("maxlength");
            elem.addContent(tmp);
            
            tmp = new Element ("var-value");
            tmp.setText ("" + mapping.getMaxlength());
            elem.addContent(tmp);
            fieldElement.addContent(elem);           
        }   
        
        if (mask != null && !mask.equals("")) {
            if (buffer.length() > 0) {
                buffer.append(",");
            }
            buffer.append("mask");
            elem = new Element ("var");
            tmp = new Element ("var-name");
            tmp.setText ("mask");
            elem.addContent(tmp);
            
            tmp = new Element ("var-value");
            tmp.setText (mask);
            elem.addContent(tmp);

⌨️ 快捷键说明

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