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.timeblock.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.joda.time.LocalDate;
20  import org.kuali.kpme.core.api.task.TaskContract;
21  import org.kuali.kpme.core.api.workarea.WorkArea;
22  import org.kuali.kpme.core.service.HrServiceLocator;
23  import org.kuali.kpme.core.util.HrConstants;
24  import org.kuali.kpme.core.util.HrContext;
25  import org.kuali.kpme.tklm.api.common.TkConstants;
26  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
27  import org.kuali.kpme.tklm.api.time.timeblock.web.TimeBlockRendererContract;
28  import org.kuali.kpme.tklm.api.time.timehourdetail.TimeHourDetail;
29  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
30  import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetailRenderer;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * Render helper to handle timeblock and time hour details display
37   */
38  public class TimeBlockRenderer implements TimeBlockRendererContract {
39  
40      private TimeBlock timeBlock;
41      private List<TimeHourDetailRenderer> detailRenderers = new ArrayList<TimeHourDetailRenderer>();
42      private String assignmentClass;
43      private String lunchLabel;
44      private String lunchLabelId;
45  
46      public TimeBlockRenderer(TimeBlock b) {
47          this.timeBlock = b;
48          for (TimeHourDetail detail : timeBlock.getTimeHourDetails()) {
49              detailRenderers.add(new TimeHourDetailRenderer(detail));
50          }
51      }
52  
53      public List<TimeHourDetailRenderer> getDetailRenderers() {
54          return detailRenderers;
55      }
56  
57      public TimeBlock getTimeBlock() {
58          return timeBlock;
59      }
60  
61      public String getTimeRange() {
62          StringBuilder b = new StringBuilder();
63          if(StringUtils.equals(timeBlock.getEarnCodeType(), HrConstants.EARN_CODE_TIME)) {
64              b.append(timeBlock.getBeginTimeDisplay().toString(TkConstants.DT_BASIC_TIME_FORMAT));
65              b.append(" - ");
66              b.append(timeBlock.getEndTimeDisplay().toString(TkConstants.DT_BASIC_TIME_FORMAT));
67          }
68  
69          return b.toString();
70      }
71  
72      public String getTitle() {
73          StringBuilder b = new StringBuilder();
74  
75          WorkArea wa = HrServiceLocator.getWorkAreaService().getWorkArea(timeBlock.getWorkArea(), LocalDate.now());
76          if(wa != null) {
77          	b.append(wa.getDescription());
78          }
79          TaskContract task = HrServiceLocator.getTaskService().getTask(timeBlock.getTask(), timeBlock.getBeginDateTime().toLocalDate());
80          if(task != null) {
81          	// do not display task description if the task is the default one
82          	// default task is created in getTask() of TaskService
83          	if(!task.getDescription().equals(HrConstants.TASK_DEFAULT_DESP)) {
84          		b.append("-" + task.getDescription());
85          	}
86          }
87          return b.toString();
88      }
89  
90      public String getAmount() {
91      	if(StringUtils.equals(timeBlock.getEarnCodeType(), HrConstants.EARN_CODE_AMOUNT)) {
92      		if(timeBlock.getAmount() != null) {
93      			return timeBlock.getEarnCode() + ": $" + timeBlock.getAmount().toString();
94      		} else {
95      			return timeBlock.getEarnCode() + ": $0.00";
96      		}
97  	    }
98      	return "";
99      }
100 
101     public String getEarnCodeType() {
102     	return timeBlock.getEarnCodeType();
103     }
104 
105 	public String getAssignmentClass() {
106 		return assignmentClass;
107 	}
108 
109 	public void setAssignmentClass(String assignmentClass) {
110 		this.assignmentClass = assignmentClass;
111 	}
112 
113 	public String getLunchLabel() {
114 		return lunchLabel;
115 	}
116 
117 	public void setLunchLabel(String lunchLabel) {
118 		this.lunchLabel = lunchLabel;
119 	}
120 
121     public String getLunchLabelId() {
122         return lunchLabelId;
123     }
124 
125     public void setLunchLabelId(String lunchLabelId) {
126         this.lunchLabelId = lunchLabelId;
127     }
128 
129     public boolean isTimeBlockEditable() {
130         return TkServiceLocator.getTKPermissionService().canEditTimeBlock(HrContext.getPrincipalId(), timeBlock);
131     }
132 
133     public boolean isDeletable() {
134         return TkServiceLocator.getTKPermissionService().canDeleteTimeBlock(HrContext.getPrincipalId(), timeBlock);
135     }
136 
137     public boolean isOvertimeEditable() {
138         return TkServiceLocator.getTKPermissionService().canEditOvertimeEarnCode(HrContext.getPrincipalId(), timeBlock);
139     }
140 }