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.util.List;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.module.endow.EndowConstants;
023    import org.kuali.kfs.module.endow.EndowPropertyConstants;
024    import org.kuali.kfs.sys.KFSConstants;
025    import org.kuali.rice.kns.bo.BusinessObject;
026    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
027    
028    public class FrequencyCodeLookupableHelperService extends KualiLookupableHelperServiceImpl {
029    
030        /**
031         * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
032         */
033        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
034    
035            // if user did not enter a frequency code
036            if (!fieldValues.containsKey(KFSConstants.GENERIC_CODE_PROPERTY_NAME) || StringUtils.isEmpty(fieldValues.get(KFSConstants.GENERIC_CODE_PROPERTY_NAME))) {
037    
038                // build search criteria based on information entered and store in this variable
039                String frequencyTypeValueBase = KFSConstants.EMPTY_STRING;
040    
041                // get the frequency type
042                if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_TYPE)) {
043    
044                    String frequencyTypeValue = fieldValues.get(EndowPropertyConstants.FREQUENCY_TYPE);
045    
046                    if (!StringUtils.isEmpty(frequencyTypeValue)) {
047                        frequencyTypeValueBase += frequencyTypeValue;
048                    }
049    
050                    // if frequency type is dayly there is nothing else to select, ignore everything else
051    
052                    // if frequency type is weekly, check for the day of the week selected
053                    if (frequencyTypeValue.equalsIgnoreCase(EndowConstants.FrequencyTypes.WEEKLY)) {
054    
055                        if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_WEEK_DAY)) {
056    
057                            String weekDay = fieldValues.get(EndowPropertyConstants.FREQUENCY_WEEK_DAY);
058    
059                            if (!StringUtils.isEmpty(weekDay)) {
060                                frequencyTypeValueBase += weekDay;
061                            }
062    
063                        }
064                    }
065    
066                    // if frequency type is semi-monthly, select a day of the month, it should be among first 15 and then next one will
067                    // be 15 days later
068                    if (frequencyTypeValue.equalsIgnoreCase(EndowConstants.FrequencyTypes.SEMI_MONTHLY)) {
069    
070                        if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH)) {
071    
072                            String monthDay = fieldValues.get(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH);
073    
074                            if (!StringUtils.isEmpty(monthDay)) {
075                                frequencyTypeValueBase += monthDay;
076                            }
077    
078                        }
079                    }
080    
081                    // if frequency type is monthly check day in month, if not selected check month end
082                    if (frequencyTypeValue.equalsIgnoreCase(EndowConstants.FrequencyTypes.MONTHLY)) {
083    
084                        if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_MONTHLY_OCCURENCE)) {
085                            String monthlyOccurence = fieldValues.get(EndowPropertyConstants.FREQUENCY_MONTHLY_OCCURENCE);
086    
087                            if (EndowConstants.FrequencyMonthly.DATE.equalsIgnoreCase(monthlyOccurence)) {
088                                if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH)) {
089    
090                                    String monthDay = fieldValues.get(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH);
091    
092                                    if (!StringUtils.isEmpty(monthDay)) {
093                                        frequencyTypeValueBase += monthDay;
094                                    }
095    
096                                }
097                            }
098    
099                            if (EndowConstants.FrequencyMonthly.MONTH_END.equalsIgnoreCase(monthlyOccurence)) {
100                                frequencyTypeValueBase += EndowConstants.FrequencyMonthly.MONTH_END;
101                            }
102    
103                        }
104    
105                    }
106    
107                    // if frequency type is quarterly or semi-annually or annually, check selected month and day in month or month end
108                    if (frequencyTypeValue.equalsIgnoreCase(EndowConstants.FrequencyTypes.QUARTERLY) || frequencyTypeValue.equalsIgnoreCase(EndowConstants.FrequencyTypes.SEMI_ANNUALLY) || frequencyTypeValue.equalsIgnoreCase(EndowConstants.FrequencyTypes.ANNUALLY)) {
109    
110                        if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_MONTH)) {
111                            String month = fieldValues.get(EndowPropertyConstants.FREQUENCY_MONTH);
112    
113                            // if month was not selected than it is replaced by a wildcard
114                            if (StringUtils.isEmpty(month)) {
115                                frequencyTypeValueBase += KFSConstants.WILDCARD_CHARACTER;
116                            }
117                            else {
118                                frequencyTypeValueBase += month;
119                            }
120    
121                        }
122                        if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_MONTHLY_OCCURENCE)) {
123                            String monthlyOccurence = fieldValues.get(EndowPropertyConstants.FREQUENCY_MONTHLY_OCCURENCE);
124    
125                            if (EndowConstants.FrequencyMonthly.DATE.equalsIgnoreCase(monthlyOccurence)) {
126                                if (fieldValues.containsKey(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH)) {
127    
128                                    String monthDay = fieldValues.get(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH);
129    
130                                    if (!StringUtils.isEmpty(monthDay)) {
131                                        frequencyTypeValueBase += monthDay;
132                                    }
133    
134                                }
135                            }
136    
137                            if (EndowConstants.FrequencyMonthly.MONTH_END.equalsIgnoreCase(monthlyOccurence)) {
138                                frequencyTypeValueBase += EndowConstants.FrequencyMonthly.MONTH_END;
139                            }
140                        }
141                    }
142    
143                }
144    
145                // the percentage sign is added for the cases where not all the code part were specified by the user
146                frequencyTypeValueBase += KFSConstants.PERCENTAGE_SIGN;
147                fieldValues.put(KFSConstants.GENERIC_CODE_PROPERTY_NAME, frequencyTypeValueBase);
148            }
149    
150            // remove the helped fields from the fieldValues as they are not user for the search
151            fieldValues.remove(EndowPropertyConstants.FREQUENCY_TYPE);
152            fieldValues.remove(EndowPropertyConstants.FREQUENCY_DAY_IN_MONTH);
153            fieldValues.remove(EndowPropertyConstants.FREQUENCY_WEEK_DAY);
154            fieldValues.remove(EndowPropertyConstants.FREQUENCY_MONTH);
155            fieldValues.remove(EndowPropertyConstants.FREQUENCY_MONTHLY_OCCURENCE);
156    
157            List searchResults = super.getSearchResults(fieldValues);
158    
159            return searchResults;
160        }
161    
162    }