All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.tool.bean.BeanInfo Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package com.clickntap.tool.bean;

import com.clickntap.tool.jdbc.JdbcBlobber;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanInfo {
    private String beanName;

    private String cacheName;

    private String createScript;

    private String currValScript;

    private String updateScript;

    private String deleteScript;

    private Map executeScriptMap;

    private Map readScriptMap;

    private Map readListScriptMap;

    private Map blobInfoMap;

    private ValidationInfo validationInfo;

    public BeanInfo(Document document, String beanName, BeanInfo beanInfo) throws Exception {
        if (document != null) {
            this.beanName = beanName;
            Element root = document.getRootElement();
            cacheName = root.attributeValue("cache");
            createScript = getText(root, "create");
            currValScript = getText(root, "curr-val");
            updateScript = getText(root, "update");
            deleteScript = getText(root, "delete");
            executeScriptMap = new HashMap();
            loadScriptMap(root, "execute", executeScriptMap, beanInfo);
            readScriptMap = new HashMap();
            loadScriptMap(root, "read", readScriptMap, beanInfo);
            readListScriptMap = new HashMap();
            loadScriptMap(root, "read-list", readListScriptMap, beanInfo);

            List elementList;
            blobInfoMap = new HashMap();
            elementList = root.elements("blob");
            for (Element element : elementList) {
                blobInfoMap.put(element.attributeValue("db"), new BlobInfo(element));
            }
            Element element = root.element("validation");
            if (element != null)
                validationInfo = new ValidationInfo(element);
        }
    }

    private String getText(Element root, String tagName) {
        Element element;
        return (element = root.element(tagName)) != null ? element.getTextTrim() : null;
    }

    private void loadScriptMap(Element root, String tagName, Map map, BeanInfo beanInfo) {
        if (beanInfo != null && beanInfo.getExecuteScriptMap() != null) {
            for (String key : beanInfo.getExecuteScriptMap().keySet()) {
                map.put(key, beanInfo.getExecuteScriptMap().get(key));
            }
        }
        if (root != null) {
            Map beanMap = new HashMap();
            List elementList;
            elementList = root.elements(tagName);
            for (Element element : elementList) {
                String key = element.attributeValue("name");
                beanMap.put(key, element.getTextTrim());
            }
            for (String key : beanMap.keySet()) {
                map.put(key, beanMap.get(key));
            }
        }
    }

    public boolean isCacheEnabled() {
        return getCacheName() != null;
    }

    public String getCacheName() {
        return cacheName;
    }

    public String getCreateScript() {
        return createScript;
    }

    public String getCurrValScript() {
        return currValScript;
    }

    public String getReadScript(String key) {
        return readScriptMap.get(key);
    }

    public String getExecuteScript(String key) {
        return executeScriptMap.get(key);
    }

    public String getDeleteScript() {
        return deleteScript;
    }

    public String getUpdateScript() {
        return updateScript;
    }

    public String getReadListScript(String key) {
        return readListScriptMap.get(key);
    }

    public ValidationInfo getValidationInfo() {
        return validationInfo;
    }

    public String getBeanName() {
        return beanName;
    }

    public BlobInfo getBlobInfo(String db) {
        if (!blobInfoMap.containsKey(db))
            throw new RuntimeException("unsupported database '" + db + "'");
        return blobInfoMap.get(db);
    }

    public Map getExecuteScriptMap() {
        return executeScriptMap;
    }

    public class BlobInfo {
        private JdbcBlobber blobber;

        private List updateScriptList;

        private String readScript;

        public BlobInfo(Element root) throws Exception {
            blobber = (JdbcBlobber) Class.forName(root.attributeValue("class")).newInstance();
            List elementList = root.elements("update");
            updateScriptList = new ArrayList(elementList.size());
            for (Element element : elementList) {
                updateScriptList.add(element.getTextTrim());
            }
            Element element = root.element("read");
            if (element != null) {
                readScript = element.getTextTrim();
            }
        }

        public JdbcBlobber getBlobber() {
            return blobber;
        }

        public List getUpdateScriptList() {
            return updateScriptList;
        }

        public String getReadScript() {
            return readScript;
        }

    }

    public class ValidationInfo {
        private Map> validationScriptListMap;

        public ValidationInfo(Element root) throws Exception {
            List elementList = root.elements("group");
            validationScriptListMap = new HashMap>(elementList.size());
            for (Element element : elementList) {
                String[] keys = StringUtils.commaDelimitedListToStringArray(element.attributeValue("name"));
                for (String key : keys) {
                    List scriptList = getValidationScriptList(key);
                    scriptList.add(element.getTextTrim());
                }
            }
        }

        public List getValidationScriptList(String key) {
            if (validationScriptListMap.containsKey(key)) {
                return validationScriptListMap.get(key);
            } else {
                List validationScriptList = new ArrayList();
                validationScriptListMap.put(key, validationScriptList);
                return validationScriptList;
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy