<commit_before>/*
 * Copyright (c) 2005-2009 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */
 
package org.mifos.framework.components.batchjobs.helpers;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.mifos.application.branchreport.BranchReportBO;
import org.mifos.application.customer.business.service.CustomerBusinessService;
import org.mifos.application.office.business.OfficeBO;
import org.mifos.application.office.business.service.OfficeBusinessService;
import org.mifos.application.reports.business.service.BranchReportConfigService;
import org.mifos.application.reports.business.service.BranchReportService;
import org.mifos.application.reports.business.service.IBranchReportService;
import org.mifos.application.reports.business.service.ReportServiceFactory;
import org.mifos.framework.components.batchjobs.MifosTask;
import org.mifos.framework.components.batchjobs.TaskHelper;
import org.mifos.framework.components.batchjobs.exceptions.BatchJobException;
import org.mifos.framework.components.logger.LoggerConstants;
import org.mifos.framework.components.logger.MifosLogManager;
import org.mifos.framework.components.logger.MifosLogger;
import org.mifos.framework.exceptions.ServiceException;
import org.mifos.framework.hibernate.helper.StaticHibernateUtil;
import org.mifos.framework.util.DateTimeService;

public class BranchReportHelper extends TaskHelper {

	private CustomerBusinessService customerBusinessService;
	private OfficeBusinessService officeBusinessService;
	private IBranchReportService branchReportService;
	private BranchReportConfigService branchReportConfigService;
	

	public BranchReportHelper(MifosTask mifosTask) {
		super(mifosTask);
		customerBusinessService = new CustomerBusinessService();
		officeBusinessService = new OfficeBusinessService();
		branchReportService = new BranchReportService();
		branchReportConfigService = ReportServiceFactory
				.getBranchReportConfigService();
	}

	@Override
	public void execute(long timeInMillis) throws BatchJobException {
		long time1 = new DateTimeService().getCurrentDateTime().getMillis();
		Session session = StaticHibernateUtil.getSessionTL();
		Transaction transaction = session.beginTransaction();
		Date runDate = new Date(timeInMillis);
		
		try {
			removeExistingBranchReportsForGivenRunDate(runDate);
			populateBranchReportBatch(session, runDate);
			transaction.commit();
		}
		catch (HibernateException e) {
			transaction.rollback();
			throw new BatchJobException(e);
		}
		catch (ServiceException e) {
			throw new BatchJobException(e);
		}
		long time2 = new DateTimeService().getCurrentDateTime().getMillis();
		long duration = time2 - time1;
	}

	void populateBranchReportBatch(Session session, Date runDate) throws BatchJobException,
			ServiceException {
		List<OfficeBO> branchOffices = officeBusinessService.getBranchOffices();
		if (branchOffices == null)
			return;
		for (OfficeBO branchOffice : branchOffices) {
			long time1 = new DateTimeService().getCurrentDateTime().getMillis();
			createBranchReport(session, branchOffice, runDate);
		}
	}

	BranchReportBO createBranchReport(Session session, OfficeBO branchOffice,
			Date runDate) throws BatchJobException {
		BranchReportBO branchReport = new BranchReportBO(branchOffice
				.getOfficeId(), runDate);<commit_msg>Remove dead code apparently used for timing.<commit_after>/*
 * Copyright (c) 2005-2009 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */
 
package org.mifos.framework.components.batchjobs.helpers;

import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.mifos.application.branchreport.BranchReportBO;
import org.mifos.application.customer.business.service.CustomerBusinessService;
import org.mifos.application.office.business.OfficeBO;
import org.mifos.application.office.business.service.OfficeBusinessService;
import org.mifos.application.reports.business.service.BranchReportConfigService;
import org.mifos.application.reports.business.service.BranchReportService;
import org.mifos.application.reports.business.service.IBranchReportService;
import org.mifos.application.reports.business.service.ReportServiceFactory;
import org.mifos.framework.components.batchjobs.MifosTask;
import org.mifos.framework.components.batchjobs.TaskHelper;
import org.mifos.framework.components.batchjobs.exceptions.BatchJobException;
import org.mifos.framework.exceptions.ServiceException;
import org.mifos.framework.hibernate.helper.StaticHibernateUtil;

public class BranchReportHelper extends TaskHelper {

	private CustomerBusinessService customerBusinessService;
	private OfficeBusinessService officeBusinessService;
	private IBranchReportService branchReportService;
	private BranchReportConfigService branchReportConfigService;
	

	public BranchReportHelper(MifosTask mifosTask) {
		super(mifosTask);
		customerBusinessService = new CustomerBusinessService();
		officeBusinessService = new OfficeBusinessService();
		branchReportService = new BranchReportService();
		branchReportConfigService = ReportServiceFactory
				.getBranchReportConfigService();
	}

	@Override
	public void execute(long timeInMillis) throws BatchJobException {
		Session session = StaticHibernateUtil.getSessionTL();
		Transaction transaction = session.beginTransaction();
		Date runDate = new Date(timeInMillis);
		
		try {
			removeExistingBranchReportsForGivenRunDate(runDate);
			populateBranchReportBatch(session, runDate);
			transaction.commit();
		}
		catch (HibernateException e) {
			transaction.rollback();
			throw new BatchJobException(e);
		}
		catch (ServiceException e) {
			throw new BatchJobException(e);
		}
	}

	void populateBranchReportBatch(Session session, Date runDate) throws BatchJobException,
			ServiceException {
		List<OfficeBO> branchOffices = officeBusinessService.getBranchOffices();
		if (branchOffices == null)
			return;
		for (OfficeBO branchOffice : branchOffices) {
			createBranchReport(session, branchOffice, runDate);
		}
	}

	BranchReportBO createBranchReport(Session session, OfficeBO branchOffice,
			Date runDate) throws BatchJobException {
		BranchReportBO branchReport = new BranchReportBO(branchOffice
				.getOfficeId(), runDate);
{'fraction_non_alphanumeric': 0.08176752624076714, 'fraction_numerical': 0.0042762731631463, 'mean_word_length': 5.794014084507042, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '987833', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2082, 'n_words': 519}