/*
 * Copyright 2011 The Kuali Foundation.
 *
 * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
 *
 * 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.
 */
package org.kuali.kfs.module.ar.web.struts;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.kuali.kfs.module.ar.ArConstants;
import org.kuali.kfs.module.ar.ArPropertyConstants;
import org.kuali.kfs.sys.KFSConstants;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.rice.core.api.datetime.DateTimeService;


/**
 * Form class for Contracts Grants Invoice Report Lookup screen.
 */
public class ContractsGrantsInvoiceDocumentErrorLogReportForm extends ContractsGrantsReportLookupForm {

    /**
     * Constructor.
     */
    public ContractsGrantsInvoiceDocumentErrorLogReportForm() {
        setHtmlFormAction("contractsGrantsInvoiceDocumentErrorLogReport");
    }

    @Override
    public Map<String, String> getFieldsForLookup() {
        Map<String, String> fieldValues = super.getFieldsForLookup();

        // Add wildcard character to start and end of accounts field so users can search for single account
        // within the delimited list of accounts without having to add the wildcards explicitly themselves.
        String accounts = fieldValues.get(ArPropertyConstants.ContractsGrantsInvoiceDocumentErrorLogLookupFields.ACCOUNTS);
        if (StringUtils.isNotBlank(accounts)) {
            // only add wildcards if they haven't already been added (for some reason this method gets called twice when generating the pdf report)
            if (!StringUtils.startsWith(accounts, KFSConstants.WILDCARD_CHARACTER)) {
                accounts = KFSConstants.WILDCARD_CHARACTER + accounts;
            }
            if (!StringUtils.endsWith(accounts, KFSConstants.WILDCARD_CHARACTER)) {
                accounts += KFSConstants.WILDCARD_CHARACTER;
            }
        }
        fieldValues.put(ArPropertyConstants.ContractsGrantsInvoiceDocumentErrorLogLookupFields.ACCOUNTS, accounts);

        // Add time component to error date since it's stored as both a date and time in the database and without this
        // records with an error date of the date being searched for won't show up in the results
        // (ex: 7/29/2014 09:35 AM is not <= 7/29/2014, but it is <= 7/29/2014 23:59:59)
        String errorDate = fieldValues.get(ArPropertyConstants.ContractsGrantsInvoiceDocumentErrorLogLookupFields.ERROR_DATE);
        if (StringUtils.isNotBlank(errorDate)) {
            Date endOfDay = DateUtils.addMilliseconds(DateUtils.ceiling(SpringContext.getBean(DateTimeService.class).getCurrentDate(), Calendar.DATE), -1);
            String endOfDayTime = KFSConstants.BLANK_SPACE + SpringContext.getBean(DateTimeService.class).toString(endOfDay, ArConstants.REPORT_TIME_FORMAT);
            // only add time string if it hasn't already been added (for some reason this method gets called twice when generating the pdf report)
            if (!StringUtils.contains(errorDate, endOfDayTime)) {
                errorDate += endOfDayTime;
            }
        }

        fieldValues.put(ArPropertyConstants.ContractsGrantsInvoiceDocumentErrorLogLookupFields.ERROR_DATE, errorDate);

        return fieldValues;
    }

}

@@ -659,58 +659,8 @@
 s;%0A%0A
-import java.util.Calendar;%0Aimport java.util.Date;%0A
 impo
@@ -726,55 +726,8 @@
 ls;%0A
-import org.apache.commons.lang.time.DateUtils;%0A
 impo
@@ -755,16 +755,24 @@
 le.ar.Ar
+Property
 Constant
@@ -805,35 +805,65 @@
 dule.ar.
-ArPropertyConstants
+report.service.ContractsGrantsReportHelperService
 ;%0Aimport
@@ -947,65 +947,8 @@
 xt;%0A
-import org.kuali.rice.core.api.datetime.DateTimeService;%0A
 %0A%0A/*
@@ -1121,16 +1121,115 @@
 upForm %7B
+%0A    private static volatile ContractsGrantsReportHelperService contractsGrantsReportHelperService;
 %0A%0A    /*
@@ -2981,213 +2981,101 @@
     
-if (StringUtils.isNotBlank(errorDate)) %7B%0A            Date endOfDay = DateUtils.addMilliseconds(DateUtils.ceiling(SpringContext.getBean(DateTimeService.class).getCurrentDate(), Calendar.DATE), -1
+errorDate = getContractsGrantsReportHelperService().appendEndTimeToDate(errorDate
 );%0A
+%0A
         
     
@@ -3074,592 +3074,481 @@
     
-    String endOfDayTime = KFSConstants.BLANK_SPACE + SpringContext.getBean(DateTimeService.class).toString(endOfDay, ArConstants.REPORT_TIME_FORMAT);%0A            // only add time string if it hasn't already been added (for some reason this method gets called twice when generating the pdf report)%0A            if (!StringUtils.contains(errorDate, endOfDayTime)) %7B%0A                errorDate += endOfDayTime;%0A            %7D%0A        %7D%0A%0A        fieldValues.put(ArPropertyConstants.ContractsGrantsInvoiceDocumentErrorLogLookupFields.ERROR_DATE, errorDate);%0A%0A        return fieldValues
+fieldValues.put(ArPropertyConstants.ContractsGrantsInvoiceDocumentErrorLogLookupFields.ERROR_DATE, errorDate);%0A%0A        return fieldValues;%0A    %7D%0A%0A    public ContractsGrantsReportHelperService getContractsGrantsReportHelperService() %7B%0A        if (contractsGrantsReportHelperService == null) %7B%0A            contractsGrantsReportHelperService = SpringContext.getBean(ContractsGrantsReportHelperService.class);%0A        %7D%0A        return contractsGrantsReportHelperService
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -3544,11 +3544,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

convert error log form to use the end time criteria logic stolen from it