Example usage for java.math BigDecimal scale

List of usage examples for java.math BigDecimal scale

Introduction

In this page you can find the example usage for java.math BigDecimal scale.

Prototype

int scale

To view the source code for java.math BigDecimal scale.

Click Source Link

Document

The scale of this BigDecimal, as returned by #scale .

Usage

From source file:org.kuali.kpme.tklm.time.detail.validation.TimeDetailValidationUtil.java

public static List<String> validateTimeEntryDetails(BigDecimal hours, BigDecimal amount, String startTimeS,
        String endTimeS, String startDateS, String endDateS, TimesheetDocument timesheetDocument,
        String selectedEarnCode, String selectedAssignment, boolean acrossDays, String timeblockId,
        String overtimePref) {/*from w  w w .  j  av  a  2  s. c om*/
    List<String> errors = new ArrayList<String>();
    LocalDate savedStartDate = TKUtils.formatDateString(startDateS);
    LocalDate savedEndDate = TKUtils.formatDateString(endDateS);

    if (timesheetDocument == null) {
        errors.add("No timesheet document found.");
    }
    if (errors.size() > 0)
        return errors;

    CalendarEntry payCalEntry = timesheetDocument.getCalendarEntry();
    EarnCode earnCode = null;
    if (StringUtils.isNotBlank(selectedEarnCode)) {
        earnCode = HrServiceLocator.getEarnCodeService().getEarnCode(selectedEarnCode,
                TKUtils.formatDateTimeStringNoTimezone(endDateS).toLocalDate());
    }
    boolean isTimeRecordMethod = earnCode != null
            && StringUtils.equalsIgnoreCase(earnCode.getRecordMethod(), HrConstants.EARN_CODE_TIME);

    errors.addAll(CalendarValidationUtil.validateDates(startDateS, endDateS));

    if (isTimeRecordMethod) {
        errors.addAll(CalendarValidationUtil.validateTimes(startTimeS, endTimeS));
    }
    if (errors.size() > 0)
        return errors;

    Long startTime;
    Long endTime;

    if (!isTimeRecordMethod) {
        startTimeS = "0:0";
        endTimeS = "0:0";
    }

    if (acrossDays && !endTimeS.equals("0:00")) {
        endDateS = startDateS;
    }

    startTime = TKUtils.convertDateStringToDateTimeWithoutZone(startDateS, startTimeS).getMillis();
    endTime = TKUtils.convertDateStringToDateTimeWithoutZone(endDateS, endTimeS).getMillis();

    errors.addAll(CalendarValidationUtil.validateInterval(payCalEntry, startTime, endTime));
    if (errors.size() > 0)
        return errors;

    if (isTimeRecordMethod) {
        if (startTimeS == null)
            errors.add("The start time is blank.");
        if (endTimeS == null)
            errors.add("The end time is blank.");
        if (startTime - endTime == 0)
            errors.add("Start time and end time cannot be equivalent");
    }
    if (errors.size() > 0)
        return errors;

    DateTime startTemp = new DateTime(startTime);
    DateTime endTemp = new DateTime(endTime);
    /*
     * KPME-2687:
     *
     * Removed 24 hour limitation. System creates continuous sequence of time blocks when !accrossDays,
     * hours between startTemp and endTemp may be over 24 hours.
     *
            if (errors.size() == 0 && !acrossDays && !StringUtils.equals(TkConstants.EARN_CODE_CPE, overtimePref)) {
    Hours hrs = Hours.hoursBetween(startTemp, endTemp);
    if (hrs.getHours() > 24) errors.add("One timeblock cannot exceed 24 hours");
            }
            if (errors.size() > 0) return errors;
            
     */

    //Check that assignment is valid within the timeblock span. 
    AssignmentDescriptionKey assignKey = HrServiceLocator.getAssignmentService()
            .getAssignmentDescriptionKey(selectedAssignment);
    Assignment assign = HrServiceLocator.getAssignmentService().getAssignmentForTargetPrincipal(assignKey,
            startTemp.toLocalDate());
    if (assign == null)
        errors.add("Assignment is not valid for start date " + TKUtils.formatDate(new LocalDate(startTime)));
    assign = HrServiceLocator.getAssignmentService().getAssignmentForTargetPrincipal(assignKey,
            endTemp.toLocalDate());
    if (assign == null)
        errors.add("Assignment is not valid for end date " + TKUtils.formatDate(new LocalDate(endTime)));
    if (errors.size() > 0)
        return errors;

    //------------------------
    // some of the simple validations are in the js side in order to reduce the server calls
    // 1. check if the begin / end time is empty - tk.calenadr.js
    // 2. check the time format - timeparse.js
    // 3. only allows decimals to be entered in the hour field
    //------------------------

    //------------------------
    // check if the overnight shift is across days
    //------------------------
    if (acrossDays && hours == null && amount == null) {
        if (savedEndDate.isAfter(savedStartDate) && startTemp.getHourOfDay() > endTemp.getHourOfDay()
                && !(endTemp.getDayOfYear() - startTemp.getDayOfYear() <= 1 && endTemp.getHourOfDay() == 0)) {
            errors.add("The \"apply to each day\" box should not be checked.");
        }
    }
    if (errors.size() > 0)
        return errors;

    //------------------------
    // check if the begin / end time are valid
    //------------------------
    if ((startTime.compareTo(endTime) > 0 || endTime.compareTo(startTime) < 0)) {
        errors.add("The time or date is not valid.");
    }
    if (errors.size() > 0)
        return errors;

    // KPME-1446 
    // -------------------------------
    // check if there is a weekend day when the include weekends flag is checked
    //--------------------------------

    //------------------------
    // Amount cannot be zero
    //------------------------
    if (amount != null && earnCode != null
            && StringUtils.equals(earnCode.getEarnCodeType(), HrConstants.EARN_CODE_AMOUNT)) {
        if (amount.equals(BigDecimal.ZERO)) {
            errors.add("Amount cannot be zero.");
        }
        if (amount.scale() > 2) {
            errors.add("Amount cannot have more than two digits after decimal point.");
        }
    }
    if (errors.size() > 0)
        return errors;

    //------------------------
    // check if the hours entered for hourly earn code is greater than 24 hours per day
    // Hours cannot be zero
    //------------------------
    if (hours != null && earnCode != null
            && StringUtils.equals(earnCode.getEarnCodeType(), HrConstants.EARN_CODE_HOUR)) {
        if (hours.equals(BigDecimal.ZERO)) {
            errors.add("Hours cannot be zero.");
        }
        if (hours.scale() > 2) {
            errors.add("Hours cannot have more than two digits after decimal point.");
        }
        /*
         * KPME-2671:
         * 
         * Replacing this conditional with the one below. Shouldn't matter if the date range spans more than one day,
         * hours shouldn't exceed 24.
         * 
         *          int dayDiff = endTemp.getDayOfYear() - startTemp.getDayOfYear() + 1;
                    if (hours.compareTo(new BigDecimal(dayDiff * 24)) == 1) {
        errors.add("Cannot enter more than 24 hours per day.");
                    }
         */
    }
    if (errors.size() > 0)
        return errors;

    /**
     * KPME-2671:
     * 
     * Generalize 24 limit to hour field on time entry form.
     * 
     */
    if (hours != null && hours.compareTo(new BigDecimal(24.0)) > 0) {
        errors.add("Hours cannot exceed 24.");
    }
    //------------------------
    // check if time blocks overlap with each other. Note that the tkTimeBlockId is used to
    // determine is it's updating an existing time block or adding a new one
    //------------------------

    boolean isRegularEarnCode = StringUtils.equals(assign.getJob().getPayTypeObj().getRegEarnCode(),
            selectedEarnCode);
    startTime = TKUtils.convertDateStringToDateTime(startDateS, startTimeS).getMillis();
    endTime = TKUtils.convertDateStringToDateTime(endDateS, endTimeS).getMillis();

    errors.addAll(validateOverlap(startTime, endTime, acrossDays, startDateS, endTimeS, startTemp, endTemp,
            timesheetDocument, timeblockId, isRegularEarnCode, selectedEarnCode));
    if (errors.size() > 0)
        return errors;

    // Accrual Hour Limits Validation
    //errors.addAll(TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimitByEarnCode(timesheetDocument, selectedEarnCode));

    return errors;
}