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.kpme.tklm.leave.calendar.web;
17  
18  import java.util.LinkedHashMap;
19  import java.util.LinkedList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.joda.time.DateTime;
24  import org.json.simple.JSONValue;
25  import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.HrConstants;
28  import org.kuali.kpme.core.util.HrContext;
29  import org.kuali.kpme.core.workarea.WorkArea;
30  import org.kuali.kpme.tklm.common.TkConstants;
31  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
32  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
33  
34  public class LeaveActionFormUtils {
35  
36      /**
37       * This method will build the JSON data structure needed for calendar
38       * manipulation and processing on the client side. 
39       *
40       * @param leaveBlocks
41       * @return
42       */
43      public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) {
44  
45          if (leaveBlocks == null || leaveBlocks.size() == 0) {
46              return "";
47          }
48  
49          List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>();
50          String timezone = HrServiceLocator.getTimezoneService().getUserTimezone();
51  
52          for (LeaveBlock leaveBlock : leaveBlocks) {
53              Map<String, Object> LeaveBlockMap = new LinkedHashMap<String, Object>();
54              
55              if(leaveBlock.getBeginTimestamp() != null && leaveBlock.getEndTimestamp() != null) {
56  	            DateTime start = leaveBlock.getBeginDateTime();
57  	        	DateTime end = leaveBlock.getEndDateTime();
58  	        	LeaveBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
59  	            LeaveBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
60  	            LeaveBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
61  	            LeaveBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
62              }
63          		
64              WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(leaveBlock.getWorkArea(), leaveBlock.getLeaveLocalDate());
65              String workAreaDesc = workArea == null ? "" : workArea.getDescription();
66              // Roles
67              Boolean getAnyApprover = HrContext.isAnyApprover();
68              LeaveBlockMap.put("isApprover", getAnyApprover);
69             
70              LeaveBlockMap.put("documentId", leaveBlock.getDocumentId());
71              LeaveBlockMap.put("title", workAreaDesc);
72              DateTime dtLeaveDate = leaveBlock.getLeaveLocalDate().toDateTimeAtStartOfDay();
73              LeaveBlockMap.put("leaveDate", dtLeaveDate.toString(HrConstants.DT_BASIC_DATE_FORMAT));
74          		
75              LeaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId().toString());
76              LeaveBlockMap.put("timezone", timezone);
77              LeaveBlockMap.put("assignment", new AssignmentDescriptionKey(leaveBlock.getJobNumber(), leaveBlock.getWorkArea(), leaveBlock.getTask()).toAssignmentKeyString());
78              LeaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId() != null ? leaveBlock.getLmLeaveBlockId() : "");
79              LeaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
80              LeaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount());
81              LeaveBlockMap.put("description", leaveBlock.getDescription());
82              LeaveBlockMap.put("leaveBlockType", leaveBlock.getLeaveBlockType());
83              LeaveBlockMap.put("editable", leaveBlock.isEditable());
84              LeaveBlockMap.put("requestStatus", leaveBlock.getRequestStatus());
85              LeaveBlockMap.put("canTransfer", LmServiceLocator.getLMPermissionService().canTransferSSTOUsage(leaveBlock));
86              leaveBlockList.add(LeaveBlockMap);
87          }
88  
89          
90          return JSONValue.toJSONString(leaveBlockList);
91          
92      }
93  
94  }