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.util.Collection;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import org.kuali.kfs.module.endow.EndowPropertyConstants;
025    import org.kuali.kfs.module.endow.businessobject.HoldingHistory;
026    import org.kuali.kfs.module.endow.businessobject.HoldingTaxLot;
027    import org.kuali.kfs.module.endow.businessobject.MonthEndDate;
028    import org.kuali.kfs.module.endow.businessobject.Security;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl;
031    import org.kuali.kfs.sys.context.SpringContext;
032    import org.kuali.rice.kns.bo.BusinessObject;
033    import org.kuali.rice.kns.lookup.HtmlData;
034    import org.kuali.rice.kns.service.BusinessObjectService;
035    import org.kuali.rice.kns.service.KualiConfigurationService;
036    import org.kuali.rice.kns.util.KNSConstants;
037    import org.kuali.rice.kns.util.UrlFactory;
038    
039    public class SecurityInquirable extends KfsInquirableImpl {
040    
041        /**
042         * @see org.kuali.kfs.sys.businessobject.inquiry.KfsInquirableImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject,
043         *      java.lang.String, boolean)
044         */
045        public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
046            Security security = (Security) businessObject;
047    
048            // if the attribute is currentHolders or holdersInHistory then we build the lookup links for Current Holders and Holders in
049            // History
050            if (EndowPropertyConstants.SECURITY_CURRENT_HOLDERS.equals(attributeName) || EndowPropertyConstants.SECURITY_HOLDERS_IN_HISTORY.equals(attributeName)) {
051    
052                Properties params = new Properties();
053                params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.SEARCH_METHOD);
054    
055                // the only difference between the two links is the BO class
056                // if currentHolders set the BO class to be HoldingTaxLot
057                if (EndowPropertyConstants.SECURITY_CURRENT_HOLDERS.equals(attributeName)) {
058                    params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, HoldingTaxLot.class.getName());
059                }
060                // if holders in history set the BO to be HoldingHistory
061                if (EndowPropertyConstants.SECURITY_HOLDERS_IN_HISTORY.equals(attributeName)) {
062                    params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, HoldingHistory.class.getName());
063    
064                    // set month end date to be by default the most recent month end date
065                    BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
066                    Collection<MonthEndDate> monthEndDates = businessObjectService.findMatchingOrderBy(MonthEndDate.class, new HashMap(), EndowPropertyConstants.MONTH_END_DATE, false);
067                    Iterator<MonthEndDate> iterator = monthEndDates.iterator();
068                    if (iterator.hasNext()) {
069                        MonthEndDate monthEndDate = iterator.next();
070                        params.put(EndowPropertyConstants.HOLDING_HISTORY_MONTH_END_DATE_ID, UrlFactory.encode(String.valueOf(monthEndDate.getMonthEndDateId())));
071                    }
072                }
073    
074                params.put(KNSConstants.DOC_FORM_KEY, "88888888");
075                params.put(KFSConstants.HIDE_LOOKUP_RETURN_LINK, "true");
076                params.put(KFSConstants.BACK_LOCATION, SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KNSConstants.APPLICATION_URL_KEY) + "/" + KFSConstants.MAPPING_PORTAL + ".do");
077                params.put(EndowPropertyConstants.HOLDING_TAX_LOT_SECURITY_ID, UrlFactory.encode(String.valueOf(security.getId())));
078                params.put(KFSConstants.SUPPRESS_ACTIONS, "true");
079                String url = UrlFactory.parameterizeUrl(KNSConstants.LOOKUP_ACTION, params);
080    
081                Map<String, String> fieldList = new HashMap<String, String>();
082                fieldList.put(EndowPropertyConstants.HOLDING_TAX_LOT_SECURITY_ID, security.getId().toString());
083    
084                return getHyperLink(Security.class, fieldList, url);
085            }
086    
087    
088            return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
089        }
090    }