001    /*
002     * Copyright 2011 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.kfs.module.cam.document.service.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.Comparator;
021    import java.util.List;
022    
023    import org.kuali.kfs.module.cam.CamsConstants;
024    import org.kuali.kfs.module.cam.businessobject.Asset;
025    import org.kuali.kfs.module.cam.businessobject.AssetRetirementGlobal;
026    import org.kuali.kfs.module.cam.businessobject.AssetRetirementGlobalDetail;
027    import org.kuali.kfs.module.cam.document.service.AssetService;
028    import org.kuali.kfs.module.cam.document.service.RetirementInfoService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.rice.kns.service.ParameterService;
031    
032    /**
033     * Implements RetirementInfoService, assists in identifying the latest retirement record
034     */
035    public class RetirementInfoServiceImpl implements RetirementInfoService {
036    
037        private ParameterService parameterService;
038        private AssetService assetService;
039    
040        /**
041         * @see org.kuali.kfs.module.cam.document.service.RetirementInfoService#setRetirementInfo(org.kuali.kfs.module.cam.businessobject.Asset)
042         */
043        public void setRetirementInfo(Asset asset) {
044            // If current status is not retired, return empty
045            if (!assetService.isAssetRetired(asset)) {
046                return;
047            }
048            List<AssetRetirementGlobalDetail> retirementHistory = asset.getAssetRetirementHistory();
049    
050            List<AssetRetirementGlobalDetail> sortableList = new ArrayList<AssetRetirementGlobalDetail>();
051    
052            for (AssetRetirementGlobalDetail assetRetirementGlobalDetail : retirementHistory) {
053                AssetRetirementGlobal assetRetirementGlobal = assetRetirementGlobalDetail.getAssetRetirementGlobal();
054                // check if document is approved
055                if (assetRetirementGlobal != null && isDocumentApproved(assetRetirementGlobal)) {
056                    sortableList.add(assetRetirementGlobalDetail);
057                }
058    
059            }
060    
061    
062            Comparator<AssetRetirementGlobalDetail> comparator = new Comparator<AssetRetirementGlobalDetail>() {
063                public int compare(AssetRetirementGlobalDetail o1, AssetRetirementGlobalDetail o2) {
064                    // sort descending based on retirement date
065                    return o2.getAssetRetirementGlobal().getRetirementDate().compareTo(o1.getAssetRetirementGlobal().getRetirementDate());
066                }
067            };
068            Collections.sort(sortableList, comparator);
069    
070            if (!sortableList.isEmpty()) {
071                asset.setRetirementInfo(sortableList.get(0));
072            }
073        }
074    
075    
076        /**
077         * Checks asset retirement document status, if approved returns true
078         * 
079         * @param assetRetirementDoc Asset Retirement Document
080         * @return "true" if approved
081         */
082        protected boolean isDocumentApproved(AssetRetirementGlobal assetRetirementDoc) {
083            assetRetirementDoc.refreshReferenceObject(CamsConstants.AssetRetirementGlobal.DOCUMENT_HEADER);
084            if (assetRetirementDoc.getDocumentHeader() != null && KFSConstants.DocumentStatusCodes.APPROVED.equals(assetRetirementDoc.getDocumentHeader().getFinancialDocumentStatusCode())) {
085                return true;
086            }
087            return false;
088        }
089    
090    
091        public ParameterService getParameterService() {
092            return parameterService;
093        }
094    
095    
096        public void setParameterService(ParameterService parameterService) {
097            this.parameterService = parameterService;
098        }
099    
100    
101        public AssetService getAssetService() {
102            return assetService;
103        }
104    
105    
106        public void setAssetService(AssetService assetService) {
107            this.assetService = assetService;
108        }
109    
110    
111        public void setMergeHistory(Asset asset) {
112            List<AssetRetirementGlobal> retirementGlobals = asset.getRetirementGlobals();
113            List<AssetRetirementGlobalDetail> mergeHistory = new ArrayList<AssetRetirementGlobalDetail>();
114    
115            for (AssetRetirementGlobal retirementGlobal : retirementGlobals) {
116                if (CamsConstants.AssetRetirementReasonCode.MERGED.equalsIgnoreCase(retirementGlobal.getRetirementReasonCode())) {
117                    List<AssetRetirementGlobalDetail> retirementDetails = retirementGlobal.getAssetRetirementGlobalDetails();
118    
119                    for (AssetRetirementGlobalDetail assetRetirementGlobalDetail : retirementDetails) {
120                        mergeHistory.add(assetRetirementGlobalDetail);
121                    }
122                }
123            }
124    
125            if (!mergeHistory.isEmpty()) {
126                asset.setMergeHistory(mergeHistory);
127            }
128        }
129    }