Java tutorial
/** * PureInfo Quake * @(#)RPMSImporter4ListNamedValue.java 1.0 Dec 10, 2005 * * 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.rpms2srm; import java.util.List; import org.apache.log4j.Logger; import org.dom4j.Element; import com.pureinfo.dolphin.DolphinHelper; import com.pureinfo.dolphin.context.LocalContextHelper; 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.exception.PureException; import com.pureinfo.force.xml.XMLUtil; import com.pureinfo.srm.namedvalue.model.TreeNamedValueRPMS; /** * <P> * Created on Dec 10, 2005 5:19:00 PM <BR> * Last modified on Dec 10, 2005 * </P> * RPMSImporter4TreeNamedValue: RPMS tree-named-value importer * * @author Why * @version 1.0, Dec 10, 2005 * @since Quake 1.0 */ public class RPMSImporter4TreeNamedValue { //logger private final static Logger logger = Logger.getLogger(RPMSImporter4TreeNamedValue.class.getName()); //configuration resource private final static String RESOURCE = "namedvalue/TreeNamedValue.rpms.xml"; //data private ISession m_sessionFrom = null; private ISession m_sessionTo = null; /** * Constructor: default */ public RPMSImporter4TreeNamedValue() { super(); } public void clear() { if (m_sessionFrom != null) { m_sessionFrom.closeQuietly(System.out); } if (m_sessionTo != null) { m_sessionTo.closeQuietly(System.out); } } private ISession getSessionFrom() throws PureException { if (m_sessionFrom == null) { m_sessionFrom = LocalContextHelper.currentSession("Local.RPMS"); } return m_sessionFrom; } private ISession getSessionTo() throws PureException { if (m_sessionTo == null) { m_sessionTo = LocalContextHelper.currentSession("Local.RPMS2SRM"); } return m_sessionTo; } public void run() throws Exception { final String SQL_RESETID = "update dpn_sequence set NEXT_VALUE=? WHERE SEQ_KEY='" + TreeNamedValueRPMS.class.getName() + ":id'"; try { //1. to load config String sFile = RPMSImporter.class.getResource(RESOURCE).getFile(); Element config = XMLUtil.fileToElement(sFile); //2. to prepare ISession sessionTo = this.getSessionTo(); IStatement statement = sessionTo.createStatement(SQL_RESETID); statement.setInt(0, 1); statement.executeUpdate(); statement.clear(false); //3. to do import List items = config.elements("type"); for (int i = 0; i < items.size(); i++) { doImport((Element) items.get(i)); } //3. to reset id statement = sessionTo.createStatement(SQL_RESETID); statement.setInt(0, 10001); statement.executeUpdate(); statement.clear(false); } finally { this.clear(); } } private void doImport(Element _element) throws Exception { int nType = XMLUtil.getAttributeValueAsInt(_element, "id"); String sTypeDesc = _element.attributeValue("desc"); String strSQL; strSQL = "delete from {this} where {this.type}=?"; ISession sessionFrom = this.getSessionFrom(); ISession sessionTo = this.getSessionTo(); IStatement statement = null; IObjects objs = null; DolphinObject obj; TreeNamedValueRPMS tnv; String sName, sValue, sParentValue, sDescription; try { //1. to delete the old statement = sessionTo.createStatement(strSQL); statement.registerAlias("this", TreeNamedValueRPMS.class); statement.setInt(0, nType); statement.executeUpdate(); statement.clear(); //2. to add new strSQL = _element.getTextTrim(); statement = sessionFrom.createQuery(strSQL, DolphinObject.class, 0); objs = statement.executeQuery(); while ((obj = objs.next()) != null) { sName = obj.getStrProperty("NAME"); sValue = obj.getStrProperty("VALUE"); sParentValue = obj.getStrProperty("PARENTVALUE"); sDescription = obj.getStrProperty("NOTE"); if (sDescription == null || (sDescription = sDescription.trim()).length() == 0) { sDescription = sTypeDesc + "" + sName; } logger.debug("to save: type=" + nType + ", value=" + sValue + ", name=" + sName); tnv = new TreeNamedValueRPMS(); tnv.setProperty("type", nType); tnv.setValue(sValue); tnv.setName(sName); tnv.setParentValue(sParentValue); tnv.setLevel(1); tnv.setDescription(sDescription); tnv.setCreateUser("system"); try { sessionTo.save(tnv); } catch (Exception ex) { Element ele = DolphinUtil.toXMLElement(tnv, "RPMSTreeNamedValue"); System.out.println(XMLUtil.toString(ele)); throw ex; } } } catch (Exception ex) { System.out.println("failed to import: type=" + nType + ", desc=" + sTypeDesc); throw ex; } finally { DolphinHelper.clear(objs, statement); } } public static void main(String[] args) { try { RPMSImporter4TreeNamedValue importer = new RPMSImporter4TreeNamedValue(); importer.run(); } catch (Exception ex) { ex.printStackTrace(System.out); } } }