daodefinition.java

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

JAVA
1,619
字号
package Jt.DAO;


import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import org.jdom.DocType;
import org.jdom.Element;

import Jt.*;
import Jt.util.JtJavaParser;
import Jt.wizard.struts.DAODefinitionForm;
import Jt.wizard.struts.FileForm;
import Jt.xml.JDOMAdapter;


/**
 * DAO Definition. This class helps define a Data Access Object (JtWizard internal class).
 */

public class DAODefinition extends JtObject {
    public static final String JtCLASS_NAME = DAODefinition.class.getName(); 
    public static final String GENERATE_DESCRIPTOR = "GENERATE_DESCRIPTOR"; 
    public static final String GENERATE_FORM = "GENERATE_FORM"; 
    public static final String INITIALIZE = "INITIALIZE";   
    public static final String READ = "READ"; 
    public static final String READ_STREAM = "READ_STREAM"; 
    public static final String READ_MAPPING_STREAM = "READ_MAPPING_STREAM"; 
    public static final String UPDATE = "UPDATE"; 
    public static final String PARSE_FILE = "PARSE_FILE"; 
    public static final int MAX_ENTRIES = 12;

    private static final long serialVersionUID = 1L;

    private String className;
    private String table;
    //private JtList keys; 
    private List keys;   // DB Keys
    private String fileName;
    private JDOMAdapter adapter = new JDOMAdapter ();
    private JtFactory factory = new JtFactory ();
    private DAODefinitionForm daoForm;
    JtHashTable attributes;
    private Hashtable mappingTable = new Hashtable ();
    
    

    public DAODefinition() {
    }

    // Attributes


    public void setClassName (String className) {
        this.className = className; 

    }

    public String getClassName () {
        return (className);
    }
   
    public List getKeys() {
        return keys;
    }

    public void setKeys(List keys) {
        this.keys = keys;
    }

    public Hashtable getMappingTable() {
        return mappingTable;
    }

    public void setMappingTable(Hashtable mappingTable) {
        this.mappingTable = mappingTable;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    
    public String getTable() {
        return table;
    }


    public void setTable(String table) {
        this.table = table;
    }

    /*
    public JtList getKeys() {
        return keys;
    }

    public void setKeys(JtList keys) {
        this.keys = keys;
    }
*/    
    
    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 void propagateException (JtObject obj) {
        if (obj == null)
            return;
        
        if (obj.getObjException() != null) {
            this.setObjException(obj.getObjException());
        }
        
    }
    
    private void addElement (JDOMAdapter adapter, String parentPath, Element element) {
        JtMessage msg = new JtMessage (JDOMAdapter.ADD_ELEMENT);

        if (adapter == null || element == null)
            return;
        
        msg.setMsgContent(element);
        msg.setMsgData(parentPath);
        adapter.processMessage(msg);
    }
 
    
    private DAOMapping getMapping (DAODefinitionForm daoForm, String attrName){
        DAOMapping arr[];
        int i;

        if (daoForm == null || attrName == null)
            return null;

        arr = (DAOMapping[]) daoForm.getDbmappings();
        
        if (arr == null)
            return null;
        for (i = 0; i < arr.length; i++) {
            if (attrName.equals (arr[i].getAttribute()))
                return (arr[i]);
        }
        
        return (null);
        
    }    

    
    private List retrieveKeys (DAODefinitionForm daoForm) {
        DAOMapping arr[];
        int i;
        LinkedList list = new LinkedList ();
        
        if (daoForm == null)
            return null;

        arr = (DAOMapping[]) daoForm.getDbmappings();
        
        for (i = 0; i < arr.length; i++) {
            
            if (arr[i].isKeyAttribute())
                list.add(arr[i]);
           
        }
        return (list);
        
    }
    
    private boolean ngenerateIds () {
        //JtIterator iterator;
        int cnt;
        Element element = new Element ("id");
        String idName;
        //String[] dbKeys;
        int i;
        DAOMapping attrMapping;  
        String columnName;
        //List keys;
        Element columnElement; 
        
        if (daoForm == null || keys == null)
            return (true);
        /*
        dbKeys = (String[]) daoForm.getDbkeys();
                        
        if (dbKeys == null)
            return (true); // check
        
        cnt = dbKeys.length;
        */
        
        //keys = retrieveKeys (daoForm);
        cnt = keys.size();
        
        if (cnt == 1) {
            
            //idName = dbKeys[0];
            attrMapping = (DAOMapping) keys.get(0);
            
            idName = attrMapping.getAttribute();
            element.setAttribute("name", idName);
            
            
            attrMapping = getMapping (daoForm, idName);
            
   
            
            if (attrMapping == null) {
                handleWarning ("DAODefinition.ngenerateIds: unable to retrieve mapping for key:" +
                        idName); 
            } else {
                
                columnElement =  addColumnSubElement (attrMapping);  
                columnName = attrMapping.getColumn();
                
                if (columnElement != null)
                    element.addContent(columnElement);
                else if (columnName != null && !columnName.equals("")) // check white spaces
                    if (!columnName.equals (attrMapping.getAttribute()))
                        element.setAttribute("column", columnName);            
/*                
                if (columnName != null && !columnName.equals("")) // check for white spaces
                    if (!columnName.equals (attrMapping.getAttribute()))  
                        element.setAttribute("column", columnName);
*/
            }
            addElement (adapter, "hibernate-mapping/class", element);
            return (true);
        }
        
        element = new Element ("composite-id");
        addElement (adapter, "hibernate-mapping/class", element);
        for (i = 0; i < cnt; i++) {
            
            attrMapping = (DAOMapping) keys.get(i);
            
            idName = attrMapping.getAttribute();

            //idName = dbKeys[i];
            if (idName == null)
                break;     
            element = new Element ("key-property");
            element.setAttribute("name", idName);  
            
            attrMapping = getMapping (daoForm, idName);
            
            if (attrMapping == null) {
                handleWarning ("DAODefinition.ngenerateIds: unable to retrieve mapping for key:" +
                        idName); 

            } else {

                columnName = attrMapping.getColumn();
                columnElement =  addColumnSubElement (attrMapping);                
                
                if (columnElement != null)
                    element.addContent(columnElement);
                else if (columnName != null && !columnName.equals("")) // check white spaces
                    if (!columnName.equals (attrMapping.getAttribute()))
                        element.setAttribute("column", columnName);    
                
/*
                if (columnName != null && !columnName.equals("")) // check for white spaces
                    if (!columnName.equals (attrMapping.getAttribute()))  
                        element.setAttribute("column", columnName);
*/
            }
            
            addElement (adapter, "hibernate-mapping/class/composite-id", element);
        }
        return (true);
        
    }
    
 /*    
    private JtIterator getAttributes () {
        
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);
        //JtFactory factory = new JtFactory ();
        Object obj;     
        JtHashTable attributes;
        JtIterator iterator;
      
        if (className == null)
            return (null);
              
        obj = factory.createObject(className);           
        
        msg.setMsgContent(obj);
        
        attributes = (JtHashTable) factory.processMessage(msg);
        
        if (attributes  == null)
            return (null);
        
        iterator = (JtIterator) attributes.processMessage(new JtMessage (JtHashTable.JtGET_KEYS));
        
        return (iterator);
        
    }
*/   
    
    
    private String[] getIDs () {
        Element elem;
        LinkedList list = new LinkedList ();
        String id;
        JtMessage msg = new JtMessage (JDOMAdapter.FIND_ELEMENT);
        String ids[];
        List children;
        int i;
        
        if (adapter == null)
            return null;
        
        msg.setMsgContent("hibernate-mapping/class/id");
        elem = (Element) factory.sendMessage (adapter, msg);
        
        if (elem != null) {
            id = elem.getAttributeValue("name");
            if (id != null)
                list.add(id);
            ids = (String[]) list.toArray(new String[0]);
            
            return ids;
        }
        
        msg = new JtMessage (JDOMAdapter.GET_CHILDREN);
        msg.setMsgContent("hibernate-mapping/class/composite-id");
        children = (List) factory.sendMessage (adapter, msg);      

        if (children == null) {
            return (null);
        }
        
        for (i = 0; i < children.size(); i++) {
            elem = (Element) children.get(i);
            
            if (!("key-property".equals(elem.getName())))
                    continue;
            
            id = elem.getAttributeValue("name");

            list.add(id);
        }  
        
        ids = (String[]) list.toArray(new String[0]);
        
        return ids;
        
    }
    
    // Updates the list (using the IDs)
    
    private void addIds (List list) {
        Element elem;
        String id;
        JtMessage msg = new JtMessage (JDOMAdapter.FIND_ELEMENT);
        List children;
        int i;
        DAOMapping attrMapping;
        String column;
        
        if (adapter == null)
            return;
        
        msg.setMsgContent("hibernate-mapping/class/id");
        elem = (Element) factory.sendMessage (adapter, msg);
        
        if (elem != null) {
            id = elem.getAttributeValue("name");
            
            attrMapping = new DAOMapping ();
            attrMapping.setAttribute(id);

            column = elem.getAttributeValue("column");
            attrMapping.setEnabled(true);
            attrMapping.setKeyAttribute(true);
           
            if (column == null || column.equals(""))
                attrMapping.setColumn(id); //check

⌨️ 快捷键说明

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