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

📄 createincludefiles.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
     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.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import java.io.*;
import java.sql.*;

import java.util.StringTokenizer;
import org.webdocwf.util.loader.*;

import java.net.*;

import org.webdocwf.util.loader.logging.Logger;
import org.webdocwf.util.loader.logging.StandardLogger;
import org.webdocwf.util.loader.wizard.AddClassPath;

/**
 * CreateIncludeFiles class creates Xml(ImportDefinition.xml) and Sql( CreateTables.sql,
 * CreateIntegrity.sql, CreateOisAdminData.sql, CreatePrimary.sql and CreateIndex.sql) files,
 * if the input data is database.
 * @author Radoslav Dutina
 * @version 1.0
 */
public class CreateIncludeFiles {

    private Document document;
    private Document documentDoml;
    private Element rootDoml = null;
    private Element childRoot1 = null;
    private Element childRoot2 = null;
    private Logger logger;
    private InputParameters generatorParameters;


    /**
    	* Construct object CreateIncludeFiles with associated parameter.
    	* @param generatorParameters represents the references to InputParameter object.
    	* @throws LoaderException
    	*/
    public CreateIncludeFiles(InputParameters generatorParameters) throws LoaderException {

        try {
						setLogger();
						this.logger.write("normal","CreateIncludeFiles is started. Generating sql and xml files is started.");  
            createIncludeFiles(generatorParameters);
						this.logger.write("normal","CreateIncludeFiles is finished. Generating sql and xml files is finished.");
        } catch (Exception e) {
            throw new LoaderException("Exception:Error while create include files.", e);
        } catch (FactoryConfigurationError e) {
            throw new LoaderException("FactoryConfigurationError:Error while create include files.", e);
        }
    }


/**
 * This method will generate all xml and sql files
 * @param generatorParameters
 * @throws LoaderException
 * @throws FactoryConfigurationError
 */
    private void createIncludeFiles(InputParameters generatorParameters) throws LoaderException, FactoryConfigurationError {
        
				this.logger.write("normal", "\tcreateIncludeFiles method is started.");
        if (generatorParameters.getSourceDriverName().equalsIgnoreCase("freetds")) {
           // if (this.logger != null) {
                
           // }
            //BufferOctopusClass.getInstance().writeToBuffer("Eroor : Freetds driver can't be used as source driver.");
            LoaderException le = new LoaderException("Exception:", (Throwable) new Exception("Freetds driver can't be used as source driver."));
						this.logger.write("full", "Eroor : Freetds driver can't be used as source driver."+le.getStackTraceAsString());
						throw le;
        }
        

        //Create an importDefinition.xml file, for csv tables
        if (generatorParameters.getSourceType().equalsIgnoreCase("csv") || generatorParameters.getSourceType().equalsIgnoreCase("access") || generatorParameters.getSourceType().equalsIgnoreCase("xml")) {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilderFactory factoryDoml = DocumentBuilderFactory.newInstance();
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                document = builder.newDocument(); // Create from whole cloth
                //for creating the doml file
                DocumentBuilder builderDoml = factoryDoml.newDocumentBuilder();
                documentDoml = builderDoml.newDocument(); // Create from whole cloth

                Element rootDoml = (Element) documentDoml.createElement("doml");
                documentDoml.appendChild(rootDoml);

                Element childRoot1 = (Element) documentDoml.createElement("database");
                rootDoml.appendChild(childRoot1);
                childRoot1.setAttribute("database", generatorParameters.getTargetType());

                Element root = (Element) document.createElement("definitionInclude");
                document.appendChild(root);
                if (generatorParameters.getSourceType().equalsIgnoreCase("csv") || generatorParameters.getSourceType().equalsIgnoreCase("xml")) {
                    String pathToSource = generatorParameters.getSourceDataBase();
                    int endNameDB = pathToSource.indexOf(";");
                    if (endNameDB != -1)
                        pathToSource = pathToSource.substring(0, endNameDB);

                    File file = new File(pathToSource);
                    String[] listFiles = file.list();
                    if (listFiles == null && listFiles.length == 0) {
                        String msg = "Path to source database is wrong, or database is empty!";
                        //if (this.logger != null) {
                            
                        //}
                        LoaderException le = new LoaderException("Exception:", new Exception(msg));
												this.logger.write("full", "Path to source database is wrong, or database is empty!"+le.getStackTraceAsString());
                        throw le;
                    }
                    int x = 0;
                    for (int i = 0; i < listFiles.length; i++) {
                        String tableName = listFiles[i].substring(0, listFiles[i].lastIndexOf("."));
                        if (!tableName.endsWith("csvext")) {
                            //includeTable
                            boolean doJob = false;
                            if (generatorParameters.getIncludeTableList().size() == 0) {
                                doJob = true;
                            } else {
                                for (int j = 0; j < generatorParameters.getIncludeTableList().size(); j++) {
                                    if (tableName.equalsIgnoreCase(generatorParameters.getIncludeTableList().get(j).toString())) {
                                        doJob = true;
                                        break;
                                    }
                                }
                            }
                            if (doJob) {
                                //System.out.println("Working...." + tableName);
                                //BufferOctopusClass.getInstance().writeToBuffer("Working...." + tableName);
                                //if (this.logger != null) {
                                    this.logger.write("normal", "Working...." + tableName);
                                //}
                                CsvTableDesignReader csvTableDesignReader = new CsvTableDesignReader(tableName, generatorParameters);
                                WriteImportDefinition writeImportDefinition = new WriteImportDefinition(document, root, csvTableDesignReader.getImportDefinition(), generatorParameters);
                                WriteSqlFiles writeSql = new WriteSqlFiles(tableName, x, csvTableDesignReader.getImportDefinition(), null, generatorParameters);
                                x++;
                            }

                        }
                    }
                    if (x < 1) {
                        String msg = "There is no tables in specified source database. Check input parameters.";
                        //if (this.logger != null) {
                            
                        //}
                        LoaderException le = new LoaderException("Exception:", new Exception(msg));
												this.logger.write("full", "There is no tables in specified source database. Check input parameters."+le.getStackTraceAsString());
                        throw le;
                    }
                } else { //access,excel database
                    JdbcParameters sourceJdbc = new JdbcParameters("source", generatorParameters);
                    try {
                        Connection conn = null;
                        URL url = null;
                        String app = "";
                        String path = AddClassPath.getClassPathString();
                        if (path != null) {
                            StringTokenizer st = new StringTokenizer(path, ";");
                            int count = 0;
                            while (st.hasMoreTokens()) {
                                GeneratorClassLoader.addURL(new File(st.nextElement().toString()).toURL());
                            }
                            Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver"));
                        } else {
                            Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver"));
                        }
                        conn = DriverManager.getConnection(sourceJdbc.getJdbcParameters("Connection.Url"), sourceJdbc.getJdbcParameters("User"), sourceJdbc.getJdbcParameters("Password"));

                        Statement stmt = conn.createStatement();
                        String[] accessType = { "TABLE" };
                        String accessDbName = generatorParameters.getSourceDataBase();
                        ResultSet rs = conn.getMetaData().getTables(accessDbName, null, "%", accessType);

                        int i = 0;
                        while (rs.next()) {
                            String tableName = rs.getString(3);
                            //includeTable
                            boolean doJob = false;
                            if (generatorParameters.getIncludeTableList().size() == 0) {
                                doJob = true;
                            } else {
                                for (int j = 0; j < generatorParameters.getIncludeTableList().size(); j++) {
                                    if (tableName.equalsIgnoreCase(generatorParameters.getIncludeTableList().get(j).toString())) {
                                        doJob = true;
                                        break;
                                    }
                                }
                            }
                            if (doJob) {
                                //System.out.println("Working...." + tableName);
                                //BufferOctopusClass.getInstance().writeToBuffer("Working...." + tableName);
                                //if (this.logger != null) {
                                    this.logger.write("normal", "Working...." + tableName);
                                //}
                                CsvTableDesignReader csvTableDesignReader = new CsvTableDesignReader(tableName, generatorParameters);

                                WriteImportDefinition writeImportDefinition = new WriteImportDefinition(document, root, csvTableDesignReader.getImportDefinition(), generatorParameters);
                                WriteSqlFiles writeSql = new WriteSqlFiles(tableName, i, csvTableDesignReader.getImportDefinition(), null, generatorParameters);
                                i++;
                            }
                        }
                        if (i < 1) {
                            String msg = "There is no tables in specified source database. Check input parameters.";
                            //if (this.logger != null) {
                              
                            //}
                            LoaderException le = new LoaderException("Exception:", new Exception(msg));
														this.logger.write("full", "Exception:There is no tables in specified source database. Check input parameters."+le.getStackTraceAsString());
                            throw le;
                        }
                        rs.close();
                    } catch (ClassNotFoundException ce) {
                        String msg = "Can't find driver class : ";
                        //if (this.logger != null) {
                           
                        //}
                        LoaderException le = new LoaderException(msg + "\n" + ce.getMessage(), (Throwable) ce);
												this.logger.write("full", "Exception:Can't find driver class!" + "\n" + le.getStackTraceAsString());

⌨️ 快捷键说明

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