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                WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(leaveBlock.getWorkArea(), leaveBlock.getLeaveDate());
058                String workAreaDesc = workArea == null ? "" : workArea.getDescription();
059                // Roles
060                Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
061                LeaveBlockMap.put("isApprover", isAnyApprover);
062               
063                LeaveBlockMap.put("documentId", leaveBlock.getDocumentId());
064                LeaveBlockMap.put("title", workAreaDesc);
065                DateTime dtLeaveDate = new DateTime(leaveBlock.getLeaveDate());
066                LeaveBlockMap.put("leaveDate", dtLeaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
067                LeaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId().toString());
068                LeaveBlockMap.put("timezone", timezone);
069                LeaveBlockMap.put("assignment", new AssignmentDescriptionKey(leaveBlock.getJobNumber(), leaveBlock.getWorkArea(), leaveBlock.getTask()).toAssignmentKeyString());
070                LeaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId() != null ? leaveBlock.getLmLeaveBlockId() : "");
071                LeaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
072                LeaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount());
073                LeaveBlockMap.put("description", leaveBlock.getDescription());
074                LeaveBlockMap.put("leaveBlockType", leaveBlock.getLeaveBlockType());
075                LeaveBlockMap.put("editable", leaveBlock.isEditable());
076                LeaveBlockMap.put("requestStatus", leaveBlock.getRequestStatus());
077                LeaveBlockMap.put("canTransfer", TkServiceLocator.getPermissionsService().canTransferSSTOUsage(leaveBlock));
078                leaveBlockList.add(LeaveBlockMap);
079            }
080    
081            
082            return JSONValue.toJSONString(leaveBlockList);
083            
084        }
085    
086    }