forked from geodmms/xdgnjobs

Dennis Kao
2014-01-15 94ae08701bbd7585a0b7e5a92d1975965a503c03
xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/AbstractOracleDatabaseJob.java
@@ -3,32 +3,32 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import com.vividsolutions.jts.util.Assert;
import com.ximple.eofms.jobs.context.AbstractOracleJobContext;
import com.ximple.eofms.util.FeatureTypeBuilderUtil;
import oracle.sql.BLOB;
import org.apache.commons.logging.Log;
import org.geotools.data.DataStore;
import org.geotools.data.jdbc.ConnectionPoolManager;
import org.geotools.data.oracle.OracleDataStore;
import org.geotools.data.oracle.OracleDataStoreFactory;
import org.geotools.data.oracle.OracleNGDataStoreFactory;
import org.geotools.jdbc.JDBCDataStore;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.vividsolutions.jts.util.Assert;
import oracle.sql.BLOB;
import com.ximple.eofms.jobs.context.AbstractOracleJobContext;
public abstract class AbstractOracleDatabaseJob implements Job
{
public abstract class AbstractOracleDatabaseJob implements Job {
    /**
     * The Oracle driver class name
     */
@@ -44,6 +44,7 @@
    private static final String CONVERTDB = "CONVERTDB";
    private static final String CONVERTFILE = "CONVERTFILE";
    private static final String CONVERTELEMIN = "CONVERTELEMIN";
    private static final String CONVERTPWTHEMES = "CONVERTPWTHEMES";
    private static final String CREATEDUMMY = "CREATEDUMMY";
    private static final String ELEMLOG = "ELEMLOG";
    private static final String ORAHOST = "ORAHOST";
@@ -54,8 +55,11 @@
    private static final String TESTMODE = "TESTMODE";
    private static final String TESTCOUNT = "TESTCOUNT";
    private static final String COPYCONNECTIVITYMODE = "COPYCONNECTIVITYMODE";
    private static final String PROFILEMODE = "PROFILEMODE";
    private static final String USEZONE121 = "USEZONE121";
    private static final String IGNORE_DBETL = "IGNORE_DBETL";
    protected static OracleDataStoreFactory dataStoreFactory = new OracleDataStoreFactory();
    protected static OracleNGDataStoreFactory dataStoreFactory = new OracleNGDataStoreFactory();
    protected String _dataPath;
    protected String _filterPath;
@@ -67,22 +71,30 @@
    protected String _convertDB;
    protected String _convertFile;
    protected String _convertElementIn;
    protected String _convertPWThemes;
    protected String _elementLogging;
    protected String _createDummy;
    protected ArrayList<String> _orgSchema = new ArrayList<String>();
    protected boolean _testMode = false;
    protected boolean _copyConnectivityMode = false;
    protected boolean _profileMode = false;
    protected boolean _useZone121 = true;
    protected boolean _useTransform = true;
    protected boolean _ignoreDBETL = false;
    protected int _testCount = -1;
    protected OracleDataStore sourceDataStore;
    protected JDBCDataStore sourceDataStore;
    private boolean driverFound = true;
    protected AbstractOracleDatabaseJob()
    {
        try
        {
    private long _processTime;
    private long _updateTime;
    private long _processTimeStart;
    private long _updateTimeStart;
    protected AbstractOracleDatabaseJob() {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (Throwable t)
        {
        } catch (Throwable t) {
            // must be running off dummy jar!
            driverFound = false;
        }
@@ -92,8 +104,7 @@
    public abstract Log getLogger();
    protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException
    {
    protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException {
        // The directory to scan is stored in the job map
        JobDataMap dataMap = jobDetail.getJobDataMap();
        _dataPath = dataMap.getString(JOBDATA_DIR);
@@ -106,6 +117,7 @@
        _convertDB = dataMap.getString(CONVERTDB);
        _convertFile = dataMap.getString(CONVERTFILE);
        _convertElementIn = dataMap.getString(CONVERTELEMIN);
        _convertPWThemes = dataMap.getString(CONVERTPWTHEMES);
        _elementLogging = dataMap.getString(ELEMLOG);
        _createDummy = dataMap.getString(CREATEDUMMY);
@@ -126,20 +138,26 @@
        String strSchema = dataMap.getString(SPATAILSCHEMA);
        StringTokenizer st = new StringTokenizer(strSchema, ",");
        while (st.hasMoreTokens())
        {
        while (st.hasMoreTokens()) {
            String aSchema = st.nextToken().trim();
            _orgSchema.add(aSchema);
        }
        _testMode = dataMap.getBooleanFromString(TESTMODE);
        _testCount = dataMap.getIntFromString(TESTCOUNT);
        _copyConnectivityMode = dataMap.getBooleanFromString(COPYCONNECTIVITYMODE);
        _profileMode = dataMap.getBooleanFromString(PROFILEMODE);
        _useZone121 = dataMap.getBooleanFromString(USEZONE121);
        _ignoreDBETL = dataMap.getBooleanFromString(IGNORE_DBETL);
        if (_useZone121) {
            FeatureTypeBuilderUtil.setDefaultFeatureSRID(3826);
        } else {
            FeatureTypeBuilderUtil.setDefaultFeatureSRID(3825);
        }
        // Validate the required input
        if (_dataPath == null)
        {
            if (logger != null)
            {
        if (_dataPath == null) {
            if (logger != null) {
                logger.warn("Cannot found data directory in configarion.");
            }
            throw new JobExecutionException("Directory not configured");
@@ -147,47 +165,40 @@
        // Make sure the directory exists
        File dir = new File(_dataPath);
        if (!dir.exists())
        {
        if (!dir.exists()) {
            logger = getLogger();
            if (logger != null)
            {
            if (logger != null) {
                logger.warn("Cannot found data directory in file system.[" + _dataPath + "]");
            }
            throw new JobExecutionException("Invalid Dir " + _dataPath);
        }
        if (_oracleHost == null)
        {
        if (_oracleHost == null) {
            logger.warn("OracleHOST is null");
            throw new JobExecutionException("Unknown Oracle Host.");
        }
        if (_oracleInstance == null)
        {
        if (_oracleInstance == null) {
            logger.warn("OracleINSTANCE is null");
            throw new JobExecutionException("Unknown Oracle Instance.");
        }
        if (_username == null)
        {
        if (_username == null) {
            logger.warn("OracleUSER is null");
            throw new JobExecutionException("Unknown Oracle Username.");
        }
        if (_password == null)
        {
        if (_password == null) {
            logger.warn("OraclePASS is null");
            throw new JobExecutionException("Unknown Oracle Password.");
        }
        if (_orgSchema == null)
        {
        if (_orgSchema == null) {
            logger.warn("OracleSchema is null");
            throw new JobExecutionException("Unknown Spatial Database Schema.");
        }
    }
    protected abstract AbstractOracleJobContext prepareJobContext(String targetSchemaName, String filterPath);
    protected abstract AbstractOracleJobContext prepareJobContext(String targetSchemaName, String filterPath,
                                                                  boolean profileMode, boolean useTransform);
    protected static byte[] getBytesFromBLOB(BLOB blob) throws SQLException
    {
    protected byte[] getBytesFromBLOB(BLOB blob) throws SQLException, BufferOverflowException {
        byte[] raw = null;
        // BLOB        blob        = (BLOB) rs.getBlob(1);
@@ -195,220 +206,328 @@
        byte[] chunk = new byte[optimalSize];
        InputStream is = blob.getBinaryStream(0);
        ByteBuffer buffer = null;    // ByteBuffer.allocate(optimalSize);
        int len;
        int len = 0;
        try
        {
            while ((len = (is.read(chunk))) != -1)
            {
                if (buffer != null)
                {
        try {
            while ((len = (is.read(chunk))) != -1) {
                if (buffer != null) {
                    buffer.limit(buffer.limit() + len);
                } else
                {
                } else {
                    buffer = ByteBuffer.allocate(len);
                }
                buffer.put(chunk);
                buffer.put(chunk, 0, len);
            }
            is.close();
            assert buffer != null;
            buffer.position(0);
            raw = buffer.array();
        } catch (IOException e)
        {
        } catch (IOException e) {
            e.printStackTrace();    // To change body of catch statement use File | Settings | File Templates.
            Assert.shouldNeverReachHere();
        } catch (BufferOverflowException e) {
            getLogger().warn("BufferOverflowException:: len = " + len + "buffer.limit=" + buffer.limit());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                getLogger().warn("InputStream cannot close", e);
            }
            ;
        }
        return raw;
    }
    public boolean isDriverFound()
    {
    public boolean isDriverFound() {
        return driverFound;
    }
    public String getDataPath()
    {
    public String getDataPath() {
        return _dataPath;
    }
    public String getFilterPath()
    {
    public String getFilterPath() {
        return _filterPath;
    }
    public String getOracleHost()
    {
    public String getOracleHost() {
        return _oracleHost;
    }
    public String getOracleInstance()
    {
    public String getOracleInstance() {
        return _oracleInstance;
    }
    public String getOraclePort()
    {
    public String getOraclePort() {
        return _oraclePort;
    }
    public String getUsername()
    {
    public String getUsername() {
        return _username;
    }
    public String getPassword()
    {
    public String getPassword() {
        return _password;
    }
    public ArrayList<String> getOriginSchema()
    {
    public ArrayList<String> getOriginSchema() {
        return _orgSchema;
    }
    public boolean isTestMode()
    {
    public boolean isTestMode() {
        return _testMode;
    }
    public int getTestCount()
    {
    public int getTestCount() {
        return _testCount;
    }
    public String getConvertDB()
    {
    public String getConvertDB() {
        return _convertDB;
    }
    public void setConvertDB(String convertDB)
    {
    public void setConvertDB(String convertDB) {
        _convertDB = convertDB;
    }
    public String getConvertFile()
    {
    public String getConvertFile() {
        return _convertFile;
    }
    public void setConvertFile(String convertFile)
    {
    public void setConvertFile(String convertFile) {
        _convertFile = convertFile;
    }
    public String getConvertElementIn()
    {
    public String getConvertElementIn() {
        return _convertElementIn;
    }
    public void setConvertElementIn(String convertElementIn)
    {
    public void setConvertElementIn(String convertElementIn) {
        _convertElementIn = convertElementIn;
    }
    public boolean checkConvertDB()
    {
    public boolean checkConvertDB() {
        return _convertDB != null && !_convertDB.equalsIgnoreCase("false") &&
                !_convertDB.equalsIgnoreCase("no") && !_convertDB.equalsIgnoreCase("0");
            !_convertDB.equalsIgnoreCase("no") && !_convertDB.equalsIgnoreCase("0");
    }
    public boolean checkConvertFile()
    {
    public boolean checkConvertFile() {
        return _convertFile != null && !_convertFile.equalsIgnoreCase("false") &&
                !_convertFile.equalsIgnoreCase("no") && !_convertFile.equalsIgnoreCase("0");
            !_convertFile.equalsIgnoreCase("no") && !_convertFile.equalsIgnoreCase("0");
    }
    public boolean checkConvertElementIn()
    {
    public boolean checkConvertElementIn() {
        return _convertElementIn != null && !_convertElementIn.equalsIgnoreCase("false") &&
                !_convertElementIn.equalsIgnoreCase("no") && !_convertElementIn.equalsIgnoreCase("0");
            !_convertElementIn.equalsIgnoreCase("no") && !_convertElementIn.equalsIgnoreCase("0");
    }
    public String getElementLogging()
    {
    public boolean checkConvertPWThemes() {
        return _convertPWThemes != null && !_convertPWThemes.equalsIgnoreCase("false") &&
            !_convertPWThemes.equalsIgnoreCase("no") && !_convertPWThemes.equalsIgnoreCase("0");
    }
    public String getElementLogging() {
        return _elementLogging;
    }
    public void setElementLogging(String elementLogging)
    {
    public void setElementLogging(String elementLogging) {
        this._elementLogging = elementLogging;
    }
    public boolean checkElementLogging()
    {
    public boolean checkElementLogging() {
        return _elementLogging != null && !_elementLogging.equalsIgnoreCase("false") &&
                !_elementLogging.equalsIgnoreCase("no") && !_elementLogging.equalsIgnoreCase("0");
            !_elementLogging.equalsIgnoreCase("no") && !_elementLogging.equalsIgnoreCase("0");
    }
    public boolean checkCreateDummy()
    {
    public boolean checkCreateDummy() {
        return _createDummy != null && !_createDummy.equalsIgnoreCase("false") &&
                !_createDummy.equalsIgnoreCase("no") && !_createDummy.equalsIgnoreCase("0");
            !_createDummy.equalsIgnoreCase("no") && !_createDummy.equalsIgnoreCase("0");
    }
    public boolean isCopyConnectivityMode()
    {
    public boolean isCopyConnectivityMode() {
        return _copyConnectivityMode;
    }
    public DataStore getSourceDataStore()
    {
    public boolean isProfileMode() {
        return _profileMode;
    }
    public boolean isTransformed() {
        return _useTransform;
    }
    public boolean isZone121() {
        return _useZone121;
    }
    public boolean isIgnoreDBETL() {
        return _ignoreDBETL;
    }
    public void set_ignoreDBETL(boolean _ignoreDBETL) {
        this._ignoreDBETL = _ignoreDBETL;
    }
    public DataStore getSourceDataStore() {
        return sourceDataStore;
    }
    protected void createSourceDataStore() throws JobExecutionException
    {
        if (sourceDataStore != null)
        {
    protected void createSourceDataStore() throws JobExecutionException {
        if (sourceDataStore != null) {
            sourceDataStore.dispose();
            sourceDataStore = null;
        }
        if (!isDriverFound())
        {
        if (!isDriverFound()) {
            throw new JobExecutionException("Oracle JDBC Driver not found.-" + JDBC_DRIVER);
        }
        Map<String, String> map = new TreeMap<String, String>();
        map.put("host", _oracleHost);
        map.put("port", _oraclePort);
        map.put("instance", _oracleInstance);
        map.put("user", _username);
        map.put("passwd", _password);
        map.put("dbtype", "oracle");
        map.put("alias", _oracleInstance);
        map.put("namespace", null);
        if (!map.containsKey(OracleDataStoreFactory.MAXCONN.key))
        {
            map.put(OracleDataStoreFactory.MAXCONN.key, "10");
        map.put(OracleNGDataStoreFactory.HOST.key, _oracleHost);
        map.put(OracleNGDataStoreFactory.PORT.key, _oraclePort);
        map.put(OracleNGDataStoreFactory.DATABASE.key, _oracleInstance);
        map.put(OracleNGDataStoreFactory.USER.key, _username);
        map.put(OracleNGDataStoreFactory.PASSWD.key, _password);
        map.put(OracleNGDataStoreFactory.DBTYPE.key, "oracle");
        map.put(OracleNGDataStoreFactory.NAMESPACE.key, null);
        if (!map.containsKey(OracleNGDataStoreFactory.MAXCONN.key)) {
            map.put(OracleNGDataStoreFactory.MAXCONN.key, "5");
        }
        if (!map.containsKey(OracleDataStoreFactory.MINCONN.key))
        {
            map.put(OracleDataStoreFactory.MINCONN.key, "1");
        if (!map.containsKey(OracleNGDataStoreFactory.MINCONN.key)) {
            map.put(OracleNGDataStoreFactory.MINCONN.key, "1");
        }
        if (!dataStoreFactory.canProcess(map))
        {
        if (!dataStoreFactory.canProcess(map)) {
            getLogger().warn("cannot process properties-");
            throw new JobExecutionException("cannot process properties-");
        }
        try
        {
            sourceDataStore = (OracleDataStore) dataStoreFactory.createDataStore(map);
        } catch (IOException e)
        {
        try {
            sourceDataStore = dataStoreFactory.createDataStore(map);
        } catch (IOException e) {
            getLogger().warn(e.getMessage(), e);
            throw new JobExecutionException(e.getMessage(), e);
        }
    }
    protected void disconnect()
    {
        ConnectionPoolManager manager = ConnectionPoolManager.getInstance();
        manager.closeAll();
    protected void disconnect() {
        // ConnectionPoolManager manager = ConnectionPoolManager.getInstance();
        // manager.closeAll();
        if (sourceDataStore != null) {
            sourceDataStore.dispose();
            sourceDataStore = null;
        }
    }
    public final void accumulateProcessTime() {
        _processTime += System.currentTimeMillis() - _processTimeStart;
    }
    public final void accumulateUpdateTime() {
        _updateTime += System.currentTimeMillis() - _updateTimeStart;
    }
    public long getProcessTime() {
        return _processTime;
    }
    public long getUpdateTime() {
        return _updateTime;
    }
    public final void markProcessTime() {
        _processTimeStart = System.currentTimeMillis();
    }
    public final void markUpdateTime() {
        _updateTimeStart = System.currentTimeMillis();
    }
    public final void resetProcessTime() {
        _processTime = 0;
    }
    public final void resetUpdateTime() {
        _updateTime = 0;
    }
    /**
     * Connectivity (Connectivity)
     *
     * @param jobContext job context
     * @throws java.sql.SQLException sql exception
     */
    protected void copyConnectivity(AbstractOracleJobContext jobContext) throws SQLException {
        Connection connection = jobContext.getOracleConnection();
        ResultSet rsMeta = connection.getMetaData().getTables(null, "BASEDB",
            AbstractOracleJobContext.CONNECTIVITY_WEBCHECK_NAME + "%",
            new String[]{"TABLE"});
        boolean found = false;
        try {
            while (rsMeta.next()) {
                String tablename = rsMeta.getString(3);
                if (AbstractOracleJobContext.CONNECTIVITY_WEBCHECK_NAME.equalsIgnoreCase(tablename)) {
                    found = true;
                    break;
                }
            }
            // } catch (SQLException e)
        } finally {
            if (rsMeta != null) {
                rsMeta.close();
                rsMeta = null;
            }
        }
        Statement stmt = connection.createStatement();
        if (found) {
            stmt.execute(AbstractOracleJobContext.TRUNCATE_CONNECTIVITY_WEBCHECK);
        } else {
            getLogger().info("Create CONNECTIVITY_WEBCHECK table.");
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK);
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK_INDEX_1);
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK_INDEX_2);
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK_INDEX_3);
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK_INDEX_4);
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK_INDEX_5);
            stmt.execute(AbstractOracleJobContext.CREATE_CONNECTIVITY_WEBCHECK_INDEX_6);
            stmt.execute(AbstractOracleJobContext.ALTER_CONNECTIVITY_WEBCHECK_1);
            stmt.execute(AbstractOracleJobContext.ALTER_CONNECTIVITY_WEBCHECK_2);
        }
        stmt.execute(AbstractOracleJobContext.COPY_CONNECTIVITY_TO_WEBCHECK);
        stmt.close();
    }
    protected void fetchTPData(AbstractOracleJobContext jobContext) {
        Connection connection = jobContext.getOracleConnection();
        try {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(AbstractOracleJobContext.FETCH_TPDATA);
            if (rs.next()) {
                short disId = rs.getShort(1);
                jobContext.setDistId(disId);
                String distName = rs.getString(2);
                jobContext.setDistName(distName);
            }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            getLogger().warn("Fetch TPDATA Error.", e);
        }
    }
    protected void createHibernateSequence(AbstractOracleJobContext jobContext) throws SQLException {
        Connection connection = jobContext.getOracleConnection();
        try {
            Statement stmt = connection.createStatement();
            stmt.execute(AbstractOracleJobContext.CREATE_HIBERNATE_SEQUENCE);
            stmt.close();
        } catch (SQLException e) {
            getLogger().warn("HIBERNATE_SEQUENCE is already exist.");
        }
    }
}