com.pureinfo.studio.db.rpms2srm.RPMSImporter4ListNamedValue.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.studio.db.rpms2srm.RPMSImporter4ListNamedValue.java

Source

/**
 * 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.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.ListNamedValueRPMS;

/**
 * <P>
 * Created on Dec 10, 2005 5:19:00 PM <BR>
 * Last modified on Dec 10, 2005
 * </P>
 * RPMSImporter4ListNamedValue: RPMS list-named-value importer
 * 
 * @author Why
 * @version 1.0, Dec 10, 2005
 * @since Quake 1.0
 */
public class RPMSImporter4ListNamedValue {
    //logger
    private final static Logger logger = Logger.getLogger(RPMSImporter4ListNamedValue.class.getName());

    //configuration resource
    private final static String RESOURCE = "namedvalue/ListNamedValue.rpms.xml";

    //data
    private ISession m_sessionFrom = null;

    private ISession m_sessionTo = null;

    /**
     * Constructor: default
     */
    public RPMSImporter4ListNamedValue() {
        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='"
                + ListNamedValueRPMS.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, 0);
            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;
        ListNamedValueRPMS lnv;
        String sName, sValue, sDescription;
        try {
            //1. to delete the old
            statement = sessionTo.createStatement(strSQL);
            statement.registerAlias("this", ListNamedValueRPMS.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");
                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);
                lnv = new ListNamedValueRPMS();
                lnv.setProperty("type", nType);
                lnv.setValue(sValue);
                lnv.setName(sName);
                lnv.setDescription(sDescription);
                lnv.setCreateUser("system");
                sessionTo.save(lnv);
            }
        } catch (Exception ex) {
            System.out.println("failed to import: type=" + nType + ", desc=" + sTypeDesc);
            ex.printStackTrace(System.out);
        } finally {
            DolphinHelper.clear(objs, statement);
        }
    }

    public static void main(String[] args) {
        try {
            RPMSImporter4ListNamedValue importer = new RPMSImporter4ListNamedValue();
            importer.run();
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }

}