Search in sources :

Example 1 with ApplicationContextHolder

use of org.mifos.application.servicefacade.ApplicationContextHolder in project head by mifos.

the class ETLReportDWHelper method execute.

@Override
public void execute(final long timeInMillis) throws BatchJobException {
    new ApplicationContextHolder();
    ArrayList<String> errors = new ArrayList<String>();
    ApplicationContext ach = ApplicationContextHolder.getApplicationContext();
    DriverManagerDataSource ds = (DriverManagerDataSource) ach.getBean("dataSource");
    DriverManagerDataSource dsDW = (DriverManagerDataSource) ach.getBean("dataSourcePentahoDW");
    Pattern pat = Pattern.compile(DATA_WAREHOUSE_DB_NAME_PATTERN);
    Matcher m = pat.matcher(dsDW.getUrl());
    String nameOfDataBase = null;
    if (m.find()) {
        nameOfDataBase = m.group(6);
    }
    if (!nameOfDataBase.equals("")) {
        try {
            dsDW.getConnection();
        } catch (SQLException ex) {
            errors.add("Data Warehouse is not configured");
            throw new BatchJobException("Data warehouse database", errors);
        }
        ConfigurationLocator configurationLocator = new ConfigurationLocator();
        String configPath = configurationLocator.getConfigurationDirectory();
        createPropertiesFileForPentahoDWReports(ds, dsDW);
        String path = configPath + "/ETL/MifosDataWarehouseETL/" + FILENAME;
        String jarPath = configPath + "/ETL/mifos-etl-plugin-1.0-SNAPSHOT.one-jar.jar";
        String javaHome = System.getProperty("java.home") + "/bin/java";
        String pathToLog = configPath + "/ETL/log";
        if (File.separatorChar == '\\') {
            // windows platform
            javaHome = javaHome.replaceAll("/", "\\\\");
            javaHome = '"' + javaHome + '"';
            jarPath = jarPath.replaceAll("/", "\\\\");
            path = path.replaceAll("/", "\\\\");
            pathToLog = pathToLog.replaceAll("/", "\\\\");
        }
        PrintWriter fw = null;
        try {
            boolean hasErrors = false;
            boolean notRun = true;
            ProcessBuilder processBuilder = new ProcessBuilder(javaHome, "-jar", jarPath, path, "false", dsDW.getUsername(), dsDW.getPassword(), dsDW.getUrl());
            processBuilder.redirectErrorStream(true);
            Process p = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            if (new File(pathToLog).exists()) {
                new File(pathToLog).delete();
            }
            File file = new File(pathToLog);
            fw = new PrintWriter(file);
            while ((line = reader.readLine()) != null) {
                fw.println(line);
                if (line.matches("^ERROR.*")) {
                    hasErrors = true;
                }
                notRun = false;
            }
            if (notRun) {
                errors.add("Data Warehouse is not configured properly");
                throw new BatchJobException("Data warehouse database", errors);
            }
            if (hasErrors) {
                errors.add("ETL error, for more details see log file: " + pathToLog);
                throw new BatchJobException("ETL error", errors);
            }
        } catch (IOException ex) {
            throw new BatchJobException(ex.getCause());
        } finally {
            if (fw != null) {
                fw.close();
            }
        }
    } else {
        errors.add("Data Warehouse is not configured");
        throw new BatchJobException("Data warehouse database", errors);
    }
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConfigurationLocator(org.mifos.framework.util.ConfigurationLocator) ApplicationContext(org.springframework.context.ApplicationContext) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) BufferedReader(java.io.BufferedReader) ApplicationContextHolder(org.mifos.application.servicefacade.ApplicationContextHolder) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with ApplicationContextHolder

use of org.mifos.application.servicefacade.ApplicationContextHolder in project head by mifos.

the class JNDIException method checkConfigurationDwDatabase.

public void checkConfigurationDwDatabase(HttpServletRequest request) {
    new ApplicationContextHolder();
    ApplicationContext ach = ApplicationContextHolder.getApplicationContext();
    DriverManagerDataSource dsDW = (DriverManagerDataSource) ach.getBean("dataSourcePentahoDW");
    Pattern pat = Pattern.compile("(jdbc:mysql://)(.*)(:)([0-9]+)(/)([a-zA-Z]*)(?)(.*)");
    Matcher m = pat.matcher(dsDW.getUrl());
    String nameOfDataBase = null;
    if (m.find()) {
        nameOfDataBase = m.group(6);
    }
    if (nameOfDataBase.equals("")) {
        request.getSession().setAttribute("configureDwDatabase", "false");
    } else {
        try {
            dsDW.getConnection();
            request.getSession().setAttribute("configureDwDatabase", "true");
        } catch (SQLException ex) {
            request.getSession().setAttribute("configureDwDatabase", "false");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ApplicationContext(org.springframework.context.ApplicationContext) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) ApplicationContextHolder(org.mifos.application.servicefacade.ApplicationContextHolder)

Aggregations

SQLException (java.sql.SQLException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 ApplicationContextHolder (org.mifos.application.servicefacade.ApplicationContextHolder)2 ApplicationContext (org.springframework.context.ApplicationContext)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 BatchJobException (org.mifos.framework.components.batchjobs.exceptions.BatchJobException)1 ConfigurationLocator (org.mifos.framework.util.ConfigurationLocator)1