com.wabacus.config.resource.dataimport.DataImportRes.java Source code

Java tutorial

Introduction

Here is the source code for com.wabacus.config.resource.dataimport.DataImportRes.java

Source

/* 
 * Copyright (C) 2010---2014 (wuweixing)<349446658@qq.com>
 * 
 * This file is part of Wabacus 
 * 
 * Wabacus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wabacus.config.resource.dataimport;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;

import com.wabacus.config.Config;
import com.wabacus.config.ConfigLoadManager;
import com.wabacus.config.resource.AbsResource;
import com.wabacus.config.resource.dataimport.configbean.AbsDataImportConfigBean;
import com.wabacus.config.resource.dataimport.configbean.ColumnMapBean;
import com.wabacus.exception.WabacusConfigLoadingException;
import com.wabacus.system.assistant.FilePathAssistant;
import com.wabacus.system.dataimport.interceptor.IDataImportInterceptor;
import com.wabacus.util.Consts_Private;
import com.wabacus.util.Tools;

public class DataImportRes extends AbsResource {
    public Object getValue(Element itemElement) {
        String key = itemElement.attributeValue("key");
        Element eleDataImport = itemElement.element("dataimport");
        if (eleDataImport == null) {
            throw new WabacusConfigLoadingException("???" + key
                    + "???<dataimport/>?");
        }
        String filetype = eleDataImport.attributeValue("filetype");
        AbsDataImportConfigBean dataimportcbean = AbsDataImportConfigBean.createDataImportConfigBean(key, filetype);
        String filename = eleDataImport.attributeValue("filename");
        if (filename != null) {
            dataimportcbean.setFilename(filename.trim());
        }
        String tablename = eleDataImport.attributeValue("tablename");
        if (tablename == null || tablename.trim().equals("")) {
            throw new WabacusConfigLoadingException(
                    "KEY" + key + "???tablename");
        }
        dataimportcbean.setTablename(tablename.trim());

        String importtype = eleDataImport.attributeValue("importtype");
        if (importtype == null || importtype.trim().equals(""))
            importtype = Consts_Private.DATAIMPORTTYPE_OVERWRITE;
        importtype = importtype.toLowerCase().trim();
        if (!importtype.equals(Consts_Private.DATAIMPORTTYPE_APPEND)
                && !importtype.equals(Consts_Private.DATAIMPORTTYPE_OVERWRITE)) {
            throw new WabacusConfigLoadingException(
                    "KEY" + key + "???importtype");
        }
        dataimportcbean.setImporttype(importtype);
        String keyfields = eleDataImport.attributeValue("keyfields");
        if (keyfields != null && !keyfields.trim().equals("")) {
            dataimportcbean.setLstKeyfields(Tools.parseStringToList(keyfields.toUpperCase(), ";", false));
        }
        String filepath = eleDataImport.attributeValue("filepath");
        if (filepath == null || filepath.trim().equals("")) {
            filepath = Config.getInstance().getSystemConfigValue("default-dataimport-filepath", "");
            if (filepath.equals(""))
                throw new WabacusConfigLoadingException("KEY" + key
                        + "??wabacus.cfg.xml?default-dataimoprt-filepath??filepath");
        }
        dataimportcbean.setFilepath(FilePathAssistant.getInstance().standardFilePath(filepath.trim()));
        String datasource = eleDataImport.attributeValue("datasource");
        if (datasource != null) {
            dataimportcbean.setDatasource(datasource.trim());
        }
        String interceptor = eleDataImport.attributeValue("interceptor");
        if (interceptor != null && !interceptor.trim().equals("")) {
            try {
                Object o = ConfigLoadManager.currentDynClassLoader.loadClassByCurrentLoader(interceptor.trim())
                        .newInstance();
                if (!(o instanceof IDataImportInterceptor)) {
                    throw new WabacusConfigLoadingException(
                            "KEY" + key + "????"
                                    + IDataImportInterceptor.class.getName() + "");
                }
                dataimportcbean.setInterceptor((IDataImportInterceptor) o);
            } catch (Exception e) {
                throw new WabacusConfigLoadingException("KEY" + key
                        + "???" + interceptor + "",
                        e);
            }
        }
        String autodetect = eleDataImport.attributeValue("autodetect");
        if (autodetect == null || !autodetect.trim().equals("false")) {
            Config.getInstance().addAutoDetectedDataImportBean(filepath, dataimportcbean);
        }

        Element eleColumnmap = eleDataImport.element("columnmap");
        if (eleColumnmap == null) {
            throw new WabacusConfigLoadingException(
                    "KEY" + key + "???<columnmap/>");
        }
        ColumnMapBean cmbean = new ColumnMapBean(dataimportcbean);
        String matchmode = eleColumnmap.attributeValue("matchmode");
        if (matchmode != null) {
            matchmode = matchmode.toLowerCase().trim();
            if (matchmode.equals(""))
                matchmode = Consts_Private.DATAIMPORT_MATCHMODE_INITIAL;
            if (!Consts_Private.LST_DATAIMPORT_MATCHMODES.contains(matchmode)) {
                throw new WabacusConfigLoadingException("KEY" + key
                        + "???matchmode" + matchmode + "??");
            }
            cmbean.setMatchmode(matchmode);
        }
        String type = eleColumnmap.attributeValue("type");
        if (type == null || type.trim().equals("")) {
            throw new WabacusConfigLoadingException("KEY" + key
                    + "??<columnmap/>?type");
        }
        type = type.trim().toLowerCase();
        List<String> lstTypes = Tools.parseStringToList(type, "=", false);
        if (lstTypes.size() == 1) {
            String exclusive = eleColumnmap.attributeValue("exclusive");
            List<String> lstExclusives = null;
            if (exclusive != null && !exclusive.trim().equals("")) {
                lstExclusives = Tools.parseStringToList(exclusive.toUpperCase(), ";", false);
            }
            if (lstTypes.get(0).equals("name")) {
                cmbean.setMaptype(ColumnMapBean.MAPTYPE_NAME);
                cmbean.setLstExclusiveColumns(lstExclusives);
            } else if (lstTypes.get(0).equals("index")) {
                cmbean.setMaptype(ColumnMapBean.MAPTYPE_INDEX);
                if (lstExclusives != null) {
                    List lstTemp = new ArrayList();
                    for (String colTmp : lstExclusives) {
                        if (colTmp.trim().equals(""))
                            continue;
                        lstTemp.add(Integer.parseInt(colTmp.trim()));
                    }
                    if (lstTemp.size() > 0) {
                        cmbean.setLstExclusiveColumns(lstTemp);
                    }
                }
            } else {
                throw new WabacusConfigLoadingException("KEY" + key
                        + "??<columnmap/>?type" + type
                        + "??");
            }
        } else if (lstTypes.size() == 2) {
            String dbcoltype = lstTypes.get(0).trim();
            String filecoltype = lstTypes.get(1).trim();
            String columnmaps = eleColumnmap.getText();
            if (columnmaps == null || columnmaps.trim().equals("")) {
                throw new WabacusConfigLoadingException("KEY" + key
                        + "??<columnmap/>?type" + type
                        + "?");
            }
            columnmaps = Tools.formatStringBlank(columnmaps);
            if (dbcoltype.equals("name") && filecoltype.equals("name")) {
                cmbean.setMaptype(ColumnMapBean.MAPTYPE_NAME_NAME);
            } else if (dbcoltype.equals("name") && filecoltype.equals("index")) {
                cmbean.setMaptype(ColumnMapBean.MAPTYPE_NAME_INDEX);
            } else if (dbcoltype.equals("index") && filecoltype.equals("name")) {
                cmbean.setMaptype(ColumnMapBean.MAPTYPE_INDEX_NAME);
            } else if (dbcoltype.equals("index") && filecoltype.equals("index")) {
                cmbean.setMaptype(ColumnMapBean.MAPTYPE_INDEX_INDEX);
            } else {
                throw new WabacusConfigLoadingException("KEY" + key
                        + "??<columnmap/>?type" + type
                        + "??");
            }
            cmbean.parseColMaps(key, columnmaps);
        } else {
            throw new WabacusConfigLoadingException("KEY" + key
                    + "??<columnmap/>?type??");
        }
        dataimportcbean.setColMapBean(cmbean);
        dataimportcbean.loadConfig(eleDataImport);
        dataimportcbean.doPostLoad();
        dataimportcbean.buildImportSqls();
        return dataimportcbean;
    }
}