package org.kuali.kfs.pdp.batch.service.impl;

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

import org.kuali.kfs.krad.util.GlobalVariables;
import org.kuali.kfs.pdp.PdpConstants;
import org.kuali.kfs.pdp.batch.service.AutoCheckFormatService;
import org.kuali.kfs.pdp.businessobject.AutoCheckFormat;
import org.kuali.kfs.pdp.businessobject.CustomerProfile;
import org.kuali.kfs.pdp.businessobject.FormatProcessSummary;
import org.kuali.kfs.pdp.businessobject.FormatSelection;
import org.kuali.kfs.pdp.service.FormatService;
import org.kuali.kfs.pdp.service.impl.exception.FormatException;
import org.kuali.rice.core.api.datetime.DateTimeService;
import org.kuali.rice.core.api.util.type.KualiInteger;

public class AutoCheckFormatServiceImpl implements AutoCheckFormatService {
	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AutoCheckFormatServiceImpl.class);
	private FormatService formatService;
	private DateTimeService dateTimeService;
	
	@Override
	public boolean processChecks() {
		
		AutoCheckFormat autoFormat = new AutoCheckFormat();
		
		//Prepare check data
        FormatSelection formatSelection = formatService.getDataForFormat(GlobalVariables.getUserSession().getPerson());
        
        autoFormat.setCampus("IR");
        
        if (formatSelection.getStartDate() != null) {
        	LOG.error("ERROR AutoCheckFormatStep: The format process is already running. It began at: " + getDateTimeService().toDateTimeString(formatSelection.getStartDate()));
            return false;
        }

        List<CustomerProfile> customers = formatSelection.getCustomerList();

        for (CustomerProfile element : customers) {

            if (formatSelection.getCampus().equals(element.getDefaultPhysicalCampusProcessingCode())) {
                element.setSelectedForFormat(Boolean.TRUE);
            }
            else {
                element.setSelectedForFormat(Boolean.FALSE);
            }
        }

        autoFormat.setPaymentDate(getDateTimeService().toDateString(getDateTimeService().getCurrentTimestamp()));
        autoFormat.setPaymentTypes(PdpConstants.PaymentTypes.ALL);
        autoFormat.setCustomers(customers);
        autoFormat.setRanges(formatSelection.getRangeList());
        
        // Mark payments for format
		try {
			Date paymentDate = getDateTimeService().convertToSqlDate(autoFormat.getPaymentDate());
			FormatProcessSummary formatProcessSummary = formatService.startFormatProcess(GlobalVariables.getUserSession().getPerson(), autoFormat.getCampus(), autoFormat.getCustomers(), paymentDate, autoFormat.getPaymentTypes());
			
			if (formatProcessSummary.getProcessSummaryList().size() == 0) {
				LOG.error("Warning AutoCheckFormatStep: There are no payments that match your selection for format process.");
				return true;
			}
			
			autoFormat.setFormatProcessSummary(formatProcessSummary);
			
		} catch (Exception e) {
			LOG.error("ERROR AutoCheckFormatStep: " + e.getMessage(), e);
			return false;
		}

		// Format checks
		KualiInteger processId = autoFormat.getFormatProcessSummary().getProcessId();

        try {
            formatService.performFormat(processId.intValue());
        }
        catch (FormatException e) {
        	LOG.error("ERROR AutoCheckFormatStep: " + e.getMessage(), e);
        	return false;
        }
		
        // You have reached the end. success!
		return true;
	}

	/**
	 * @return the dateTimeService
	 */
	public DateTimeService getDateTimeService() {
		return dateTimeService;
	}

	/**
	 * @param dateTimeService the dateTimeService to set
	 */
	public void setDateTimeService(DateTimeService dateTimeService) {
		this.dateTimeService = dateTimeService;
	}

}

@@ -1578,16 +1578,1016 @@
 tomers =
+ markPaymentsForFormat(formatSelection);%0A%0A        autoFormat.setPaymentDate(getDateTimeService().toDateString(getDateTimeService().getCurrentTimestamp()));%0A        autoFormat.setPaymentTypes(PdpConstants.PaymentTypes.ALL);%0A        autoFormat.setCustomers(customers);%0A        autoFormat.setRanges(formatSelection.getRangeList());%0A        %0A        // Mark payments for format%0A%09%09generateFormatProcesSummaryForPayments(autoFormat);%0A%0A%09%09// Format checks%0A%09%09KualiInteger processId = autoFormat.getFormatProcessSummary().getProcessId();%0A%0A        try %7B%0A            formatService.performFormat(processId.intValue());%0A        %7D%0A        catch (FormatException e) %7B%0A        %09LOG.error(%22ERROR AutoCheckFormatStep: %22 + e.getMessage(), e);%0A        %09return false;%0A        %7D%0A%09%09%0A        // You have reached the end. success!%0A%09%09return true;%0A%09%7D%0A%0A%09/**%0A%09 * @param formatSelection%0A%09 * @return%0A%09 */%0A%09private List%3CCustomerProfile%3E markPaymentsForFormat(%0A%09%09%09FormatSelection formatSelection) %7B%0A%09%09List%3CCustomerProfile%3E customers =
  formatS
@@ -2954,340 +2954,150 @@
   %7D%0A
-%0A        autoFormat.setPaymentDate(getDateTimeService().toDateString(getDateTimeService().getCurrentTimestamp()));%0A        autoFormat.setPaymentTypes(PdpConstants.PaymentTypes.ALL);%0A        autoFormat.setCustomers(customers);%0A        autoFormat.setRanges(formatSelection.getRangeList());%0A        %0A        // Mark payments for f
+%09%09return customers;%0A%09%7D%0A%0A%09/**%0A%09 * @param autoFormat%0A%09 */%0A%09private void generateFormatProcesSummaryForPayments(%0A%09%09%09AutoCheckFormat autoF
 ormat
+) %7B
 %0A%09%09t
@@ -3608,21 +3608,16 @@
 %09%09return
- true
 ;%0A%09%09%09%7D%0A%09
@@ -3788,411 +3788,13 @@
 turn
- false;%0A%09%09%7D%0A%0A%09%09// Format checks%0A%09%09KualiInteger processId = autoFormat.getFormatProcessSummary().getProcessId();%0A%0A        try %7B%0A            formatService.performFormat(processId.intValue());%0A        %7D%0A        catch (FormatException e) %7B%0A        %09LOG.error(%22ERROR AutoCheckFormatStep: %22 + e.getMessage(), e);%0A        %09return false;%0A        %7D%0A%09%09%0A        // You have reached the end. success!%0A%09%09return true;
+;%0A%09%09%7D
 %0A%09%7D%0A

clean up the code into smaller testable units