001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.timesheet.validation;
017
018 import java.util.List;
019
020 import org.kuali.hr.time.assignment.Assignment;
021 import org.kuali.hr.time.calendar.Calendar;
022 import org.kuali.hr.time.calendar.CalendarEntries;
023 import org.kuali.hr.time.service.base.TkServiceLocator;
024 import org.kuali.hr.time.timesheet.TimeSheetInitiate;
025 import org.kuali.hr.time.timesheet.TimesheetDocument;
026 import org.kuali.hr.time.util.TkConstants;
027 import org.kuali.rice.kew.api.exception.WorkflowException;
028 import org.kuali.rice.kns.document.MaintenanceDocument;
029 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
030
031 public class TimeSheetInitiateValidation extends MaintenanceDocumentRuleBase {
032 @Override
033 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
034 boolean valid = true;
035 TimeSheetInitiate timeInit = (TimeSheetInitiate)this.getNewBo();
036 Calendar pc = TkServiceLocator.getCalendarService().getCalendarByGroup(timeInit.getPyCalendarGroup());
037 if(pc == null) {
038 this.putFieldError("pyCalendarGroup", "timeSheetInit.payCalendar.Invalid");
039 valid = false;
040 } else {
041 if(pc.getCalendarTypes() != null && !pc.getCalendarTypes().equalsIgnoreCase(TkConstants.CALENDAR_TYPE_PAY)) {
042 this.putFieldError("pyCalendarGroup", "timeSheetInit.payCalendar.Invalid");
043 valid = false;
044 }
045 }
046
047 CalendarEntries pce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(timeInit.getHrPyCalendarEntriesId());
048 if(pce == null) {
049 this.putFieldError("hrPyCalendarEntriesId", "timeSheetInit.payCalEntriesId.Invalid");
050 valid = false;
051 }
052
053 List<Assignment> activeAssignments = TkServiceLocator.getAssignmentService().getAssignmentsByCalEntryForTimeCalendar(timeInit.getPrincipalId(), pce);
054 if (activeAssignments.isEmpty()) {
055 this.putFieldError("principalId", "timeSheetInit.principalId.Assignments.Empty");
056 valid = false;
057 }
058
059 if(valid) {
060 this.createTimeSheetDocument(timeInit, pce);
061 }
062 return valid;
063 }
064
065 protected void createTimeSheetDocument(TimeSheetInitiate timeInit, CalendarEntries entries) {
066 try {
067 TimesheetDocument tsd = TkServiceLocator.getTimesheetService().openTimesheetDocument(timeInit.getPrincipalId(), entries);
068 if (tsd != null) {
069 timeInit.setDocumentId(tsd.getDocumentId());
070 }
071 } catch (WorkflowException e) {
072 // TODO Auto-generated catch block
073 e.printStackTrace();
074 }
075 }
076 }