001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leavecalendar.web;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.joda.time.DateTime;
020    import org.joda.time.format.ISODateTimeFormat;
021    import org.json.simple.JSONValue;
022    import org.kuali.hr.lm.leaveblock.LeaveBlock;
023    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
024    import org.kuali.hr.time.roles.TkUserRoles;
025    import org.kuali.hr.time.service.base.TkServiceLocator;
026    import org.kuali.hr.time.timeblock.TimeBlock;
027    import org.kuali.hr.time.timeblock.TimeHourDetail;
028    import org.kuali.hr.time.util.TKContext;
029    import org.kuali.hr.time.util.TKUtils;
030    import org.kuali.hr.time.util.TkConstants;
031    import org.kuali.hr.time.workarea.WorkArea;
032    import org.kuali.rice.krad.util.GlobalVariables;
033    
034    import java.util.*;
035    
036    public class LeaveActionFormUtils {
037    
038        /**
039         * This method will build the JSON data structure needed for calendar
040         * manipulation and processing on the client side. 
041         *
042         * @param leaveBlocks
043         * @return
044         */
045        public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) {
046    
047            if (leaveBlocks == null || leaveBlocks.size() == 0) {
048                return "";
049            }
050    
051            List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>();
052            String timezone = TkServiceLocator.getTimezoneService().getUserTimezone();
053    
054            for (LeaveBlock leaveBlock : leaveBlocks) {
055                Map<String, Object> LeaveBlockMap = new LinkedHashMap<String, Object>();
056                
057                if(leaveBlock.getBeginTimestamp() != null && leaveBlock.getEndTimestamp() != null) {
058                        DateTime start = new DateTime(leaveBlock.getBeginTimestamp().getTime());
059                            DateTime end = new DateTime(leaveBlock.getEndTimestamp().getTime());
060                            LeaveBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
061                        LeaveBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
062                        LeaveBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
063                        LeaveBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
064                }
065                            
066                WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(leaveBlock.getWorkArea(), leaveBlock.getLeaveDate());
067                String workAreaDesc = workArea == null ? "" : workArea.getDescription();
068                // Roles
069                Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
070                LeaveBlockMap.put("isApprover", isAnyApprover);
071               
072                LeaveBlockMap.put("documentId", leaveBlock.getDocumentId());
073                LeaveBlockMap.put("title", workAreaDesc);
074                DateTime dtLeaveDate = new DateTime(leaveBlock.getLeaveDate());
075                LeaveBlockMap.put("leaveDate", dtLeaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
076                            
077                LeaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId().toString());
078                LeaveBlockMap.put("timezone", timezone);
079                LeaveBlockMap.put("assignment", new AssignmentDescriptionKey(leaveBlock.getJobNumber(), leaveBlock.getWorkArea(), leaveBlock.getTask()).toAssignmentKeyString());
080                LeaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId() != null ? leaveBlock.getLmLeaveBlockId() : "");
081                LeaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
082                LeaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount());
083                LeaveBlockMap.put("description", leaveBlock.getDescription());
084                LeaveBlockMap.put("leaveBlockType", leaveBlock.getLeaveBlockType());
085                LeaveBlockMap.put("editable", leaveBlock.isEditable());
086                LeaveBlockMap.put("requestStatus", leaveBlock.getRequestStatus());
087                LeaveBlockMap.put("canTransfer", TkServiceLocator.getPermissionsService().canTransferSSTOUsage(leaveBlock));
088                leaveBlockList.add(LeaveBlockMap);
089            }
090    
091            
092            return JSONValue.toJSONString(leaveBlockList);
093            
094        }
095    
096    }