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.inquiry;
017    
018    import java.sql.Date;
019    import java.text.ParseException;
020    import java.util.ArrayList;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Properties;
025    
026    import org.kuali.kfs.module.endow.EndowPropertyConstants;
027    import org.kuali.kfs.module.endow.businessobject.KEMID;
028    import org.kuali.kfs.module.endow.businessobject.KEMIDCurrentAvailableBalance;
029    import org.kuali.kfs.module.endow.businessobject.KEMIDCurrentBalance;
030    import org.kuali.kfs.module.endow.businessobject.KEMIDHistoricalBalance;
031    import org.kuali.kfs.module.endow.businessobject.KemidAgreement;
032    import org.kuali.kfs.module.endow.businessobject.KemidAuthorizations;
033    import org.kuali.kfs.module.endow.businessobject.KemidBenefittingOrganization;
034    import org.kuali.kfs.module.endow.businessobject.KemidCombineDonorStatement;
035    import org.kuali.kfs.module.endow.businessobject.KemidPayoutInstruction;
036    import org.kuali.kfs.module.endow.businessobject.KemidReportGroup;
037    import org.kuali.kfs.module.endow.businessobject.KemidSourceOfFunds;
038    import org.kuali.kfs.module.endow.businessobject.KemidUseCriteria;
039    import org.kuali.kfs.module.endow.businessobject.Security;
040    import org.kuali.kfs.module.endow.businessobject.Tickler;
041    import org.kuali.kfs.module.endow.document.service.KEMService;
042    import org.kuali.kfs.sys.KFSConstants;
043    import org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl;
044    import org.kuali.kfs.sys.context.SpringContext;
045    import org.kuali.rice.kns.bo.BusinessObject;
046    import org.kuali.rice.kns.lookup.HtmlData;
047    import org.kuali.rice.kns.service.DateTimeService;
048    import org.kuali.rice.kns.service.KualiConfigurationService;
049    import org.kuali.rice.kns.util.KNSConstants;
050    import org.kuali.rice.kns.util.UrlFactory;
051    
052    public class KemidInquirableImpl extends KfsInquirableImpl {
053    
054        /**
055         * @see org.kuali.rice.kns.inquiry.KualiInquirableImpl#getBusinessObject(java.util.Map)
056         */
057        @Override
058        public BusinessObject getBusinessObject(Map fieldValues) {
059    
060            KEMID kemid = (KEMID) super.getBusinessObject(fieldValues);
061            KEMService kemService = SpringContext.getBean(KEMService.class);
062            String currentProcessDateString = kemService.getCurrentSystemProcessDate();
063    
064            setViewableAgreements(kemid);
065            setViewableAuthorizations(kemid);
066            setViewableSourcesOfFunds(kemid);
067            setViewableBenefittingOrgs(kemid);
068            setViewablePayoutInstructions(kemid, currentProcessDateString);
069            setViewableUseCriteria(kemid);
070            setViewableReportGroups(kemid, currentProcessDateString);
071            setViewableCombineDonorStatements(kemid, currentProcessDateString);
072    
073            return kemid;
074        }
075    
076    
077        /**
078         * Sets the viewable agreements list - if an agreement is not active it is not viewable
079         * 
080         * @param kemid
081         */
082        private void setViewableAgreements(KEMID kemid) {
083            // show only active agreements
084            List<KemidAgreement> activeKemidAgreements = new ArrayList<KemidAgreement>();
085            List<KemidAgreement> kemidAgreements = kemid.getKemidAgreements();
086    
087            for (KemidAgreement kemidAgreement : kemidAgreements) {
088                if (kemidAgreement.isActive()) {
089                    activeKemidAgreements.add(kemidAgreement);
090                }
091            }
092    
093            kemid.setKemidAgreements(activeKemidAgreements);
094        }
095    
096        /**
097         * Sets the viewable Authorizations list - if an Authorizations is not active it is not viewable
098         * 
099         * @param kemid
100         */
101        private void setViewableAuthorizations(KEMID kemid) {
102            // show only active Authorizations
103            List<KemidAuthorizations> activeKemidAuthorizations = new ArrayList<KemidAuthorizations>();
104            List<KemidAuthorizations> kemidAuthorizations = kemid.getKemidAuthorizations();
105    
106            for (KemidAuthorizations kemidAuthorization : kemidAuthorizations) {
107                if (kemidAuthorization.isActive()) {
108                    activeKemidAuthorizations.add(kemidAuthorization);
109                }
110            }
111    
112            kemid.setKemidAuthorizations(activeKemidAuthorizations);
113        }
114    
115    
116        /**
117         * Sets the viewable sources of funds list - if a source of funds is not active it is not viewable
118         * 
119         * @param kemid
120         */
121        private void setViewableSourcesOfFunds(KEMID kemid) {
122            // show only the active source of funds
123            List<KemidSourceOfFunds> activeKemidSourcesOfFunds = new ArrayList<KemidSourceOfFunds>();
124            List<KemidSourceOfFunds> kemidSourcesOfFunds = kemid.getKemidSourcesOfFunds();
125    
126            for (KemidSourceOfFunds kemidSourceOfFunds : kemidSourcesOfFunds) {
127                if (kemidSourceOfFunds.isActive()) {
128                    activeKemidSourcesOfFunds.add(kemidSourceOfFunds);
129                }
130            }
131    
132            kemid.setKemidSourcesOfFunds(activeKemidSourcesOfFunds);
133        }
134    
135        /**
136         * Sets the viewable sources of funds list - if a source of funds is not active it is not viewable
137         * 
138         * @param kemid
139         */
140        private void setViewableBenefittingOrgs(KEMID kemid) {
141            // show only the active benefitting organizations
142            List<KemidBenefittingOrganization> activeKemidBenefittingOrgs = new ArrayList<KemidBenefittingOrganization>();
143            List<KemidBenefittingOrganization> kemidBenefittingOrgs = kemid.getKemidBenefittingOrganizations();
144    
145            for (KemidBenefittingOrganization kemidBenefittingOrganization : kemidBenefittingOrgs) {
146                if (kemidBenefittingOrganization.isActive()) {
147                    activeKemidBenefittingOrgs.add(kemidBenefittingOrganization);
148                }
149            }
150    
151            kemid.setKemidBenefittingOrganizations(activeKemidBenefittingOrgs);
152        }
153    
154        /**
155         * Ste the viewable Payout Instructions - a record is no longer viewable if the payout termination date is less than the current
156         * processor system date.
157         * 
158         * @param kemid
159         * @param currentProcessDateString
160         */
161        private void setViewablePayoutInstructions(KEMID kemid, String currentProcessDateString) {
162            // a record is no longer viewable if the payout termination date is less than the current processor system date
163            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
164            List<KemidPayoutInstruction> activeKemidPayoutInstructions = new ArrayList<KemidPayoutInstruction>();
165            List<KemidPayoutInstruction> kemidPayoutInstructions = kemid.getKemidPayoutInstructions();
166    
167            try {
168                Date currentProcessDate = dateTimeService.convertToSqlDate(currentProcessDateString);
169                for (KemidPayoutInstruction kemidPayoutInstruction : kemidPayoutInstructions) {
170                    if (kemidPayoutInstruction.getEndDate() == null || kemidPayoutInstruction.getEndDate().compareTo(currentProcessDate) < 0) {
171                        activeKemidPayoutInstructions.add(kemidPayoutInstruction);
172                    }
173                }
174    
175                kemid.setKemidPayoutInstructions(activeKemidPayoutInstructions);
176            }
177            catch (ParseException ex) {
178    
179            }
180        }
181    
182        /**
183         * Sets the viewable use criteria list - if a use criteria is not active it is not viewable
184         * 
185         * @param kemid
186         */
187        private void setViewableUseCriteria(KEMID kemid) {
188            // show only the active use criteria
189            List<KemidUseCriteria> activeKemidUseCriteria = new ArrayList<KemidUseCriteria>();
190            List<KemidUseCriteria> kemidUseCriteria = kemid.getKemidUseCriteria();
191    
192            for (KemidUseCriteria useCriteria : kemidUseCriteria) {
193                if (useCriteria.isActive()) {
194                    activeKemidUseCriteria.add(useCriteria);
195                }
196            }
197    
198            kemid.setKemidUseCriteria(activeKemidUseCriteria);
199        }
200    
201        /**
202         * Sets the viewable report groups - a record is no longer viewable if the report Group terminated date is less than the current
203         * processor system date.
204         * 
205         * @param kemid
206         * @param currentProcessDateString
207         */
208        private void setViewableReportGroups(KEMID kemid, String currentProcessDateString) {
209    
210            // a record is no longer viewable if the report Group terminated date is less than the current processor system date
211            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
212            List<KemidReportGroup> activeKemidReportGroups = new ArrayList<KemidReportGroup>();
213            List<KemidReportGroup> kemidReportGroups = kemid.getKemidReportGroups();
214    
215            try {
216                Date currentProcessDate = dateTimeService.convertToSqlDate(currentProcessDateString);
217                for (KemidReportGroup kemidReportGroup : kemidReportGroups) {
218                    if (kemidReportGroup.getDateTerminated() == null || kemidReportGroup.getDateTerminated().compareTo(currentProcessDate) < 0) {
219                        activeKemidReportGroups.add(kemidReportGroup);
220                    }
221                }
222    
223                kemid.setKemidReportGroups(activeKemidReportGroups);
224            }
225            catch (ParseException ex) {
226    
227            }
228        }
229    
230    
231        /**
232         * Sets the viewable combine donor statements - a record is no longer viewable if the terminate combine date is less than the
233         * current processor system date.
234         * 
235         * @param kemid
236         * @param currentProcessDateString
237         */
238        private void setViewableCombineDonorStatements(KEMID kemid, String currentProcessDateString) {
239            // a record is no longer viewable if the terminate combine date is less than the current processor system date
240            DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
241            List<KemidCombineDonorStatement> activeKemidCombineDonorStatement = new ArrayList<KemidCombineDonorStatement>();
242            List<KemidCombineDonorStatement> kemidCombineDonorStatements = kemid.getKemidCombineDonorStatements();
243    
244            try {
245                Date currentProcessDate = dateTimeService.convertToSqlDate(currentProcessDateString);
246                for (KemidCombineDonorStatement combineDonorStatement : kemidCombineDonorStatements) {
247                    if (combineDonorStatement.getTerminateCombineDate() == null || combineDonorStatement.getTerminateCombineDate().compareTo(currentProcessDate) < 0) {
248                        activeKemidCombineDonorStatement.add(combineDonorStatement);
249                    }
250                }
251    
252                kemid.setKemidCombineDonorStatements(activeKemidCombineDonorStatement);
253            }
254            catch (ParseException ex) {
255    
256            }
257        }
258    
259        /**
260         * @see org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject,
261         *      java.lang.String, boolean)
262         */
263        public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
264            KEMID kemid = (KEMID) businessObject;
265    
266            // if the attribute is currentAvailableFunds, currentBalances, historicalBalances, ticklers then we build the lookup links
267            // for
268            // Current Available Funds, Current Balances, Historical Balances and Ticklers
269            if (EndowPropertyConstants.KEMID_CURRENT_AVAILABLE_FUNDS.equalsIgnoreCase(attributeName) || EndowPropertyConstants.KEMID_CURRENT_BALANCES.equalsIgnoreCase(attributeName) || EndowPropertyConstants.KEMID_HISTORICAL_BALANCES.equalsIgnoreCase(attributeName) || EndowPropertyConstants.KEMID_TICKLERS.equals(attributeName)) {
270                // || EndowPropertyConstants.KEMID_RECURRING_TRANSFERS.equalsIgnoreCase(attributeName)) {
271    
272                Properties params = new Properties();
273                params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.SEARCH_METHOD);
274    
275                // the only difference between the two links is the BO class
276                // if currentAvailableFunds set the BO class to be KEMIDCurrentAvailableBalance
277                if (EndowPropertyConstants.KEMID_CURRENT_AVAILABLE_FUNDS.equals(attributeName)) {
278                    params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, KEMIDCurrentAvailableBalance.class.getName());
279                }
280                // if currentBalances set the BO to be KEMIDCurrentBalance
281                if (EndowPropertyConstants.KEMID_CURRENT_BALANCES.equals(attributeName)) {
282                    params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, KEMIDCurrentBalance.class.getName());
283                }
284                // if historicalBalances set the BO to be KEMIDHistoricalBalance
285                if (EndowPropertyConstants.KEMID_HISTORICAL_BALANCES.equals(attributeName)) {
286                    params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, KEMIDHistoricalBalance.class.getName());
287                }
288    
289                // if ticklers set the BO to be Tickler
290                if (EndowPropertyConstants.KEMID_TICKLERS.equals(attributeName)) {
291                    params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, Tickler.class.getName());
292                }
293    
294                // if ticklers set the BO to be EndowmentRecurringCashTransfer
295                // if (EndowPropertyConstants.KEMID_RECURRING_TRANSFERS.equals(attributeName)) {
296                // params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, EndowmentRecurringCashTransfer.class.getName());
297                // }
298    
299                params.put(KNSConstants.DOC_FORM_KEY, "88888888");
300                params.put(KFSConstants.HIDE_LOOKUP_RETURN_LINK, "true");
301                params.put(KFSConstants.BACK_LOCATION, SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KNSConstants.APPLICATION_URL_KEY) + "/" + KFSConstants.MAPPING_PORTAL + ".do");
302    
303                if (EndowPropertyConstants.KEMID_TICKLERS.equals(attributeName)) {
304                    params.put(EndowPropertyConstants.TICKLER_LOOKUP_KEMID, UrlFactory.encode(kemid.getKemid()));
305                }
306                // else if (EndowPropertyConstants.KEMID_RECURRING_TRANSFERS.equalsIgnoreCase(attributeName)) {
307                // params.put(EndowPropertyConstants.ENDOWMENT_RECURRING_CASH_TRANSF_SOURCE_KEMID, UrlFactory.encode(kemid.getKemid()));
308                // }
309                else {
310                    params.put(EndowPropertyConstants.KEMID, UrlFactory.encode(kemid.getKemid()));
311                }
312                params.put(KFSConstants.SUPPRESS_ACTIONS, "true");
313                String url = UrlFactory.parameterizeUrl(KNSConstants.LOOKUP_ACTION, params);
314    
315                Map<String, String> fieldList = new HashMap<String, String>();
316    
317                if (EndowPropertyConstants.KEMID_TICKLERS.equals(attributeName)) {
318                    fieldList.put(EndowPropertyConstants.TICKLER_LOOKUP_KEMID, kemid.getKemid());
319                }
320                else {
321                    fieldList.put(EndowPropertyConstants.KEMID, kemid.getKemid());
322                }
323    
324                return getHyperLink(Security.class, fieldList, url);
325            }
326    
327    
328            return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
329        }
330    
331    
332    }