Java tutorial
/** * PureInfo Quake * @(#)ProjectBatchImportRunner 1.0 2007-1-11 * * 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.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.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.SRMTypes; import com.pureinfo.srm.auth.domain.IObjUserMappingMgr; import com.pureinfo.srm.auth.domain.ISRMUserMgr; import com.pureinfo.srm.auth.model.ObjUserMapping; import com.pureinfo.srm.auth.model.SRMUser; import com.pureinfo.srm.project.helper.ProjectPersonHelper; import com.pureinfo.srm.project.model.Project; import com.pureinfo.srm.weight.helper.WeightBuildHelper; import com.pureinfo.srm.xls2srm.model.Xls2srmForm; import com.pureinfo.studio.db.xls2srm.IConvert2RelativeTable; /** * * <P> * Created on 2007-1-11 20:00:35 <BR> * Last modified on 2007-1-11 * </P> * * * @author Administrator * @version 1.0, 2007-1-11 * @since Command 1.0 */ public class ProjectBatchImportRunner implements IClearable { // logger private final static Logger logger = Logger.getLogger(ProjectBatchImportRunner.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; // private List m_sameNameAdmins = new ArrayList(); /** * Constructor */ public ProjectBatchImportRunner() { super(); } /** * Constructor * * @param _sProvider * connection provider */ public ProjectBatchImportRunner(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(); 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(); copy(oldObj, newObj, errorDataEachList); 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); 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); beforeSave(newObj, errorDataEachList); 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; } 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; } // this.handleProjetAdmin(oldObj, newObj, _nChooseIfRepeat); if (_nChooseIfRepeat != Xls2srmForm.JUSTFORCHECK) { save(newObj); convert2RelatedTable(newObj, oldObj, false, errorDataEachList, _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 _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"); 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 _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; 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: break; case Xls2srmForm.MERGEDATAWHENREPEAT: { String strMatchSQL = m_xmlConfig.elementTextTrim("merge-properties"); if (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(); } } /** * 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; 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").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) { 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("")) 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); 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, null, null); _newObj.setProperty(sTo, value); 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); } } catch (Exception ex) { throw new PureException(PureException.INVALID_VALUE, "convert " + sFrom + "[" + value + "] to " + sTo, ex); } } // bConvertError = validateAdminExist(_errorDataList, _oldObj.getStrProperty("")); return bConvertError; } private boolean validateAdminExist(List _errorDataList, String _sAdminName) throws PureException { ISRMUserMgr mgr = (ISRMUserMgr) ArkContentHelper.getContentMgrOf(SRMUser.class); if (mgr.findAllOfTrueName(_sAdminName).size() == 0) { _errorDataList.add(":" + _sAdminName + " "); return true; } return false; } 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; try { query = session.createQuery(strSQL, DolphinObject.class, 1); return query.executeStat(); } finally { if (query != null) query.clear(false); session.closeQuietly(logger); } } 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(false); // 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) 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, sValue, 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) throws Exception { ArkContent obj = (ArkContent) _newObj; IContentMgr mgr = ArkContentHelper.getContentMgrOf(_newObj.getClass()); logger.debug("obj Id : " + obj.getIntProperty("id", 0)); 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"))) { 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 { 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)); } } /** * @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); } private void handleProjetAdmin(DolphinObject _oldObj, DolphinObject _newObj, int _nChooseIfRepeat) throws PureException { Element special = m_xmlConfig.element("data"); List properties = special.element("special").elements(); Element element; String sFrom, sTo; for (int i = 0; i < properties.size(); i++) { element = (Element) properties.get(i); sFrom = element.attributeValue("from"); if (sFrom == null) { throw new PureException(PureException.SETTING_MISSING, "property from in special-" + i + ": " + XMLUtil.toString(element)); } sTo = element.attributeValue("to"); if (sTo == null) { throw new PureException(PureException.SETTING_MISSING, "property to in special-" + i + ": " + XMLUtil.toString(element)); } // skip the property which is not updateable if (_newObj.isValidInstance()) { PropertyMetadata property = m_entityMetadata.lookupPropertyByName(sTo, true); if (!property.isUpdateable()) { continue; // skip } } // ref: String sRef = element.attributeValue("ref"); if ("projectBatchAdmin".equals(sRef)) { String sAdminName = _oldObj.getPropertyAsString(sFrom); List list = this.geAdminsOfName(sAdminName); if (list != null && list.size() > 0) { LocalContextHelper.currentSession().autoGenerateProperty(_newObj, "id"); int nProjectId = _newObj.getIntProperty("id", -1); SRMUser admin = (SRMUser) list.get(0); _newObj.setProperty(sTo, admin.getId()); if (_nChooseIfRepeat != Xls2srmForm.JUSTFORCHECK) { ObjUserMapping pp = new ObjUserMapping(); pp.setObjType(SRMTypes.PROJECT); pp.setObjId(nProjectId); pp.setUserId(admin.getId()); pp.setUserType(String.valueOf(1)); pp.setUserName(sAdminName); ProjectPersonHelper.setAdmin(pp, true); pp.setOrderNo(0); pp.setWeightYear(WeightBuildHelper.getWeightYear(new Project())); IObjUserMappingMgr ppMgr = (IObjUserMappingMgr) ArkContentHelper .getContentMgrOf(SRMTypes.OBJ_USER_MAPPING); ppMgr.save(pp); if (list.size() > 1) { _newObj.setProperty("isAdminNameMutiple", true); _newObj.setProperty("isAdminNameMutipleChecked", false); DolphinObject obj = new DolphinObject(); obj.setProperty("projectId", nProjectId); obj.setProperty("projectName", _newObj.getStrProperty("projectName")); obj.setProperty("administratorId", admin.getId()); obj.setProperty("admins", list); m_sameNameAdmins.add(obj); } } } } } } private List geAdminsOfName(String _sAdminName) throws PureException { ISRMUserMgr mgr = (ISRMUserMgr) ArkContentHelper.getContentMgrOf(SRMUser.class); return mgr.findAllOfTrueName(_sAdminName); } 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); } public List getSameNameAdmins() { return this.m_sameNameAdmins; } }