⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableenginefactory.java

📁 本系统有十分强大的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.opencustomer.framework.db.util.engine;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.opencustomer.framework.db.util.engine.configuration.Configuration;
import org.opencustomer.framework.db.util.engine.configuration.DateFormatter;
import org.opencustomer.framework.db.util.engine.configuration.DateSearch;
import org.opencustomer.framework.db.util.engine.configuration.DefaultFormatter;
import org.opencustomer.framework.db.util.engine.configuration.Entity;
import org.opencustomer.framework.db.util.engine.configuration.EnumFormatter;
import org.opencustomer.framework.db.util.engine.configuration.EnumSearch;
import org.opencustomer.framework.db.util.engine.configuration.Join;
import org.opencustomer.framework.db.util.engine.configuration.ListSelectSearch;
import org.opencustomer.framework.db.util.engine.configuration.Property;
import org.opencustomer.framework.db.util.engine.configuration.Search;
import org.opencustomer.framework.db.util.engine.configuration.StringFormatter;
import org.opencustomer.framework.db.util.engine.configuration.TextSearch;
import org.opencustomer.framework.db.util.engine.configuration.TextSelectSearch;
import org.opencustomer.framework.db.vo.BaseVO;
import org.opencustomer.framework.util.EnumUtility;
import org.opencustomer.framework.webapp.util.html.Formatter;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TableEngineFactory {

    private final static Logger log = Logger.getLogger(TableEngineFactory.class);
    
    private Hashtable<String, TableEngine> engines = new Hashtable<String, TableEngine>();
    
    public TableEngineFactory(File packageDir) {
        
        File[] configurationFiles = packageDir.listFiles(new FileFilter() {
            public boolean accept(File file) {
                if(file.isFile() && file.getName().endsWith(".xml")) {
                    return true;
                } else {
                    return false;
                }
            };
        });
        
        for(File file : configurationFiles) {
            loadConfiguration(file);
        }
    }
    
    public final Configuration getConfiguration(String name) {
        return engines.get(name).getConfiguration();
    }
    
    public final TableEngine getEngine(String name) {
        return getEngine(name, (Integer[])null);
    }

    public final TableEngine getEngine(String name, Integer... columns) {
        TableEngine engine = (TableEngine)engines.get(name).clone();
        
        if(columns != null) {
            List<Property> newProperties = new ArrayList<Property>();
            
            for(Integer column : columns) {
                newProperties.add(engine.getConfiguration().getProperties().get(column));
            }
            
            engine.getConfiguration().setProperties(newProperties);
        } else {
            engine.getConfiguration().getProperties().clear();
            engine.getConfiguration().getProperties().addAll(engine.getConfiguration().getDefaultProperties().values());
        }
        
        // get group properties
        for(Property property : engine.getConfiguration().getProperties()) {
            if(property.isGroup())
                engine.getConfiguration().getGroupProperties().add(property);
        }

        engine.optimizeQuery();
        
        return engine;
    }

    private void loadConfiguration(File file) throws TableEngineException {
        if(log.isDebugEnabled()) {
            log.debug("load list configuraton: "+file);
        }
        
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = builder.parse(in);
            Node root = document.getDocumentElement();
            if("configuration".equals(root.getNodeName())) {
                Configuration conf = new Configuration(file.getName().substring(0, file.getName().indexOf(".")));
                
                NodeList nodes = root.getChildNodes();
                for(int i=0; i<nodes.getLength(); i++) {
                    if(nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                        if("entity".equals(nodes.item(i).getNodeName())) {
                            conf.setEntity(parseEntity(nodes.item(i)));
                        } else if("join".equals(nodes.item(i).getNodeName())) {
                            conf.getJoins().add(parseJoin(nodes.item(i)));                            
                        } else if("restriction".equals(nodes.item(i).getNodeName())) {
                            conf.getRestrictions().add(parseRestriction(nodes.item(i)));                            
                        } else if("property".equals(nodes.item(i).getNodeName())) {
                            Property property = parseProperty(conf, nodes.item(i));
                            if(property.isId()) {
                                if(conf.getId() == null) {
                                    conf.setId(property);
                                } else {
                                    throw new TableEngineException("found duplicate id property");
                                }
                            }
                            property.setAlias("alias_"+conf.getProperties().size());
                            property.setPosition(conf.getProperties().size());
                            conf.getProperties().add(property);                            
                        } 
                    }
                }
                
                if(log.isDebugEnabled())
                    log.debug("add list renderer: "+conf.getName());
                
                engines.put(conf.getName(), new TableEngine(conf));
            } else {
                throw new TableEngineException("invalid attribute found: '"+root.getNodeName()+"' (need: 'configuration')");
            }
        } catch(Exception e) {
            if(in != null) {
                try {
                    in.close();
                } catch(IOException e2) {
                }
            }
            throw new TableEngineException("could not load engine", e);
        }
    }
    
    private Entity parseEntity(Node node) throws TableEngineException {
        Entity entity = new Entity();
        
        NamedNodeMap attributes = node.getAttributes();
        for(int i=0; i<attributes.getLength(); i++) {
            Node attributeNode = attributes.item(i);
            if(attributeNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                String name = attributeNode.getNodeName();
                if("class".equals(name)) {
                    try {
                    entity.setClazz((Class<? extends BaseVO>)Class.forName(attributeNode.getNodeValue()));
                    } catch(ClassNotFoundException e) {
                        throw new TableEngineException(e);
                    }
                } else if("alias".equals(name)) {
                    entity.setAlias(attributeNode.getNodeValue());
                } else if("messageKey".equals(name)) {
                    entity.setMessageKey(attributeNode.getNodeValue());
                }
            }
        }
        
        return entity;
    }
    
    private String parseRestriction(Node node) throws TableEngineException {
        String restriction = null;
        
        NamedNodeMap attributes = node.getAttributes();
        for(int i=0; i<attributes.getLength(); i++) {
            Node attributeNode = attributes.item(i);
            if(attributeNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                String name = attributeNode.getNodeName();
                if("hql".equals(name)) {
                    restriction = attributeNode.getNodeValue();
                }
            }
        }
        
        return restriction;
    }
    
    private Join parseJoin(Node node) throws TableEngineException {
        Join join = new Join();
        
        NamedNodeMap attributes = node.getAttributes();
        for(int i=0; i<attributes.getLength(); i++) {
            Node attributeNode = attributes.item(i);
            if(attributeNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                String name = attributeNode.getNodeName();
                if("name".equals(name)) {
                    join.setName(attributeNode.getNodeValue());
                } else if("alias".equals(name)) {
                    join.setAlias(attributeNode.getNodeValue());
                } else if("type".equals(name)) {
                    join.setType(EnumUtility.valueOf(Join.Type.class, attributeNode.getNodeValue()));
                } else if("messageKey".equals(name)) {
                    join.setMessageKey(attributeNode.getNodeValue());
                }
            }
        }
        
        return join;
    }
    
    private Property parseProperty(Configuration configuration, Node node) throws TableEngineException {
        Property property = new Property();
        
        NamedNodeMap attributes = node.getAttributes();
        for(int i=0; i<attributes.getLength(); i++) {
            Node attributeNode = attributes.item(i);
            if(attributeNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                String name = attributeNode.getNodeName();
                if("name".equals(name)) {
                    property.setName(attributeNode.getNodeValue());
                } else if("altName".equals(name)) {
                    property.setAltName(attributeNode.getNodeValue());
                } else if("messageKey".equals(name)) {
                    property.setMessageKey(attributeNode.getNodeValue());
                } else if("entityMessageKey".equals(name)) {
                    property.setEntityMessageKey(attributeNode.getNodeValue());
                } else if("type".equals(name)) {
                    property.setSortable(Boolean.parseBoolean(attributeNode.getNodeValue()));
                } else if("id".equals(name)) {
                    property.setId(Boolean.parseBoolean(attributeNode.getNodeValue()));
                } else if("sortable".equals(name)) {
                    property.setSortable(Boolean.parseBoolean(attributeNode.getNodeValue()));
                } else if("default".equals(name)) {
                    configuration.getDefaultProperties().put(Integer.parseInt(attributeNode.getNodeValue()), property);
                } else if("group".equals(name)) {
                    property.setGroup(Boolean.parseBoolean(attributeNode.getNodeValue()));
                }
            }
        }

⌨️ 快捷键说明

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