Example usage for java.sql Date equals

List of usage examples for java.sql Date equals

Introduction

In this page you can find the example usage for java.sql Date equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Compares two dates for equality.

Usage

From source file:org.kuali.kfs.coa.document.validation.impl.AccountGlobalRule.java

/**
 * This method checks to see if any expiration date field rules were violated Loops through each detail object and calls
 * {@link AccountGlobalRule#checkExpirationDate(MaintenanceDocument, AccountGlobalDetail)}
 *
 * @param maintenanceDocument/*from  w  w  w  .j a va  2 s . c  o m*/
 * @return false on rules violation
 */
protected boolean checkExpirationDate(MaintenanceDocument maintenanceDocument) {
    LOG.info("checkExpirationDate called");

    boolean success = true;
    Date newExpDate = newAccountGlobal.getAccountExpirationDate();

    // If creating a new account if acct_expiration_dt is set then
    // the acct_expiration_dt must be changed to a date that is today or later
    // unless the date was valid upon submission, this is an approval action
    // and the approver hasn't changed the value
    if (maintenanceDocument.isNew() && ObjectUtils.isNotNull(newExpDate)) {
        Date oldExpDate = null;

        if (maintenanceDocument.getDocumentHeader().getWorkflowDocument().isApprovalRequested()) {
            try {
                MaintenanceDocument oldMaintDoc = (MaintenanceDocument) SpringContext
                        .getBean(DocumentService.class)
                        .getByDocumentHeaderId(maintenanceDocument.getDocumentNumber());
                AccountGlobal oldAccountGlobal = (AccountGlobal) oldMaintDoc.getDocumentBusinessObject();
                if (ObjectUtils.isNotNull(oldAccountGlobal)) {
                    oldExpDate = oldAccountGlobal.getAccountExpirationDate();
                }
            } catch (WorkflowException ex) {
                LOG.warn("Error retrieving maintenance doc for doc #" + maintenanceDocument.getDocumentNumber()
                        + ". This shouldn't happen.", ex);
            }
        }

        if (ObjectUtils.isNull(oldExpDate) || !oldExpDate.equals(newExpDate)) {
            if (!newExpDate.after(today) && !newExpDate.equals(today)) {
                putFieldError("accountExpirationDate",
                        KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
                success &= false;
            }
        }
    }

    // a continuation account is required if the expiration date is completed.
    success &= checkContinuationAccount(maintenanceDocument, newExpDate);

    for (AccountGlobalDetail detail : newAccountGlobal.getAccountGlobalDetails()) {
        success &= checkExpirationDate(maintenanceDocument, detail);
    }
    return success;
}

From source file:com.alibaba.wasp.jdbc.TestJdbcResultSet.java

@Test
public void testDatetime() throws SQLException {
    trace("test DATETIME");
    ResultSet rs;/*from  w w  w  .j  a  v a2 s. c o m*/
    Object o;

    // rs = stat.executeQuery("call date '99999-12-23'");
    // rs.next();
    // assertEquals("99999-12-23", rs.getString(1));
    // rs = stat.executeQuery("call timestamp '99999-12-23 01:02:03.000'");
    // rs.next();
    // assertEquals("99999-12-23 01:02:03.0", rs.getString(1));
    // rs = stat.executeQuery("call date '-99999-12-23'");
    // rs.next();
    // assertEquals("-99999-12-23", rs.getString(1));
    // rs = stat.executeQuery("call timestamp '-99999-12-23 01:02:03.000'");
    // rs.next();
    // assertEquals("-99999-12-23 01:02:03.0", rs.getString(1));

    stat = conn.createStatement();
    // stat.execute("CREATE TABLE test(ID INT PRIMARY KEY,VALUE DATETIME)");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (1,'2011-11-11 0:0:0', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (2,'2002-02-02 02:02:02', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (3,'1800-01-01 0:0:0', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (4,'9999-12-31 23:59:59', 13, 'testDatetime')");
    stat.execute(
            "INSERT INTO test (column1,column6,column2,column3) VALUES (5,'9999-12-31 23:59:59', 13, 'testDatetime')");
    // stat.execute("INSERT INTO test (column1,column6,column2,column3) VALUES(5,NULL)");
    rs = stat.executeQuery("SELECT column1,column6 FROM test where column3='testDatetime' ORDER BY column1");
    // assertResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] {
    // Types.INTEGER, Types.TIMESTAMP }, new int[] { 10, 23 }, new int[] { 0,
    // 10 });
    // rs = stat.executeQuery("SELECT * FROM test ORDER BY ID");
    // assertResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] {
    // Types.INTEGER, Types.TIMESTAMP }, new int[] { 10, 23 }, new int[] { 0,
    // 10 });
    rs.next();
    java.sql.Date date;
    java.sql.Time time;
    Timestamp ts;
    date = rs.getDate(2);
    assertTrue(!rs.wasNull());
    time = rs.getTime(2);
    assertTrue(!rs.wasNull());
    ts = rs.getTimestamp(2);
    assertTrue(!rs.wasNull());
    trace("Date: " + date.toString() + " Time:" + time.toString() + " Timestamp:" + ts.toString());
    trace("Date ms: " + date.getTime() + " Time ms:" + time.getTime() + " Timestamp ms:" + ts.getTime());
    trace("1970 ms: " + Timestamp.valueOf("1970-01-01 00:00:00.0").getTime());
    assertEquals(Timestamp.valueOf("2011-11-11 00:00:00.0").getTime(), date.getTime());
    assertEquals(Timestamp.valueOf("1970-01-01 00:00:00.0").getTime(), time.getTime());
    assertEquals(Timestamp.valueOf("2011-11-11 00:00:00.0").getTime(), ts.getTime());
    assertTrue(date.equals(java.sql.Date.valueOf("2011-11-11")));
    assertTrue(time.equals(java.sql.Time.valueOf("00:00:00")));
    assertTrue(ts.equals(Timestamp.valueOf("2011-11-11 00:00:00.0")));
    assertFalse(rs.wasNull());
    o = rs.getObject(2);
    trace(o.getClass().getName());
    assertTrue(o instanceof Timestamp);
    assertTrue(((Timestamp) o).equals(Timestamp.valueOf("2011-11-11 00:00:00")));
    assertFalse(rs.wasNull());
    rs.next();
    date = rs.getDate("COLUMN6");
    assertTrue(!rs.wasNull());
    time = rs.getTime("COLUMN6");
    assertTrue(!rs.wasNull());
    ts = rs.getTimestamp("COLUMN6");
    assertTrue(!rs.wasNull());
    trace("Date: " + date.toString() + " Time:" + time.toString() + " Timestamp:" + ts.toString());
    assertEquals("2002-02-02", date.toString());
    assertEquals("02:02:02", time.toString());
    assertEquals("2002-02-02 02:02:02.0", ts.toString());
    rs.next();
    assertEquals("1800-01-01", rs.getDate("column6").toString());
    assertEquals("00:00:00", rs.getTime("column6").toString());
    assertEquals("1800-01-01 00:00:00.0", rs.getTimestamp("column6").toString());
    rs.next();
    assertEquals("9999-12-31", rs.getDate("Column6").toString());
    assertEquals("23:59:59", rs.getTime("Column6").toString());
    assertEquals("9999-12-31 23:59:59.0", rs.getTimestamp("Column6").toString());
    // assertTrue(!rs.next());
}

From source file:org.kuali.ole.coa.document.validation.impl.AccountGlobalRule.java

/**
 * This method checks to see if any expiration date field rules were violated in relation to the given detail record
 * //from  w  w  w  .ja  v  a2  s  . c  o  m
 * @param maintenanceDocument
 * @param detail - the account detail we are investigating
 * @return false on rules violation
 */
protected boolean checkExpirationDate(MaintenanceDocument maintenanceDocument, AccountGlobalDetail detail) {
    boolean success = true;
    Date newExpDate = newAccountGlobal.getAccountExpirationDate();

    // load the object by keys
    Account account = (Account) SpringContext.getBean(BusinessObjectService.class)
            .findByPrimaryKey(Account.class, detail.getPrimaryKeys());
    if (ObjectUtils.isNotNull(account)) {
        Date oldExpDate = account.getAccountExpirationDate();

        // When updating an account expiration date, the date must be today or later
        // (except for C&G accounts). Only run this test if this maint doc
        // is an edit doc
        if (isUpdatedExpirationDateInvalid(account, newAccountGlobal)) {
            putFieldError("accountExpirationDate",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
            success &= false;
        }

        // If creating a new account if acct_expiration_dt is set and the fund_group is not "CG" then
        // the acct_expiration_dt must be changed to a date that is today or later
        if (ObjectUtils.isNotNull(newExpDate) && ObjectUtils.isNull(newAccountGlobal.getSubFundGroup())) {
            if (ObjectUtils.isNotNull(account.getSubFundGroup())) {
                if (!account.isForContractsAndGrants()) {
                    if (!newExpDate.after(today) && !newExpDate.equals(today)) {
                        putGlobalError(OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
                        success &= false;
                    }
                }
            }
        }
        // acct_expiration_dt can not be before acct_effect_dt
        Date effectiveDate = account.getAccountEffectiveDate();
        if (ObjectUtils.isNotNull(effectiveDate) && ObjectUtils.isNotNull(newExpDate)) {
            if (newExpDate.before(effectiveDate)) {
                putGlobalError(
                        OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_CANNOT_BE_BEFORE_EFFECTIVE_DATE);
                success &= false;
            }
        }
    }

    return success;
}

From source file:org.kuali.ole.coa.document.validation.impl.AccountRule.java

/**
 * This method checks to see if the new expiration date is different from the old expiration and if it has if it is invalid
 *
 * @param maintDoc//from   w  w w  . j a  v  a  2s  . c o  m
 * @return true if expiration date has changed and is invalid
 */
protected boolean isUpdatedExpirationDateInvalid(MaintenanceDocument maintDoc) {

    // if this isn't an Edit document, we're not interested
    if (!maintDoc.isEdit()) {
        return false;
    }

    Date oldExpDate = oldAccount.getAccountExpirationDate();
    Date newExpDate = newAccount.getAccountExpirationDate();
    Date today = new Date(getDateTimeService().getCurrentDate().getTime());
    today.setTime(DateUtils.truncate(today, Calendar.DAY_OF_MONTH).getTime()); // remove any time components

    // When updating an account expiration date, the date must be today or later
    // Only run this test if this maintenance doc
    // is an edit doc
    boolean expDateHasChanged = false;

    // if the old version of the account had no expiration date, and the new
    // one has a date
    if (ObjectUtils.isNull(oldExpDate) && ObjectUtils.isNotNull(newExpDate)) {
        expDateHasChanged = true;
    }

    // if there was an old and a new expDate, but they're different
    else if (ObjectUtils.isNotNull(oldExpDate) && ObjectUtils.isNotNull(newExpDate)) {
        if (!oldExpDate.equals(newExpDate)) {
            expDateHasChanged = true;
        }
    }

    // if the expiration date hasn't changed, we're not interested
    if (!expDateHasChanged) {
        return false;
    }

    // make a shortcut to the newAccount
    Account newAccount = (Account) maintDoc.getNewMaintainableObject().getBusinessObject();

    // expirationDate must be today or later than today (cannot be before today)
    if (newExpDate.equals(today) || newExpDate.after(today)) {
        return false;
    } else {
        return true;
    }
}

From source file:org.kuali.kra.proposaldevelopment.hierarchy.service.impl.ProposalHierarchyServiceImpl.java

protected int getCorrespondingParentPeriod(Budget parentBudget, Budget childBudget) {
    int correspondingStart = -1;

    // using start date of first period as start date and end date of last period
    // as end because budget start and end are not particularly reliable
    Date childStart = childBudget.getBudgetPeriod(0).getStartDate();
    Date parentStart = parentBudget.getBudgetPeriod(0).getStartDate();
    Date parentEnd = parentBudget.getBudgetPeriod(parentBudget.getBudgetPeriods().size() - 1).getEndDate();
    // check that child budget starts somewhere during parent budget
    if (childStart.compareTo(parentStart) >= 0 && childStart.compareTo(parentEnd) < 0) {
        // check that child budget starts on one of the budget period starts
        List<BudgetPeriod> parentPeriods = parentBudget.getBudgetPeriods();
        for (int i = 0; i < parentPeriods.size(); i++) {
            if (childStart.equals(parentPeriods.get(i).getStartDate())) {
                correspondingStart = i;//www .  j av  a2  s.  co  m
                break;
            }
        }
    }
    return correspondingStart;
}

From source file:org.kuali.kfs.coa.document.validation.impl.AccountGlobalRule.java

/**
 * This method checks to see if any expiration date field rules were violated in relation to the given detail record
 *
 * @param maintenanceDocument//from  w w  w. j  a  v a2  s  .  c  o m
 * @param detail - the account detail we are investigating
 * @return false on rules violation
 */
protected boolean checkExpirationDate(MaintenanceDocument maintenanceDocument, AccountGlobalDetail detail) {
    boolean success = true;
    Date newExpDate = newAccountGlobal.getAccountExpirationDate();

    Date prevExpDate = null;

    // get previous expiration date for possible check later
    if (maintenanceDocument.getDocumentHeader().getWorkflowDocument().isApprovalRequested()) {
        try {
            MaintenanceDocument oldMaintDoc = (MaintenanceDocument) SpringContext.getBean(DocumentService.class)
                    .getByDocumentHeaderId(maintenanceDocument.getDocumentNumber());
            AccountGlobal oldAccountGlobal = (AccountGlobal) oldMaintDoc.getDocumentBusinessObject();
            if (ObjectUtils.isNotNull(oldAccountGlobal)) {
                prevExpDate = oldAccountGlobal.getAccountExpirationDate();
            }
        } catch (WorkflowException ex) {
            LOG.warn("Error retrieving maintenance doc for doc #" + maintenanceDocument.getDocumentNumber()
                    + ". This shouldn't happen.", ex);
        }
    }

    // load the object by keys
    Account account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Account.class,
            detail.getPrimaryKeys());
    if (ObjectUtils.isNotNull(account)) {
        Date oldExpDate = account.getAccountExpirationDate();

        // When updating an account expiration date, the date must be today or later
        // (except for C&G accounts). Only run this test if this maint doc
        // is an edit doc
        if (isUpdatedExpirationDateInvalid(account, newAccountGlobal)) {
            // if the date was valid upon submission, and this is an approval,
            // we're not interested unless the approver changed the value
            if (ObjectUtils.isNull(prevExpDate) || !prevExpDate.equals(newExpDate)) {
                putFieldError("accountExpirationDate",
                        KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
                success &= false;
            }
        }

        // If creating a new account if acct_expiration_dt is set and the fund_group is not "CG" then
        // the acct_expiration_dt must be changed to a date that is today or later
        // unless the date was valid upon submission, this is an approval action
        // and the approver hasn't changed the value
        if (maintenanceDocument.isNew() && ObjectUtils.isNotNull(newExpDate)) {
            if (ObjectUtils.isNull(prevExpDate) || !prevExpDate.equals(newExpDate)) {
                if (ObjectUtils.isNotNull(newExpDate)
                        && ObjectUtils.isNull(newAccountGlobal.getSubFundGroup())) {
                    if (ObjectUtils.isNotNull(account.getSubFundGroup())) {
                        if (!account.isForContractsAndGrants()) {
                            if (!newExpDate.after(today) && !newExpDate.equals(today)) {
                                putGlobalError(KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
                                success &= false;
                            }
                        }
                    }
                }
            }
        }

        // acct_expiration_dt can not be before acct_effect_dt
        Date effectiveDate = account.getAccountEffectiveDate();
        if (ObjectUtils.isNotNull(effectiveDate) && ObjectUtils.isNotNull(newExpDate)) {
            if (newExpDate.before(effectiveDate)) {
                putGlobalError(
                        KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_CANNOT_BE_BEFORE_EFFECTIVE_DATE);
                success &= false;
            }
        }
    }

    return success;
}

From source file:org.kuali.kfs.coa.document.validation.impl.AccountRule.java

/**
 * This method checks to see if the new expiration date is different from the old expiration and if it has if it is invalid
 *
 * @param maintDoc//from ww w  .j a v  a2 s .  com
 * @return true if expiration date has changed and is invalid
 */
protected boolean isUpdatedExpirationDateInvalid(MaintenanceDocument maintDoc) {

    // if this isn't an Edit document, we're not interested
    if (!maintDoc.isEdit()) {
        return false;
    }

    Date oldExpDate = oldAccount.getAccountExpirationDate();
    Date newExpDate = newAccount.getAccountExpirationDate();
    Date today = new Date(getDateTimeService().getCurrentDate().getTime());
    today.setTime(DateUtils.truncate(today, Calendar.DAY_OF_MONTH).getTime()); // remove any time components

    // if the date was valid upon submission, and this is an approval,
    // we're not interested unless the approver changed the value
    if (maintDoc.getDocumentHeader().getWorkflowDocument().isApprovalRequested()) {
        try {
            MaintenanceDocument oldMaintDoc = (MaintenanceDocument) SpringContext.getBean(DocumentService.class)
                    .getByDocumentHeaderId(maintDoc.getDocumentNumber());
            Account oldAccount = (Account) oldMaintDoc.getDocumentBusinessObject();

            if (ObjectUtils.isNotNull(oldAccount.getAccountExpirationDate())
                    && oldAccount.getAccountExpirationDate().equals(newExpDate)) {
                return false;
            }
        } catch (WorkflowException ex) {
            LOG.warn("Error retrieving maintenance doc for doc #" + maintDoc.getDocumentNumber()
                    + ". This shouldn't happen.", ex);
        }
    }

    // When updating an account expiration date, the date must be today or later
    // Only run this test if this maintenance doc
    // is an edit doc
    boolean expDateHasChanged = false;

    // if the old version of the account had no expiration date, and the new
    // one has a date
    if (ObjectUtils.isNull(oldExpDate) && ObjectUtils.isNotNull(newExpDate)) {
        expDateHasChanged = true;
    }

    // if there was an old and a new expDate, but they're different
    else if (ObjectUtils.isNotNull(oldExpDate) && ObjectUtils.isNotNull(newExpDate)) {
        if (!oldExpDate.equals(newExpDate)) {
            expDateHasChanged = true;
        }
    }

    // if the expiration date hasn't changed, we're not interested
    if (!expDateHasChanged) {
        return false;
    }

    // make a shortcut to the newAccount
    Account newAccount = (Account) maintDoc.getNewMaintainableObject().getBusinessObject();

    /* expirationDate must be today or later than today (cannot be before today).
     * This rule doesn't apply to certain fund groups (C&G perhaps) according to
     * the parameter.
     */
    Collection<String> fundGroups = SpringContext.getBean(ParameterService.class).getParameterValuesAsString(
            Account.class, KFSConstants.ChartApcParms.EXPIRATION_DATE_BACKDATING_FUND_GROUPS);
    if (fundGroups != null && ObjectUtils.isNotNull(newAccount.getSubFundGroup())
            && fundGroups.contains(newAccount.getSubFundGroup().getFundGroupCode())) {
        return false;
    }
    return newExpDate.before(today);
}

From source file:org.kuali.ole.coa.document.validation.impl.AccountRule.java

/**
 * This method checks to see if any expiration date field rules were violated
 *
 * @param maintenanceDocument//from   ww w  . java 2  s  .co  m
 * @return false on rules violation
 */
protected boolean checkExpirationDate(MaintenanceDocument maintenanceDocument) {

    LOG.debug("checkExpirationDate called");

    boolean success = true;

    Date oldExpDate = oldAccount.getAccountExpirationDate();
    Date newExpDate = newAccount.getAccountExpirationDate();
    Date today = new Date(getDateTimeService().getCurrentTimestamp().getTime());
    today.setTime(DateUtils.truncate(today, Calendar.DAY_OF_MONTH).getTime()); // remove any time components

    // When updating an account expiration date, the date must be today or later
    // Only run this test if this maintenance doc
    // is an edit doc

    //MSU Contribution OLEMI-8567 DTT-565 OLECNTRB-972
    if (isUpdatedExpirationDateInvalid(maintenanceDocument)) {
        Account newAccount = (Account) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
        if (newAccount.isClosed()) {
            /*If the Account is being closed and the date is before today's date, the EXP date can only be today*/
            putFieldError("accountExpirationDate",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_ACCT_CANNOT_BE_CLOSED_EXP_DATE_INVALID);
        } else {
            /*If the Account is not being closed and the date is before today's date, the EXP date can only be today or at a later date*/
            putFieldError("accountExpirationDate",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
        }
        success &= false;
    }

    // a continuation account is required if the expiration date is completed.
    if (ObjectUtils.isNotNull(newExpDate)) {
        if (StringUtils.isBlank(newAccount.getContinuationAccountNumber())) {
            putFieldError("continuationAccountNumber",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_CONTINUATION_ACCT_REQD_IF_EXP_DATE_COMPLETED);
        }
        if (StringUtils.isBlank(newAccount.getContinuationFinChrtOfAcctCd())) {
            putFieldError("continuationFinChrtOfAcctCd",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_CONTINUATION_FINCODE_REQD_IF_EXP_DATE_COMPLETED);
            // putGlobalError(OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_CONTINUATION_ACCT_REQD_IF_EXP_DATE_COMPLETED);
            success &= false;
        }
    }

    // If creating a new account if acct_expiration_dt is set then
    // the acct_expiration_dt must be changed to a date that is today or later
    if (maintenanceDocument.isNew() && ObjectUtils.isNotNull(newExpDate)) {
        if (!newExpDate.after(today) && !newExpDate.equals(today)) {
            putFieldError("accountExpirationDate",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
            // putGlobalError(OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
            success &= false;
        }
    }

    // acct_expiration_dt can not be before acct_effect_dt
    Date effectiveDate = newAccount.getAccountEffectiveDate();
    if (ObjectUtils.isNotNull(effectiveDate) && ObjectUtils.isNotNull(newExpDate)) {
        if (newExpDate.before(effectiveDate)) {
            putFieldError("accountExpirationDate",
                    OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_CANNOT_BE_BEFORE_EFFECTIVE_DATE);
            // putGlobalError(OLEKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_CANNOT_BE_BEFORE_EFFECTIVE_DATE);
            success &= false;
        }
    }

    return success;
}

From source file:org.kuali.kfs.coa.document.validation.impl.AccountRule.java

/**
 * This method checks to see if any expiration date field rules were violated
 *
 * @param maintenanceDocument//from   w w  w  . j  a va 2s  .c o  m
 * @return false on rules violation
 */
protected boolean checkExpirationDate(MaintenanceDocument maintenanceDocument) {

    LOG.info("checkExpirationDate called");

    boolean success = true;

    Date oldExpDate = oldAccount.getAccountExpirationDate();
    Date newExpDate = newAccount.getAccountExpirationDate();
    Date today = new Date(getDateTimeService().getCurrentTimestamp().getTime());
    today.setTime(DateUtils.truncate(today, Calendar.DAY_OF_MONTH).getTime()); // remove any time components

    // When updating an account expiration date, the date must be today or later
    // Only run this test if this maintenance doc
    // is an edit doc

    if (isUpdatedExpirationDateInvalid(maintenanceDocument)) {
        Account newAccount = (Account) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
        if (newAccount.isClosed()) {
            /*If the Account is being closed and the date is before today's date, the EXP date can only be today*/
            putFieldError("accountExpirationDate",
                    KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_ACCT_CANNOT_BE_CLOSED_EXP_DATE_INVALID);
        } else {
            /*If the Account is not being closed and the date is before today's date, the EXP date can only be today or at a later date*/
            putFieldError("accountExpirationDate",
                    KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
        }
        success &= false;
    }

    // a continuation account is required if the expiration date is completed.
    if (ObjectUtils.isNotNull(newExpDate)) {
        if (StringUtils.isBlank(newAccount.getContinuationAccountNumber())) {
            putFieldError("continuationAccountNumber",
                    KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_CONTINUATION_ACCT_REQD_IF_EXP_DATE_COMPLETED);
        }
        if (StringUtils.isBlank(newAccount.getContinuationFinChrtOfAcctCd())) {
            putFieldError("continuationFinChrtOfAcctCd",
                    KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_CONTINUATION_FINCODE_REQD_IF_EXP_DATE_COMPLETED);
            success &= false;
        }
    }

    // If creating a new account if acct_expiration_dt is set then
    // the acct_expiration_dt must be changed to a date that is today or later
    if (maintenanceDocument.isNew() && ObjectUtils.isNotNull(newExpDate)) {
        Collection<String> fundGroups = SpringContext.getBean(ParameterService.class)
                .getParameterValuesAsString(Account.class,
                        KFSConstants.ChartApcParms.EXPIRATION_DATE_BACKDATING_FUND_GROUPS);
        if (fundGroups == null || ObjectUtils.isNull(newAccount.getSubFundGroup())
                || !fundGroups.contains(newAccount.getSubFundGroup().getFundGroupCode())) {
            if (!newExpDate.after(today) && !newExpDate.equals(today)) {
                putFieldError("accountExpirationDate",
                        KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_TODAY_LATER);
                success &= false;
            }
        }
    }

    // acct_expiration_dt can not be before acct_effect_dt
    Date effectiveDate = newAccount.getAccountEffectiveDate();
    if (ObjectUtils.isNotNull(effectiveDate) && ObjectUtils.isNotNull(newExpDate)) {
        if (newExpDate.before(effectiveDate)) {
            putFieldError("accountExpirationDate",
                    KFSKeyConstants.ERROR_DOCUMENT_ACCMAINT_EXP_DATE_CANNOT_BE_BEFORE_EFFECTIVE_DATE);
            success &= false;
        }
    }

    return success;
}

From source file:org.kuali.kfs.module.ar.batch.service.impl.VerifyBillingFrequencyServiceImpl.java

@Override
public Date[] getStartDateAndEndDateOfPreviousBillingPeriod(ContractsAndGrantsBillingAward award,
        AccountingPeriod currPeriod) {/*from w  w  w. j  a v  a2  s  . c om*/
    Date[] startDt_EndDt = new Date[2];
    Date previousAccountingPeriodEndDay = null;
    Date previousAccountingPeriodStartDay = null;
    Date tmpEndDate;
    Date lastBilledDate = award.getLastBilledDate();
    String billingFrequency = award.getBillingFrequencyCode();
    ArrayList<Date> periodEndDateListOfCurrFiscalYear = getSortedListOfPeriodEndDatesOfCurrentFiscalYear(
            currPeriod);

    // this is used later on when obtaining the last date of the previous fiscal year as the previousAccountPeriodEndDay
    // it subtracts one from the fiscal year for the currPeriod passed in rather than assuming we want the
    // previous fiscal year to the current active fiscal year, just in case a past period is sent in as currPeriod
    // this is mostly just to facilitate unit tests but does make the code more robust (theoretically at least)
    int previousYear = currPeriod.getUniversityFiscalYear() - 1;

    int periodEndDateListOfCurrFiscalYearSize = 0;
    if (periodEndDateListOfCurrFiscalYear != null) {
        periodEndDateListOfCurrFiscalYearSize = periodEndDateListOfCurrFiscalYear.size();
    }

    // 2.billed monthly. ( Milestone and Predetermined Scheduled billing frequencies will also be invoiced as monthly.)
    if (billingFrequency.equalsIgnoreCase(ArConstants.MONTHLY_BILLING_SCHEDULE_CODE)
            || billingFrequency.equalsIgnoreCase(ArConstants.MILESTONE_BILLING_SCHEDULE_CODE)
            || billingFrequency.equalsIgnoreCase(ArConstants.PREDETERMINED_BILLING_SCHEDULE_CODE)) {
        // 2.1 find end date
        if (lastBilledDate != null) {
            if (periodEndDateListOfCurrFiscalYearSize > 0 && currPeriod.getUniversityFiscalPeriodEndDate()
                    .equals(periodEndDateListOfCurrFiscalYear.get(0))) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime()); // assume the calendar date, discussion
            } else {
                int i = -1;
                for (i = 0; i < periodEndDateListOfCurrFiscalYear.size(); i++) {
                    if (currPeriod.getUniversityFiscalPeriodEndDate()
                            .equals(periodEndDateListOfCurrFiscalYear.get(i))) {
                        break;
                    }
                }

                previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(i - 1);
            }
        } else {
            if (periodEndDateListOfCurrFiscalYearSize > 0 && currPeriod.getUniversityFiscalPeriodEndDate()
                    .equals(periodEndDateListOfCurrFiscalYear.get(0))) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime());
            } else {
                // get end date by award's beginning date
                // if the lastBilledDate = null, means the the award is billed from its start date till the previous period end
                // date, so the calculation would be:
                int i = -1;
                for (i = 0; i < periodEndDateListOfCurrFiscalYear.size(); i++) {
                    if (currPeriod.getUniversityFiscalPeriodEndDate()
                            .equals(periodEndDateListOfCurrFiscalYear.get(i))) {
                        break;
                    }
                }

                previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(i - 1);
            }
        }

        // 2.2 find start date
        // PreviousAccountingPeriodStartDate = previous accounting period endDate of previous accounting period + 1 day
        // for example current date is 2012.8.16, then PreviousAccountingPeriodStartDate = 2012.6.30 + 1day, which is 2012.7.1

        AccountingPeriod period = accountingPeriodService.getByDate(previousAccountingPeriodEndDay);
        if (period.getUniversityFiscalYear().intValue() < currPeriod.getUniversityFiscalYear().intValue()) {

            if (lastBilledDate == null) {
                previousAccountingPeriodStartDay = award.getAwardBeginningDate();
            } else {
                ArrayList<Date> acctPeriodEndDateListOfPreviousFiscalYear = getSortedListOfPeriodEndDatesOfCurrentFiscalYear(
                        period);

                int i = -1;
                for (i = acctPeriodEndDateListOfPreviousFiscalYear.size() - 1; i >= 0; i -= 1) {
                    tmpEndDate = acctPeriodEndDateListOfPreviousFiscalYear.get(i);

                    if (tmpEndDate.before(previousAccountingPeriodEndDay)) {
                        previousAccountingPeriodStartDay = calculateNextDay(tmpEndDate);
                        break;
                    }
                }

            }
        } else if (period.getUniversityFiscalYear().intValue() == currPeriod.getUniversityFiscalYear()
                .intValue()) {
            if (lastBilledDate == null) {
                previousAccountingPeriodStartDay = award.getAwardBeginningDate();
            } else {
                if (periodEndDateListOfCurrFiscalYearSize > 0
                        && previousAccountingPeriodEndDay.equals(periodEndDateListOfCurrFiscalYear.get(0))) {
                    final Date firstDayOfCurrentFiscalYear = new Date(universityDateService
                            .getFirstDateOfFiscalYear(currPeriod.getUniversityFiscalYear()).getTime());
                    previousAccountingPeriodStartDay = firstDayOfCurrentFiscalYear;
                } else {
                    int i = -1;
                    for (i = 0; i < periodEndDateListOfCurrFiscalYear.size(); i++) {
                        tmpEndDate = periodEndDateListOfCurrFiscalYear.get(i);

                        if (!tmpEndDate.before(previousAccountingPeriodEndDay)) {
                            break;
                        }
                    }
                    previousAccountingPeriodStartDay = calculateNextDay(
                            periodEndDateListOfCurrFiscalYear.get(i - 1));
                }
            }
        }
    }

    // 3.billed quarterly
    if (billingFrequency.equalsIgnoreCase(ArConstants.QUATERLY_BILLING_SCHEDULE_CODE)) {
        // 3.1 find end date
        if (lastBilledDate != null) {
            if (periodEndDateListOfCurrFiscalYearSize > 2 && !currPeriod.getUniversityFiscalPeriodEndDate()
                    .after(periodEndDateListOfCurrFiscalYear.get(2))) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime());
            } else {
                int i = 0;
                for (i = 2; i < periodEndDateListOfCurrFiscalYear.size(); i += 3) {
                    // find the PreviousAccountingPeriodEndDate by current fiscal period end date and last billed date.
                    // for exmple, if current date is 2011.10.8, then the code will get out from for loop when looping to i =5
                    // (2011.12.31), so previous end date is 2011.9.30(i=5-3=2)
                    if (!currPeriod.getUniversityFiscalPeriodEndDate()
                            .after(periodEndDateListOfCurrFiscalYear.get(i))) {
                        break;
                    }
                }
                previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(i - 3);
            }

        } else {

            if (periodEndDateListOfCurrFiscalYearSize > 0 && currPeriod.getUniversityFiscalPeriodEndDate()
                    .equals(periodEndDateListOfCurrFiscalYear.get(0))) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime());
            } else {
                Date dt = accountingPeriodService.getByDate(award.getAwardBeginningDate())
                        .getUniversityFiscalPeriodEndDate();

                int i = -1;
                for (i = 2; i < periodEndDateListOfCurrFiscalYear.size(); i += 3) {
                    // find the closest period end date by the award beginning date,
                    // for exmple award is created on 7/15/2012 and billed quarterly, then the next billing date for this award is
                    // 9/30/2012
                    if (!dt.after(periodEndDateListOfCurrFiscalYear.get(i))) {
                        break;
                    }
                }
                previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(i);

            }
        }
        // 3.2 find start date
        // PreviousAccountingPeriodStartDate falls into previous fiscal year
        AccountingPeriod period = accountingPeriodService.getByDate(previousAccountingPeriodEndDay);
        if (lastBilledDate == null) {
            previousAccountingPeriodStartDay = award.getAwardBeginningDate();
        } else {
            if (period.getUniversityFiscalYear().intValue() < currPeriod.getUniversityFiscalYear().intValue()) {

                ArrayList<Date> acctPeriodEndDateListOfPreviousFiscalYear = getSortedListOfPeriodEndDatesOfCurrentFiscalYear(
                        period);

                int j = -1;
                for (j = acctPeriodEndDateListOfPreviousFiscalYear.size() - 1; j >= 0; j -= 3) {
                    tmpEndDate = acctPeriodEndDateListOfPreviousFiscalYear.get(j);

                    if (tmpEndDate.before(previousAccountingPeriodEndDay)) {
                        previousAccountingPeriodStartDay = calculateNextDay(tmpEndDate);
                        break;
                    }
                }

            } else if (period.getUniversityFiscalYear().intValue() == currPeriod.getUniversityFiscalYear()
                    .intValue()) {

                if (periodEndDateListOfCurrFiscalYearSize > 2
                        && !previousAccountingPeriodEndDay.after(periodEndDateListOfCurrFiscalYear.get(2))) {
                    final Date firstDayOfCurrentFiscalYear = new Date(universityDateService
                            .getFirstDateOfFiscalYear(currPeriod.getUniversityFiscalYear()).getTime());
                    previousAccountingPeriodStartDay = firstDayOfCurrentFiscalYear;
                } else {
                    int i = -1;
                    for (i = 2; i < periodEndDateListOfCurrFiscalYear.size(); i += 3) {
                        tmpEndDate = periodEndDateListOfCurrFiscalYear.get(i);

                        if (!tmpEndDate.before(previousAccountingPeriodEndDay)) {
                            break;
                        }
                    }
                    previousAccountingPeriodStartDay = calculateNextDay(
                            periodEndDateListOfCurrFiscalYear.get(i - 3));
                }
            }
        }
    }

    // 4.billed semi-annually
    if (billingFrequency.equalsIgnoreCase(ArConstants.SEMI_ANNUALLY_BILLING_SCHEDULE_CODE)) {
        // 4.1 find end date
        if (lastBilledDate != null) {
            // if the current month is in the first fiscal semi-year of the current year,
            // then get the last day of the previous fiscal year as PreviousAccountingPeriodEndDate
            if (!currPeriod.getUniversityFiscalPeriodEndDate()
                    .after(periodEndDateListOfCurrFiscalYear.get(5))) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime());
            } else {
                int i = -1;
                for (i = 5; i < periodEndDateListOfCurrFiscalYear.size(); i += 6) {
                    if (!currPeriod.getUniversityFiscalPeriodEndDate()
                            .after(periodEndDateListOfCurrFiscalYear.get(i))) {
                        break;
                    }
                }
                previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(i - 6);
            }
        } else {
            Date dt = accountingPeriodService.getByDate(award.getAwardBeginningDate())
                    .getUniversityFiscalPeriodEndDate();

            if (accountingPeriodService.getByDate(award.getAwardBeginningDate()).getUniversityFiscalYear()
                    .compareTo(currPeriod.getUniversityFiscalYear()) < 0) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime());
            } else {
                for (int i = 5; i < periodEndDateListOfCurrFiscalYear.size(); i += 6) {
                    // find the closest period end date by the award beginning date,
                    // for exmple award is created on 7/15/2012 and billed annually, then the next billing date for this award
                    // is 12/31/2012
                    if (dt.before(periodEndDateListOfCurrFiscalYear.get(i))
                            || dt.equals(periodEndDateListOfCurrFiscalYear.get(i))) {
                        previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(i);
                        break;
                    }
                }
            }
        }

        // 4.2 find start date
        // PreviousAccountingPeriodStartDate falls into previous fiscal year
        AccountingPeriod period = accountingPeriodService.getByDate(previousAccountingPeriodEndDay);
        if (lastBilledDate == null) {
            previousAccountingPeriodStartDay = award.getAwardBeginningDate();
        } else {
            if (period.getUniversityFiscalYear() < currPeriod.getUniversityFiscalYear()) {
                ArrayList<Date> periodEndDateListOfPreviousFiscalYear = getSortedListOfPeriodEndDatesOfCurrentFiscalYear(
                        period);

                int i = -1;
                for (i = periodEndDateListOfPreviousFiscalYear.size() - 1; i >= 0; i -= 6) {
                    tmpEndDate = periodEndDateListOfPreviousFiscalYear.get(i);

                    if (tmpEndDate.before(previousAccountingPeriodEndDay)) {
                        previousAccountingPeriodStartDay = calculateNextDay(tmpEndDate);
                        break;
                    }
                }

            }
            // PreviousAccountingPeriodStartDate falls into current fiscal year
            else if (period.getUniversityFiscalYear().intValue() == currPeriod.getUniversityFiscalYear()
                    .intValue()) {

                // previousAccoutingPeriodEndDay falls in the first fiscal period
                if (!previousAccountingPeriodEndDay.after(periodEndDateListOfCurrFiscalYear.get(5))) {
                    final Date firstDayOfCurrentFiscalYear = new Date(universityDateService
                            .getFirstDateOfFiscalYear(currPeriod.getUniversityFiscalYear()).getTime());
                    previousAccountingPeriodStartDay = firstDayOfCurrentFiscalYear;
                }
                // previousAccoutingPeriodEndDay does not falls in the first fiscal period
                else {
                    int i = -1;
                    for (i = 5; i < periodEndDateListOfCurrFiscalYear.size(); i += 6) {
                        tmpEndDate = periodEndDateListOfCurrFiscalYear.get(i);

                        if (!tmpEndDate.before(previousAccountingPeriodEndDay)) {
                            break;
                        }
                    }
                    previousAccountingPeriodStartDay = calculateNextDay(
                            periodEndDateListOfCurrFiscalYear.get(i - 6));

                }
            }
        }
    }

    // 5.billed annually
    if (billingFrequency.equalsIgnoreCase(ArConstants.ANNUALLY_BILLING_SCHEDULE_CODE)) {
        // 5.1 find end date
        if (lastBilledDate != null) {
            previousAccountingPeriodEndDay = new Date(
                    universityDateService.getLastDateOfFiscalYear(previousYear).getTime()); // assume the calendar date, discussion needed
        } else {
            if (accountingPeriodService.getByDate(award.getAwardBeginningDate()).getUniversityFiscalYear()
                    .compareTo(currPeriod.getUniversityFiscalYear()) < 0) {
                previousAccountingPeriodEndDay = new Date(
                        universityDateService.getLastDateOfFiscalYear(previousYear).getTime());
            } else {
                previousAccountingPeriodEndDay = periodEndDateListOfCurrFiscalYear.get(11);
            }
        }

        // 5.2 find start date
        if (lastBilledDate == null) {
            previousAccountingPeriodStartDay = award.getAwardBeginningDate();
        } else {
            previousAccountingPeriodStartDay = new Date(
                    universityDateService.getFirstDateOfFiscalYear(previousYear).getTime());
        }
    }

    // 6.billed for LOC Review - A Random billing period
    if (billingFrequency.equalsIgnoreCase(ArConstants.LOC_BILLING_SCHEDULE_CODE)) {

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        previousAccountingPeriodEndDay = new Date(cal.getTime().getTime());

        // 5.2 find start date
        if (lastBilledDate == null) {
            previousAccountingPeriodStartDay = award.getAwardBeginningDate();
        } else {
            previousAccountingPeriodStartDay = calculateNextDay(lastBilledDate);
        }
    }

    startDt_EndDt[0] = previousAccountingPeriodStartDay;
    startDt_EndDt[1] = previousAccountingPeriodEndDay;
    return startDt_EndDt;
}