View Javadoc

1   /**
2    * Copyright 2004-2013 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.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  			// TODO Auto-generated catch block
73  			e.printStackTrace();
74  		}
75      }
76  }