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.timesheet;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.joda.time.DateTime;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.api.assignment.Assignment;
23  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
24  import org.kuali.kpme.core.api.job.Job;
25  import org.kuali.kpme.core.api.namespace.KPMENamespace;
26  import org.kuali.kpme.core.document.calendar.CalendarDocument;
27  import org.kuali.kpme.core.role.KPMERole;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.TKUtils;
30  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
31  import org.kuali.kpme.tklm.api.time.timesheet.TimesheetDocumentContract;
32  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
33  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
34  import org.kuali.kpme.tklm.time.timesummary.TimeSummary;
35  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  
38  import java.util.*;
39  
40  
41  public class TimesheetDocument extends CalendarDocument implements TimesheetDocumentContract {
42  
43  	private static final long serialVersionUID = 405213065253263185L;
44  
45  	/**
46  	 * This static member is needed by document search, to trigger the correct calendar document
47  	 * opening when clicking on a doc id link in the search results.
48  	 * It is distinguished from "HrConstants.LEAVE_CALENDAR_TYPE".
49  	 */
50  	public static final String TIMESHEET_DOCUMENT_TYPE = "TimesheetDocument";
51  
52  	private List<Job> jobs = new LinkedList<Job>();
53  	private List<TimeBlock> timeBlocks = new LinkedList<TimeBlock>();
54  	private Map<Long, Job> jobNumberToJobMap = new HashMap<Long,Job>();
55      private TimesheetDocumentHeader documentHeader;
56  
57  	public TimesheetDocument(TimesheetDocumentHeader documentHeader) {
58  		this.documentHeader = documentHeader;
59  		setCalendarType(TIMESHEET_DOCUMENT_TYPE);
60  	}
61  
62  	@Override
63  	public TimesheetDocumentHeader getDocumentHeader() {
64  		return documentHeader;
65  	}
66  
67  	public void setDocumentHeader(TimesheetDocumentHeader documentHeader) {
68  		this.documentHeader = documentHeader;
69  	}
70  
71  	public List<Job> getJobs() {
72  		return jobs;
73  	}
74  
75  	public void setJobs(List<Job> jobs) {
76  		this.jobs = jobs;
77  		jobNumberToJobMap.clear();
78  		if(jobs!=null){
79  			for(Job job : jobs){
80  				jobNumberToJobMap.put(job.getJobNumber(), job);
81  			}
82  		}
83  	}
84  
85  	public List<TimeBlock> getTimeBlocks() {
86  		return timeBlocks;
87  	}
88  
89  	public void setTimeBlocks(List<TimeBlock> timeBlocks) {
90  		this.timeBlocks = timeBlocks;
91  	}
92  
93  	public TimeSummary getTimeSummary() {
94          return (TimeSummary)TkServiceLocator.getTimeSummaryService().getTimeSummary(getPrincipalId(), getTimeBlocks(), getCalendarEntry(), getAssignmentMap());
95  	}
96  
97      @Override
98  	public String getPrincipalId(){
99  		return getDocumentHeader() == null ? null : getDocumentHeader().getPrincipalId();
100 	}
101 
102 	public Job getJob(Long jobNumber){
103 		return jobNumberToJobMap.get(jobNumber);
104 	}
105 
106 
107 
108     @Override
109 	public String getDocumentId(){
110 		return getDocumentHeader() == null ? null : getDocumentHeader().getDocumentId();
111 	}
112 
113     public Map<String, List<LocalDate>> getEarnCodeMap() {
114         Map<String, List<LocalDate>> earnCodeMap = new HashMap<String, List<LocalDate>>();
115         for(TimeBlock tb : getTimeBlocks()) {
116             if(!earnCodeMap.containsKey(tb.getEarnCode())) {
117                 List<LocalDate> lst = new ArrayList<LocalDate>();
118                 lst.add(tb.getBeginDateTime().toLocalDate());
119                 earnCodeMap.put(tb.getEarnCode(), lst);
120             } else {
121                 earnCodeMap.get(tb.getEarnCode()).add(tb.getBeginDateTime().toLocalDate());
122             }
123         }
124         return earnCodeMap;
125     }
126 	
127     public Map<String, String> getAssignmentDescriptions(boolean clockOnlyAssignments, LocalDate date) {
128         Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
129         List<Assignment> dayAssignments = getAssignmentMap().get(date);
130         if (CollectionUtils.isNotEmpty(dayAssignments)) {
131             for (Assignment assignment : dayAssignments) {
132                 String principalId = GlobalVariables.getUserSession().getPrincipalId();
133 
134                 if (HrServiceLocator.getHRPermissionService().canViewCalendarDocumentAssignment(principalId, this, assignment)) {
135                     TimeCollectionRule tcr = null;
136                     if (assignment.getJob() != null) {
137                         tcr = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(), assignment.getGroupKeyCode(), LocalDate.now());
138                     }
139                     boolean isSynchronous = tcr == null || tcr.isClockUserFl();
140                     if (!clockOnlyAssignments || isSynchronous) {
141                         assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
142                     }
143                 }
144             }
145         }
146         
147         return assignmentDescriptions;
148     }
149     
150     public Map<String, String> getAssignmentDescriptionsOfApprovals(boolean clockOnlyAssignments) {
151         Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
152         List<Assignment> allAssignments = getAllAssignments();
153         for (Assignment assignment : allAssignments) {
154         	String principalId = GlobalVariables.getUserSession().getPrincipalId();
155 
156         	if (HrServiceLocator.getHRPermissionService().canViewCalendarDocumentAssignment(principalId, this, assignment)) {
157         		TimeCollectionRule tcr = null;
158         		if(assignment.getJob() != null)
159         			tcr = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(), assignment.getGroupKeyCode(), LocalDate.now());
160         		boolean isSynchronous = tcr == null || tcr.isClockUserFl();
161                 if (!clockOnlyAssignments || isSynchronous) {
162     				Long workArea = assignment.getWorkArea();
163                     String dept = assignment.getJob() == null ? StringUtils.EMPTY : assignment.getJob().getDept();
164                     String groupKeyCode = assignment.getGroupKeyCode();
165         			DateTime startOfToday = LocalDate.now().toDateTimeAtStartOfDay();
166                     boolean isApproverOrReviewerForCurrentAssignment =
167                             HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), workArea, startOfToday)
168         					|| HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), workArea, startOfToday)
169         					|| HrServiceLocator.getKPMERoleService().principalHasRoleInWorkArea(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName(), workArea, startOfToday)
170                             || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName(), dept, groupKeyCode, startOfToday)
171                             || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName(), dept, groupKeyCode, startOfToday);
172                     if(isApproverOrReviewerForCurrentAssignment) {
173                     	assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
174                     }
175                 }
176             }
177         }
178         
179         return assignmentDescriptions;
180     }
181 }