View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.lm.leavecalendar.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.joda.time.format.ISODateTimeFormat;
21  import org.json.simple.JSONValue;
22  import org.kuali.hr.lm.leaveblock.LeaveBlock;
23  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
24  import org.kuali.hr.time.roles.TkUserRoles;
25  import org.kuali.hr.time.service.base.TkServiceLocator;
26  import org.kuali.hr.time.timeblock.TimeBlock;
27  import org.kuali.hr.time.timeblock.TimeHourDetail;
28  import org.kuali.hr.time.util.TKContext;
29  import org.kuali.hr.time.util.TKUtils;
30  import org.kuali.hr.time.util.TkConstants;
31  import org.kuali.hr.time.workarea.WorkArea;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  
34  import java.util.*;
35  
36  public class LeaveActionFormUtils {
37  
38      /**
39       * This method will build the JSON data structure needed for calendar
40       * manipulation and processing on the client side. 
41       *
42       * @param leaveBlocks
43       * @return
44       */
45      public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) {
46  
47          if (leaveBlocks == null || leaveBlocks.size() == 0) {
48              return "";
49          }
50  
51          List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>();
52          String timezone = TkServiceLocator.getTimezoneService().getUserTimezone();
53  
54          for (LeaveBlock leaveBlock : leaveBlocks) {
55              Map<String, Object> LeaveBlockMap = new LinkedHashMap<String, Object>();
56              
57              if(leaveBlock.getBeginTimestamp() != null && leaveBlock.getEndTimestamp() != null) {
58  	            DateTime start = new DateTime(leaveBlock.getBeginTimestamp().getTime());
59  	        	DateTime end = new DateTime(leaveBlock.getEndTimestamp().getTime());
60  	        	LeaveBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
61  	            LeaveBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
62  	            LeaveBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
63  	            LeaveBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
64              }
65          		
66              WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(leaveBlock.getWorkArea(), leaveBlock.getLeaveDate());
67              String workAreaDesc = workArea == null ? "" : workArea.getDescription();
68              // Roles
69              Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
70              LeaveBlockMap.put("isApprover", isAnyApprover);
71             
72              LeaveBlockMap.put("documentId", leaveBlock.getDocumentId());
73              LeaveBlockMap.put("title", workAreaDesc);
74              DateTime dtLeaveDate = new DateTime(leaveBlock.getLeaveDate());
75              LeaveBlockMap.put("leaveDate", dtLeaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
76          		
77              LeaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId().toString());
78              LeaveBlockMap.put("timezone", timezone);
79              LeaveBlockMap.put("assignment", new AssignmentDescriptionKey(leaveBlock.getJobNumber(), leaveBlock.getWorkArea(), leaveBlock.getTask()).toAssignmentKeyString());
80              LeaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId() != null ? leaveBlock.getLmLeaveBlockId() : "");
81              LeaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
82              LeaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount());
83              LeaveBlockMap.put("description", leaveBlock.getDescription());
84              LeaveBlockMap.put("leaveBlockType", leaveBlock.getLeaveBlockType());
85              LeaveBlockMap.put("editable", leaveBlock.isEditable());
86              LeaveBlockMap.put("requestStatus", leaveBlock.getRequestStatus());
87              LeaveBlockMap.put("canTransfer", TkServiceLocator.getPermissionsService().canTransferSSTOUsage(leaveBlock));
88              leaveBlockList.add(LeaveBlockMap);
89          }
90  
91          
92          return JSONValue.toJSONString(leaveBlockList);
93          
94      }
95  
96  }