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.businessobject.inquiry;
017    
018    import java.util.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    import java.util.Properties;
022    
023    import org.kuali.kfs.coa.businessobject.Organization;
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.document.service.AssetLocationService;
028    import org.kuali.kfs.module.cam.document.service.AssetService;
029    import org.kuali.kfs.module.cam.document.service.EquipmentLoanOrReturnService;
030    import org.kuali.kfs.module.cam.document.service.PaymentSummaryService;
031    import org.kuali.kfs.module.cam.document.service.RetirementInfoService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.KFSPropertyConstants;
034    import org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl;
035    import org.kuali.kfs.sys.context.SpringContext;
036    import org.kuali.rice.kns.bo.BusinessObject;
037    import org.kuali.rice.kns.lookup.HtmlData;
038    import org.kuali.rice.kns.util.KNSConstants;
039    import org.kuali.rice.kns.util.ObjectUtils;
040    import org.kuali.rice.kns.util.UrlFactory;
041    import org.kuali.rice.kns.web.ui.Section;
042    
043    public class AssetInquirableImpl extends KfsInquirableImpl {
044        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AssetInquirableImpl.class);
045    
046        /**
047         * Executes service methods to populate appropriate data in the Asset BO.
048         * 
049         * @see org.kuali.rice.kns.inquiry.KualiInquirableImpl#getBusinessObject(java.util.Map)
050         */
051        @Override
052        public BusinessObject getBusinessObject(Map fieldValues) {
053            Asset asset = (Asset) super.getBusinessObject(fieldValues);
054    
055            if (ObjectUtils.isNotNull(asset)) {
056                // Identifies the latest location information
057                AssetLocationService assetlocationService = SpringContext.getBean(AssetLocationService.class);
058                assetlocationService.setOffCampusLocation(asset);
059    
060                // Calculates payment summary and depreciation summary based on available payment records
061                PaymentSummaryService paymentSummaryService = SpringContext.getBean(PaymentSummaryService.class);
062                paymentSummaryService.calculateAndSetPaymentSummary(asset);
063    
064                // Identifies the merge history and separation history based on asset disposition records
065                AssetService assetService = SpringContext.getBean(AssetService.class);
066                assetService.setSeparateHistory(asset);
067    
068                // Finds out the latest retirement info, is asset is currently retired.
069                RetirementInfoService retirementInfoService = SpringContext.getBean(RetirementInfoService.class);
070                retirementInfoService.setRetirementInfo(asset);
071                retirementInfoService.setMergeHistory(asset);
072    
073                // Finds out the latest equipment loan or return information if available
074                EquipmentLoanOrReturnService equipmentLoanOrReturnService = SpringContext.getBean(EquipmentLoanOrReturnService.class);
075                equipmentLoanOrReturnService.setEquipmentLoanInfo(asset);
076            }
077    
078            return asset;
079        }
080    
081        /**
082         * Hide payments if there are more then the allowable number.
083         * 
084         * @see org.kuali.rice.kns.inquiry.KualiInquirableImpl#getSections(org.kuali.rice.kns.bo.BusinessObject)
085         */
086        @Override
087        public List<Section> getSections(BusinessObject businessObject) {
088            List<Section> sections = super.getSections(businessObject);
089    
090            // sectionToRemove is hoky but it looks like that section.setHidden doesn't work on inquirable. And to avoid
091            // ConcurrentModificationException we do this
092            Section sectionToRemove = null;
093    
094            Asset asset = (Asset) businessObject;
095            for (Section section : sections) {
096                if (CamsConstants.Asset.SECTION_ID_PAYMENT_INFORMATION.equals(section.getSectionId()) && asset.getAssetPayments().size() > CamsConstants.Asset.ASSET_MAXIMUM_NUMBER_OF_PAYMENT_DISPLAY) {
097                    // Hide the payment section if there are more then CamsConstants.ASSET_MAXIMUM_NUMBER_OF_PAYMENT_DISPLAY
098                    // section.setHidden(true);
099                    sectionToRemove = section;
100                }
101            }
102    
103            if (sectionToRemove != null) {
104                sections.remove(sectionToRemove);
105            }
106            return sections;
107        }
108    
109        @Override
110        public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
111            if (CamsPropertyConstants.Asset.ORGANIZATION_CODE.equals(attributeName) && businessObject instanceof Asset) {
112                Asset asset = (Asset) businessObject;
113                Properties parameters = new Properties();
114                parameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.START_METHOD);
115                parameters.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, Organization.class.getName());
116                parameters.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, asset.getOrganizationOwnerAccount().getChartOfAccountsCode());
117                parameters.put(KFSPropertyConstants.ORGANIZATION_CODE, asset.getOrganizationOwnerAccount().getOrganizationCode());
118    
119                Map<String, String> fieldList = new HashMap<String, String>();
120                fieldList.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, asset.getOrganizationOwnerAccount().getChartOfAccountsCode());
121                fieldList.put(KFSPropertyConstants.ORGANIZATION_CODE, asset.getOrganizationOwnerAccount().getOrganizationCode());
122                return getHyperLink(Organization.class, fieldList, UrlFactory.parameterizeUrl(KNSConstants.INQUIRY_ACTION, parameters));
123            }
124            return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
125        }
126    }