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.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Properties;
023    
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.AssetPayment;
028    import org.kuali.kfs.module.cam.document.authorization.AssetAuthorizer;
029    import org.kuali.kfs.module.cam.document.authorization.AssetPaymentDocumentAuthorizer;
030    import org.kuali.kfs.sys.KFSConstants;
031    import org.kuali.kfs.sys.KFSPropertyConstants;
032    import org.kuali.kfs.sys.context.SpringContext;
033    import org.kuali.kfs.sys.document.authorization.FinancialSystemMaintenanceDocumentAuthorizerBase;
034    import org.kuali.rice.kns.bo.BusinessObject;
035    import org.kuali.rice.kns.lookup.HtmlData;
036    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
037    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
038    import org.kuali.rice.kns.service.BusinessObjectService;
039    import org.kuali.rice.kns.service.DocumentHelperService;
040    import org.kuali.rice.kns.util.GlobalVariables;
041    import org.kuali.rice.kns.util.KNSConstants;
042    import org.kuali.rice.kns.util.ObjectUtils;
043    import org.kuali.rice.kns.util.UrlFactory;
044    
045    /**
046     * This class overrides the base getActionUrls method for Asset Payment. Even though it's a payment lookup screen we are maintaining
047     * assets.
048     */
049    public class AssetPaymentLookupableHelperServiceImpl extends AssetLookupableHelperServiceImpl {
050        
051        private BusinessObjectService businessObjectService;
052    
053        /**
054         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject,
055         *      List pkNames)
056         */
057        @Override
058        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
059            AssetPayment assetPayment = (AssetPayment) businessObject;
060    
061            // Same thing but we're maintaining assets, not asset payments.
062            Map<String, Object> primaryKeys = new HashMap<String, Object>();
063            primaryKeys.put(CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER, assetPayment.getCapitalAssetNumber());
064            Asset asset = (Asset) businessObjectService.findByPrimaryKey(Asset.class, primaryKeys);
065    
066            List assetPrimaryKey = new ArrayList();
067            assetPrimaryKey.add(CamsPropertyConstants.Asset.CAPITAL_ASSET_NUMBER);
068    
069            List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
070            
071            // For retired asset, all action link will be hidden.
072            if (assetService.isAssetRetired(asset)) {
073                anchorHtmlDataList.add(super.getViewAssetUrl(asset));
074            }
075            else {
076                anchorHtmlDataList.add(this.getAssetUrl(asset));
077                anchorHtmlDataList.add(super.getLoanUrl(asset));
078                anchorHtmlDataList.add(super.getMergeUrl(asset));
079                anchorHtmlDataList.add(this.getSeparateUrl(assetPayment));
080                anchorHtmlDataList.add(super.getTransferUrl(asset));
081                anchorHtmlDataList.add(this.getPaymentUrl(asset));
082            }
083            return anchorHtmlDataList;
084        }
085    
086        /**
087         * Returns the url for any drill down links within the lookup. Override so that documentNumber is not an inquiry if it doesn't exist.
088         * @see org.kuali.rice.kns.lookup.Lookupable#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject, java.lang.String)
089         */
090        @Override
091        public HtmlData getInquiryUrl(BusinessObject businessObject, String propertyName) {
092            if (KFSPropertyConstants.DOCUMENT_NUMBER.equals(propertyName)) {
093                AssetPayment assetPayment = (AssetPayment) businessObject;
094                
095                if (ObjectUtils.isNull(assetPayment.getDocumentHeader())) {
096                    // If the document isn't found, don't show the inquirable
097                    return new AnchorHtmlData(KFSConstants.EMPTY_STRING, KFSConstants.EMPTY_STRING);
098                }
099            }
100            return super.getInquiryUrl(businessObject, propertyName);
101        }
102    
103        protected HtmlData getPaymentUrl(Asset asset) {
104            AssetPaymentDocumentAuthorizer assetPaymentAuhorizer =new AssetPaymentDocumentAuthorizer();
105            boolean isAuhtorize = assetPaymentAuhorizer.canInitiate(CamsConstants.DocumentTypeName.ASSET_PAYMENT, GlobalVariables.getUserSession().getPerson());
106            
107            if (assetService.isCapitalAsset(asset) && isAuhtorize) {
108                Properties parameters = new Properties();
109                parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.DOC_HANDLER_METHOD);
110                parameters.put(CamsPropertyConstants.AssetPaymentDocument.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
111                parameters.put(KFSConstants.PARAMETER_COMMAND, "initiate");
112                parameters.put(KFSConstants.DOCUMENT_TYPE_NAME, CamsConstants.DocumentTypeName.ASSET_PAYMENT);
113    
114                String href = UrlFactory.parameterizeUrl(CamsConstants.StrutsActions.ONE_UP + CamsConstants.StrutsActions.PAYMENT, parameters);
115    
116                return new AnchorHtmlData(href, KNSConstants.DOC_HANDLER_METHOD, CamsConstants.AssetActions.PAYMENT);
117            } else {
118                return new AnchorHtmlData("", "", "");
119            }
120        }
121    
122        
123        protected HtmlData getSeparateUrl(AssetPayment assetPayment) {
124            Asset asset = assetPayment.getAsset();
125            
126            FinancialSystemMaintenanceDocumentAuthorizerBase documentAuthorizer = (FinancialSystemMaintenanceDocumentAuthorizerBase) SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(CamsConstants.DocumentTypeName.ASSET_ADD_GLOBAL);
127            boolean isAuthorized = documentAuthorizer.isAuthorized(asset, CamsConstants.CAM_MODULE_CODE, CamsConstants.PermissionNames.SEPARATE, GlobalVariables.getUserSession().getPerson().getPrincipalId());
128    
129            if (isAuthorized) {
130                Properties parameters = getSeparateParameters(assetPayment.getAsset());
131                parameters.put(CamsPropertyConstants.AssetGlobal.SEPERATE_SOURCE_PAYMENT_SEQUENCE_NUMBER, assetPayment.getPaymentSequenceNumber().toString());
132                String href = UrlFactory.parameterizeUrl(KFSConstants.MAINTENANCE_ACTION, parameters);
133                return new AnchorHtmlData(href, KFSConstants.MAINTENANCE_NEW_METHOD_TO_CALL, CamsConstants.AssetActions.SEPARATE);
134            } else {
135                    return new AnchorHtmlData("", "", "");
136                }
137            }
138            
139        protected HtmlData getAssetUrl(Asset asset) {
140            AssetAuthorizer assetAuthorizer = new AssetAuthorizer();
141            boolean isAuthorized = assetAuthorizer.canMaintain(asset, GlobalVariables.getUserSession().getPerson());
142            
143            if (isAuthorized) {
144                Properties parameters = new Properties();            
145                parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
146                parameters.put(CamsPropertyConstants.AssetPaymentDocument.CAPITAL_ASSET_NUMBER, asset.getCapitalAssetNumber().toString());
147                parameters.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, Asset.class.getName());
148        
149                String href = UrlFactory.parameterizeUrl(KFSConstants.MAINTENANCE_ACTION, parameters);
150                return new AnchorHtmlData(href, KFSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, KFSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
151            } else {
152                return new AnchorHtmlData("", "", "");
153            }
154        }
155    
156        /**
157         * Gets the businessObjectService attribute.
158         * 
159         * @return Returns the businessObjectService.
160         */
161        public BusinessObjectService getBusinessObjectService() {
162            return businessObjectService;
163        }
164    
165        /**
166         * Sets the businessObjectService attribute value.
167         * 
168         * @param businessObjectService The businessObjectService to set.
169         */
170        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
171            this.businessObjectService = businessObjectService;
172        }
173    }