⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timesheetutils.java

📁 一个很好的开源项目管理系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }        else            throw new RuntimeException("Invalid timesheetPeriod:"                    + timesheetPeriod);        int noOfDays = getNoOfDaysInTimesheet(calFromDt);        long fromDtMillis = calFromDt.getTimeInMillis();        Calendar calThruDt = Calendar.getInstance();        calThruDt.setTime(thruDt);        long thruDtMillis = calThruDt.getTimeInMillis();        if ((fromDtMillis + (noOfDays - 1) * 24 * 60 * 60 * 1000) != thruDtMillis) {            throw new InvalidTimesheetPeriodException("fromDt=" + fromDt                    + " thruDt=" + thruDt + " diff="                    + (thruDtMillis - fromDtMillis) + " period="                    + timesheetPeriod);        }    }    public void calculateTimesheetAmount(Timesheet timesheet, List partyRates)            throws InvalidTimesheetPeriodException, RateNotFoundException,            CurrencyMismatchException, InvalidRateDateRangeException,            ConflictingRateDateRangeException {        validateTimesheetPeriod(timesheet);        validateHours(timesheet);        validateRates(timesheet.getPartyCd(), partyRates);        if (timesheet.getTimeEntries() == null) {            timesheet.setTotalAmt(new BigDecimal("0.00"));            return;        }        Money timesheetAmt = null;        Iterator it = timesheet.getTimeEntries().iterator();        while (it.hasNext()) {            TimeEntry timeEntry = (TimeEntry) it.next();            // should never be the case but check anyway.            Validate.isTrue(timesheet.getTimesheetId().equals(                    timeEntry.getTimesheetId()),                    "The timesheet ids in timesheet and timeEntry conflict. timesheet="                            + timesheet + " timeEntry=" + timeEntry);            Calendar calFromDt = Calendar.getInstance();            calFromDt.setTime(timesheet.getFromDt());            int noOfDays = getNoOfDaysInTimesheet(calFromDt);            calculateTimeEntryLineAmount(timeEntry, partyRates, timesheet                    .getFromDt(), noOfDays);            // note that if line total is zero its currency will not be set            if (timeEntry.getLineTotalAmt().compareTo(new BigDecimal("0.00")) != 0) {                if (timesheetAmt == null) {                    timesheetAmt = new Money(                            timeEntry.getLineTotalAmt(),                            Currency                                    .getInstance(timeEntry.getLineTotalAmtCur()));                }                else {                    timesheetAmt = timesheetAmt.add(new Money(timeEntry                            .getLineTotalAmt(), Currency.getInstance(timeEntry                            .getLineTotalAmtCur())));                }            }        }        if (timesheetAmt != null) {            timesheet.setTotalAmt(timesheetAmt.getAmount());            timesheet.setTotalAmtCur(timesheetAmt.getCurrency()                    .getCurrencyCode());        }        else {            timesheet.setTotalAmt(new BigDecimal("0.00"));            // no currency is set.        }    }    //  package level access for testing.    void calculateTimeEntryLineAmount(TimeEntry timeEntry, List partyRates,            Date fromDt, int noOfDays) throws RateNotFoundException,            CurrencyMismatchException {        Money lineAmt = null;        // get the hours for each day.        for (int i = 0; i < noOfDays; i++) {            BigDecimal hours = (BigDecimal) getSimpleProperty(timeEntry, "day"                    + i);            if (hours != null && hours.compareTo(new BigDecimal("0.00")) != 0) {                Money dayAmt = calculateAmountForDay(hours, getTimeEntryDate(                        fromDt, i), timeEntry.getRateTypeCd(), partyRates);                if (lineAmt == null) {                    lineAmt = dayAmt;                }                else {                    if (dayAmt != null) {                        lineAmt = lineAmt.add(dayAmt);                    }                }            }        }        if (lineAmt != null) {            timeEntry.setLineTotalAmt(lineAmt.getAmount());            timeEntry.setLineTotalAmtCur(lineAmt.getCurrency()                    .getCurrencyCode());        }        else {            timeEntry.setLineTotalAmt(new BigDecimal("0.00"));            // currency is not set.        }    }    public String convertDateToString(Date date, String dateFormat) {        if (date == null)            return null;        StringBuffer buf = new StringBuffer();        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);        formatter.setLenient(false);        formatter.format(date, buf, new FieldPosition(0));        return buf.toString();    }    public Date convertStringToDate(String dateString, String dateFormat) {        if ((dateString == null) || (dateString.length() == 1))            return null;        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);        formatter.setLenient(false);        return formatter.parse(dateString, new ParsePosition(0));    }    // package level access for testing.    Money calculateAmountForDay(BigDecimal hours, Date date, String rateTypeCd,            List partyRates) throws RateNotFoundException {        Validate.notNull(hours, "hours cannot be null");        Validate.notNull(date, "date cannot be null");        Validate.notNull(rateTypeCd, "rateTypeCd cannot be null");        Validate.notNull(partyRates, "partyRates cannot be null");        BigDecimal zero = new BigDecimal("0.00");        Money amount = null;        Iterator it = partyRates.iterator();        while (it.hasNext()) {            PartyRate partyRate = (PartyRate) it.next();            if (rateTypeCd.equals(partyRate.getRateTypeCd())) {                if (isDateInRange(partyRate.getFromDt(), partyRate.getThruDt(),                        date)) {                    // if rate is 0.00 set amount to 0.00.                    if (partyRate.getRate().compareTo(zero) == 0) {                        amount = new Money(zero, Currency.getInstance(partyRate                                .getRateCur()));                    }                    else {                        Money rate = new Money(partyRate.getRate(), Currency                                .getInstance(partyRate.getRateCur()));                        amount = rate.multiply(hours);                    }                    break;                }            }        }        if (amount == null)            throw new RateNotFoundException("hours=" + hours + " date=" + date                    + " rateTypeCd=" + rateTypeCd);        return amount;    }    // package level access for testing    void validateRates(String partyCd, List partyRates)            throws InvalidRateDateRangeException,            ConflictingRateDateRangeException {        // For rows with the same 'rateType' check the date ranges for        // conflict.        List filterPropertyNames = new ArrayList();        filterPropertyNames.add("rateTypeCd");        Iterator it = partyRates.iterator();        while (it.hasNext()) {            PartyRate partyRate = (PartyRate) it.next();            Validate.notNull(partyRate.getRateTypeCd(),                    " rateTypeCd cannot be null. partyRate=" + partyRate);            Validate.notNull(partyRate.getFromDt(),                    " fromDt cannot be null. partyRate=" + partyRate);            Validate.notNull(partyRate.getRate(),                    " rate cannot be null. partyRate=" + partyRate);            Validate.notNull(partyRate.getRateCur(),                    "rate currency cannot be null. partyRate=" + partyRate);            Validate.notNull(partyRate.getPartyCd(),                    "partyCd cannot be null. partyRate=" + partyRate);            Validate.isTrue(partyCd.equals(partyRate.getPartyCd()),                    "the rate is not for the same party. partyCd=" + partyCd                            + " partyRate=" + partyRate);            BigDecimal zero = new BigDecimal("0.00");            Validate.isTrue(partyRate.getRate().compareTo(zero) >= 0,                    " rate cannot be negative");            Predicate predicate = new DateRangePredicate(partyRate,                    filterPropertyNames);            Collection filteredCollection = CollectionUtils.select(partyRates,                    predicate);            Date fromDt = partyRate.getFromDt();            Date thruDt = partyRate.getThruDt();            if (thruDt == null)                thruDt = getDefaultThruDtForRate();            if (fromDt.after(thruDt)) {                throw new InvalidRateDateRangeException("partyRate="                        + partyRate);            }            if (!DateRangeValidationUtils.validateMultiRowDateRanges(                    filteredCollection, fromDt, thruDt, "fromDt", "thruDt")) {                throw new ConflictingRateDateRangeException(                        "Conflict in range rates");            }        }    }    // package level access for testing    void validateHours(Timesheet timesheet) {        List timeEntries = timesheet.getTimeEntries();        if (timeEntries == null)            return;        Calendar calFromDt = Calendar.getInstance();        calFromDt.setTime(timesheet.getFromDt());        int noOfDays = getNoOfDaysInTimesheet(calFromDt);        BigDecimal hours0 = new BigDecimal("0.00");        BigDecimal hours24 = new BigDecimal("24.00");        // validate each hour entry is in range 0-24        for (int i = 0; i < timeEntries.size(); i++) {            TimeEntry te = (TimeEntry) timeEntries.get(i);            // get the hours for each day.            for (int j = 0; j < noOfDays; j++) {                BigDecimal hours = (BigDecimal) getSimpleProperty(te, "day" + j);                               if (hours != null) {                    if (hours.compareTo(hours0) < 0                            || hours.compareTo(hours24) > 0) {                        throw new InvalidHoursException("for day=" + j + " hours="                                + hours + " not in range 0-24");                    }                }            }        }        // validate that the total hours for a day across multiple time        // entries is not over 24.        if (timeEntries.size() > 1) {            for (int i = 0; i < noOfDays; i++) {                BigDecimal totalHoursForDay = new BigDecimal("0.00");                for (int j = 0; j < timeEntries.size(); j++) {                    TimeEntry te = (TimeEntry) timeEntries.get(j);                    BigDecimal hours = (BigDecimal) getSimpleProperty(te, "day"                            + i);                    if (hours != null) {                        totalHoursForDay = totalHoursForDay.add(hours);                    }                }                if (totalHoursForDay.compareTo(hours24) > 0) {                    throw new InvalidHoursException("For day " + i                            + " the total hours=" + totalHoursForDay + " > 24");                }            }        }        // Paranoid check. Making sure that no time entries have been made in        // timesheet beyond the 'ThruDt'        if (noOfDays < MAX_DAYS) {            for (int i = 0; i < timeEntries.size(); i++) {                TimeEntry te = (TimeEntry) timeEntries.get(i);                // get the hours for each day.                for (int j = noOfDays; j < MAX_DAYS; j++) {                    BigDecimal hours = (BigDecimal) getSimpleProperty(te, "day"                            + j);                    if (hours != null)                        throw new IllegalStateException(                                "Hours entered for days beyound thruDt");                }            }        }    }    private boolean isDateInRange(Date fromDt, Date thruDt, Date date) {        Validate.notNull(fromDt, "fromDt cannot be null");        Validate.notNull(date, "date cannot be null");        if (thruDt == null) {            thruDt = getDefaultThruDtForRate();        }        if (date.compareTo(fromDt) >= 0 && date.compareTo(thruDt) <= 0)            return true;        else            return false;    }    private Date getDefaultThruDtForRate() {        Calendar calendar = Calendar.getInstance();        calendar.set(3999, 12, 31);        return calendar.getTime();    }    private Date getTimeEntryDate(Date fromDt, int offset) {        Validate.notNull(fromDt, "fromDt cannot be null");        Calendar cal = Calendar.getInstance();        cal.setTime(fromDt);        cal.add(Calendar.DAY_OF_YEAR, offset);        return cal.getTime();    }    public Object getSimpleProperty(Object obj, String propertyName) {        try {            return PropertyUtils.getSimpleProperty(obj, propertyName);        }        catch (Exception e) {            throw new RuntimeException("Error while getting property:"                    + propertyName + " from object:" + obj, e);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -