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.cab.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.kuali.kfs.module.cab.CabConstants;
025    import org.kuali.kfs.module.cab.CabPropertyConstants;
026    import org.kuali.kfs.module.cab.businessobject.GeneralLedgerEntry;
027    import org.kuali.kfs.module.cab.businessobject.GeneralLedgerEntryAsset;
028    import org.kuali.kfs.module.cab.businessobject.PurchasingAccountsPayableDocument;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.rice.kim.bo.impl.KimAttributes;
031    import org.kuali.rice.kim.bo.types.dto.AttributeSet;
032    import org.kuali.rice.kim.service.KIMServiceLocator;
033    import org.kuali.rice.kim.util.KimConstants;
034    import org.kuali.rice.kns.bo.BusinessObject;
035    import org.kuali.rice.kns.lookup.CollectionIncomplete;
036    import org.kuali.rice.kns.lookup.HtmlData;
037    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
038    import org.kuali.rice.kns.lookup.LookupUtils;
039    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
040    import org.kuali.rice.kns.service.BusinessObjectService;
041    import org.kuali.rice.kns.util.GlobalVariables;
042    import org.kuali.rice.kns.util.KNSConstants;
043    
044    /**
045     * This class overrides the base getActionUrls method
046     */
047    public class GeneralLedgerEntryLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
048        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GeneralLedgerEntryLookupableHelperServiceImpl.class);
049        private BusinessObjectService businessObjectService;
050    
051        /**
052         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject,
053         *      java.util.List)
054         */
055        @Override
056        public List<HtmlData> getCustomActionUrls(BusinessObject bo, List pkNames) {
057            AttributeSet permissionDetails = new AttributeSet();
058            permissionDetails.put(KimAttributes.NAMESPACE_CODE, "KFS-CAB");
059            permissionDetails.put(KimAttributes.ACTION_CLASS, "PurApLineAction");
060    
061            if (!KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KNSConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.USE_SCREEN, permissionDetails, null)) {
062                return super.getEmptyActionUrls();
063            }
064    
065            GeneralLedgerEntry entry = (GeneralLedgerEntry) bo;
066            List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
067            if (entry.isActive()) {
068                AnchorHtmlData processLink = new AnchorHtmlData("../cabGlLine.do?methodToCall=process&" + CabPropertyConstants.GeneralLedgerEntry.GENERAL_LEDGER_ACCOUNT_IDENTIFIER + "=" + entry.getGeneralLedgerAccountIdentifier(), "process", "process");
069                processLink.setTarget(entry.getGeneralLedgerAccountIdentifier().toString());
070                anchorHtmlDataList.add(processLink);
071            }
072            else {
073                List<GeneralLedgerEntryAsset> generalLedgerEntryAssets = entry.getGeneralLedgerEntryAssets();
074                if (!generalLedgerEntryAssets.isEmpty()) {
075                    AnchorHtmlData viewDocLink = new AnchorHtmlData("../cabGlLine.do?methodToCall=viewDoc&" + "documentNumber" + "=" + generalLedgerEntryAssets.get(0).getCapitalAssetManagementDocumentNumber(), "viewDoc", generalLedgerEntryAssets.get(0).getCapitalAssetManagementDocumentNumber());
076                    viewDocLink.setTarget(generalLedgerEntryAssets.get(0).getCapitalAssetManagementDocumentNumber());
077                    anchorHtmlDataList.add(viewDocLink);
078                }
079                else {
080                    anchorHtmlDataList.add(new AnchorHtmlData("", "n/a", "n/a"));
081                }
082            }
083            return anchorHtmlDataList;
084        }
085    
086        /**
087         * This method will remove all PO related transactions from display on GL results
088         * 
089         * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
090         */
091        @Override
092        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
093            // update status code from user input value to DB value.
094            updateStatusCodeCriteria(fieldValues);
095    
096            List<? extends BusinessObject> searchResults = super.getSearchResults(fieldValues);
097            if (searchResults == null || searchResults.isEmpty()) {
098                return searchResults;
099            }
100            Integer searchResultsLimit = LookupUtils.getSearchResultsLimit(GeneralLedgerEntry.class);
101            Long matchingResultsCount = null;
102            List<GeneralLedgerEntry> newList = new ArrayList<GeneralLedgerEntry>();
103            for (BusinessObject businessObject : searchResults) {
104                GeneralLedgerEntry entry = (GeneralLedgerEntry) businessObject;
105                if (!CabConstants.PREQ.equals(entry.getFinancialDocumentTypeCode())) {
106                    if (!CabConstants.CM.equals(entry.getFinancialDocumentTypeCode())) {
107                        newList.add(entry);
108                    }
109                    else if (CabConstants.CM.equals(entry.getFinancialDocumentTypeCode())) {
110                        Map<String, String> cmKeys = new HashMap<String, String>();
111                        cmKeys.put(CabPropertyConstants.PurchasingAccountsPayableDocument.DOCUMENT_NUMBER, entry.getDocumentNumber());
112                        // check if CAB PO document exists, if not included
113                        Collection<PurchasingAccountsPayableDocument> matchingCreditMemos = businessObjectService.findMatching(PurchasingAccountsPayableDocument.class, cmKeys);
114                        if (matchingCreditMemos == null || matchingCreditMemos.isEmpty()) {
115                            newList.add(entry);
116                        }
117                    }
118                }
119            }
120            matchingResultsCount = Long.valueOf(newList.size());
121            if (matchingResultsCount.intValue() <= searchResultsLimit.intValue()) {
122                matchingResultsCount = new Long(0);
123            }
124            return new CollectionIncomplete(newList, matchingResultsCount);
125        }
126    
127    
128        /**
129         * Update activity status code to the value used in DB. The reason is the value from user input will be 'Y' or 'N'. However,
130         * these two status code are now replaced by 'N','E' and 'P'.
131         * 
132         * @param fieldValues
133         */
134        protected void updateStatusCodeCriteria(Map<String, String> fieldValues) {
135            String activityStatusCode = null;
136            if (fieldValues.containsKey(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE)) {
137                activityStatusCode = (String) fieldValues.get(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE);
138            }
139    
140            if (KFSConstants.NON_ACTIVE_INDICATOR.equalsIgnoreCase(activityStatusCode)) {
141                // not processed in CAMs: 'N'
142                fieldValues.put(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE, CabConstants.ActivityStatusCode.NEW);
143            }
144            else if (KFSConstants.ACTIVE_INDICATOR.equalsIgnoreCase(activityStatusCode)) {
145                // processed in CAMs: 'E' or 'P'
146                fieldValues.put(CabPropertyConstants.GeneralLedgerEntry.ACTIVITY_STATUS_CODE, CabConstants.ActivityStatusCode.PROCESSED_IN_CAMS + KNSConstants.OR_LOGICAL_OPERATOR + CabConstants.ActivityStatusCode.ENROUTE);
147            }
148    
149        }
150    
151        /**
152         * Gets the businessObjectService attribute.
153         * 
154         * @return Returns the businessObjectService.
155         */
156        public BusinessObjectService getBusinessObjectService() {
157            return businessObjectService;
158        }
159    
160        /**
161         * Sets the businessObjectService attribute value.
162         * 
163         * @param businessObjectService The businessObjectService to set.
164         */
165        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
166            this.businessObjectService = businessObjectService;
167        }
168    }