Search in sources :

Example 1 with BatchJobException

use of org.mifos.framework.components.batchjobs.exceptions.BatchJobException 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 BatchJobException

use of org.mifos.framework.components.batchjobs.exceptions.BatchJobException in project head by mifos.

the class GenerateMeetingsForCustomerAndSavingsHelper method execute.

@Override
public void execute(@SuppressWarnings("unused") final long timeInMillis) throws BatchJobException {
    workingDays = new FiscalCalendarRules().getWorkingDaysAsJodaTimeDays();
    officeCurrentAndFutureHolidays = new HashMap<Short, List<Holiday>>();
    long taskStartTime = new DateTimeService().getCurrentDateTime().getMillis();
    List<Integer> customerAndSavingsAccountIds = findActiveCustomerAndSavingsAccountIdsThatRequiredMeetingsToBeGenerated();
    int accountCount = customerAndSavingsAccountIds.size();
    if (accountCount == 0) {
        return;
    }
    List<String> errorList = new ArrayList<String>();
    int currentRecordNumber = 0;
    int outputIntervalForBatchJobs = GeneralConfig.getOutputIntervalForBatchJobs();
    int batchSize = GeneralConfig.getBatchSizeForBatchJobs();
    // jpw - hardcoded recordCommittingSize to 500 because now only accounts that need more schedules are returned
    int recordCommittingSize = 500;
    infoLogBatchParameters(accountCount, outputIntervalForBatchJobs, batchSize, recordCommittingSize);
    long startTime = new DateTimeService().getCurrentDateTime().getMillis();
    Integer currentAccountId = null;
    int updatedRecordCount = 0;
    try {
        StaticHibernateUtil.getSessionTL();
        StaticHibernateUtil.startTransaction();
        for (Integer accountId : customerAndSavingsAccountIds) {
            currentRecordNumber++;
            currentAccountId = accountId;
            AccountBO accountBO = legacyAccountDao.getAccount(accountId);
            List<Holiday> currentAndFutureHolidays = getOfficeCurrentAndFutureHolidays(accountBO.getOffice().getOfficeId());
            ScheduledDateGeneration scheduleGenerationStrategy = new HolidayAndWorkingDaysAndMoratoriaScheduledDateGeneration(workingDays, currentAndFutureHolidays);
            if (accountBO instanceof CustomerAccountBO) {
                ((CustomerAccountBO) accountBO).generateNextSetOfMeetingDates(scheduleGenerationStrategy);
                updatedRecordCount++;
            } else if (accountBO instanceof SavingsBO) {
                ((SavingsBO) accountBO).generateNextSetOfMeetingDates(workingDays, currentAndFutureHolidays);
                updatedRecordCount++;
            }
            if (currentRecordNumber % batchSize == 0) {
                StaticHibernateUtil.flushAndClearSession();
                getLogger().debug("completed HibernateUtil.flushAndClearSession()");
            }
            if (updatedRecordCount > 0) {
                if (updatedRecordCount % recordCommittingSize == 0) {
                    StaticHibernateUtil.commitTransaction();
                    StaticHibernateUtil.getSessionTL();
                    StaticHibernateUtil.startTransaction();
                }
            }
            if (currentRecordNumber % outputIntervalForBatchJobs == 0) {
                long time = System.currentTimeMillis();
                String message = "" + currentRecordNumber + " processed, " + (accountCount - currentRecordNumber) + " remaining, " + updatedRecordCount + " updated, batch time: " + (time - startTime) + " ms";
                logMessage(message);
                startTime = time;
            }
        }
        StaticHibernateUtil.commitTransaction();
        long time = System.currentTimeMillis();
        String message = "" + currentRecordNumber + " processed, " + (accountCount - currentRecordNumber) + " remaining, " + updatedRecordCount + " updated, batch time: " + (time - startTime) + " ms";
        logMessage(message);
    } catch (Exception e) {
        logMessage("account " + currentAccountId.intValue() + " exception " + e.getMessage());
        StaticHibernateUtil.rollbackTransaction();
        errorList.add(currentAccountId.toString());
        getLogger().error("Unable to generate schedules for account with ID " + currentAccountId, e);
    } finally {
        StaticHibernateUtil.closeSession();
    }
    if (errorList.size() > 0) {
        throw new BatchJobException(SchedulerConstants.FAILURE, errorList);
    }
    logMessage("GenerateMeetingsForCustomerAndSavings ran in " + (new DateTimeService().getCurrentDateTime().getMillis() - taskStartTime));
}
Also used : CustomerAccountBO(org.mifos.customers.business.CustomerAccountBO) ArrayList(java.util.ArrayList) SavingsBO(org.mifos.accounts.savings.business.SavingsBO) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) CustomerAccountBO(org.mifos.customers.business.CustomerAccountBO) AccountBO(org.mifos.accounts.business.AccountBO) HolidayAndWorkingDaysAndMoratoriaScheduledDateGeneration(org.mifos.schedule.internal.HolidayAndWorkingDaysAndMoratoriaScheduledDateGeneration) ScheduledDateGeneration(org.mifos.schedule.ScheduledDateGeneration) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) Holiday(org.mifos.application.holiday.business.Holiday) ArrayList(java.util.ArrayList) List(java.util.List) DateTimeService(org.mifos.framework.util.DateTimeService) FiscalCalendarRules(org.mifos.config.FiscalCalendarRules) HolidayAndWorkingDaysAndMoratoriaScheduledDateGeneration(org.mifos.schedule.internal.HolidayAndWorkingDaysAndMoratoriaScheduledDateGeneration)

Example 3 with BatchJobException

use of org.mifos.framework.components.batchjobs.exceptions.BatchJobException in project head by mifos.

the class LoanArrearsAgingHelper method generateLoanArrearsAging.

private void generateLoanArrearsAging() throws BatchJobException {
    StaticHibernateUtil.startTransaction();
    try {
        Query insert_select = StaticHibernateUtil.getSessionTL().getNamedQuery("generateLoanArrearsAging");
        insert_select.setParameter("CURRENT_DATE", new LocalDate().toString());
        insert_select.executeUpdate();
        StaticHibernateUtil.commitTransaction();
    } catch (Exception e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new BatchJobException(e);
    }
}
Also used : BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) Query(org.hibernate.Query) LocalDate(org.joda.time.LocalDate) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException)

Example 4 with BatchJobException

use of org.mifos.framework.components.batchjobs.exceptions.BatchJobException in project head by mifos.

the class LoanArrearsAgingHelper method deleteAllLoanArrearsAging.

private void deleteAllLoanArrearsAging() throws BatchJobException {
    StaticHibernateUtil.startTransaction();
    try {
        Query delete = StaticHibernateUtil.getSessionTL().getNamedQuery("deleteAllLoanArrearsAging");
        delete.executeUpdate();
        StaticHibernateUtil.commitTransaction();
    } catch (Exception e) {
        StaticHibernateUtil.rollbackTransaction();
        throw new BatchJobException(e);
    }
}
Also used : BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) Query(org.hibernate.Query) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException)

Example 5 with BatchJobException

use of org.mifos.framework.components.batchjobs.exceptions.BatchJobException in project head by mifos.

the class ApplyPenaltyToLoanAccountsHelper method execute.

@Override
public void execute(final long timeInMillis) throws BatchJobException {
    setCurrentDates(timeInMillis);
    List<String> errorList = new ArrayList<String>();
    List<LoanBO> loanAccounts;
    try {
        loanAccounts = getLoanAccounts();
    } catch (Exception e) {
        throw new BatchJobException(e);
    }
    if (loanAccounts != null && !loanAccounts.isEmpty()) {
        Integer loanAccountId = null;
        try {
            for (LoanBO loanAccount : loanAccounts) {
                loanAccountId = loanAccount.getAccountId();
                List<AccountPenaltiesEntity> penaltyEntities = new ArrayList<AccountPenaltiesEntity>(loanAccount.getAccountPenalties());
                for (AccountPenaltiesEntity penaltyEntity : penaltyEntities) {
                    List<LoanScheduleEntity> lateInstallments = loanAccount.getDetailsOfLateInstallmentsPeriod(new LocalDate(penaltyEntity.getCreatedDate()), currentLocalDate);
                    for (LoanScheduleEntity entity : lateInstallments) {
                        //check grace period for installment period type
                        if (penaltyEntity.getPenalty().getPeriodType().getPenaltyPeriod() == PenaltyPeriod.INSTALLMENTS && penaltyEntity.hasPeriodType()) {
                            if (lateInstallments.get(0).getInstallmentId().equals(entity.getInstallmentId()) && checkGracePeriodTypeInstallments(lateInstallments, penaltyEntity.getPenalty().getPeriodDuration())) {
                                continue;
                            }
                        } else //check grace period for daily period type
                        if (penaltyEntity.getPenalty().getPeriodType().getPenaltyPeriod() == PenaltyPeriod.DAYS && penaltyEntity.hasPeriodType()) {
                            if (checkGracePeriodTypeDays(entity, penaltyEntity.getPenalty().getPeriodDuration())) {
                                continue;
                            }
                        }
                        LoanPenaltyScheduleEntity penaltySchedule = entity.getPenaltyScheduleEntity(penaltyEntity.getPenalty().getPenaltyId());
                        if (checkPeriod(penaltyEntity, new LocalDate(entity.getActionDate().getTime())) || (penaltySchedule != null && penaltySchedule.isOn(currentLocalDate))) {
                            continue;
                        }
                        if (penaltyEntity.isAmountPenalty()) {
                            addAmountPenalty(penaltyEntity, loanAccount, entity);
                        } else {
                            addRatePenalty(penaltyEntity, loanAccount, entity);
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (loanAccountId != null) {
                getLogger().error(String.format("ApplyPenaltyToLoanAccountsTask execute failed with exception %s: %s at loan account %s", e.getClass().getName(), e.getMessage(), loanAccountId.toString()), e);
                errorList.add(loanAccountId.toString());
            }
            StaticHibernateUtil.rollbackTransaction();
        } finally {
            StaticHibernateUtil.closeSession();
        }
    }
    if (!errorList.isEmpty()) {
        throw new BatchJobException(SchedulerConstants.FAILURE, errorList);
    }
}
Also used : LoanScheduleEntity(org.mifos.accounts.loan.business.LoanScheduleEntity) AccountPenaltiesEntity(org.mifos.accounts.business.AccountPenaltiesEntity) LoanBO(org.mifos.accounts.loan.business.LoanBO) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) LoanPenaltyScheduleEntity(org.mifos.accounts.loan.business.LoanPenaltyScheduleEntity)

Aggregations

BatchJobException (org.mifos.framework.components.batchjobs.exceptions.BatchJobException)18 ArrayList (java.util.ArrayList)8 DateTimeService (org.mifos.framework.util.DateTimeService)6 Date (java.util.Date)3 Query (org.hibernate.Query)3 LocalDate (org.joda.time.LocalDate)3 PersistenceException (org.mifos.framework.exceptions.PersistenceException)3 ServiceException (org.mifos.framework.exceptions.ServiceException)3 Session (org.hibernate.Session)2 AccountBO (org.mifos.accounts.business.AccountBO)2 LoanBO (org.mifos.accounts.loan.business.LoanBO)2 Holiday (org.mifos.application.holiday.business.Holiday)2 CustomerAccountBO (org.mifos.customers.business.CustomerAccountBO)2 CustomerPersistence (org.mifos.customers.persistence.CustomerPersistence)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 SQLException (java.sql.SQLException)1