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.leaveadjustment.service;
017    
018    import java.util.Date;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.hr.lm.LMConstants;
023    import org.kuali.hr.lm.leaveadjustment.LeaveAdjustment;
024    import org.kuali.hr.lm.leaveblock.LeaveBlock;
025    import org.kuali.hr.time.HrBusinessObject;
026    import org.kuali.hr.time.principal.PrincipalHRAttributes;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
029    import org.kuali.hr.time.util.TKContext;
030    import org.kuali.rice.kns.document.MaintenanceDocument;
031    
032    public class LeaveAdjustmentMaintainableServiceImpl extends HrBusinessObjectMaintainableImpl{
033    
034            private static final long serialVersionUID = 2500485054776792764L;
035    
036            @Override
037            public HrBusinessObject getObjectById(String id) {
038                    // TODO Auto-generated method stub
039                    return null;
040            }
041            
042            @Override
043            public void saveBusinessObject() {
044                    super.saveBusinessObject();
045                    
046                    // create leave block 
047                    LeaveAdjustment la = (LeaveAdjustment) this.getBusinessObject();
048                    LeaveBlock aLeaveBlock = new LeaveBlock();
049                    aLeaveBlock.setPrincipalId(la.getPrincipalId());
050                    aLeaveBlock.setLeaveDate(la.getEffectiveDate());
051                    aLeaveBlock.setEarnCode(la.getEarnCode());
052                    aLeaveBlock.setAccrualCategory(la.getAccrualCategory());
053                    aLeaveBlock.setDescription("Leave Adjustment");
054                    aLeaveBlock.setLeaveAmount(la.getAdjustmentAmount());   // can be negative or positive.  leave as is.
055                    aLeaveBlock.setAccrualGenerated(false);
056                    aLeaveBlock.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.LEAVE_ADJUSTMENT_MAINT);
057                    aLeaveBlock.setRequestStatus(LMConstants.REQUEST_STATUS.APPROVED);
058                    aLeaveBlock.setBlockId(0L);
059                    
060                    TkServiceLocator.getLeaveBlockService().saveLeaveBlock(aLeaveBlock, TKContext.getPrincipalId());                
061            }
062            
063            @SuppressWarnings("rawtypes")
064            @Override
065            public Map populateBusinessObject(Map<String, String> fieldValues,
066                            MaintenanceDocument maintenanceDocument, String methodToCall) {
067                    if (fieldValues.containsKey("principalId")
068                                    && StringUtils.isNotEmpty(fieldValues.get("principalId"))
069                                    && fieldValues.containsKey("effectiveDate")
070                                    && StringUtils.isNotEmpty(fieldValues.get("effectiveDate"))) {
071                            Date effDate = new Date(fieldValues.get("effectiveDate"));
072                            PrincipalHRAttributes principalHRAttrObj = TkServiceLocator.getPrincipalHRAttributeService()
073                                                    .getPrincipalCalendar(fieldValues.get("principalId"), effDate);
074                            String lpString = (principalHRAttrObj != null) ? principalHRAttrObj.getLeavePlan() : "";
075                            fieldValues.put("leavePlan", lpString);
076                    }
077                    return super.populateBusinessObject(fieldValues, maintenanceDocument,
078                                    methodToCall); 
079            }
080    
081    
082    }