Java tutorial
/** * PureInfo Command * @(#)SchoolSCITxtImportRunner.java 1.0 2007-8-10 * * 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.txt2SRM.impl; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Date; 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.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.PureFactory; import com.pureinfo.force.PureSystem; import com.pureinfo.force.encrypt.PasswordEncrypter; import com.pureinfo.force.exception.PureException; import com.pureinfo.force.lang.DateTimeUtil; import com.pureinfo.force.lang.StrUtil; import com.pureinfo.force.xml.XMLUtil; import com.pureinfo.importer.ref.IImportorRef; import com.pureinfo.srm.SRMTypes; import com.pureinfo.srm.product.ProductConstants; import com.pureinfo.srm.product.domain.IProductMgr; import com.pureinfo.srm.sci.helper.SCIMappingHelper; import com.pureinfo.srm.xls2srm.model.Xls2srmForm; import com.pureinfo.studio.db.xls2srm.IConvert2RelativeTable; /** * <P> * Created on 2007-8-10 03:35:35<BR> * Last modified on 2007-8-10 * </P> * SchoolSCITxtImportRunner , copy from TxtImportRunner.java * * @author sunjie * @version 1.0, 2007-8-10 * @since Command 1.0 */ public class SchoolSCITxtImportRunner { // logger private final static Logger logger = Logger.getLogger(SchoolSCITxtImportRunner.class.getName()); private EntityMetadata m_entityMetadata; // connection provider private String m_sProvider; private String m_sDivide; // configuration private Element m_xmlConfig; // runtime cache private ISession m_sessionFrom; private ISession m_sessionTo; private Map m_pageValue; /** * Constructor */ public SchoolSCITxtImportRunner() { super(); } /** * Constructor * * @param _sProvider * connection provider */ public SchoolSCITxtImportRunner(String _sProvider) { this.setProvider(_sProvider); } /** * 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, String _sDivide) throws PureException { m_sDivide = _sDivide; 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 sTemp = m_xmlConfig.attributeValue("temp"); if (sTemp != null && "true".equals(sTemp)) { return true; } return false; } public void clear() { if (m_sessionFrom != null) { m_sessionFrom.closeQuietly(System.err); } if (m_sessionTo != null) { m_sessionTo.closeQuietly(System.err); } } /** * @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(); int nCount = 1; boolean isToTemp = isToTemp(); try { prepare(); objs = this.loadOld(_sFileName, m_sDivide); 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) { // if EI: if (oldObj.getStrProperty("Title") != null) { if (oldObj.getProperty("Ei classification codes") == null || oldObj.getPropertyAsString("Document type").startsWith("Conference article")) { continue; } } List errorDataEachList = new ArrayList(); StringBuffer error = new StringBuffer(); if (checkObjIsNull(oldObj, sViewProperty)) continue; oldObj = changeOldObjPropertysFitToSystems(oldObj); // it will be checked in the last,so at the beganning ,we will // new an instance. newObj = (DolphinObject) clazz.newInstance(); copy(oldObj, newObj, errorDataEachList); getFirstValue(oldObj, newObj); if (errorDataEachList.size() > 0) { error.append("" + nCount + ":"); for (int i = 0; i < errorDataEachList.size(); i++) { error.append(errorDataEachList.get(i)); logger.debug(errorDataEachList.get(i)); } } errorDataEachList.clear(); convert(oldObj, newObj, errorDataEachList); 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() : ""); fill(oldObj, newObj, _nChooseIfRepeat); mappingProperties(newObj); makeIsFirstDept4SCIImport(oldObj, newObj); conversAuthors2insideAuthorInfo(newObj, oldObj); beforeSave(newObj, errorDataEachList); changeNewObjPropertysFitToSystems(newObj); 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.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; } logger.debug("" + nCount + ""); nCount++; continue; } error.setLength(0); if (_nChooseIfRepeat == Xls2srmForm.JUSTFORCHECK) { // newObj = makeNewObject2(clazz, newObj, // Xls2srmForm.COVERDATAWHENREPEAT); } else { newObj = makeNewObject2(clazz, newObj, _nChooseIfRepeat, isToTemp); // System.out.println(">>>" + newObj); } if (newObj == null) { if (_nChooseIfRepeat == Xls2srmForm.MERGEDATAWHENREPEAT) { _repeatList.add("" + nCount + "!"); } else { _repeatList.add("" + nCount + "!"); } nCount++; continue; } if (_nChooseIfRepeat != Xls2srmForm.JUSTFORCHECK) { save(newObj); convert2RelatedTable(newObj, oldObj, false, errorDataEachList, _nChooseIfRepeat); } nCount++; } // endwhile lastUpdate(); long lEnd = System.currentTimeMillis(); logger.debug(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; } /** * C1 * * @param _oldObj * @param _newObj * @return * @throws PureException */ private boolean isC1ContainSchoolKeyWords(DolphinObject _oldObj, DolphinObject _newObj) throws PureException { String c1 = _oldObj.getPropertyAsString("C1"); String sSchoolName = _newObj.getPropertyAsString("schoolName"); if (StringUtils.isEmpty(c1) || StringUtils.isEmpty(sSchoolName)) { return false; } return isStringContainKeyWords(c1, sSchoolName); } private boolean isStringContainKeyWords(String _str, String _schoolCNName) throws PureException { if (StringUtils.isEmpty(_str) || StringUtils.isEmpty(_schoolCNName)) { return false; } boolean b = false; String s = _str; String allKeyWords = SCIMappingHelper.getInstance().isContainSchool(_schoolCNName); if (StringUtils.isEmpty(allKeyWords)) { return false; } String[] keyArr = allKeyWords.split("\\|"); for (int i = 0; i < keyArr.length; i++) { if (keyArr[i] == null || keyArr[i] == "") { continue; } if (s.toLowerCase().indexOf(keyArr[i]) >= 0) { b = true; break; } } return b; } /** * SCI,:"" * * @param _sNewObj * @throws PureException */ private void makeIsFirstDept4SCIImport(DolphinObject _oldObj, DolphinObject _newObj) throws PureException { String sENChoolName = _newObj.getPropertyAsString("schoolNameEN"); String sCNSchoolName = _newObj.getPropertyAsString("schoolName"); String sFirstDept = _newObj.getPropertyAsString("firstAuthorsDepts"); if (sENChoolName == null || sFirstDept == null) return; if (isStringContainKeyWords(sFirstDept, sCNSchoolName)) { _newObj.setProperty("isFirstDept", true); _newObj.setProperty("isDeptEnContainSchool", true); } else { _newObj.setProperty("isFirstDept", false); if (isC1ContainSchoolKeyWords(_oldObj, _newObj)) _newObj.setProperty("isDeptEnContainSchool", true); else _newObj.setProperty("isDeptEnContainSchool", false); } } /** * SCI * * @param _sOldObj * @param _sNewObj */ private void mappingProperties(DolphinObject _newObj) { logger.debug("execute method mappingProperties..."); Element copy = m_xmlConfig.element("data"); List properties = copy.element("mapping").elements(); Element element; String sName, sENValue, sCNValue; for (int i = 0; i < properties.size(); i++) { element = (Element) properties.get(i); sName = element.attributeValue("name"); sENValue = _newObj.getPropertyAsString(sName); logger.debug("object " + sName + "'s value:" + sENValue); if (sENValue == null || sENValue.length() == 0) { continue; } sCNValue = SCIMappingHelper.getCNName(sENValue); logger.debug("and CN value:" + sCNValue); if (sCNValue != null && sCNValue.length() > 0) { _newObj.setProperty(sName, sCNValue); } } } /** * ,, * * @param _sOldObj * @param _sNewObj * @param _sErrorDataEachList */ private void getFirstValue(DolphinObject _oldObj, DolphinObject _newObj) { if (logger.isDebugEnabled()) { logger.debug("execute method getFirstValue..."); } Element convert = m_xmlConfig.element("data"); List properties = convert.element("copy").elements(); Element element; String sFrom, sTo, value, getFirestValue = null; for (int i = 0; i < properties.size(); i++) { element = (Element) properties.get(i); getFirestValue = element.attributeValue("getFirestValue"); if (getFirestValue == null || (!(getFirestValue.equals("true")))) continue; sFrom = element.attributeValue("from").toUpperCase(); sTo = element.attributeValue("to"); if (!_oldObj.hasProperty(sFrom)) { continue; } value = _oldObj.getStrProperty(sFrom); if (value != null) { logger.debug(sFrom + " full value:" + value); String[] perValue = null; // ";","," if (sFrom.equals("SC")) { perValue = value.split(PureSystem.getProperty("srm.xls2srm.split.semicolon")); } // "." else if (sFrom.equals("C1")) { perValue = getFirstAuthorDept(value); } // " " else if (sFrom.equals("AU")) { perValue = value.split(" "); } for (int j = 0; j < perValue.length; j++) { String firstValue = perValue[j]; logger.debug("firstValue:" + firstValue); if (firstValue != null && firstValue.length() > 0) { _newObj.setProperty(sTo, firstValue); break; } } } } } /** * [Fang, H.; Yu, Y. L.; Wang, X. G.; Chu, X. Q.; Pan, * X. D.] Zhejiang Univ, Dept Plant Protect, Coll Agr & Biotechnol, Hangzhou * 310029, Peoples R China. [Yang, X. E.] Zhejiang Univ, MOE Key Lab * Environm Remediat & Ecosyst Hlth, Coll Environm & Resource Sci, Hangzhou * 310029, Peoples R China. [][] 2008-11-4 * * @param _value * @return */ private String[] getFirstAuthorDept(String _value) { String s = _value.replaceAll("\\[[^\\]]*?\\]", ""); return s.split("\\" + PureSystem.getProperty("srm.xls2srm.split.period")); } /** * @param obj * @param _sViewProperty * @throws PureException */ private DolphinObject changeOldObjPropertysFitToSystems(DolphinObject _obj) throws PureException { String sValue = null; DolphinObject obj = _obj; sValue = obj.getPropertyAsString("Pages"); if (sValue != null) { // p 237-242 int temp = sValue.indexOf("-"); if (temp >= 0) { obj.setProperty("EP", sValue.substring(temp + 1)); obj.setProperty("BP", sValue.substring(sValue.indexOf(" ") + 1, temp)); } else { temp = sValue.indexOf(" "); if (temp >= 0) { obj.setProperty("BP", sValue.substring(temp + 1)); } } } sValue = obj.getPropertyAsString("Ei classification codes"); if (sValue != null) { obj.setProperty("isEiClassificationCodes", "true"); if (sValue.indexOf(" ") >= 0) { obj.setProperty("eiClassificationCodes", sValue.substring(0, sValue.indexOf(" "))); } } sValue = obj.getPropertyAsString("Document type"); if (sValue != null) { sValue = sValue.substring(0, sValue.indexOf("(")).trim(); obj.setProperty("Document type", sValue); } // ei sValue = obj.getPropertyAsString("C1"); if (sValue != null) { if (sValue.indexOf(".") >= 0) { obj.setProperty("cFirstAuthor", sValue.substring(0, sValue.indexOf("."))); } else { obj.setProperty("cFirstAuthor", sValue); } } sValue = obj.getPropertyAsString("Authors"); if (sValue != null) { if (sValue.indexOf(";") >= 0) { obj.setProperty("firstAuthorName", sValue.substring(0, sValue.indexOf(";"))); } else { // obj.setProperty("firstAuthorName", sValue); } } sValue = obj.getPropertyAsString("AU"); if (sValue != null) { if (sValue.indexOf(" ", sValue.indexOf(",") + 2) >= 0) { obj.setProperty("firstAuthorName", sValue.substring(0, sValue.indexOf(" ", sValue.indexOf(",") + 2))); } else { // obj.setProperty("firstAuthorName", sValue); } } sValue = obj.getPropertyAsString("Volume"); if (sValue != null) { if (sValue.indexOf("v ") >= 0) { sValue = sValue.substring(2); } obj.setProperty("Volume", sValue); } sValue = obj.getPropertyAsString("Issue"); if (sValue != null) { if (sValue.indexOf("n ") >= 0) { sValue = sValue.substring(2); } obj.setProperty("Issue", sValue); } String sYear = String.valueOf(DateTimeUtil.get(new Date(), DateTimeUtil.YEAR)); sValue = obj.getPropertyAsString("Accession number"); if (sValue != null) { sValue = sValue.substring(0, 2); if (Integer.parseInt(sValue) >= 60) sYear = String.valueOf(Integer.parseInt(sYear.substring(0, 2)) - 1); obj.setProperty("indexIndexDate", sYear.substring(0, 2) + sValue); } if (obj.getPropertyAsString("indexIndexDate") == null) obj.setProperty("indexIndexDate", sYear); obj.update(); return obj; } /** * @param obj * @param _sViewProperty * @throws PureException */ private DolphinObject changeNewObjPropertysFitToSystems(DolphinObject _obj) throws PureException { String sValue = _obj.getPropertyAsString("englishDept"); String sIndexForm = _obj.getStrProperty("indexForm"); if (sValue != null) { if (isZjuByMailCode(sValue, sIndexForm)) { _obj.setPropertyWithString("isZju", "true"); } else { _obj.setPropertyWithString("isZju", "false"); _obj.setPropertyNull("insideAuthorId"); } } return _obj; } private boolean isZjuByMailCode(String _sValue, String _sIndexForm) { logger.debug("obj indexForm:" + _sIndexForm); if (ProductConstants.INDEX_FORM_EI.equals(_sIndexForm)) { if (_sValue.indexOf("China") < 0) { return false; } } if (ProductConstants.INDEX_FORM_SCI.equals(_sIndexForm) || ProductConstants.INDEX_FORM_ISTP.equals(_sIndexForm)) { if (_sValue.indexOf("Peoples R China") < 0) { return false; } } if (_sValue.indexOf("Zhejiang University,") < 0 && _sValue.indexOf("Zhejiang Univ,") < 0 && _sValue.indexOf("Zhejiang Univ.,") < 0 && (_sValue.indexOf("ZHEJIANG-UNIV,") < 0)) { return false; } if (_sValue.indexOf("310012") >= 0 || _sValue.indexOf("310014") >= 0 || _sValue.indexOf("310018") >= 0 || _sValue.indexOf("310032") >= 0 || _sValue.indexOf("310033") >= 0 || _sValue.indexOf("310035") >= 0) { return false; } return true; } // ========================================================================= // 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(); 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); } } } } /** * @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("authors2insideAuthorInfoForProduct")) continue; sFrom = element.attributeValue("from").toUpperCase(); 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; String strSQL = null; String strSQLProperty = m_xmlConfig.elementTextTrim("match-properties"); strSQL = getSqlStrBuffer(_newObj, strSQLProperty, false); if (strSQL != null) { newObj = getObjAsSql(strSQL, _clazz); } if (isToTemp && newObj == null) { strSQL = getSqlStrBuffer(_newObj, strSQLProperty, true); if (strSQL != null) newObj = getObjAsSql(strSQL, _clazz); } logger.debug("--->>newObj:" + newObj); if (newObj == null) { // System.out.println(">>>null"); // -- newObj = _newObj; } else { logger.debug("-->>repeat:" + _nChooseIfRepeat); switch (_nChooseIfRepeat) { // case Xls2srmForm.NEWDATAWHENREPEAT: newObj = _newObj; break; // case Xls2srmForm.LEAPDATAWHENREPEAT: newObj = null; break; // case Xls2srmForm.COVERDATAWHENREPEAT: boolean bIsFromFormal = ((ArkContent) newObj).isFromFormal(); logger.debug("newObj from formal?: " + bIsFromFormal); DolphinUtil.copyUpdateableProperties(_newObj, newObj); break; // () case Xls2srmForm.MERGEDATAWHENREPEAT: { String[] mergeProperties = getMergeProperties(); if (mergeProperties != null) { for (int i = 0; i < mergeProperties.length; i++) { String sPropertyValue = _newObj.getPropertyAsString(mergeProperties[i]); if (sPropertyValue != null) { newObj.setProperty(mergeProperties[i], sPropertyValue); } } } // newObj.update(); break; } default: throw new PureException(PureException.INVALID_VALUE, ""); } } return newObj; } private String[] getMergeProperties() { String strMatchSQL = m_xmlConfig.elementTextTrim("merge-properties"); if (strMatchSQL.length() > 0) { return StrUtil.split(strMatchSQL, ","); } return null; } /** * @param _sStrSQL * @param _sClazz * @return the obj according the sql ; * @throws Exception */ private DolphinObject getObjAsSql(String _sStrSQL, Class _clazz) throws Exception { ISession session = getSession(); IStatement query = null; IObjects objs = null; if (isToTemp()) { try { query = session.createQuery("select * from {this}0 where " + _sStrSQL, _clazz, 1); objs = query.executeQuery(false); DolphinObject obj = objs.next(); if (obj != null) return obj; } finally { DolphinHelper.clear(objs, query); } } try { query = session.createQuery("select * from {this} where " + _sStrSQL, _clazz, 1); objs = query.executeQuery(false); return objs.next(); } finally { DolphinHelper.clear(objs, query); } } /** * return sql states if no properties set return null * * @param _sNewObj * @param _sStrSQL * @param _sIsToTemp */ private String getSqlStrBuffer(DolphinObject _newObj, String _sStrSQL, boolean _bIsToTemp) { StringBuffer sbuff = null; boolean bStringProperty; if (_sStrSQL.length() > 0) { StringTokenizer st = new StringTokenizer(_sStrSQL, ",", false); // if (_bIsToTemp) { // sbuff = new StringBuffer("select * from {this}0 where "); // } // else { // sbuff = new StringBuffer("select * from {this} where "); // } sbuff = new StringBuffer(); try { String sValueWhole, sValue, sTableRowValue; while (st.hasMoreElements()) { sValueWhole = (String) st.nextElement(); sValue = sValueWhole.substring(0, sValueWhole.indexOf("-")); sTableRowValue = sValueWhole.substring(sValueWhole.indexOf("-") + 1); 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)) { if (sbuff.length() > 0) { sbuff.append(" and "); } sbuff.append(sTableRowValue); sbuff.append('='); if (bStringProperty) sbuff.append('\''); String sPropertyValue = _newObj.getPropertyAsString(sValue); sPropertyValue = StrUtil.escapeEncode(sPropertyValue); sPropertyValue = StrUtil.sqlEncode(sPropertyValue); sPropertyValue = removeBlank(sPropertyValue); sbuff.append(sPropertyValue); if (bStringProperty) sbuff.append('\''); } } } return sbuff.length() > 0 ? sbuff.toString() : null; } finally { sbuff.setLength(0); } } return null; } 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, String _sDivide) throws PureException { TxtObjectsImpl objs = new TxtObjectsImpl(_sFileName, _sDivide); 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"); logger.debug(_sRequiredCells); _sRequiredCellsSplit = _sRequiredCells.split(","); for (int i = _sRequiredCellsSplit.length - 1; i >= 0; i--) { logger.debug(_sRequiredCellsSplit[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(); } } /** * Copys the properties from 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; 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 (!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"); sValue = _oldObj.getPropertyAsString(sFrom); if (sValue != null) { if (sReg != null) { if ((sValue.length() > 0) && (!sValue.matches(sReg))) { _errorCopyList.add(element.attributeValue("from") + ":" + sValue + "" + (sValidatorDesc == null ? "" : sValidatorDesc + ";")); continue; } } if (bBool) { if (sValue.equals("")) sValue = "true"; if (sValue.equals("")) sValue = "false"; } if (bPassword) sValue = PasswordEncrypter.encode(sValue); sValue = StrUtil.sqlEncode(sValue); try { 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) throws Exception { Element convert = m_xmlConfig.element("data"); List properties = convert.element("convert").elements(); Element element; String sFrom, sTo, sRef, sForeignKey; Object value = null; boolean bConvertError = false; boolean bvalidate = true; for (int i = 0; i < properties.size(); i++) { bvalidate = true; element = (Element) properties.get(i); if (element.attributeValue("provider") != null) continue; 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); if (element.attributeValue("validate") != null && element.attributeValue("validate").equals("false")) bvalidate = false; value = this.lookupRefValue(sRef, _oldObj.getProperty(sFrom), sForeignKey, sFrom, _errorDataList, expandCondition, expandPropertyDebug, bvalidate); if (value == null) { bConvertError = true; } else { _newObj.setProperty(sTo, value); } } 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 { 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); results = query.executeQuery(); // to fetch the result DolphinObject result = results.next(); // to return if (result == null) { if (_bvalidate) _list.add(_sPropertyName + "\"" + _oKey.toString() + "\"" + sExpandPropertyDebug); } 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 objPropertyValue = 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 (_nChooseIfRepeat != Xls2srmForm.JUSTFORCHECK && sRef != null && sRef.startsWith("#")) { IImportorRef ref = (IImportorRef) PureFactory.getBean(sRef.substring(1)); _newObj.setProperty(element.attributeValue("name"), ref.convert(_oldObj, _newObj, element.attributeValue("name"), 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 != null && sValue.length() > 0 && sValue.charAt(0) == '$') { sValue = sValue.substring(3, sValue.length() - 1); if (this.getPageValue().size() > 0) { objPropertyValue = this.getPageValue().get(sValue); if (objPropertyValue != null) { String sPropertyValue = objPropertyValue.toString(); sPropertyValue = getSubString(element, sPropertyValue); _newObj.setPropertyWithString(sName, sPropertyValue); } } } else { if (_oldObj.getProperty(sName) != null) { DolphinUtil.setPropertyWithString(_newObj, m_entityMetadata, sName, _oldObj.getPropertyAsString(sName)); } else { DolphinUtil.setPropertyWithString(_newObj, m_entityMetadata, sName, sValue); } } } } } } private String getSubString(Element _element, String _sValue) { 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) { return _sValue.substring(nBeganIndex, nEndIndex); } } return _sValue; } /** * Saves the new object into SRM database. * * @param _newObj * @throws Exception */ private void save(DolphinObject _newObj) throws Exception { IContentMgr mgr = ArkContentHelper.getContentMgrOf(_newObj.getClass()); if (((ArkContent) _newObj).getArkType() == SRMTypes.PRODUCT) { _newObj = ((IProductMgr) mgr).doAllRepeatIndex(_newObj); } ArkContent obj = (ArkContent) _newObj; 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(); logger.debug(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"))) { requiredList.add(entry.getKey()); } } } for (int i = requiredList.size(); i > 0; i--) { if (!containList.contains(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); _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; } /** * 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 { logger.debug("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)); } } /** * @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); } }