View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.time.batch;
17  
18  import java.sql.Timestamp;
19  import java.util.List;
20  
21  import org.apache.log4j.Logger;
22  import org.joda.time.DateTime;
23  import org.joda.time.DateTimeZone;
24  import org.kuali.kpme.core.assignment.Assignment;
25  import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
26  import org.kuali.kpme.core.batch.BatchJob;
27  import org.kuali.kpme.core.calendar.Calendar;
28  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
29  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
30  import org.kuali.kpme.core.service.HrServiceLocator;
31  import org.kuali.kpme.core.util.TKUtils;
32  import org.kuali.kpme.tklm.common.TkConstants;
33  import org.kuali.kpme.tklm.time.clocklog.ClockLog;
34  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
35  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
36  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
37  import org.quartz.JobDataMap;
38  import org.quartz.JobExecutionContext;
39  import org.quartz.JobExecutionException;
40  
41  public class EndPayPeriodJob extends BatchJob {
42  	
43  	private static final Logger LOG = Logger.getLogger(EndPayPeriodJob.class);
44  	
45  	public void execute(JobExecutionContext context) throws JobExecutionException {
46  		
47  		
48  		String batchUserPrincipalId = getBatchUserPrincipalId();
49  		if (batchUserPrincipalId != null) {
50  			JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
51  	
52  			String hrCalendarEntryId = jobDataMap.getString("hrCalendarEntryId");
53  			
54  		    DateTime currentDateTime =  new DateTime();
55  			LOG.info("EndOfPayPeiodJob is running at " + currentDateTime.toString() + " for hrCalendarEntryId " + hrCalendarEntryId);
56  		    
57  	        CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(hrCalendarEntryId);
58  	        Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
59  	        calendarEntry.setCalendarObj(calendar);
60  	        
61  	        
62  	    	String calendarName = calendarEntry.getCalendarName();
63  	    	DateTime scheduleDate = calendarEntry.getBatchEndPayPeriodFullDateTime();
64  	    	
65  	    	List<PrincipalHRAttributes> principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getActiveEmployeesForPayCalendar(calendarName, scheduleDate.toLocalDate());
66  	    	for (PrincipalHRAttributes principalHRAttribute : principalHRAttributes) {
67  	    		String pId = principalHRAttribute.getPrincipalId();
68  	    	    
69  	    		List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignmentsByCalEntryForTimeCalendar(pId, calendarEntry);
70  	    		for (Assignment assignment : assignments) {
71  	    			String jobNumber = String.valueOf(assignment.getJobNumber());
72  	    			String workArea = String.valueOf(assignment.getWorkArea());
73  	    			String task = String.valueOf(assignment.getTask());
74  	    			
75  	    			ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(pId, jobNumber, workArea, task, calendarEntry);
76  	    	    	if (lastClockLog != null && TkConstants.ON_THE_CLOCK_CODES.contains(lastClockLog.getClockAction())) {
77  	    	    		runEndPayPeriodJobForUser(calendarEntry, pId, lastClockLog, batchUserPrincipalId);
78  	    	    	}
79  	    		}
80  	    	}
81          } else {
82          	String principalName = getBatchUserPrincipalName();
83          	LOG.error("Could not run batch jobs due to missing batch user " + principalName);
84          }
85  	}
86  	
87  	
88  	protected void runEndPayPeriodJobForUser(CalendarEntry calendarEntry, String principalId, ClockLog openClockLog, String batchUserPrincipalId) {
89  LOG.info("EndOfPayPeiodJob started for user " + principalId + " and clockLog " + openClockLog.getTkClockLogId());
90  		DateTime endPeriodDateTime = calendarEntry.getEndPeriodFullDateTime();
91  	        
92          // need to use the user's time zone and the system time zone to figure out the system time of endPeriodDatTime in the user's timezone
93          DateTimeZone userTimezone = DateTimeZone.forID(HrServiceLocator.getTimezoneService().getUserTimezone(principalId));
94  		DateTimeZone systemTimeZone = TKUtils.getSystemDateTimeZone();
95  		// time to use to create the CO clock log
96          DateTime coLogDateTime = TKUtils.convertTimeForDifferentTimeZone(endPeriodDateTime,systemTimeZone,userTimezone);
97  	        
98          CalendarEntry nextCalendarEntry = HrServiceLocator.getCalendarEntryService().getNextCalendarEntryByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry);
99          DateTime beginNextPeriodDateTime = nextCalendarEntry.getBeginPeriodFullDateTime();
100         // time to use to create the CI clock log
101         DateTime ciLogDateTime = TKUtils.convertTimeForDifferentTimeZone(beginNextPeriodDateTime,systemTimeZone,userTimezone);
102         
103         String ipAddress = openClockLog.getIpAddress();
104 	    TimesheetDocumentHeader timesheetDocumentHeader = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(principalId, calendarEntry.getBeginPeriodFullDateTime(), endPeriodDateTime);
105 	    if (timesheetDocumentHeader != null) {
106 	        TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(timesheetDocumentHeader.getDocumentId());
107 	        AssignmentDescriptionKey assignmentKey = new AssignmentDescriptionKey(openClockLog.getJobNumber(), openClockLog.getWorkArea(), openClockLog.getTask());
108 	        Assignment assignment = timesheetDocument.getAssignment(assignmentKey);
109 	        ClockLog clockOutLog = TkServiceLocator.getClockLogService().processClockLog(coLogDateTime, assignment, calendarEntry, ipAddress,
110 	            		endPeriodDateTime.toLocalDate(), timesheetDocument, TkConstants.CLOCK_OUT, false, principalId, batchUserPrincipalId);
111 
112             TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService()
113             		.getDocumentHeader(principalId, nextCalendarEntry.getBeginPeriodFullDateTime(), nextCalendarEntry.getEndPeriodFullDateTime());
114             TimesheetDocument nextTimeDoc = null;
115             if(nextTdh != null) {
116             	nextTimeDoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(nextTdh.getDocumentId());
117             }
118             ClockLog clockInLog = TkServiceLocator.getClockLogService().processClockLog(ciLogDateTime, assignment, nextCalendarEntry, ipAddress,
119             		beginNextPeriodDateTime.toLocalDate(), nextTimeDoc, TkConstants.CLOCK_IN, false, principalId, batchUserPrincipalId);
120 
121             // add 5 seconds to clock in log's timestamp so it will be found as the latest clock action
122             Timestamp ts= clockInLog.getTimestamp();
123             java.util.Calendar cal = java.util.Calendar.getInstance();
124             cal.setTimeInMillis(ts.getTime());
125             cal.add(java.util.Calendar.SECOND, 5);
126             Timestamp later = new Timestamp(cal.getTime().getTime());
127             clockInLog.setTimestamp(later);
128             TkServiceLocator.getClockLogService().saveClockLog(clockInLog);
129 LOG.info("Clock Out log created is  " + clockOutLog.getTkClockLogId() + ",  Clock In Log created is " + clockInLog.getTkClockLogId());  
130 	    }
131 LOG.info("EndOfPayPeiodJob is finished for user " + principalId + " and clockLog " + openClockLog.getTkClockLogId());
132 	}
133 	
134 
135 }