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.rules.shiftdifferential.shift;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.collections.MapUtils;
20  import org.joda.time.DateTimeZone;
21  import org.joda.time.Interval;
22  import org.kuali.kpme.core.util.HrConstants;
23  import org.kuali.kpme.core.util.TKUtils;
24  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
25  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlockContract;
26  import org.kuali.kpme.tklm.api.time.timehourdetail.TimeHourDetail;
27  import org.kuali.kpme.tklm.time.rules.shiftdifferential.ShiftDifferentialRule;
28  import org.kuali.kpme.tklm.time.rules.shiftdifferential.ruletype.ShiftDifferentialRuleType;
29  import org.kuali.kpme.tklm.time.rules.shiftdifferential.service.ShiftTypeService;
30  import org.kuali.kpme.tklm.time.rules.shiftdifferential.service.ShiftTypeServiceBase;
31  
32  import java.math.BigDecimal;
33  import java.util.ArrayList;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  import java.util.TreeSet;
39  
40  
41  public class Shift {
42      private Interval shiftInterval;
43      private ShiftDifferentialRule rule;
44      private Set<ShiftBlock> shiftBlocks = new TreeSet<>();
45      private Map<String, Interval> previousGapIntervals = new HashMap<>();
46      private Long totalShiftTime;
47      private DateTimeZone zone;
48  
49      private ShiftTypeService shiftTypeService;
50  
51  
52      public Shift(ShiftDifferentialRule rule, Interval shiftInterval, DateTimeZone zone) {
53          this.shiftInterval = shiftInterval;
54          this.rule = rule;
55          this.zone = zone;
56      }
57  
58      public Interval getShiftInterval() {
59          return shiftInterval;
60      }
61  
62      public void setShiftInterval(Interval shiftInterval) {
63          this.shiftInterval = shiftInterval;
64      }
65  
66      public ShiftDifferentialRule getRule() {
67          return rule;
68      }
69  
70      public void setRule(ShiftDifferentialRule rule) {
71          this.rule = rule;
72      }
73  
74      public Set<ShiftBlock> getShiftBlocks() {
75          return shiftBlocks;
76      }
77  
78      public void setShiftBlocks(Set<ShiftBlock> shiftBlocks) {
79          this.shiftBlocks = shiftBlocks;
80      }
81  
82      public void addShiftBlock(TimeBlock timeBlock) {
83          ShiftBlock sb = new ShiftBlock(timeBlock, rule, shiftInterval, zone);
84          getShiftBlocks().add(sb);
85      }
86  
87      public DateTimeZone getZone() {
88          return zone;
89      }
90  
91      public void setZone(DateTimeZone zone) {
92          this.zone = zone;
93      }
94  
95      public void processShift() {
96          getShiftTypeService().processShift(this);
97      }
98  
99      public Long getTotalShiftTime() {
100         if (this.totalShiftTime == null) {
101             processShift();
102         }
103         return totalShiftTime;
104     }
105 
106     public boolean exceedsMinHours() {
107         Long shiftDuration = getTotalShiftTime();
108         return rule.getMinHours().compareTo(TKUtils.convertMillisToHours(shiftDuration)) <= 0;
109     }
110 
111     public Long getNegativeAdjustmentTime() {
112         return getShiftTypeService().getNegativeAdjustmentTime(this);
113     }
114 
115     public Long getFullShiftPremiumTime() {
116         return getShiftTypeService().getFullShiftPremium(this);
117     }
118 
119     public Map<String, Interval> getPreviousGapIntervals() {
120         if (MapUtils.isEmpty(this.previousGapIntervals)) {
121             processShift();
122         }
123         return this.previousGapIntervals;
124     }
125 
126 
127     public void setPreviousGapIntervals(Map<String, Interval> previousGapIntervals) {
128         this.previousGapIntervals = previousGapIntervals;
129     }
130 
131     public void setTotalShiftTime(long totalShiftTime) {
132         this.totalShiftTime = totalShiftTime;
133     }
134 
135     public boolean exceedsMaxGap(Interval gapInterval, BigDecimal maxGap) {
136         if (gapInterval == null) {
137             return false;
138         }
139         BigDecimal gapMinutes = TKUtils.convertMillisToMinutes(gapInterval.toDurationMillis());
140 
141         return (gapMinutes.compareTo(maxGap) > 0);
142     }
143 
144     protected ShiftTypeService getShiftTypeService() {
145         if (shiftTypeService == null) {
146             if (rule.getRuleTypeObj() == null) {
147                 //fall back to base logic
148                 shiftTypeService = new ShiftTypeServiceBase();
149             } else {
150                 shiftTypeService = rule.getRuleTypeObj().getShiftTypeService();
151             }
152         }
153         return shiftTypeService;
154     }
155 }