1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timesheet.validation;
17
18 import java.util.List;
19
20 import org.kuali.hr.time.assignment.Assignment;
21 import org.kuali.hr.time.calendar.Calendar;
22 import org.kuali.hr.time.calendar.CalendarEntries;
23 import org.kuali.hr.time.service.base.TkServiceLocator;
24 import org.kuali.hr.time.timesheet.TimeSheetInitiate;
25 import org.kuali.hr.time.timesheet.TimesheetDocument;
26 import org.kuali.hr.time.util.TkConstants;
27 import org.kuali.rice.kew.api.exception.WorkflowException;
28 import org.kuali.rice.kns.document.MaintenanceDocument;
29 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30
31 public class TimeSheetInitiateValidation extends MaintenanceDocumentRuleBase {
32 @Override
33 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
34 boolean valid = true;
35 TimeSheetInitiate timeInit = (TimeSheetInitiate)this.getNewBo();
36 Calendar pc = TkServiceLocator.getCalendarService().getCalendarByGroup(timeInit.getPyCalendarGroup());
37 if(pc == null) {
38 this.putFieldError("pyCalendarGroup", "timeSheetInit.payCalendar.Invalid");
39 valid = false;
40 } else {
41 if(pc.getCalendarTypes() != null && !pc.getCalendarTypes().equalsIgnoreCase(TkConstants.CALENDAR_TYPE_PAY)) {
42 this.putFieldError("pyCalendarGroup", "timeSheetInit.payCalendar.Invalid");
43 valid = false;
44 }
45 }
46
47 CalendarEntries pce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(timeInit.getHrPyCalendarEntriesId());
48 if(pce == null) {
49 this.putFieldError("hrPyCalendarEntriesId", "timeSheetInit.payCalEntriesId.Invalid");
50 valid = false;
51 }
52
53 List<Assignment> activeAssignments = TkServiceLocator.getAssignmentService().getAssignmentsByCalEntryForTimeCalendar(timeInit.getPrincipalId(), pce);
54 if (activeAssignments.isEmpty()) {
55 this.putFieldError("principalId", "timeSheetInit.principalId.Assignments.Empty");
56 valid = false;
57 }
58
59 if(valid) {
60 this.createTimeSheetDocument(timeInit, pce);
61 }
62 return valid;
63 }
64
65 protected void createTimeSheetDocument(TimeSheetInitiate timeInit, CalendarEntries entries) {
66 try {
67 TimesheetDocument tsd = TkServiceLocator.getTimesheetService().openTimesheetDocument(timeInit.getPrincipalId(), entries);
68 if (tsd != null) {
69 timeInit.setDocumentId(tsd.getDocumentId());
70 }
71 } catch (WorkflowException e) {
72
73 e.printStackTrace();
74 }
75 }
76 }