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.endow.businessobject.lookup;
017    
018    import java.math.BigDecimal;
019    import java.util.ArrayList;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.module.endow.EndowPropertyConstants;
024    import org.kuali.kfs.module.endow.businessobject.TransactionArchive;
025    import org.kuali.rice.kns.bo.BusinessObject;
026    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
027    
028    public class TransactionArchiveLookupableHelperService extends KualiLookupableHelperServiceImpl {
029    
030        /**
031         * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
032         */
033        @Override
034        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
035            
036            // Initialize values.
037            BigDecimal greaterAmt = null;
038            BigDecimal lessAmt    = null;
039            
040            // Get the greater than and less than amounts.
041            try {
042                greaterAmt = new BigDecimal(fieldValues.remove(EndowPropertyConstants.TRANSACTION_ARCHIVE_GREATER_AMOUNT));
043                lessAmt    = new BigDecimal(fieldValues.remove(EndowPropertyConstants.TRANSACTION_ARCHIVE_LESS_AMOUNT));
044            }
045            catch (NumberFormatException nfe) {}
046            
047            List<TransactionArchive> transactionArchives = (List<TransactionArchive>)super.getSearchResults(fieldValues);
048            List<TransactionArchive> removalList = new ArrayList<TransactionArchive>();
049            for (TransactionArchive transactionArchive : transactionArchives) {
050                
051                // Get principal and income amounts.
052                BigDecimal principalAmt = transactionArchive.getPrincipalCashAmount();
053                BigDecimal incomeAmt    = transactionArchive.getIncomeCashAmount();
054                
055                // Case 1: Equal to or greater than.
056                if (greaterAmt != null && lessAmt == null) {
057                    if (!isPrincipalIncomeGreaterEqual(greaterAmt, principalAmt, incomeAmt)) {
058                        removalList.add(transactionArchive);
059                    }
060                }
061                // Case 2: Equal to or less than.
062                else if (greaterAmt == null && lessAmt != null) {
063                    if (!isPrincipalIncomeLessEqual(lessAmt, principalAmt, incomeAmt)) {
064                        removalList.add(transactionArchive);
065                    }
066                }
067                // Case 3: Falls within in the range of greater than and less than, inclusive.
068                // Case 4: Value is exact.
069                else if (greaterAmt != null && lessAmt != null) {
070                    // If both greater and less than values are the same, just check
071                    // if either the principal or income value is equal to the
072                    // greater/less amount.
073                    if (greaterAmt.compareTo(lessAmt) == 0 && 
074                        !isPrincipalIncomeEqual(lessAmt, principalAmt, incomeAmt)) {
075                        removalList.add(transactionArchive);
076                    }
077                    // Check if either the income or principal value fall within the
078                    // range of the greater and less than values.
079                    else if (greaterAmt.compareTo(lessAmt) != 0 &&
080                             !isPrincipalIncomeInRange(greaterAmt, lessAmt, principalAmt, incomeAmt)) {
081                        removalList.add(transactionArchive);
082                    }
083                }
084            }
085            
086            // Remove items from the list.
087            transactionArchives.removeAll(removalList);
088            
089            return transactionArchives;
090        }
091        
092        /**
093         * Determines if either the principal or income amount is equal to the specified amount.
094         * 
095         * @param amount
096         * @param principal
097         * @param income
098         * @return true if principal or income is equal to the specified amount
099         */
100        private boolean isPrincipalIncomeEqual(BigDecimal amount, BigDecimal principal, BigDecimal income) {
101            return (principal.compareTo(amount) == 0 || income.compareTo(amount) == 0);
102        }
103        
104        /**
105         * Determines if the principal or income amount is within the specified range.
106         *
107         * @param greaterAmount
108         * @param lessAmount
109         * @param principal
110         * @param income
111         * @return true if the principal or income amount is within the greater and less than amount.
112         */
113        private boolean isPrincipalIncomeInRange(BigDecimal greaterAmount, BigDecimal lessAmount, BigDecimal principal, BigDecimal income) {
114    
115            return (isPrincipalIncomeGreaterEqual(greaterAmount, principal, income) && 
116                    isPrincipalIncomeLessEqual(lessAmount, principal, income));
117        }
118        
119        /**
120         * Determines if the principal or income amount is less than or equal to the specified amount. 
121         *
122         * @param amount
123         * @param principal
124         * @param income
125         * @return true if either the principal or income amounts are less than or equal to the specified amount.
126         */
127        private boolean isPrincipalIncomeLessEqual(BigDecimal amount, BigDecimal principal, BigDecimal income) {
128            return (principal.compareTo(amount) <= 0 || income.compareTo(amount) <= 0);
129        }
130        
131        /**
132         * Determines if the principal or income amount is less than or equal to the specified amount.
133         *
134         * @param amount
135         * @param principal
136         * @param income
137         * @return true if either the principal or income amounts are greater than or equal to the specified amount.
138         */
139        private boolean isPrincipalIncomeGreaterEqual(BigDecimal amount, BigDecimal principal, BigDecimal income) {
140            return (principal.compareTo(amount) >= 0 || income.compareTo(amount) >= 0);
141        }
142    
143        /**
144         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#allowsMaintenanceNewOrCopyAction()
145         */
146        @Override
147        public boolean allowsMaintenanceNewOrCopyAction() {
148    
149            return false;
150        }
151    
152    }