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 org.kuali.hr.time.calendar.Calendar;
019    import org.kuali.hr.time.calendar.CalendarEntries;
020    import org.kuali.hr.time.service.base.TkServiceLocator;
021    import org.kuali.hr.time.timesheet.TimeSheetInitiate;
022    import org.kuali.hr.time.timesheet.TimesheetDocument;
023    import org.kuali.rice.kew.api.exception.WorkflowException;
024    import org.kuali.rice.kns.document.MaintenanceDocument;
025    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
026    
027    public class TimeSheetInitiateValidation extends MaintenanceDocumentRuleBase {
028        @Override
029            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
030            boolean valid = true;
031            TimeSheetInitiate timeInit = (TimeSheetInitiate)this.getNewBo();
032            Calendar pc = TkServiceLocator.getCalendarService().getCalendarByGroup(timeInit.getPyCalendarGroup());
033            if(pc == null) {
034                    this.putFieldError("pyCalendarGroup", "timeSheetInit.payCalendar.Invalid");
035                    valid = false;
036            }
037            
038            CalendarEntries pce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(timeInit.getHrPyCalendarEntriesId());
039            if(pce == null) {
040                    this.putFieldError("hrPyCalendarEntriesId", "timeSheetInit.payCalEntriesId.Invalid");
041                    valid = false;
042            }
043            
044            if(valid) {
045                            this.createTimeSheetDocument(timeInit, pce);
046                    }
047            return valid;
048        }
049    
050        protected void createTimeSheetDocument(TimeSheetInitiate timeInit, CalendarEntries entries) {
051            try {
052                    TimesheetDocument tsd = TkServiceLocator.getTimesheetService().openTimesheetDocument(timeInit.getPrincipalId(), entries);
053                    if(tsd != null) {
054                            timeInit.setDocumentId(tsd.getDocumentId());
055                    }
056                    } catch (WorkflowException e) {
057                            // TODO Auto-generated catch block
058                            e.printStackTrace();
059                    }
060        }
061    }