<commit_before>package org.mifos.framework.components.batchjobs.helpers;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.mifos.application.servicefacade.ApplicationContextHolder;
import org.mifos.framework.components.batchjobs.TaskHelper;
import org.mifos.framework.components.batchjobs.exceptions.BatchJobException;
import org.mifos.framework.util.ConfigurationLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class ETLReportDWHelper extends TaskHelper {

    private final String FILENAME = "DataWarehouseInitialLoad.kjb";
    private static final Logger LOGGER = Logger.getLogger(ETLReportDWHelper.class);

    @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("(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("")) {
        	
        	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"; 
            String cmd = "-jar " + jarPath + " " + path + " false";   
            if (File.separatorChar == '\\') { // windows platform
                javaHome=javaHome.replaceAll("/", "\\\\");
                javaHome='"'+javaHome+'"';
                cmd = cmd.replaceAll("/", "\\\\");
            }
            try {
            	boolean hasErrors = false;
            	cmd = javaHome +" "+ cmd +" "+ dsDW.getUsername() + " " + dsDW.getPassword() + " " + dsDW.getUrl();
                Process p = Runtime.getRuntime().exec(cmd);
                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);
				PrintWriter fw = new PrintWriter(file);
                while ((line = reader.readLine ()) != null) {
                	fw.println(line);
                	if (line.matches("^ERROR.*")) {
                		hasErrors = true;
                	}
                }
                fw.close();
                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());
            }
        } 
        

    }

    private void createPropertiesFileForPentahoDWReports(DriverManagerDataSource ds, DriverManagerDataSource dsDW) {
        try {
            String pathToJNDIDirectory = System.getProperty("user.dir") + "/simple-jndi";
            String pathToJNDIPropertiesFile = System.getProperty("user.dir") + "/simple-jndi/jdbc.properties";
            if (File.separatorChar == '\\') {
                pathToJNDIDirectory = pathToJNDIDirectory.replaceAll("/", "\\\\");
                pathToJNDIPropertiesFile = pathToJNDIPropertiesFile.replaceAll("/", "\\\\");
            }
            if(!new File(pathToJNDIDirectory).exists()){
            	new File(pathToJNDIDirectory).mkdir();
            }
            File file = new File(pathToJNDIPropertiesFile);
            PrintWriter fw = new PrintWriter(file);
            fw.println("SourceDB/type=javax.sql.DataSource");
            fw.println("SourceDB/driver=com.mysql.jdbc.Driver");
            fw.println("SourceDB/url=" + ds.getUrl());
            fw.println("SourceDB/user=" + ds.getUsername());
            fw.println("SourceDB/password=" + ds.getPassword());
            fw.println("DestinationDB/type=javax.sql.DataSource");
            fw.println("DestinationDB/driver=com.mysql.jdbc.Driver");
            fw.println("DestinationDB/url=" + dsDW.getUrl());
            fw.println("DestinationDB/user=" + dsDW.getUsername());
            fw.println("DestinationDB/password=" + dsDW.getUsername());
            fw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
<commit_msg>MIFOS-5829: Added information in ETL when database is not configured<commit_after>package org.mifos.framework.components.batchjobs.helpers;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.mifos.application.servicefacade.ApplicationContextHolder;
import org.mifos.framework.components.batchjobs.TaskHelper;
import org.mifos.framework.components.batchjobs.exceptions.BatchJobException;
import org.mifos.framework.util.ConfigurationLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class ETLReportDWHelper extends TaskHelper {

    private final String FILENAME = "DataWarehouseInitialLoad.kjb";
    private static final Logger LOGGER = Logger.getLogger(ETLReportDWHelper.class);

    @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("(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("")) {
        	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"; 
            String cmd = "-jar " + jarPath + " " + path + " false";   
            if (File.separatorChar == '\\') { // windows platform
                javaHome=javaHome.replaceAll("/", "\\\\");
                javaHome='"'+javaHome+'"';
                cmd = cmd.replaceAll("/", "\\\\");
            }
            try {
            	boolean hasErrors = false;
            	cmd = javaHome +" "+ cmd +" "+ dsDW.getUsername() + " " + dsDW.getPassword() + " " + dsDW.getUrl();
                Process p = Runtime.getRuntime().exec(cmd);
                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);
				PrintWriter fw = new PrintWriter(file);
                while ((line = reader.readLine ()) != null) {
                	fw.println(line);
                	if (line.matches("^ERROR.*")) {
                		hasErrors = true;
                	}
                }
                fw.close();
                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());
            }
		} else {
			errors.add("Data Warehouse is not configured");
			throw new BatchJobException("Data warehouse database", errors);
		}
        

    }

    private void createPropertiesFileForPentahoDWReports(DriverManagerDataSource ds, DriverManagerDataSource dsDW) {
        try {
            String pathToJNDIDirectory = System.getProperty("user.dir") + "/simple-jndi";
            String pathToJNDIPropertiesFile = System.getProperty("user.dir") + "/simple-jndi/jdbc.properties";
            if (File.separatorChar == '\\') {
                pathToJNDIDirectory = pathToJNDIDirectory.replaceAll("/", "\\\\");
                pathToJNDIPropertiesFile = pathToJNDIPropertiesFile.replaceAll("/", "\\\\");
            }
            if(!new File(pathToJNDIDirectory).exists()){
            	new File(pathToJNDIDirectory).mkdir();
            }
            File file = new File(pathToJNDIPropertiesFile);
            PrintWriter fw = new PrintWriter(file);
            fw.println("SourceDB/type=javax.sql.DataSource");
            fw.println("SourceDB/driver=com.mysql.jdbc.Driver");
            fw.println("SourceDB/url=" + ds.getUrl());
            fw.println("SourceDB/user=" + ds.getUsername());
            fw.println("SourceDB/password=" + ds.getPassword());
            fw.println("DestinationDB/type=javax.sql.DataSource");
            fw.println("DestinationDB/driver=com.mysql.jdbc.Driver");
            fw.println("DestinationDB/url=" + dsDW.getUrl());
            fw.println("DestinationDB/user=" + dsDW.getUsername());
            fw.println("DestinationDB/password=" + dsDW.getUsername());
            fw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

{'fraction_non_alphanumeric': 0.12931985294117648, 'fraction_numerical': 0.0014705882352941176, 'mean_word_length': 2.480806142034549, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6748881', 'n_tokens_mistral': 3155, 'n_tokens_neox': 2993, 'n_words': 537}