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.sql.Timestamp;
019    import java.util.Calendar;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.module.cam.CamsConstants;
025    import org.kuali.kfs.module.cam.CamsPropertyConstants;
026    import org.kuali.kfs.module.cam.businessobject.Asset;
027    import org.kuali.kfs.module.cam.businessobject.AssetDepreciationConvention;
028    import org.kuali.kfs.module.cam.businessobject.AssetType;
029    import org.kuali.kfs.module.cam.document.service.AssetDateService;
030    import org.kuali.kfs.module.cam.document.service.AssetService;
031    import org.kuali.kfs.sys.KFSPropertyConstants;
032    import org.kuali.kfs.sys.businessobject.UniversityDate;
033    import org.kuali.kfs.sys.context.SpringContext;
034    import org.kuali.kfs.sys.service.UniversityDateService;
035    import org.kuali.rice.kns.exception.ValidationException;
036    import org.kuali.rice.kns.service.BusinessObjectService;
037    import org.kuali.rice.kns.service.DateTimeService;
038    import org.kuali.rice.kns.util.ObjectUtils;
039    
040    
041    public class AssetDateServiceImpl implements AssetDateService {
042    
043        private AssetService assetService;
044        private UniversityDateService universityDateService;
045        private DateTimeService dateTimeService;
046        private BusinessObjectService businessObjectService;
047    
048        /**
049         * @see org.kuali.kfs.module.cam.document.service.AssetDateService#checkAndUpdateFiscalYearAndPeriod(org.kuali.kfs.module.cam.businessobject.Asset,
050         *      org.kuali.kfs.module.cam.businessobject.Asset)
051         */
052        public void checkAndUpdateFiscalYearAndPeriod(Asset oldAsset, Asset newAsset) {
053            if (assetService.isInServiceDateChanged(oldAsset, newAsset)) {
054                Integer newPostingYear = null;
055                String newPostingPeriodCode = null;
056    
057                if (ObjectUtils.isNotNull(newAsset.getCapitalAssetInServiceDate())) {
058                    Map<String, Object> primaryKeys = new HashMap<String, Object>();
059                    primaryKeys.put(KFSPropertyConstants.UNIVERSITY_DATE, newAsset.getCapitalAssetInServiceDate());
060                    UniversityDate inServiceDate = (UniversityDate) businessObjectService.findByPrimaryKey(UniversityDate.class, primaryKeys);
061    
062                    if (ObjectUtils.isNotNull(inServiceDate)) {
063                        newPostingYear = inServiceDate.getUniversityFiscalYear();
064                        newPostingPeriodCode = inServiceDate.getUniversityFiscalAccountingPeriod();
065                    }
066                }
067                // if the user blank in-service date, posting year and period code will be blank also.
068                newAsset.setFinancialDocumentPostingYear(newPostingYear);
069                newAsset.setFinancialDocumentPostingPeriodCode(newPostingPeriodCode);
070            }
071        }
072    
073        /**
074         * @see org.kuali.module.cams.service.AssetDetailInformationService#checkAndUpdateLastInventoryDate(org.kuali.kfs.module.cam.businessobject.Asset,
075         *      org.kuali.kfs.module.cam.businessobject.Asset)
076         */
077        public void checkAndUpdateLastInventoryDate(Asset oldAsset, Asset newAsset) {
078            if (!StringUtils.equalsIgnoreCase(oldAsset.getCampusCode(), newAsset.getCampusCode()) || !StringUtils.equalsIgnoreCase(oldAsset.getBuildingCode(), newAsset.getBuildingCode()) || !StringUtils.equalsIgnoreCase(oldAsset.getBuildingRoomNumber(), newAsset.getBuildingRoomNumber()) || !StringUtils.equalsIgnoreCase(oldAsset.getBuildingSubRoomNumber(), newAsset.getBuildingSubRoomNumber()) || !StringUtils.equalsIgnoreCase(oldAsset.getCampusTagNumber(), newAsset.getCampusTagNumber())) {
079                Timestamp timestamp = new Timestamp(dateTimeService.getCurrentDate().getTime());
080                newAsset.setLastInventoryDate(timestamp);
081            }
082        }
083    
084        /**
085         * @see org.kuali.module.cams.service.AssetDetailInformationService#checkAndUpdateDepreciationDate(org.kuali.kfs.module.cam.businessobject.Asset,
086         *      org.kuali.kfs.module.cam.businessobject.Asset)
087         */
088        public void checkAndUpdateDepreciationDate(Asset oldAsset, Asset newAsset) {
089            if (assetService.isAssetTypeCodeChanged(oldAsset, newAsset) && assetService.isAssetDepreciableLifeLimitZero(newAsset)) {
090                // If Asset Type changed to Depreciable Life Limit to "0", set both In-service Date and Depreciation Date to NULL.
091                newAsset.setDepreciationDate(null);
092                newAsset.setCapitalAssetInServiceDate(null);
093            }
094            else if (assetService.isInServiceDateChanged(oldAsset, newAsset) || assetService.isFinancialObjectSubTypeCodeChanged(oldAsset, newAsset)) {
095                Map<String, String> primaryKeys = new HashMap<String, String>();
096                primaryKeys.put(CamsPropertyConstants.AssetDepreciationConvention.FINANCIAL_OBJECT_SUB_TYPE_CODE, newAsset.getFinancialObjectSubTypeCode());
097                AssetDepreciationConvention depreciationConvention = (AssetDepreciationConvention) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(AssetDepreciationConvention.class, primaryKeys);
098                newAsset.setDepreciationDate(computeDepreciationDate(newAsset.getCapitalAssetType(), depreciationConvention, newAsset.getCapitalAssetInServiceDate()));
099            }
100        }
101    
102    
103        /**
104         * @see org.kuali.kfs.module.cam.document.service.AssetDateService#computeDepreciationDate(org.kuali.kfs.module.cam.businessobject.AssetType,
105         *      org.kuali.kfs.module.cam.businessobject.AssetDepreciationConvention, java.sql.Date)
106         */
107        public java.sql.Date computeDepreciationDate(AssetType assetType, AssetDepreciationConvention depreciationConvention, java.sql.Date inServiceDate) {
108            java.sql.Date depreciationDate = null;
109            if (assetType.getDepreciableLifeLimit() != null && assetType.getDepreciableLifeLimit().intValue() != 0) {
110                if (depreciationConvention == null || CamsConstants.DepreciationConvention.CREATE_DATE.equalsIgnoreCase(depreciationConvention.getDepreciationConventionCode())) {
111                    depreciationDate = inServiceDate;
112                }
113                else {
114                    Integer fiscalYear = universityDateService.getFiscalYear(inServiceDate);
115                    if (fiscalYear == null) {
116                        throw new ValidationException("University Fiscal year is not defined for date - " + inServiceDate);
117                    }
118    
119                    java.sql.Date newInServiceFiscalYearStartDate = new java.sql.Date(universityDateService.getFirstDateOfFiscalYear(fiscalYear).getTime());
120                    String conventionCode = depreciationConvention.getDepreciationConventionCode();
121    
122                    if (CamsConstants.DepreciationConvention.FULL_YEAR.equalsIgnoreCase(conventionCode)) {
123                        depreciationDate = newInServiceFiscalYearStartDate;
124                    }
125                    else if (CamsConstants.DepreciationConvention.HALF_YEAR.equalsIgnoreCase(conventionCode)) {
126                        // Half year depreciation convention
127                        Calendar calendar = Calendar.getInstance();
128                        calendar.setTime(newInServiceFiscalYearStartDate);
129                        calendar.add(Calendar.MONTH, 6);
130                        depreciationDate = new java.sql.Date(calendar.getTimeInMillis());
131                    }
132    
133                }
134            }
135            return depreciationDate;
136        }
137    
138        public AssetService getAssetService() {
139            return assetService;
140        }
141    
142        public void setAssetService(AssetService assetService) {
143            this.assetService = assetService;
144        }
145    
146        public UniversityDateService getUniversityDateService() {
147            return universityDateService;
148        }
149    
150        public void setUniversityDateService(UniversityDateService universityDateService) {
151            this.universityDateService = universityDateService;
152        }
153    
154        /**
155         * Gets the dateTimeService attribute.
156         * 
157         * @return Returns the dateTimeService.
158         */
159        public DateTimeService getDateTimeService() {
160            return dateTimeService;
161        }
162    
163        /**
164         * Sets the dateTimeService attribute value.
165         * 
166         * @param dateTimeService The dateTimeService to set.
167         */
168        public void setDateTimeService(DateTimeService dateTimeService) {
169            this.dateTimeService = dateTimeService;
170        }
171    
172        /**
173         * Gets the businessObjectService attribute.
174         * 
175         * @return Returns the businessObjectService.
176         */
177        public BusinessObjectService getBusinessObjectService() {
178            return businessObjectService;
179        }
180    
181        /**
182         * Sets the businessObjectService attribute value.
183         * 
184         * @param businessObjectService The businessObjectService to set.
185         */
186        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
187            this.businessObjectService = businessObjectService;
188        }
189    
190    }