⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loadjobwriter.java

📁 数据仓库工具
💻 JAVA
字号:
/*
     LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
    Copyright (C) 2003  Together
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.webdocwf.util.loader.generator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.util.Enumeration;
import java.util.Hashtable;
import java.io.File;
import java.io.*;
import java.util.*;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.Method;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.webdocwf.util.loader.LoaderException;
import org.webdocwf.util.loader.logging.Logger;
import org.webdocwf.util.loader.logging.StandardLogger;

/**
 * Class LoadJobWriter creates and loads LoaderJob.xml file from the input
 * parameters and source database.
 * @author Radoslav Dutina
 * @version 1.0
 */
public class LoadJobWriter {

    private Document document;
    private Logger logger;

    /**
     * Construct object LoadJobWriter with associated parameter.
     * @param generatorParameters represents the references to InputParameter object.
     * @throws LoaderException
     */

    public LoadJobWriter(InputParameters generatorParameters) throws LoaderException {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            setLogger();
            this.logger.write("normal", "Generating of LoaderJob.olj file is started.");
            loadJobWriter(generatorParameters, factory);
            this.logger.write("normal", "Generating of LoaderJob.olj file is finished.");
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            String msg = "Parser with specified options can't be built! Exception has occurred when parser is" + "try to parse the LoaderJob.olj file!";
            LoaderException le = new LoaderException(msg + "\n" + pce.getMessage(), (Throwable) pce);
            throw le;
        }
    }
    /**
     * This method generate LoaderJob.olj fajl
     * @param generatorParameters
     * @param factory
     * @throws ParserConfigurationException
     * @throws LoaderException
     */
    private void loadJobWriter(InputParameters generatorParameters, DocumentBuilderFactory factory) throws ParserConfigurationException, LoaderException {
				
				this.logger.write("normal", "\tloadJobWriter method is started.");
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.newDocument(); // Create from whole cloth
       
        // Create root element (loaderJob)
        Element root = (Element) document.createElement("loaderJob");
        document.appendChild(root);

        LoaderJobAttributes loaderJob = new LoaderJobAttributes();

        root.setAttribute("logMode", loaderJob.getLogMode());
        root.setAttribute("objectIDIncrement", loaderJob.getObjectIDIncrement());
        root.setAttribute("objectIDTableName", loaderJob.getObjectIDTableName());
        root.setAttribute("objectIDColumnName", loaderJob.getObjectIDColumnName());

        // Create child (jdbcDefaultParameters)
        Element childRoot = (Element) document.createElement("jdbcDefaultParameters");
        root.appendChild(childRoot);

        // Create child  (jdbcSourceParameters)
        Element childRoot2 = (Element) document.createElement("jdbcSourceParameters");
        childRoot.appendChild(childRoot2);

        JdbcParameters sourceJdbc = new JdbcParameters("source", generatorParameters);

        childRoot2.setAttribute("dbVendor", sourceJdbc.getDbVendor());
        childRoot2.setAttribute("driverName", sourceJdbc.getDriverName());

        // Create child (jdbcSourceParameter)
        Hashtable sourceTable = sourceJdbc.getAllParameters();
        Enumeration enumSource = sourceTable.keys();
        while (enumSource.hasMoreElements()) {
            Element childRoot3 = (Element) document.createElement("jdbcSourceParameter");
            childRoot2.appendChild(childRoot3);
            String key = (String) enumSource.nextElement();
            childRoot3.setAttribute("name", key);
            childRoot3.setAttribute("value", (String) sourceTable.get(key));
        }

        // Create child (jdbcTargetParameters)
        Element childRoot5 = (Element) document.createElement("jdbcTargetParameters");
        childRoot.appendChild(childRoot5);

        JdbcParameters targetJdbc = new JdbcParameters("target", generatorParameters);

        //Reading the value of AlterTablePrimaryKey parameter.
        //      generatorParameters.setAlterTablePrimaryKey(targetJdbc.getAlterTablePrimaryKey());

        childRoot5.setAttribute("dbVendor", targetJdbc.getDbVendor());
        childRoot5.setAttribute("driverName", targetJdbc.getDriverName());

        // Create child (jdbcTargetParameter)
        Hashtable targetTable = targetJdbc.getAllParameters();
        Enumeration enumTarget = targetTable.keys();
        while (enumTarget.hasMoreElements()) {
            Element childRoot6 = (Element) document.createElement("jdbcTargetParameter");
            childRoot5.appendChild(childRoot6);
            String key = (String) enumTarget.nextElement();
            childRoot6.setAttribute("name", key);
            childRoot6.setAttribute("value", (String) targetTable.get(key));
        }

        Vector sqlStatement = generatorParameters.getSqlToGenerate();
        int sqlFileType = 0;
        while (sqlFileType < 6) {
            // Create child (sql)
            if (sqlFileType == 3) {
                // Create child (definitionInclude)
                Element childRoot20 = (Element) document.createElement("definitionInclude");
                root.appendChild(childRoot20);
                // Create child (include)
                Element childRoot21 = (Element) document.createElement("include");
                childRoot20.appendChild(childRoot21);

                IncludeTagAttributes includeTagAttributes = new IncludeTagAttributes();

                //relative path from LoaderJob.olj file
                String pathXml = includeTagAttributes.getHref();
                pathXml.trim();
                childRoot21.setAttribute("href", pathXml);
                childRoot21.setAttribute("parse", includeTagAttributes.getParse());
            }
            if (sqlFileType == 4 && generatorParameters.getAlterTablePrimaryKey().equalsIgnoreCase("false")) {
                // only for hsql database
            } else {
                if (generatorParameters.getRestoreMode().equalsIgnoreCase("false")) {
                    if (sqlStatement.get(sqlFileType) != null) {
                        Element childRoot10 = (Element) document.createElement("sql");
                        root.appendChild(childRoot10);
                        SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(sqlFileType);
                        childRoot10.setAttribute("name", sqlTagAttributes.getName());
                        //              childRoot10.setAttribute("logMode", sqlTagAttributes.getLogMode());
                        childRoot10.setAttribute("commit", sqlTagAttributes.getCommit());
                        //ZK added next line 7.5.2004
                        childRoot10.setAttribute("onErrorContinue", sqlTagAttributes.getOnErrorContinue());

                        // Create child (sqlStmt)
                        Element childRoot11 = (Element) document.createElement("sqlStmt");
                        childRoot10.appendChild(childRoot11);

                        // Create child (include)
                        Element childRoot12 = (Element) document.createElement("include");
                        childRoot11.appendChild(childRoot12);

                        //relative path from LoaderJob.olj file
                        String pathSql = sqlTagAttributes.getHref();
                        pathSql.trim();
                        childRoot12.setAttribute("href", pathSql);
                        childRoot12.setAttribute("parse", sqlTagAttributes.getParse());
                    } //end of sqlStatement.get(sqlFileType)

                } else if (generatorParameters.getRestoreMode().equalsIgnoreCase("true")) {
                    //this is use only for restoring the database
                    Element childRoot10 = (Element) document.createElement("sql");
                    root.appendChild(childRoot10);
                    SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(sqlFileType);
                    childRoot10.setAttribute("name", sqlTagAttributes.getName());
                    //              childRoot10.setAttribute("logMode", sqlTagAttributes.getLogMode());
                    childRoot10.setAttribute("commit", sqlTagAttributes.getCommit());
                    //ZK added next line 10.5.2004
                    childRoot10.setAttribute("onErrorContinue", sqlTagAttributes.getOnErrorContinue());
                    // Create child (sqlStmt)
                    Element childRoot11 = (Element) document.createElement("sqlStmt");
                    childRoot10.appendChild(childRoot11);

                    // Create child (include)
                    Element childRoot12 = (Element) document.createElement("include");
                    childRoot11.appendChild(childRoot12);
                    //relative path from LoaderJob.olj file
                    String pathSql = sqlTagAttributes.getHref();
                    pathSql.trim();
                    childRoot12.setAttribute("href", pathSql);
                    childRoot12.setAttribute("parse", sqlTagAttributes.getParse());
                }
            }
            sqlFileType++;
        }
        if (generatorParameters.getGenerateXml().equalsIgnoreCase("true")) {
            try {
                File file = null;
                String slash = null;
                if (generatorParameters.getGeneratorOutput().equalsIgnoreCase("")) {
                    file = new File("");
                    slash = "";
                } else {
                    file = new File(generatorParameters.getGeneratorOutput());
                    slash = "/";
                }
                if (!file.exists())
                    file.mkdirs();

                FileOutputStream os = new FileOutputStream(file + slash + "LoaderJob.olj");
                OutputFormat of = new OutputFormat(); //xerces apache.xml.serializer
                of.setIndenting(true);
                of.setIndent(4);
                of.setMethod(Method.XML);
                of.setPreserveSpace(false);
                of.setLineWidth(0);
                XMLSerializer out = new XMLSerializer(os, of);

                //IOException
                out.serialize(document);
            } catch (Exception e) {
                String msg = "Exception has occurred when we try to set output format for LoaderJob.olj file.";
                //if (this.logger != null) {
								LoaderException le = new LoaderException(msg + "\n" + e.getMessage(), (Throwable) e);
                this.logger.write("full", "Exception has occurred when we try to set output format for LoaderJob.olj file." + "\n" + le.getStackTraceAsString());
                //}
                throw le;
            }
        }
        this.logger.write("normal", "\tloadJobWriter method is finished.");
    }

    /**
     * This method will set logger object
     * @param logger
     */
    private void setLogger() {
        this.logger = StandardLogger.getCentralLogger();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -