com.pureinfo.studio.db.xls2srm.impl.XlsImportRunner.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.studio.db.xls2srm.impl.XlsImportRunner.java

Source

/**
 * PureInfo Quake
 * @(#)XlsImportRunner.java   1.0 2005-9-20
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.studio.db.xls2srm.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.dom4j.Element;

import com.pureinfo.ark.ArkExceptionTypes;
import com.pureinfo.ark.content.ArkContentHelper;
import com.pureinfo.ark.content.domain.IContentMgr;
import com.pureinfo.ark.content.model.ArkContent;
import com.pureinfo.dolphin.DolphinHelper;
import com.pureinfo.dolphin.context.LocalContextHelper;
import com.pureinfo.dolphin.mapping.EntityMetadata;
import com.pureinfo.dolphin.mapping.PropertyMetadata;
import com.pureinfo.dolphin.model.DolphinObject;
import com.pureinfo.dolphin.model.DolphinUtil;
import com.pureinfo.dolphin.model.IObjects;
import com.pureinfo.dolphin.persister.ISession;
import com.pureinfo.dolphin.persister.IStatement;
import com.pureinfo.force.ForceConstants;
import com.pureinfo.force.PureFactory;
import com.pureinfo.force.PureSystem;
import com.pureinfo.force.container.IClearable;
import com.pureinfo.force.encrypt.PasswordEncrypter;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.lang.NumberUtil;
import com.pureinfo.force.lang.StrUtil;
import com.pureinfo.force.object.DataTypes;
import com.pureinfo.force.xml.XMLUtil;
import com.pureinfo.importer.ref.IImportorRef;
import com.pureinfo.srm.auth.domain.IPersonMgr;
import com.pureinfo.srm.auth.domain.ISRMUserMgr;
import com.pureinfo.srm.auth.model.Person;
import com.pureinfo.srm.auth.model.SRMUser;
import com.pureinfo.srm.citepaper.model.CitePaper;
import com.pureinfo.srm.patent.model.PatentFee;
import com.pureinfo.srm.product.ProductConstants;
import com.pureinfo.srm.product.domain.IProductMgr;
import com.pureinfo.srm.product.model.Product;
import com.pureinfo.srm.project.ProjectCodeGenerator;
import com.pureinfo.srm.xls2srm.model.Xls2srmForm;
import com.pureinfo.studio.db.xls2srm.IConvert2RelativeTable;

/**
 * <P>
 * Created on 2005-9-20 13:17:26 <BR>
 * Last modified on 2005-9-20
 * </P>
 * XlsImportRunner: export SRMUser from old RWSK database. author Why
 * 
 * @version 1.0, 2005-9-20
 * @since Quake 1.0
 */
public class XlsImportRunner implements IClearable {
    // logger
    private final static Logger logger = Logger.getLogger(XlsImportRunner.class.getName());

    private EntityMetadata m_entityMetadata;

    // connection provider
    private String m_sProvider;

    // configuration
    private Element m_xmlConfig;

    private Map m_pageValue = new HashMap();

    // runtime cache
    private ISession m_sessionFrom;

    private ISession m_sessionTo;

    /**
     * Constructor
     */
    public XlsImportRunner() {
        super();
    }

    /**
     * Constructor
     * 
     * @param _sProvider
     *            connection provider
     */
    public XlsImportRunner(String _sProvider) {
        this.setProvider(_sProvider);
    }

    /**
     * @see com.pureinfo.force.container.IClearable#clear()
     */
    public void clear() {
        if (m_sessionFrom != null) {
            m_sessionFrom.closeQuietly(System.err);
        }
        if (m_sessionTo != null) {
            m_sessionTo.closeQuietly(System.err);
        }
    }

    /**
     * Returns the provider.
     * 
     * @return the provider.
     */
    public String getProvider() {
        return m_sProvider;
    }

    /**
     * Sets the provider.
     * 
     * @param _sProvider
     *            the provider to set.
     */
    public void setProvider(String _sProvider) {
        m_sProvider = _sProvider;
    }

    public void config(Element _xmlConfig) throws PureException {
        m_xmlConfig = _xmlConfig;
        String sClass = m_xmlConfig.attributeValue("class");
        m_entityMetadata = DolphinHelper.lookupEntityMetadataByName(sClass, true);
    }

    /**
     * Returns the entityMetadata.
     * 
     * @return the entityMetadata.
     */
    public EntityMetadata getEntityMetadata() {
        return m_entityMetadata;
    }

    public boolean isToTemp() throws PureException {
        String sClass = m_xmlConfig.attributeValue("temp");
        if (sClass != null && "true".equals(sClass)) {
            return true;
        }
        return false;
    }

    /**
     * @throws Exception
     */
    public int run(String _sFileName, List _errorDataList, List _repeatList) throws PureException {
        return run(_sFileName, _errorDataList, _repeatList, false, Xls2srmForm.JUSTFORCHECK);
    }

    /**
     * @throws Exception
     */
    public int run(String _sFileName, List _errorDataList, List _repeatList, boolean _bBreakenIfError,
            int _nChooseIfRepeat) throws PureException {

        if (m_xmlConfig == null) {
            throw new PureException(PureException.SETTING_MISSING, "please call config() first");
        }

        long lStart = System.currentTimeMillis();
        IObjects objs = null;
        DolphinObject oldObj, newObj;
        Class clazz = m_entityMetadata.getEntityClass();

        long lThisTime = System.currentTimeMillis();
        String sThisTime = getNow(String.valueOf(lThisTime));

        int nCount = 1;

        boolean isToTemp = isToTemp();
        try {
            prepare();

            objs = this.loadOld(_sFileName);

            String sViewProperty = m_xmlConfig.attributeValue("view-property");
            if (sViewProperty == null || (sViewProperty = sViewProperty.trim()).length() == 0) {
                sViewProperty = "ID";
            } else {
                // sViewProperty = sViewProperty.toUpperCase();
            }

            while ((oldObj = objs.next()) != null) {

                List errorDataEachList = new ArrayList();
                StringBuffer error = new StringBuffer();

                if (checkObjIsNull(oldObj, sViewProperty))
                    continue;
                oldObj = ChangePropertysToString(oldObj, sViewProperty);

                // it will be checked in the last,so at the beganning ,we will
                // new an instance.
                newObj = (DolphinObject) clazz.newInstance();

                beforeCopy(newObj);

                copy(oldObj, newObj, errorDataEachList);
                if (errorDataEachList != null) {
                    if (errorDataEachList.size() > 0) {
                        error.append("" + nCount + ":");
                        for (int i = 0; i < errorDataEachList.size(); i++) {
                            error.append(errorDataEachList.get(i));
                        }
                    }
                    errorDataEachList.clear();
                }

                convert(oldObj, newObj, errorDataEachList, _nChooseIfRepeat);

                fill(oldObj, newObj, _nChooseIfRepeat);

                // ====wind.li@2007-04-29
                // =====BEGIN===========
                errorDataEachList = checkMaybeRequiredCells(oldObj);

                if (errorDataEachList != null) {
                    for (int i = 0; i < errorDataEachList.size(); i++) {
                        error.append("" + nCount + ":").append(errorDataEachList.get(i) + "");
                    }
                    errorDataEachList.clear();
                }
                // ====END===========
                if (errorDataEachList != null) {
                    if (errorDataEachList.size() > 0) {
                        if (error.length() <= 0)
                            error.append("" + nCount + ":");
                        for (int i = 0; i < errorDataEachList.size(); i++) {
                            error.append("" + errorDataEachList.get(i) + ",");
                        }
                    }
                    errorDataEachList.clear();
                }
                String errorEach = (error.length() > 0 ? error.toString() : "");

                conversAuthors2insideAuthorInfo(newObj, oldObj);

                if (errorDataEachList == null)
                    errorDataEachList = new ArrayList();

                beforeSave(newObj, errorDataEachList);

                calcProbWeight(oldObj, newObj, _nChooseIfRepeat);

                if (errorDataEachList != null) {
                    for (int i = 0; i < errorDataEachList.size(); i++) {
                        String errorElement = errorDataEachList.get(i).toString();
                        if (errorEach.indexOf(errorElement) > 0) {
                            errorDataEachList.remove(errorElement);
                        }
                    }
                    if (errorDataEachList.size() > 0) {
                        if (error.length() <= 0)
                            error.append("" + nCount + ":");
                        for (int i = 0; i < errorDataEachList.size(); i++) {
                            error.append("" + errorDataEachList.get(i) + " ");
                        }
                    }

                    errorDataEachList.clear();
                }
                convert2RelatedTable(newObj, oldObj, true, errorDataEachList, Xls2srmForm.COVERDATAWHENREPEAT);

                if (errorDataEachList != null) {
                    if (errorDataEachList.size() > 0) {
                        if (error.length() <= 0)
                            error.append("" + nCount + ":");
                        for (int i = 0; i < errorDataEachList.size(); i++) {
                            error.append(errorDataEachList.get(i));
                        }
                    }

                    errorDataEachList.clear();
                }
                if (error.length() > 0) {
                    _errorDataList.add(error.toString());
                }

                if (error.length() > 0) {
                    error.setLength(0);
                    if (_bBreakenIfError) {
                        logger.debug("" + nCount + "");
                        break;
                    }
                    nCount++;
                    continue;

                }
                error.setLength(0);

                if (_nChooseIfRepeat == Xls2srmForm.JUSTFORCHECK) {
                    // newObj = makeNewObject2(clazz, newObj,
                    // Xls2srmForm.COVERDATAWHENREPEAT);
                } else {
                    newObj = makeNewObject2(clazz, newObj, _nChooseIfRepeat, isToTemp);
                }
                if (newObj == null) {
                    if (_nChooseIfRepeat == Xls2srmForm.MERGEDATAWHENREPEAT) {
                        _repeatList.add("" + nCount + "!");
                    } else {
                        _repeatList.add("" + nCount + "!");
                    }
                    nCount++;
                    continue;
                }

                if (_nChooseIfRepeat != Xls2srmForm.JUSTFORCHECK) {
                    try {
                        save(newObj, _errorDataList);
                    } catch (Exception e) {
                        logger.debug(":");
                        e.printStackTrace();
                        _repeatList.add("" + nCount + ":" + e.getMessage());
                        nCount++;
                        continue;
                    }

                    convert2RelatedTable(newObj, oldObj, false, errorDataEachList, _nChooseIfRepeat);
                    convertImpactFactorTo3index(newObj, oldObj);
                    convertDupTagForHistory(newObj, oldObj, sThisTime);
                    doAfterImport(oldObj, newObj, _nChooseIfRepeat);
                }

                nCount++;

            } // endwhile
            lastUpdate();

            long lEnd = System.currentTimeMillis();
            System.out.println(clazz.getName() + ": all finished - time used: " + (lEnd - lStart) + "ms");
        } catch (Exception ex) {
            throw new PureException(PureException.DATABASE_ACCESS, "class=" + clazz.getName(), ex);
        } finally {
            if (objs != null)
                objs.clear();
        }
        return nCount - 1;
    }

    /**
     * @param _sOldObj
     * @param _sNewObj
     * @param _sChooseIfRepeat
     * @throws PureException
     */
    private void doAfterImport(DolphinObject _oldObj, DolphinObject _newObj, int _nChooseIfRepeat)
            throws PureException {
        Element calc = m_xmlConfig.element("data");
        if (calc.element("after-import") == null)
            return;
        List properties = calc.element("after-import").elements();
        IImportorRef ref;
        for (Iterator iter = properties.iterator(); iter.hasNext();) {
            Element element = (Element) iter.next();
            String sRef = element.attributeValue("ref");
            if (sRef != null && sRef.startsWith("#")) {
                ref = (IImportorRef) PureFactory.getBean(sRef.substring(1));
                String toProperty = element.attributeValue("to");
                String value = (String) ref.convert(_oldObj, _newObj, element.attributeValue("name"),
                        element.attributeValue("to"), null, LocalContextHelper.currentSession("Local"),
                        m_entityMetadata, String.valueOf(_nChooseIfRepeat), null);
                if (StringUtils.isNotEmpty(toProperty)) {
                    _newObj.setProperty(toProperty, value);
                }
            }
        }

    }

    private void convertDupTagForHistory(DolphinObject _newObj, DolphinObject _oldObj, String sThisTime)
            throws PureException {
        Element convert = m_xmlConfig.element("data");
        List properties = convert.element("convert").elements();
        Element element;
        String sFrom, sClass, sRelativeInterface;
        String sTo = null;

        for (int i = 0; i < properties.size(); i++) {

            element = (Element) properties.get(i);
            if (element.attributeValue("provider") == null
                    || !(element.attributeValue("provider").equals("convertDupTagByCodeInMachine")))
                continue;
            if (_newObj instanceof Product) {
                Product product = (Product) _newObj;
                if (product.getProductForm().equals(ProductConstants.SPRODUCT_FORM_3INDEX)) {
                    _newObj.setProperty("dupTag", sThisTime);
                }
            }
            _newObj.update();
            sFrom = element.attributeValue("from");
            sRelativeInterface = element.attributeValue("provider");
            sClass = element.attributeValue("relativeClass");

            if (!_oldObj.hasProperty(sFrom)) {
                continue;
            }

            if (sFrom.equals("")) {
                sTo = _oldObj.getPropertyAsString(sFrom);
            }

            try {
                IConvert2RelativeTable conv2relative = (IConvert2RelativeTable) PureFactory
                        .getBean(sRelativeInterface);

                conv2relative.save(_newObj, null, null, null, _newObj.getPropertyAsString("dupTag"), sTo, null,
                        null);
            } catch (Exception ex) {
                throw new PureException(PureException.INVALID_VALUE,
                        "convert " + sFrom + "[" + _oldObj.getProperty(sFrom) + "] to " + sClass, ex);
            }
        }
    }

    private void calcProbWeight(DolphinObject _oldObj, DolphinObject _newObj, int _nChooseIfRepeat)
            throws PureException {
        Element calc = m_xmlConfig.element("data");
        if (calc.element("calc") == null)
            return;
        List properties = calc.element("calc").elements();
        for (Iterator iter = properties.iterator(); iter.hasNext();) {
            Element element = (Element) iter.next();
            String sRef = element.attributeValue("ref");
            if (sRef != null && sRef.startsWith("#")) {
                IImportorRef ref = (IImportorRef) PureFactory.getBean(sRef.substring(1));
                String propName = element.attributeValue("name");

                logger.debug(">>>>>>>:::::::name = " + propName);
                Double d = (Double) ref.convert(_oldObj, _newObj, propName, String.valueOf(_nChooseIfRepeat), null,
                        LocalContextHelper.currentSession("Local"), m_entityMetadata, null, null);
                _newObj.setProperty(propName, d.doubleValue());
                _newObj.update();
            }
        }
    }

    /**
     * @param _sOldObj
     * @param _sViewProperty
     * @throws PureException
     */
    private DolphinObject ChangePropertysToString(DolphinObject _sOldObj, String _sViewProperty)
            throws PureException {

        _sOldObj.removeProperty(_sViewProperty);
        Iterator itr = _sOldObj.getProperties(false).entrySet().iterator();
        Object oKey, oValue = null;
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();
            oKey = entry.getKey();
            oValue = _sOldObj.getProperty(oKey.toString());

            if (oValue != null) {

                if (oValue instanceof Double) {
                    oValue = String.valueOf(oValue);
                    // oValue = NumberUtil.format(((Number) oValue), "#");
                }

                if (oValue instanceof Date) {
                    Date datDate = (Date) oValue;
                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    oValue = new String(format.format(datDate));
                }

                if (oValue instanceof Long) {
                    oValue = NumberUtil.format(((Number) oValue), "#");
                }

                if (oValue instanceof Boolean) {
                    if (oValue.equals(Boolean.FALSE))
                        oValue = "false";

                    if (oValue.equals(Boolean.TRUE))
                        oValue = "true";

                }

                _sOldObj.setProperty(oKey.toString(), oValue);
            }
        }
        return _sOldObj;

    }

    // =========================================================================
    // inside logic

    /**
     * @param _sOldObj
     * @param _sViewProperty
     */
    private boolean checkObjIsNull(DolphinObject _sOldObj, String _sViewProperty) {

        _sOldObj.removeProperty(_sViewProperty);
        Iterator itr = _sOldObj.getProperties(false).entrySet().iterator();
        Object oKey = null;
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();
            oKey = entry.getKey();
            if (_sOldObj.getProperty(oKey.toString()) != null) {
                return false;
            }
        }
        return true;

    }

    /**
     * @param _newObj
     * @param _oldObj
     * @throws Exception
     */
    private void conversAuthors2insideAuthorInfo(DolphinObject _newObj, DolphinObject _oldObj) throws Exception {
        Element convert = m_xmlConfig.element("data");
        List properties = convert.element("convert").elements();
        Element element;
        String sFrom, value, sProvider = null;

        for (int i = 0; i < properties.size(); i++) {

            element = (Element) properties.get(i);
            sProvider = element.attributeValue("provider");
            if (sProvider == null || (!(sProvider.equals("authors2insideAuthorInfoForProduct"))))
                continue;

            // sFrom = element.attributeValue("from").toUpperCase();
            sFrom = element.attributeValue("from");
            if (!_oldObj.hasProperty(sFrom)) {
                continue;
                // throw new PureException(PureException.PROPERTY_NOTFOUND,
                // sFrom);
            }

            // else

            value = _oldObj.getStrProperty(sFrom);

            if (value != null) {
                String[] perValue = value.split(PureSystem.getProperty("srm.xls2srm.split.authors"));

                _newObj.setProperty("authorsNum", perValue == null ? 0 : perValue.length);

                try {
                    for (int j = 0; j < perValue.length; j++) {
                        String perUnitValue = perValue[j];
                        int pos = perUnitValue
                                .indexOf(PureSystem.getProperty("srm.xls2srm.split.authorType.began"));

                        if (pos > 0) {
                            continue;
                        }
                        int count = j + 1;
                        _newObj.setProperty("insideAuthorNo", count);

                        Object oUserId = this.lookupRefValue("srm_auth_user.COMMANDUSER_ID", perUnitValue,
                                "TRUE_NAME");

                        _newObj.setProperty("insideAuthorId", oUserId == null ? (new Integer(0)) : oUserId);

                        break;
                    }
                } catch (Exception ex) {
                    throw new PureException(PureException.INVALID_VALUE,
                            "convert " + sFrom + "[" + perValue.toString() + "] to " + "insideAuthorInfo", ex);
                }
            }
        }

        // Add By wind.li 06/10/26
        // provider:author2insturctorForProduct 
        // instructorinstructorTypeinstructorName
        // id
        // START----
        for (int i = 0; i < properties.size(); i++) {
            element = (Element) properties.get(i);
            sProvider = element.attributeValue("provider");
            if (sProvider == null || (!(sProvider.equals("author2insturctorForProduct"))))
                continue;

            sFrom = element.attributeValue("from");
            if (!_oldObj.hasProperty(sFrom)) {
                continue;
            }

            // else
            value = _oldObj.getStrProperty(sFrom);

            if (value != null) {
                ISRMUserMgr userMgr = (ISRMUserMgr) ArkContentHelper.getContentMgrOf(SRMUser.class);
                SRMUser user = (SRMUser) userMgr.lookupByName(value);
                if (user != null) {
                    _newObj.setProperty("instructor", user.getId());
                    _newObj.setProperty("instructorType", ProductConstants.INSTRUCTOR_TYPE_SRMUSER);
                    _newObj.setProperty("instructorName", value);
                } else {
                    IPersonMgr personMgr = (IPersonMgr) ArkContentHelper.getContentMgrOf(Person.class);
                    Person person = personMgr.lookupByName(value);
                    if (person != null) {
                        _newObj.setProperty("instructor", person.getId());
                        _newObj.setProperty("instructorType", ProductConstants.INSTRUCTOR_TYPE_PERSON);
                        _newObj.setProperty("instructorName", value);
                    } else {
                        _newObj.setProperty("instructor", 0);
                        _newObj.setProperty("instructorType", ProductConstants.INSTRUCTOR_TYPE_UNKOWN);
                        _newObj.setProperty("instructorName", value);
                    }
                }
            }
        }
        // END----

        for (int j = 0; j < properties.size(); j++) {

            element = (Element) properties.get(j);
            sProvider = element.attributeValue("provider");
            if (sProvider == null || (!(sProvider.equals("code2projectId"))))
                continue;

            sFrom = element.attributeValue("from");
            if (!_oldObj.hasProperty(sFrom)) {
                continue;
            }

            value = _oldObj.getStrProperty(sFrom);

            if (value != null) {

                try {
                    _newObj.setProperty("projectNumber", ProjectCodeGenerator.getProjectCode(value));

                } catch (Exception ex) {
                    throw new PureException(PureException.INVALID_VALUE,
                            "convert " + sFrom + "[" + value + "] to " + "insideAuthorInfo", ex);
                }
            }
        }

    }

    /**
     * @param _sNewObj
     * @throws PureException
     */
    private void convertImpactFactorTo3index(DolphinObject _newObj, DolphinObject _oldObj) throws PureException {

        Element convert = m_xmlConfig.element("data");
        List properties = convert.element("convert").elements();
        Element element;
        // Element element =convert.element("convert");
        String sFrom, sClass, sRelativeInterface;
        for (int i = 0; i < properties.size(); i++) {

            element = (Element) properties.get(i);
            if (element.attributeValue("provider") == null
                    || !(element.attributeValue("provider").equals("convertImpactFactorTo3index")))
                continue;

            // sFrom = element.attributeValue("from").toUpperCase();
            sFrom = element.attributeValue("from");
            sRelativeInterface = element.attributeValue("provider");
            sClass = element.attributeValue("relativeClass");

            if (!_oldObj.hasProperty(sFrom)) {
                continue;
            }

            try {
                // Class clazz = Class.forName(sClass);
                IConvert2RelativeTable conv2relative = (IConvert2RelativeTable) PureFactory
                        .getBean(sRelativeInterface);

                conv2relative.save(_newObj, null, null, null, null, null, null, null);
            } catch (Exception ex) {
                throw new PureException(PureException.INVALID_VALUE,
                        "convert " + sFrom + "[" + _oldObj.getProperty(sFrom) + "] to " + sClass, ex);
            }
        }
    }

    /**
     * @param _sNewObj
     * @throws PureException
     */
    private void convert2RelatedTable(DolphinObject _newObj, DolphinObject _oldObj, boolean _bCheck,
            List _errorDataEachList, int _nChooseIfRepeat) throws PureException {

        Element convert = m_xmlConfig.element("data");
        List properties = convert.element("convert").elements();
        Element element;
        // Element element =convert.element("convert");
        String sFrom, sClass, sId, sRelativeFrom, sRelativeTo, sRef, sFk, sRelativeInterface;
        for (int i = 0; i < properties.size(); i++) {

            element = (Element) properties.get(i);
            if (element.attributeValue("provider") == null
                    || !(element.attributeValue("provider").equals("convert2RelativeTableForProduct")))
                continue;

            // sFrom = element.attributeValue("from").toUpperCase();
            sFrom = element.attributeValue("from");
            sRelativeInterface = element.attributeValue("provider");
            sClass = element.attributeValue("relativeClass");
            sId = element.attributeValue("relativeId");
            sRelativeFrom = element.attributeValue("relativeFrom");
            sRelativeTo = element.attributeValue("relativeTo");
            sRef = element.attributeValue("ref");
            sFk = element.attributeValue("fk");

            // to convert property value
            if (!_oldObj.hasProperty(sFrom)) {
                continue;
                // throw new PureException(PureException.PROPERTY_NOTFOUND,
                // sFrom);
            }

            // else
            try {
                Class clazz = Class.forName(sClass);
                IConvert2RelativeTable conv2relative = (IConvert2RelativeTable) PureFactory
                        .getBean(sRelativeInterface);
                if (_bCheck) {
                    conv2relative.check(sFrom, _oldObj.getProperty(sFrom), sRef, sFk, _errorDataEachList);
                } else {
                    if (_nChooseIfRepeat == Xls2srmForm.COVERDATAWHENREPEAT) {
                        conv2relative.delete(_newObj, sId, clazz);
                    }
                    conv2relative.save(_newObj, _oldObj.getProperty(sFrom), clazz, sId, sRelativeFrom, sRelativeTo,
                            sRef, sFk);
                }
            } catch (Exception ex) {
                throw new PureException(PureException.INVALID_VALUE,
                        "convert " + sFrom + "[" + _oldObj.getProperty(sFrom) + "] to " + sClass, ex);
            }
        }
    }

    private DolphinObject makeNewObject2(Class _clazz, DolphinObject _newObj, int _nChooseIfRepeat,
            boolean isToTemp) throws Exception {
        DolphinObject newObj = null;
        StringBuffer sbuff = null;
        boolean bStringProperty;
        String strSQL = m_xmlConfig.elementTextTrim("match-properties");
        if (strSQL != null && strSQL.length() > 0) {
            StringTokenizer st = new StringTokenizer(strSQL, ",", false);
            // 
            if (isToTemp) {
                sbuff = new StringBuffer("select * from {this}0 where ");
            } else {
                sbuff = new StringBuffer("select * from {this} where ");
            }
            try {
                String sValue;
                while (st.hasMoreElements()) {
                    sValue = (String) st.nextElement();

                    // sValue = StrUtil.sqlEncode(sValue);
                    if (_newObj.getProperty(sValue) != null) {

                        bStringProperty = _newObj.getProperty(sValue) instanceof String
                                || _newObj.getProperty(sValue) instanceof Date;
                        if ((bStringProperty && _newObj.getPropertyAsString(sValue).trim().length() > 0)
                                || (!bStringProperty)) {
                            sbuff.append("{this.");
                            sbuff.append(sValue);
                            sbuff.append("}=");

                            if (bStringProperty)
                                sbuff.append("'");

                            String sPropertyValue = _newObj.getPropertyAsString(sValue);
                            //  
                            // sPropertyValue = sPropertyValue.replaceAll(":",
                            // "");
                            while (sPropertyValue.indexOf("{") >= 0) {
                                sPropertyValue = sPropertyValue.substring(0, sPropertyValue.indexOf("{"))
                                        + sPropertyValue.substring(sPropertyValue.indexOf("{") + 1,
                                                sPropertyValue.length());
                            }
                            while (sPropertyValue.indexOf("}") >= 0) {
                                sPropertyValue = sPropertyValue.substring(0, sPropertyValue.indexOf("}"))
                                        + sPropertyValue.substring(sPropertyValue.indexOf("}") + 1,
                                                sPropertyValue.length());
                            }
                            sPropertyValue = StrUtil.escapeEncode(sPropertyValue);
                            sPropertyValue = StrUtil.sqlEncode(sPropertyValue);
                            sPropertyValue = removeBlank(sPropertyValue);

                            sbuff.append(sPropertyValue);
                            if (bStringProperty)
                                sbuff.append("'");
                            sbuff.append(" and ");
                        }
                    }
                }
                int nLength = sbuff.length() - 5;
                sbuff.setLength(nLength);
                strSQL = sbuff.toString();
            } finally {
                sbuff.setLength(0);
            }

            ISession session = getSession();
            logger.debug("to make new obj from sql:" + strSQL);
            IStatement query = session.createQuery(strSQL, _clazz, 1);
            IObjects objs = null;
            try {
                objs = query.executeQuery(false);
                newObj = objs.next();
                logger.debug(">>>>>>>>>>>>" + (newObj == null));
            } finally {
                DolphinHelper.clear(objs, query);
            }
        }

        if (newObj == null) {
            if (_nChooseIfRepeat == Xls2srmForm.MERGEDATAWHENREPEAT) {
                newObj = null;
            } else {
                newObj = _newObj;
            }
        } else {
            switch (_nChooseIfRepeat) {
            // 
            case Xls2srmForm.NEWDATAWHENREPEAT:
                newObj = _newObj;
                break;
            // 
            case Xls2srmForm.LEAPDATAWHENREPEAT:
                newObj = null;
                break;
            // 
            case Xls2srmForm.COVERDATAWHENREPEAT:
                String sExcludeProps = m_xmlConfig.elementTextTrim("exclude-props-overrite");
                if (sExcludeProps == null || sExcludeProps.length() == 0) {
                    DolphinUtil.copyUpdateableProperties(_newObj, newObj);
                } else {
                    Iterator iter = _newObj.getProperties(false).entrySet().iterator();
                    sExcludeProps = ',' + sExcludeProps + ',';
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        String sName = (String) entry.getKey();
                        if (entry.getValue() != null && sExcludeProps.indexOf("," + sName + ",") < 0) {
                            newObj.setProperty(sName, entry.getValue());
                        }
                    }
                }
                break;
            // 
            case Xls2srmForm.MERGEDATAWHENREPEAT: {
                String strMatchSQL = m_xmlConfig.elementTextTrim("merge-properties");
                if (strMatchSQL != null && strMatchSQL.length() > 0) {
                    StringTokenizer st2 = new StringTokenizer(strMatchSQL, ",", false);
                    String sValue2;
                    while (st2.hasMoreElements()) {
                        sValue2 = (String) st2.nextElement();
                        if (_newObj.getProperty(sValue2) != null) {
                            newObj.setProperty(sValue2, _newObj.getProperty(sValue2));
                        }
                    }
                }
                // newObj.update();
                break;
            }

            default:
                throw new PureException(PureException.INVALID_VALUE, "");
            }

        }
        return newObj;
    }

    private ISession getSession() throws Exception {
        if (m_sessionFrom == null) {
            m_sessionFrom = LocalContextHelper.currentSession(m_sProvider);
        }
        return m_sessionFrom;
    }

    private void prepare() throws Exception {
        this.executeSQLElement("prepare");
    }

    /**
     * Loads the old objects collection.
     * 
     * @return the old objects collection
     * @throws Exception
     */
    private IObjects loadOld(String _sFileName) throws PureException {
        XlsObjectsImpl objs = new XlsObjectsImpl(_sFileName);

        return objs;
    }

    /**
     * @param _sHeads
     * @throws PureException
     */
    private List checkExcelHead(String[] _sHeads) throws PureException {

        List needHeadList = new ArrayList();
        List sHeadList = new ArrayList();
        List lostHeadList = new ArrayList();
        String _sRequiredCells;
        String[] _sRequiredCellsSplit;
        try {

            _sRequiredCells = m_xmlConfig.elementTextTrim("required-cells");
            if (_sRequiredCells.length() > 0) {
                _sRequiredCellsSplit = _sRequiredCells.split(",");
                for (int i = _sRequiredCellsSplit.length - 1; i >= 0; i--) {
                    needHeadList.add(_sRequiredCellsSplit[i]);
                }

                for (int i = _sHeads.length - 1; i >= 0; i--) {
                    sHeadList.add(_sHeads[i]);
                }

                for (int i = needHeadList.size(); i > 0; i--) {
                    if (!sHeadList.contains(needHeadList.get(i - 1))) {
                        lostHeadList.add(needHeadList.get(i - 1));
                    }
                }
            }
            return lostHeadList;
        } finally {
            if (needHeadList != null)
                needHeadList.clear();
            if (sHeadList != null)
                sHeadList.clear();
        }
    }

    private void beforeCopy(DolphinObject _newObj) {
        Element data = m_xmlConfig.element("data");
        Element beforeCopy = data.element("before-copy");
        if (beforeCopy == null)
            return;
        List properties = beforeCopy.elements();
        for (Iterator iter = properties.iterator(); iter.hasNext();) {
            Element element = (Element) iter.next();
            String sType = element.attributeValue("type");
            if ("int".equals(sType)) {
                _newObj.setProperty(element.attributeValue("name"),
                        Integer.parseInt(element.attributeValue("value")));
            } else {
                _newObj.setProperty(element.attributeValue("name"), element.attributeValue("value"));
            }
        }
    }

    /**
     * Copys the properties form the old object.
     * 
     * @param _oldObj
     * @param _newObj
     * @throws Exception
     */
    private void copy(DolphinObject _oldObj, DolphinObject _newObj, List _errorCopyList) throws Exception {
        Element copy = m_xmlConfig.element("data");
        List properties = copy.element("copy").elements();
        Element element;
        String sFrom, sTo, sReg = null, sValidatorDesc = null;
        PropertyMetadata property = null;
        SimpleDateFormat format = new SimpleDateFormat(ForceConstants.DATETIME_FORMAT_STR);
        String sValue;
        boolean bBool = false, bPassword = false;
        for (int i = 0; i < properties.size(); i++) {
            element = (Element) properties.get(i);
            sTo = element.attributeValue("to");
            if (sTo.startsWith("#")) {
                if (_newObj instanceof Product) {
                    sFrom = element.attributeValue("from");
                    sValue = _oldObj.getPropertyAsString(sFrom);
                    if (StringUtils.isNotEmpty(sValue)) {
                        sValue = StrUtil.sqlEncode(sValue);
                        _newObj.setProperty(sTo.substring(1), sValue);
                        _newObj.update();
                        String sCode = _newObj.getPropertyAsString("codeInMachine");
                        String weightYear = _newObj.getPropertyAsString("weightYear");
                        IProductMgr mgr = (IProductMgr) ArkContentHelper.getContentMgrOf(Product.class);
                        String sql = "update srm_prj_obj_dept_mapping";
                        sql += " set weight_year = " + weightYear;
                        sql += " ,LAST_MODIFIED_TIME = '" + format.format(new Date());
                        sql += "' where obj_id = (select product_id from srm_prj_product where CODE_IN_MACHINE = '"
                                + sCode + "')";
                        sql += " and obj_type = 4102";
                        IStatement query = mgr.createQuery(sql, 1);
                        query.executeUpdate();
                        query.clear(false);

                        String sql2 = "update srm_prj_obj_team_mapping";
                        sql2 += " set weight_year = " + weightYear;
                        sql2 += " , LAST_MODIFIED_TIME = '" + format.format(new Date());
                        sql2 += "' where obj_id = (select product_id from srm_prj_product where CODE_IN_MACHINE = '"
                                + sCode + "')";
                        sql2 += " and obj_type = 4102";
                        query = mgr.createQuery(sql2, 1);
                        query.executeUpdate();
                        query.clear(false);

                        String sql3 = "update srm_prj_obj_user_mapping";
                        sql3 += " set weight_year = " + weightYear;
                        sql3 += " , LAST_MODIFIED_TIME = '" + format.format(new Date());
                        sql3 += "' where obj_id = (select product_id from srm_prj_product where CODE_IN_MACHINE = '"
                                + sCode + "')";
                        sql3 += " and obj_type = 4102";
                        query = mgr.createQuery(sql3, 1);
                        query.executeUpdate();
                        if (query != null)
                            query.clear();
                    }
                } else if (_newObj instanceof CitePaper) {
                    sFrom = element.attributeValue("from");
                    sValue = _oldObj.getPropertyAsString(sFrom);
                    if (StringUtils.isNotEmpty(sValue)) {
                        sValue = StrUtil.sqlEncode(sValue);
                        _newObj.setProperty(sTo.substring(1), sValue);
                        _newObj.update();
                        String weightYear = _newObj.getPropertyAsString("weightYear");
                        String sCode = _newObj.getPropertyAsString("serialNo");
                        IProductMgr mgr = (IProductMgr) ArkContentHelper.getContentMgrOf(Product.class);
                        String sql = "update srm_prj_obj_dept_mapping";
                        sql += " set weight_year = " + weightYear;
                        sql += " , LAST_MODIFIED_TIME = '" + format.format(new Date());
                        sql += "' where obj_id = (select cite_paper_id from srm_product_cite_paper where serial_no = '"
                                + sCode + "')";
                        sql += " and obj_type = 4110";
                        IStatement query = mgr.createQuery(sql, 1);
                        query.executeUpdate();
                        query.clear(false);

                        String sql2 = "update srm_prj_obj_team_mapping";
                        sql2 += " set weight_year = " + weightYear;
                        sql2 += " , LAST_MODIFIED_TIME = '" + format.format(new Date());
                        sql2 += "' where obj_id = (select cite_paper_id from srm_product_cite_paper where serial_no = '"
                                + sCode + "')";
                        sql2 += " and obj_type = 4110";
                        query = mgr.createQuery(sql2, 1);
                        query.executeUpdate();
                        query.clear(false);

                        String sql3 = "update srm_prj_obj_user_mapping";
                        sql3 += " set weight_year = " + weightYear;
                        sql3 += " , LAST_MODIFIED_TIME = '" + format.format(new Date());
                        sql3 += "' where obj_id = (select cite_paper_id from srm_product_cite_paper where serial_no = '"
                                + sCode + "')";
                        sql3 += " and obj_type = 4110";
                        query = mgr.createQuery(sql3, 1);
                        query.executeUpdate();
                        if (query != null)
                            query.clear();
                    }
                }
            } else if (!isPropertySet(_newObj, sTo)) {
                property = m_entityMetadata.lookupPropertyByName(sTo, true);
                // skip the property which is not updateable when inserting
                if (_newObj.isValidInstance()) {

                    if (!property.isUpdateable()) {
                        continue;
                    }
                }
                sReg = property.getValidator4Java();
                sValidatorDesc = property.getValidatorDesc();

                if (sReg == null) {
                    if (element.attributeValue("format") != null
                            && element.attributeValue("format").equals("int")) {
                        sReg = "[0-9]+(.[0-9]+)?";
                        sValidatorDesc = "";
                    }
                    if (element.attributeValue("format") != null
                            && element.attributeValue("format").equals("date")) {
                        sReg = "[0-9]{1,4}\\-[0-9]{1,2}\\-[0-9]{1,2}";
                        sValidatorDesc = ":1949-10-1";
                    }

                }

                if (element.attributeValue("format") != null && element.attributeValue("format").equals("bool")) {
                    bBool = true;
                }

                if (element.attributeValue("format") != null
                        && element.attributeValue("format").equals("#password2str")) {
                    bPassword = true;
                }
                // sFrom = element.attributeValue("from").toUpperCase();
                sFrom = element.attributeValue("from");
                sValue = _oldObj.getPropertyAsString(sFrom);

                // subString
                if (element.attributeValue("subString") != null) {
                    String sSubStringValue = element.attributeValue("subString");
                    int nBeganIndex = 0;
                    int nEndIndex = sSubStringValue.length() - 1;

                    if (sSubStringValue.indexOf(",") > 0) {
                        String sBeganIndex = sSubStringValue.substring(0, sSubStringValue.indexOf(","));
                        nBeganIndex = Integer.parseInt(sBeganIndex);
                        String sEndIndex = sSubStringValue.substring(sSubStringValue.indexOf(",") + 1);
                        nEndIndex = Integer.parseInt(sEndIndex);
                    }
                    if (sValue != null && sValue.length() >= nEndIndex) {
                        sValue = sValue.substring(nBeganIndex, nEndIndex);
                    }
                }

                if (sValue != null && sFrom != null && sValue.equals(">10.0") && sFrom.equals("CitedHalfLife"))
                    sValue = "11";
                if (sValue != null) {
                    if (DataTypes.DATE == property.getDataType()) {
                        if (sValue.length() > 10) {
                            sValue = sValue.substring(0, 10);
                        }
                    }

                    if (sReg != null) {
                        if ((sValue.length() > 0) && (!sValue.matches(sReg))) {
                            _errorCopyList.add(element.attributeValue("from") + ":" + sValue + "1"
                                    + (sValidatorDesc == null ? "" : sValidatorDesc + ";"));
                            continue;
                        }
                    }

                    if (bBool) {

                        if (sValue.equals(""))
                            sValue = "true";

                        if (sValue.equals("") || StringUtils.isEmpty(sValue))
                            sValue = "false";
                    }

                    if (bPassword)
                        sValue = PasswordEncrypter.encode(sValue);

                    sValue = StrUtil.sqlEncode(sValue);

                    try {
                        boolean bInt = DataTypes.isInteger(m_entityMetadata.getProperty(sTo, true).getDataType());
                        if (bInt) {
                            int nValue = (int) Double.parseDouble(sValue);
                            _newObj.setProperty(sTo, nValue);
                        } else {
                            DolphinUtil.setPropertyWithString(_newObj, m_entityMetadata, sTo, sValue);
                        }

                    } catch (Exception ex) {
                        _errorCopyList.add("" + element.attributeValue("from") + ":" + sValue + ""
                                + (sValidatorDesc == null ? "" : sValidatorDesc + ";"));
                        continue;
                    }

                }

            }
        }

    }

    /**
     * Returns <code>true</code> if the specified property has been set.
     * 
     * @param _obj
     *            a dolphin object
     * @param _sProperty
     *            property name
     * @return <code>true</code> if the specified property has been set;
     *         <code>false</code>, otherwise.
     */
    public static boolean isPropertySet(DolphinObject _obj, String _sProperty) {
        if (_obj.isValidInstance()) {
            return _obj.getNewProperty(_sProperty) != null;
        }
        // else
        return _obj.getProperty(_sProperty) != null;
    }

    /**
     * Converts the properties whose type are different from SRM.
     * 
     * @param _oldObj
     * @param _newObj
     * @throws Exception
     */
    private boolean convert(DolphinObject _oldObj, DolphinObject _newObj, List _errorDataList, int _nChooseIfRepeat)
            throws Exception {
        Element convert = m_xmlConfig.element("data");
        List properties = convert.element("convert").elements();
        Element element;
        String sFrom, sTo, sRef, sForeignKey;
        Object value = null;
        Object valueFrom = null;
        boolean bConvertError = false;
        boolean bvalidate = true;

        for (int i = 0; i < properties.size(); i++) {
            element = (Element) properties.get(i);
            logger.debug(
                    element.attributeValue("from") + ":" + _oldObj.getProperty(element.attributeValue("from")));
            if (element.attributeValue("provider") != null) {
                sRef = element.attributeValue("ref");
                sFrom = element.attributeValue("from");
                sTo = element.attributeValue("to");
                if (sRef != null && sRef.charAt(0) == '#') {
                    // if ( _nChooseIfRepeat != Xls2srmForm.JUSTFORCHECK
                    IImportorRef ref = (IImportorRef) PureFactory.getBean(sRef.substring(1));

                    value = ref.convert(_oldObj, _newObj, sFrom, String.valueOf(_nChooseIfRepeat), null,
                            LocalContextHelper.currentSession("Local"), m_entityMetadata,
                            m_xmlConfig.elementTextTrim("match-properties"), m_xmlConfig.attributeValue("temp"));

                    _newObj.setProperty(sTo, value);
                    checkIsValidPatent(_newObj, _errorDataList);
                    continue;
                }

                continue;
            }
            // sFrom = element.attributeValue("from").toUpperCase();
            sFrom = element.attributeValue("from");
            sTo = element.attributeValue("to");
            sRef = element.attributeValue("ref");

            // to convert property value
            if (!_oldObj.hasProperty(sFrom)) {
                continue;
                // throw new PureException(PureException.PROPERTY_NOTFOUND,
                // sFrom);
            }
            if (isPropertySet(_newObj, sTo))
                continue; // skip

            // else
            try {
                sForeignKey = element.attributeValue("fk");
                String expandCondition = null, expandPropertyDebug = null;
                StringBuffer expandConditionBuffer = new StringBuffer();
                StringBuffer expandPropertyDebugBuffer = new StringBuffer();
                Element expandElement;
                List expands = element.elements("expand");
                if (expands.size() > 0) {
                    for (int j = 0; j < expands.size(); j++) {
                        expandElement = (Element) expands.get(i);
                        String expandPropertyValue = _oldObj.getProperty(expandElement.attributeValue("from"))
                                .toString();
                        expandConditionBuffer.append(" " + expandElement.attributeValue("to"));
                        expandConditionBuffer.append("=");
                        expandConditionBuffer.append(expandPropertyValue);
                        expandConditionBuffer.append(" and ");
                        expandPropertyDebugBuffer.append("(" + expandElement.attributeValue("from") + "\""
                                + expandPropertyValue + "\"" + ")");
                    }
                }
                if (expandConditionBuffer.length() > 0) {
                    expandCondition = expandConditionBuffer.toString();
                    expandPropertyDebug = expandPropertyDebugBuffer.toString();
                }
                expandConditionBuffer.setLength(0);

                valueFrom = _oldObj.getProperty(sFrom);
                if (element.attributeValue("subString") != null) {
                    String sSubStringValue = element.attributeValue("subString");
                    int nBeganIndex = 0;
                    int nEndIndex = sSubStringValue.length() - 1;

                    if (sSubStringValue.indexOf(",") > 0) {
                        String sBeganIndex = sSubStringValue.substring(0, sSubStringValue.indexOf(","));
                        nBeganIndex = Integer.parseInt(sBeganIndex);

                        String sEndIndex = sSubStringValue.substring(sSubStringValue.indexOf(",") + 1);

                        nEndIndex = Integer.parseInt(sEndIndex);
                        // logger.debug(""+nBeganIndex+""+nEndIndex);
                    }
                    if (valueFrom != null && ((String) valueFrom).length() >= nEndIndex) {
                        valueFrom = ((String) valueFrom).substring(nBeganIndex, nEndIndex);
                        // logger.debug(" "+valueFrom);
                    }
                }

                if (element.attributeValue("validate") != null
                        && element.attributeValue("validate").equals("false"))
                    bvalidate = false;
                value = this.lookupRefValue(sRef, valueFrom, sForeignKey, sFrom, _errorDataList, expandCondition,
                        expandPropertyDebug, bvalidate);
                if (value == null) {
                    bConvertError = true;
                } else {
                    _newObj.setProperty(sTo, value);
                    System.out.println("=======" + _newObj.getProperty(sTo));
                }

            } catch (Exception ex) {
                throw new PureException(PureException.INVALID_VALUE,
                        "convert " + sFrom + "[" + value + "] to " + sTo, ex);
            }

        }

        return bConvertError;
    }

    protected Object lookupRefValue(String _sRef, Object _oKey, String _sForeignKey) throws Exception {
        if (_oKey == null)
            return null;

        // to analyze
        int nPos = _sRef.indexOf('.');
        String sRefTable = _sRef.substring(0, nPos);
        String sRefField = _sRef.substring(nPos + 1);

        // to construct SQL
        String fk = (_sForeignKey != null && (_sForeignKey = _sForeignKey.trim()).length() > 0) ? _sForeignKey
                : "ID";
        String strSQL = "SELECT " + sRefField + " AS VALUE FROM " + sRefTable + " WHERE " + fk + "=";
        if (_oKey instanceof String) {
            String _sValue = (String) _oKey;

            _sValue = _sValue.replaceAll(":", "");
            _sValue = StrUtil.escapeEncode(_sValue);
            _sValue = StrUtil.sqlEncode(_sValue);
            _sValue = removeBlank(_sValue);

            strSQL += "'" + _sValue + "'";
        } else {
            strSQL += _oKey.toString().trim();
        }
        // to execute query
        ISession session = this.getSession();
        IStatement query = null;
        IObjects results = null;
        try {
            query = session.createQuery(strSQL, DolphinObject.class, 1);
            results = query.executeQuery(false);

            // to fetch the result
            DolphinObject result = results.next();

            // to return
            return result == null ? null : result.getProperty("VALUE");
        } finally {
            DolphinHelper.clear(results, query);
        }
    }

    private Object lookupRefValue(String _sRef, Object _oKey, String _sForeignKey, String _sPropertyName,
            List _list, String _sExpandCondition, String _sExpandPropertyDebug, boolean _bvalidate)
            throws Exception {
        System.out.println("|" + _sRef + "|" + _oKey + "|" + _sForeignKey + "|" + _sPropertyName + "|" + _list + "|"
                + _sExpandCondition + "|" + _sExpandPropertyDebug + "|" + _bvalidate + "|");
        if (_oKey == null || _oKey.toString().length() == 0)
            return null;

        String sExpandCondition = _sExpandCondition == null ? " " : _sExpandCondition;
        String sExpandPropertyDebug = _sExpandPropertyDebug == null ? "" : _sExpandPropertyDebug;
        // to analyze
        int nPos = _sRef.indexOf('.');
        String sRefTable = _sRef.substring(0, nPos);
        String sRefField = _sRef.substring(nPos + 1);

        // to construct SQL
        String fk = (_sForeignKey != null && (_sForeignKey = _sForeignKey.trim()).length() > 0) ? _sForeignKey
                : "ID";
        String strSQL = "SELECT " + sRefField + " AS VALUE FROM " + sRefTable + " WHERE " + sExpandCondition + fk
                + "=";
        String _sKey;
        if (_oKey instanceof String) {
            String _sValue = (String) _oKey;

            _sValue = _sValue.replaceAll(":", "");
            _sValue = StrUtil.escapeEncode(_sValue);
            _sValue = StrUtil.sqlEncode(_sValue);
            _sValue = removeBlank(_sValue);
            strSQL += "'" + _sValue + "'";
        } else {
            _sKey = _oKey.toString();
            strSQL += _sKey.trim();
        }
        // to execute query
        ISession session = this.getSession();
        IStatement query = null;
        IObjects results = null;
        try {
            query = session.createQuery(strSQL, DolphinObject.class, 1);
            System.out.println(">>>>>>>>>>" + strSQL);
            results = query.executeQuery(false);

            // to fetch the result
            DolphinObject result = results.next();

            // to return
            if (result == null) {
                System.out.println(".................................>>>>>>>>>" + _bvalidate);
                if (_bvalidate)
                    System.out.println(">>>>>>>>>>>>>>>in<<<<<<<<<<<<<<<<<");
                _list.add(_sPropertyName + "\"" + _oKey.toString() + "\"" + sExpandPropertyDebug);
            }
            if (result != null) {
                System.out.println("...................." + result.getProperty("VALUE"));
            }
            return result == null ? null : result.getProperty("VALUE");
        } finally {
            DolphinHelper.clear(results, query);
        }
    }

    /**
     * Fills the properties which are required not null in SRM.
     * 
     * @param _newObj
     * @throws Exception
     */
    private void fill(DolphinObject _oldObj, DolphinObject _newObj, int _nChooseIfRepeat) throws Exception {
        Element fill = m_xmlConfig.element("data");
        List properties = fill.element("fill").elements();
        Element element;
        String sName, sValue;
        Object sPropertyValue = null;
        for (int i = 0; i < properties.size(); i++) {
            element = (Element) properties.get(i);
            sName = element.attributeValue("name");
            if (sName == null) {
                throw new PureException(PureException.SETTING_MISSING,
                        "property name in fill-" + i + ": " + XMLUtil.toString(element));
            }

            // skip the property which is not updateable
            if (_newObj.isValidInstance()) {
                PropertyMetadata property = m_entityMetadata.lookupPropertyByName(sName, true);
                if (!property.isUpdateable()) {
                    continue; // skip
                }
            }

            sValue = element.attributeValue("value");
            if (sValue == null) {
                throw new PureException(PureException.SETTING_MISSING,
                        "property value in fill-" + i + ": " + XMLUtil.toString(element));
            }

            // ref:
            String sRef = element.attributeValue("ref");
            if (sRef != null && sRef.startsWith("#")) {
                IImportorRef ref = (IImportorRef) PureFactory.getBean(sRef.substring(1));
                _newObj.setProperty(element.attributeValue("name"),
                        ref.convert(_oldObj, _newObj, null, String.valueOf(_nChooseIfRepeat), null,
                                LocalContextHelper.currentSession("Local"), m_entityMetadata, null, null));
            }

            if (!isPropertySet(_newObj, sName)) {
                if (sValue.length() > 0 && sValue.charAt(0) == '@') {
                    // to fill with a specified property
                    sValue = sValue.substring(1); // trim '@'
                    _newObj.setProperty(sName, _newObj.getProperty(sValue));
                } else {
                    if (sValue.length() > 0 && sValue.charAt(0) == '$') {
                        sValue = sValue.substring(1, sValue.length() - 1);
                        if (this.getPageValue().size() > 0) {
                            sPropertyValue = this.getPageValue().get(sValue);

                            if (sPropertyValue != null)
                                _newObj.setPropertyWithString(sName, sPropertyValue.toString());
                        }
                    }
                    // to fill with a constant
                    DolphinUtil.setPropertyWithString(_newObj, m_entityMetadata, sName, sValue);
                }
            }
        }
    }

    /**
     * Saves the new object into SRM database.
     * 
     * @param _newObj
     * @throws Exception
     */

    private void save(DolphinObject _newObj, List _errorDataList) throws Exception {
        ArkContent obj = (ArkContent) _newObj;

        IContentMgr mgr = ArkContentHelper.getContentMgrOf(_newObj.getClass());
        // updated by Opensky when obj is PatentFee 2007-09-18
        // begin
        if (obj instanceof PatentFee) {
            PatentFee fee = (PatentFee) obj;
            int nFeeType = 0;
            String sFeeType = fee.getFeeType();
            nFeeType = StringUtils.isNotEmpty(sFeeType) ? Integer.parseInt(sFeeType) : 0;
            if (nFeeType == 5) {
                _errorDataList.add("" + fee.getId() + "");
            } else {
                fee.setFeeStatus(1);
                mgr.save(fee);
            }
        }
        // End
        else {
            try {
                if (isToTemp()) {
                    mgr.saveToTemp(obj, false);
                } else {
                    mgr.save(obj);
                }
            } catch (Exception ex) {
                Iterator itr = _newObj.getProperties(false).entrySet().iterator();
                Object oValue;
                while (itr.hasNext()) {
                    Map.Entry entry = (Map.Entry) itr.next();
                    oValue = entry.getValue();
                    System.out.println(entry.getKey() + "=" + oValue);
                }
                throw ex;
            }
        }
    }

    /**
     * Saves the new object into SRM database.
     * 
     * @param _newObj
     * @throws Exception
     */
    private boolean beforeSave(DolphinObject _newObj, List _errorDataList) throws Exception {

        List requiredList = new ArrayList();

        List containList = new ArrayList();

        List lostList = new ArrayList();

        boolean bError = false;

        try {
            Iterator itr = _newObj.getProperties(false).entrySet().iterator();
            Object oKey = null;
            while (itr.hasNext()) {
                Map.Entry entryContain = (Map.Entry) itr.next();
                oKey = entryContain.getKey();
                containList.add(oKey);
            }

            PropertyMetadata propertyMetadata, propertyMetadata1;

            Iterator itrRequied = m_entityMetadata.getProperties().entrySet().iterator();

            while (itrRequied.hasNext()) {
                Map.Entry entry = (Map.Entry) itrRequied.next();
                propertyMetadata = m_entityMetadata.lookupPropertyByName(entry.getKey().toString(), true);
                if (!propertyMetadata.isNullable()) {
                    if (!(entry.getKey().equals("createTime") || entry.getKey().equals("id"))) {
                        // System.out.println(""+entry.getKey());
                        requiredList.add(entry.getKey());
                    }
                }
            }

            for (int i = requiredList.size(); i > 0; i--) {

                if (!containList.contains(requiredList.get(i - 1))) {
                    // System.out.println(":"+requiredList.get(i - 1));
                    lostList.add(requiredList.get(i - 1));
                }
            }

            if (lostList.size() > 0) {
                bError = true;
                for (int i = lostList.size(); i > 0; i--) {
                    propertyMetadata1 = m_entityMetadata.lookupPropertyByName(lostList.get(i - 1).toString(), true);
                    //System.out.println(":"+propertyMetadata1.getDesc().toUpperCase());
                    _errorDataList.add(propertyMetadata1.getDesc().toUpperCase());
                }
            }

        } catch (Exception ex) {
            throw ex;
        } finally {
            if (requiredList != null)
                requiredList.clear();
            if (containList != null)
                containList.clear();
            if (lostList != null)
                lostList.clear();
        }

        return bError;
    }

    private void checkIsValidPatent(DolphinObject _newObj, List _errorDataList) throws PureException {
        if (_newObj == null)
            throw new PureException(ArkExceptionTypes.CONTENT_NOTFOUND);
        // if (_newObj instanceof Patent) {// ????
        if (_newObj instanceof PatentFee) { // update by sunjie
            PatentFee patent = (PatentFee) _newObj;
            // System.out.println(patent.getPatentId()+"********************************");
            int nPatentId = patent.getPatentId();
            if (nPatentId == 0)
                _errorDataList.add("Id");
        }
    }

    /**
     * Updates the imported data at last.
     * 
     * @throws Exception
     */
    private void lastUpdate() throws Exception {
        this.executeSQLElement("last-update");
    }

    private void executeSQLElement(String _sName) throws Exception {
        Element ele = m_xmlConfig.element(_sName);
        if (ele == null || "false".equals(ele.attributeValue("enabled"))) {
            return;
        }

        // else
        List eleSQLs = ele.elements("sql");
        if (eleSQLs.isEmpty())
            return;

        // else, to execute the SQLs
        String strSQL;
        ISession session = this.getSession();
        for (int i = 0; i < eleSQLs.size(); i++) {
            strSQL = ((Element) eleSQLs.get(i)).getTextTrim();
            try {
                System.out.println("to execute: " + strSQL);
                IStatement statement = session.createStatement(strSQL);
                this.registerAlias(statement, ele.elements("alias"));
                statement.executeUpdate();
                statement.clear();
            } catch (Exception ex) {
                throw new PureException(PureException.DATABASE_ACCESS, strSQL, ex);
            }
        }
    }

    /**
     * Registers alias for statement.
     * 
     * @param _statement
     * @param _aliasElements
     * @throws Exception
     */
    private void registerAlias(IStatement _statement, List _aliasElements) throws Exception {
        _statement.registerAlias("this", m_entityMetadata.getEntityClass());

        Element eleAlias;
        String sAlias, sClassName;
        for (int i = 0; i < _aliasElements.size(); i++) {
            eleAlias = (Element) _aliasElements.get(i);
            sAlias = eleAlias.attributeValue("name");
            sClassName = eleAlias.attributeValue("class");
            _statement.registerAlias(sAlias, Class.forName(sClassName));
        }
    }

    /**
     * wind.li@2007-04-29 
     * 
     * @return
     */
    private List checkMaybeRequiredCells(DolphinObject _old) throws PureException {
        Element element = m_xmlConfig.element("maybe-required-cells");
        if (element == null)
            return null;
        List cells = element.elements();
        if (cells == null)
            return null;
        Element ele;
        List lostCellList = new ArrayList();
        for (Iterator iter = cells.iterator(); iter.hasNext();) {
            ele = (Element) iter.next();
            Element eleCellName = ele.element("name");
            if (eleCellName == null) {
                throw new PureException(PureException.PROPERTY_VALUE_REQUIRED, "<cell><name>");
            }
            String sCellName = eleCellName.getTextTrim();

            Element eleConditions = ele.element("conditions");
            if (eleConditions == null) {
                if (_old.getProperty(sCellName) == null) {
                    lostCellList.add(sCellName);
                    continue;
                }
            }
            List conditions = eleConditions.elements("condition");
            if (conditions == null || conditions.size() == 0) {
                if (_old.getProperty(sCellName) == null) {
                    lostCellList.add(sCellName);
                    continue;
                }
            }
            for (Iterator iter2 = conditions.iterator(); iter2.hasNext();) {
                Element condition = (Element) iter2.next();
                Element eleCell = condition.element("cell");
                if (eleCell == null) {
                    throw new PureException(PureException.PROPERTY_VALUE_REQUIRED,
                            "<cell>(name = " + sCellName + ")<condition><cell>");
                }
                Element eleOperator = condition.element("operator");
                if (eleOperator == null) {
                    throw new PureException(PureException.PROPERTY_VALUE_REQUIRED,
                            "<cell>(name = " + sCellName + ")<condition><operator>");
                }
                Element eleValue = condition.element("value");
                if (eleValue == null) {
                    throw new PureException(PureException.PROPERTY_VALUE_REQUIRED,
                            "<cell>(name = " + sCellName + ")<condition><value>");
                }
                String sCell = eleCell.getTextTrim();
                String sOperator = eleOperator.getTextTrim().toUpperCase();
                String sValue = eleValue.getTextTrim();
                String sCellValue = _old.getPropertyAsString(sCell);
                if (sCellValue != null && sCellValue.trim().length() > 0) {
                    if ("IN".equals(sOperator)) {
                        if (sValue != null && sValue.indexOf("," + sCellValue + ",") != -1) {
                            if (_old.getProperty(sCellName) == null) {
                                lostCellList.add(sCellName);
                                continue;
                            }
                        }
                    }
                    if ("EQUAL".equals(sOperator)) {
                        if (sValue != null && sValue.equals(sCellValue)) {
                            if (_old.getProperty(sCellName) == null) {
                                lostCellList.add(sCellName);
                                continue;
                            }
                        }
                    }
                }
            }
        }

        return lostCellList;
    }

    /**
     * @param _sString
     * @throws PureException
     * @throws IOException
     * @throws FileNotFoundException
     */
    public List check(String _sFileName) throws PureException, FileNotFoundException, IOException {

        POIFSFileSystem fs;
        HSSFSheet m_sheet = null;
        FileInputStream fileInputStream = new FileInputStream(_sFileName);
        String[] m_heads;

        fs = new POIFSFileSystem(fileInputStream);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        m_sheet = wb.getSheetAt(0);
        HSSFRow row = m_sheet.getRow(0);
        HSSFCell cell;

        // TITLE
        // BY lily
        List list = new ArrayList(row.getLastCellNum());
        for (int i = 0; i < row.getLastCellNum(); i++) {
            cell = row.getCell((short) i);
            if (cell == null) {
                break;
            }
            list.add(cell.getStringCellValue().trim());
        }
        m_heads = new String[list.size()];
        list.toArray(m_heads);
        list.clear();
        fileInputStream.close();
        return checkExcelHead(m_heads);

    }

    public String removeBlank(String _sValue) {
        _sValue = _sValue.replaceAll("    ", "");
        return _sValue;
    }

    public Map getPageValue() {
        return m_pageValue;
    }

    public void setPageValue(Map _hPageValue) {
        m_pageValue = _hPageValue;
    }

    public void setPageValue(String _sName, String _sValue) {
        m_pageValue.put(_sName, _sValue);
    }

    private String getNow(String sBefore) {
        String sThisTime = String.valueOf(System.currentTimeMillis());
        if (!sBefore.equals(sThisTime)) {
            return sThisTime;
        }
        System.out.println();
        return getNow(sThisTime);
    }
}