crud.java

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

JAVA
609
字号
package Jt.wizard.struts;

import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import Jt.JtAdapter;
import Jt.JtContext;
import Jt.JtFactory;
import Jt.JtHashTable;
import Jt.JtIterator;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.DAO.JtDAOAdapter;
import Jt.DAO.JtDAOStrategy;
import Jt.examples.struts.MemberForm;
import Jt.hibernate.JtHibernateAdapter;


import org.apache.struts.action.ActionForm;


/**
 * Implements CRUD (Internal JtWizard Class).
 */

public class CRUD extends JtObject {
    private static final long serialVersionUID = 1L;
    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 EXECUTE_QUERY = "EXECUTE_QUERY";
    public static final String JtCLASS_NAME = CRUD.class.getName(); 
    private JtFactory factory = new JtFactory ();
    //private transient JtAdapter adapter;
    private transient JtDAOStrategy adapter;
    private String key;
    private String key1;
    private String key2;
    private String classname;
    private Object classInstance;
    private String query;
    private boolean initted = false;
    


    public String getKey2() {
        return key2;
    }



    public void setKey2(String key2) {
        this.key2 = key2;
    }



    public String getKey1() {
        return key1;
    }



    public void setKey1(String key1) {
        this.key1 = key1;
    }



    public String getKey() {
        return key;
    }



    public void setKey(String key) {
        this.key = key;
    }



    public String getQuery() {
        return query;
    }



    public void setQuery(String query) {
        this.query = query;
    }



    public String getClassname() {
        return classname;
    }



    public void setClassname(String classname) {
        this.classname = classname;
    }



    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);


    }

    
    
    private Object readObject (ActionForm form, String content, String data, String attachment) {
        JtMessage msg = new JtMessage ();
        Object keyValue;
        Object obj;
        ActionForm oform;
        
        if (form == null)
            return (null); // check
        
        if (classname == null) {
            handleError ("classname attributes needs to set.");
            return (null);
        }    
        
        if (classInstance == null) { 
            classInstance = factory.createObject (classname);
            if (classInstance == null) {
                handleError ("unable to create an object of class: " + classname);
                return (null);
            }    
                
        }
        
        
       // adapter = (JtDAOAdapter) factory.createObject (JtHibernateAdapter.JtCLASS_NAME);

        
        msg.setMsgId (JtDAOAdapter.JtREAD);
        msg.setMsgContent(classInstance);
        
        
        if (key == null) {
            handleError ("key attribute needs to be set.");      
            return (null);
        }
        
        if (content == null) {
            //if (key == null) {
            //    handleError ("key attribute needs to be set.");      
            //    return (null);
            //}
            keyValue = getValue (form, key);                           
            msg.setMsgData (keyValue);
        } else {
           //msg.setMsgData (content);
           // Use the String representation of the key 
           factory.setValue (classInstance, key, content);
           
           if (key1 != null)
               factory.setValue (classInstance, key1, data);              
           
           if (key2 != null)
               factory.setValue (classInstance, key2, attachment);      
           
           // Retrieve the key
           keyValue = factory.getValue(classInstance, key);
           
           if (key1 == null && key2 == null)
               msg.setMsgData (keyValue);
           else
               msg.setMsgData (classInstance);
        }   
        
        obj = factory.sendMessage (adapter, msg);
        
        // Propagate the exception
        
        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
            return (null);
        }
        
        if (obj == null)
           handleUIError ("Unable to find item");
        
        factory.sendMessage (adapter, new JtMessage (JtObject.JtREMOVE));
        
        if (obj == null)
            return (null);
        
        oform = (ActionForm) factory.createObject(form.getClass().getName());
        copyToForm (obj, oform);
        
        
        return (oform);        
    }
    
    private JtHashTable getAttributes (Object obj) {
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        JtFactory fact = new JtFactory ();
        
        if (obj == null)
            return (null);
        msg.setMsgContent(obj);
        fact.setStopClass(obj.getClass().getSuperclass());
        
        return ((JtHashTable) fact.processMessage(msg));
        
    }
    
    
    private void copyFormAttributes (ActionForm form, Object obj) {
        JtHashTable attributes;
        JtIterator iterator;
        String attribute;
        Object value;
        
        if (obj == null || form == null)
            return;
        //attributes = getAttributes (obj);
        attributes = getAttributes (form);
        
        if (attributes == null)
            return;
        iterator = (JtIterator) attributes.processMessage(new JtMessage (JtHashTable.JtGET_KEYS));
        
        if (iterator == null)
            return;
        
        for (;;) {
            attribute = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
            if (attribute == null)
                break;
            value = factory.getValue(form,attribute);
            factory.setValue (obj, attribute, value);
                
        }
        
    }
    
    private String DateToString (Date date) {
        String sdate;
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        
        if (date == null)
            return null;
        try {

            sdate = df.format (date);

        } catch (Exception e) {
            handleException (e);
            return null;
        }
        
        return (sdate);

    }
    
    private void copyToForm (Object obj, ActionForm form) {
        JtHashTable attributes;
        JtIterator iterator;
        String attribute;
        Object value;
        
        if (obj == null || form == null)
            return;
        //attributes = getAttributes (obj);
        attributes = getAttributes (form);
        
        if (attributes == null)
            return;
        iterator = (JtIterator) attributes.processMessage(new JtMessage (JtHashTable.JtGET_KEYS));
        
        if (iterator == null)
            return;
        
        for (;;) {
            attribute = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
            if (attribute == null)
                break;
            value = factory.getValue(obj,attribute);
            if (value instanceof Date)
                factory.setValue (form, attribute, DateToString ((Date) value));
            else
                factory.setValue (form, attribute, value);
                

⌨️ 快捷键说明

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