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.ar.businessobject.lookup;
017    
018    import java.util.Collection;
019    import java.util.Date;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Properties;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.kuali.kfs.module.ar.document.service.CustomerOpenItemReportService;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.context.SpringContext;
030    import org.kuali.rice.kns.bo.BusinessObject;
031    import org.kuali.rice.kns.bo.PersistableBusinessObject;
032    import org.kuali.rice.kns.lookup.CollectionIncomplete;
033    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
034    import org.kuali.rice.kns.service.DataDictionaryService;
035    import org.kuali.rice.kns.service.KualiConfigurationService;
036    import org.kuali.rice.kns.util.KNSConstants;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    import org.kuali.rice.kns.web.comparator.CellComparatorHelper;
039    import org.kuali.rice.kns.web.format.DateFormatter;
040    import org.kuali.rice.kns.web.format.Formatter;
041    import org.kuali.rice.kns.web.struts.form.LookupForm;
042    import org.kuali.rice.kns.web.ui.Column;
043    import org.kuali.rice.kns.web.ui.ResultRow;
044    
045    public class CustomerOpenItemReportLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
046    
047        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CustomerOpenItemReportLookupableHelperServiceImpl.class);
048        private Map fieldConversions;
049        private DataDictionaryService dataDictionaryService;
050    
051        /**
052         * Get the search results that meet the input search criteria.
053         * 
054         * @param fieldValues - Map containing prop name keys and search values
055         * @return a List of found business objects
056         */
057        @Override
058        public List getSearchResults(Map fieldValues) {
059            List results;
060            LOG.debug("\n\n\n\n ***********************    getSearchResults() started\n");
061    
062            setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
063            setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
064    
065            /*
066            String customerNumber = ((String[]) getParameters().get(KFSConstants.CustomerOpenItemReport.CUSTOMER_NUMBER))[0];
067            List results = SpringContext.getBean(CustomerOpenItemReportService.class).getPopulatedReportDetails(customerNumber);
068            */
069            String reportName =((String[]) getParameters().get(KFSConstants.CustomerOpenItemReport.REPORT_NAME))[0];
070            if  (StringUtils.equals(reportName, KFSConstants.CustomerOpenItemReport.HISTORY_REPORT_NAME)) {
071                String customerNumber = ((String[]) getParameters().get(KFSConstants.CustomerOpenItemReport.CUSTOMER_NUMBER))[0];
072                results = SpringContext.getBean(CustomerOpenItemReportService.class).getPopulatedReportDetails(customerNumber);
073            } else {
074                results = SpringContext.getBean(CustomerOpenItemReportService.class).getPopulatedReportDetails(getParameters());
075            }
076            LOG.info("\t\t sending results back... \n\n\n");
077            return new CollectionIncomplete(results, new Long(results.size()));
078        }
079    
080        /**
081         * @return a List of the names of fields which are marked in data dictionary as return fields.
082         */
083        /*
084        @Override
085        public List getReturnKeys() {
086            List returnKeys;
087            returnKeys = new ArrayList(fieldConversions.keySet());
088            LOG.info("\n\n\t\t THIS OVERRIDE IS WORKING (GETRETURNKEYS)... \n\n\n");
089    
090            return returnKeys;
091        }
092        */
093    
094        /**
095         * @return a List of the names of fields which are marked in data dictionary as return fields.
096         */
097        @Override
098        protected Properties getParameters(BusinessObject bo, Map fieldConversions, String lookupImpl, List pkNames) {
099            Properties parameters = new Properties();
100            parameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, KNSConstants.RETURN_METHOD_TO_CALL);
101            parameters.put(KNSConstants.DOC_FORM_KEY, getDocFormKey());
102            parameters.put(KNSConstants.REFRESH_CALLER, lookupImpl);
103            if (getReferencesToRefresh() != null) {
104                parameters.put(KNSConstants.REFERENCES_TO_REFRESH, getReferencesToRefresh());
105            }
106    
107            String encryptedList = "";
108    
109            Iterator returnKeys = getReturnKeys().iterator();
110            while (returnKeys.hasNext()) {
111                String fieldNm = (String) returnKeys.next();
112    
113                Object fieldVal = ObjectUtils.getPropertyValue(bo, fieldNm);
114                if (fieldVal == null) {
115                    fieldVal = KNSConstants.EMPTY_STRING;
116                }
117    
118                // Encrypt value if it is a secure field
119                // TODO: revisit authorization checks in this class
120    
121                if (fieldConversions.containsKey(fieldNm)) {
122                    fieldNm = (String) fieldConversions.get(fieldNm);
123                }
124    
125                // need to format date in url
126                if (fieldVal instanceof Date) {
127                    DateFormatter dateFormatter = new DateFormatter();
128                    fieldVal = dateFormatter.format(fieldVal);
129                }
130    
131                parameters.put(fieldNm, fieldVal.toString());
132            }
133    
134            return parameters;
135        }
136    
137        /**
138         * This method performs the lookup and returns a collection of lookup items
139         * 
140         * @param lookupForm
141         * @param kualiLookupable
142         * @param resultTable
143         * @param bounded
144         * @return
145         */
146        @Override
147        public Collection performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
148            Collection displayList;
149            LOG.info("\n\n\t\t THIS OVERRIDE IS WORKING (performLookup)... \n\n\n");
150            // call search method to get results
151            if (bounded) {
152                displayList = getSearchResults(lookupForm.getFieldsForLookup());
153            }
154            else {
155                displayList = getSearchResultsUnbounded(lookupForm.getFieldsForLookup());
156            }
157            // MJM get resultTable populated here
158    
159            HashMap<String, Class> propertyTypes = new HashMap<String, Class>();
160    
161            boolean hasReturnableRow = false;
162    
163            // iterate through result list and wrap rows with return url and action urls
164            for (Iterator iter = displayList.iterator(); iter.hasNext();) {
165                BusinessObject element = (BusinessObject) iter.next();
166    
167                // String returnUrl = getReturnUrl(element, lookupForm.getFieldConversions(),
168                // lookupForm.getLookupableImplServiceName());
169                // String actionUrls = getActionUrls(element);
170                String returnUrl = "www.bigfrickenRETURNurl";
171                String actionUrls = "www.someACTIONurl";
172    
173                List<Column> columns = getColumns();
174                for (Iterator iterator = columns.iterator(); iterator.hasNext();) {
175    
176                    Column col = (Column) iterator.next();
177                    Formatter formatter = col.getFormatter();
178    
179                    // pick off result column from result list, do formatting
180                    String propValue = KNSConstants.EMPTY_STRING;
181                    Object prop = ObjectUtils.getPropertyValue(element, col.getPropertyName());
182    
183                    // formatters
184                    if (prop != null) {
185                        // for Dates, always use DateFormatter
186                        if (prop instanceof Date)
187                            formatter = new DateFormatter();
188    
189                        if (formatter != null)
190                            propValue = (String) formatter.format(prop);
191                        else
192                            propValue = prop.toString();
193                    }
194    
195                    // comparator
196                    Class propClass = propertyTypes.get(col.getPropertyName());
197                    col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass));
198                    col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass));
199    
200                    col.setPropertyValue(propValue);
201    
202                    if (StringUtils.isNotBlank(propValue)) {
203                        if (StringUtils.equals(KFSConstants.CustomerOpenItemReport.DOCUMENT_NUMBER, col.getPropertyName())) {
204                            String propertyURL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.WORKFLOW_URL_KEY) + "/DocHandler.do?docId=" + propValue + "&command=displayDocSearchView";
205                            col.setPropertyURL(propertyURL);
206                        } else col.setPropertyURL("");
207                    }
208                }
209    
210                ResultRow row = new ResultRow(columns, returnUrl, actionUrls);
211                if (element instanceof PersistableBusinessObject)
212                    row.setObjectId(((PersistableBusinessObject) element).getObjectId());
213    
214                boolean rowReturnable = isResultReturnable(element);
215                row.setRowReturnable(rowReturnable);
216                if (rowReturnable)
217                    hasReturnableRow = true;
218    
219                resultTable.add(row);
220            }
221            lookupForm.setHasReturnableRow(hasReturnableRow);
222    
223            return displayList;
224        }
225    
226    }
227