timesheetamountprocessing.java
来自「一个很好的开源项目管理系统源代码」· Java 代码 · 共 160 行
JAVA
160 行
package net.java.workeffort.service.batch;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import net.java.workeffort.infrastructure.sequence.SequenceManager;import net.java.workeffort.service.BaseService;import net.java.workeffort.service.domain.TimeEntry;import net.java.workeffort.service.domain.Timesheet;import net.java.workeffort.service.support.TimesheetUtils;import org.apache.commons.lang.Validate;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * Batch job to calculate timesheet amounts. Sets amount totals for timesheets * which have been approved. It thoroughly validates the timesheet before * processing. See the <code>TestTimesheetUtils</code> for all the unit tests. * <p> * If a calculation exception occurs for a timesheet, the exception details are * written to the TIMESHEET_BATCH_DETAIL table . The batch job will continue * processing other timesheets. * <p> * The property 'processDelay' defaults to 5 days. This is the delay between the * end (thru) date of the timesheet and when the timesheet will be processed by * the batch job. For example if a timesheets end (thru) date is Jun/30. it will * be processed on July 5th by the batch job. To change the default, set * 'processDelay' property in the <code>batchJob-context.xml</code> file. This * delay provides an interval between the approval and the timesheet amount * calculation for the case where a correction needs to be made to the timesheet * after it has been approved. * <p> * The property 'timesheetUtils' should be initialized before the class is used. * <p> * Note: The currency codes used in the system (Party rates) should be valid as * per ISO 4217 code. See <code>java.util.Currency<code> for more info. * <p> * See <code>batchJob-context.xml</code> for info on scheduling using quartz. * <p> * Transaction management using spring. * @author Antony Joseph */public class TimesheetAmountProcessing extends BaseService implements ITimesheetAmountProcessing { protected static final Log logger = LogFactory .getLog(TimesheetAmountProcessing.class); private TimesheetUtils timesheetUtils; private Integer processDelay = new Integer(5); public void setTimesheetUtils(TimesheetUtils timesheetUtils) { this.timesheetUtils = timesheetUtils; } public void setProcessDelay(Integer processDelay) { Validate.isTrue(processDelay.intValue() > 0, " process delay as to be greate than zero"); this.processDelay = processDelay; } public synchronized void processAmounts(Date jobFireDt) { Long batchJobId = SequenceManager.getSequence("batchJob").getNextId(); int lineNo = 0; Map map = new HashMap(); map.put("batchJobId", batchJobId); map.put("lineNo", new Integer(lineNo++)); map.put("details", "Batch job started"); map.put("createdTs", new Date()); dao.insert("Timesheet.insertTimesheetBatchDetail", map, false); int timesheetCnt = 0; List timesheets = getApprovedTimesheets(getQueryDt(jobFireDt)); Iterator it = timesheets.iterator(); while (it.hasNext()) { Timesheet timesheet = (Timesheet) it.next(); // populate the timesheet with its time Entries. Map timeEntryQuery = new HashMap(); timeEntryQuery.put("timesheetId", timesheet.getTimesheetId()); List timeEntries = dao.queryForList("Timesheet.getListTimeEntry", timeEntryQuery); timesheet.setTimeEntries(timeEntries); List partyRates = getPartyRates(timesheet.getPartyCd()); boolean ok = true; try { timesheetUtils.calculateTimesheetAmount(timesheet, partyRates); } catch (Exception e) { // a calculation exception occured. Store pertinent info ok = false; String details = "timesheetId=" + timesheet.getTimesheetId() + " partyCd=" + timesheet.getPartyCd() + " ex=" + e.getClass().getName() + " msg:" + e.getMessage(); map.put("batchJobId", batchJobId); map.put("lineNo", new Integer(lineNo++)); // 255 limit for VARCHAR in mysql. if (details.length() > 255) map.put("details", details.substring(0, 255)); else map.put("details", details); map.put("createdTs", new Date()); dao.insert("Timesheet.insertTimesheetBatchDetail", map, false); logger.warn(details); } if (ok) { timesheetCnt++; timesheet.setProcessedFlg("Y"); timesheet.setProcessedTs(new Date()); timesheet.setBatchJobId(batchJobId); updateTimesheetAmounts(timesheet); } } map.put("batchJobId", batchJobId); map.put("lineNo", new Integer(lineNo++)); map.put("details", "Batch job completed. Timesheets processed:" + timesheetCnt); map.put("createdTs", new Date()); dao.insert("Timesheet.insertTimesheetBatchDetail", map, false); } private List getApprovedTimesheets(Date queryDt) { return dao.queryForList("Timesheet.getListApprovedTimesheets", queryDt); } private List getPartyRates(String partyCd) { return dao.queryForList("Party.getListPartyRate", partyCd); } private void updateTimesheetAmounts(Timesheet timesheet) { dao.update("Timesheet.updateTimesheetTotalAmt", timesheet, false); if (timesheet.getTimeEntries() != null) { Iterator it = timesheet.getTimeEntries().iterator(); while (it.hasNext()) { TimeEntry te = (TimeEntry) it.next(); dao.update("Timesheet.updateTimeEntryTotal", te, false); } } } private Date getQueryDt(Date jobFireDt) { Calendar cal = Calendar.getInstance(); cal.setTime(jobFireDt); // thruDt in timesheet is stored without any hours, minutes seconds. // Thats why 1 is added to the processDelay. cal.add(Calendar.DAY_OF_YEAR, -1 * (processDelay.intValue() + 1)); return cal.getTime(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?