Coverage Report - org.kuali.rice.kew.bo.lookup.DocumentRouteHeaderValueLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentRouteHeaderValueLookupableHelperServiceImpl
0%
0/118
0%
0/58
5.375
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.bo.lookup;
 17  
 
 18  
 import org.apache.commons.beanutils.PropertyUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.config.ConfigContext;
 21  
 import org.kuali.rice.kew.docsearch.*;
 22  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 23  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 24  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 25  
 import org.kuali.rice.kew.util.KEWConstants;
 26  
 import org.kuali.rice.kew.util.KEWPropertyConstants;
 27  
 import org.kuali.rice.kew.web.KeyValueSort;
 28  
 import org.kuali.rice.kim.bo.Person;
 29  
 import org.kuali.rice.kns.bo.BusinessObject;
 30  
 import org.kuali.rice.kns.lookup.HtmlData;
 31  
 import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
 32  
 import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
 33  
 import org.kuali.rice.kns.service.DateTimeService;
 34  
 import org.kuali.rice.kns.util.GlobalVariables;
 35  
 import org.kuali.rice.kns.util.KNSConstants;
 36  
 import org.kuali.rice.kns.web.comparator.CellComparatorHelper;
 37  
 import org.kuali.rice.kns.web.format.BooleanFormatter;
 38  
 import org.kuali.rice.kns.web.format.CollectionFormatter;
 39  
 import org.kuali.rice.kns.web.format.DateFormatter;
 40  
 import org.kuali.rice.kns.web.format.Formatter;
 41  
 import org.kuali.rice.kns.web.struts.form.LookupForm;
 42  
 import org.kuali.rice.kns.web.ui.Column;
 43  
 import org.kuali.rice.kns.web.ui.Field;
 44  
 import org.kuali.rice.kns.web.ui.ResultRow;
 45  
 import org.kuali.rice.kns.web.ui.Row;
 46  
 
 47  
 import java.lang.reflect.InvocationTargetException;
 48  
 import java.sql.Date;
 49  
 import java.util.*;
 50  
 
 51  
 /**
 52  
  * This is a description of what this class does - chris don't forget to fill this in.
 53  
  *
 54  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 55  
  *
 56  
  */
 57  0
 public class DocumentRouteHeaderValueLookupableHelperServiceImpl extends
 58  
 KualiLookupableHelperServiceImpl {
 59  
 
 60  
         private static final long serialVersionUID = -5162419674659967408L;
 61  
         DateTimeService dateTimeService;
 62  
         DocumentLookupCriteriaProcessor processor;
 63  
 
 64  
 
 65  
         /**
 66  
          * This overridden method ...
 67  
          *
 68  
          * @see org.kuali.rice.kew.bo.lookup.DocumentRouteHeaderValueLookupableHelperService#setDateTimeService(org.kuali.rice.kns.service.DateTimeService)
 69  
          */
 70  
         public void setDateTimeService(DateTimeService dateTimeService) {
 71  0
                 this.dateTimeService = dateTimeService;
 72  0
         }
 73  
 
 74  
 
 75  
         /**
 76  
          * This overridden method ...
 77  
          *
 78  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject, java.util.List)
 79  
          */
 80  
         @Override
 81  
         public List<HtmlData> getCustomActionUrls(BusinessObject businessObject,
 82  
                         List pkNames) {
 83  
                 // TODO chris - THIS METHOD NEEDS JAVADOCS
 84  0
                 return super.getCustomActionUrls(businessObject, pkNames);
 85  
         }
 86  
 
 87  
 
 88  
 
 89  
 
 90  
 
 91  
 
 92  
 
 93  
         /**
 94  
          * This overridden method ...
 95  
          *
 96  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#performLookup(org.kuali.rice.kns.web.struts.form.LookupForm, java.util.Collection, boolean)
 97  
          */
 98  
         @Override
 99  
         public Collection performLookup(LookupForm lookupForm,
 100  
                         Collection resultTable, boolean bounded) {
 101  
 
 102  
                 //TODO: KNS updates to make this not require code from the parent
 103  
 
 104  
 
 105  0
             Map<String,String> fieldsForLookup = lookupForm.getFieldsForLookup();
 106  0
             DocSearchCriteriaDTO criteria = constructCriteria(fieldsForLookup);
 107  
 
 108  
             //TODO: move this into actual adapter as well
 109  0
             Collection displayList=null;
 110  0
             DocumentSearchResultComponents components = KEWServiceLocator.getDocumentSearchService().getList(GlobalVariables.getUserSession().getPrincipalId(), criteria);
 111  0
             List<DocumentSearchResult> result = components.getSearchResults();
 112  
 //            for (DocumentSearchResult documentSearchResult : result) {
 113  0
                         displayList = result;//.getResultContainers();
 114  
 //                }
 115  
 
 116  
                 //####BEGIN COPIED CODE#########
 117  0
         setBackLocation((String) lookupForm.getFieldsForLookup().get(KNSConstants.BACK_LOCATION));
 118  0
         setDocFormKey((String) lookupForm.getFieldsForLookup().get(KNSConstants.DOC_FORM_KEY));
 119  
 
 120  
 //###COMENTED OUT
 121  
 //                  Collection displayList;
 122  
 //        // call search method to get results
 123  
 //        if (bounded) {
 124  
 //            displayList = getSearchResults(lookupForm.getFieldsForLookup());
 125  
 //        }
 126  
 //        else {
 127  
 //            displayList = getSearchResultsUnbounded(lookupForm.getFieldsForLookup());
 128  
 //        }
 129  
 //##COMENTED OUT
 130  
 
 131  0
         HashMap<String,Class> propertyTypes = new HashMap<String, Class>();
 132  
 
 133  0
         boolean hasReturnableRow = false;
 134  
 
 135  0
         List returnKeys = getReturnKeys();
 136  0
         List pkNames = getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(getBusinessObjectClass());
 137  0
         Person user = GlobalVariables.getUserSession().getPerson();
 138  
 
 139  
         // iterate through result list and wrap rows with return url and action urls
 140  
 
 141  
 //COMMENTING THIS OUT FOR NOW
 142  
 //        for (Iterator iter = displayList.iterator(); iter.hasNext();) {
 143  
 //            BusinessObject element = (BusinessObject) iter.next();
 144  
 //                if(element instanceof PersistableBusinessObject){
 145  
 //                lookupForm.setLookupObjectId(((PersistableBusinessObject)element).getObjectId());
 146  
 //            }
 147  
 //                BusinessObject element = null;
 148  
 //                BusinessObjectRestrictions businessObjectRestrictions = getBusinessObjectAuthorizationService().getLookupResultRestrictions(element, user);
 149  
 
 150  
 //            HtmlData returnUrl = getReturnUrl(element, lookupForm, returnKeys, businessObjectRestrictions);
 151  
 //          String actionUrls = getActionUrls(element, pkNames, businessObjectRestrictions);
 152  
 //ADDED (4 lines)
 153  0
         for (DocumentSearchResult aResult : result)
 154  
         {
 155  
             //TODO: where to get these from?
 156  0
             HtmlData returnUrl = new AnchorHtmlData();
 157  0
             String actionUrls = "";
 158  
 
 159  
 //TODO: convert columns either here or in the getColumns method
 160  
 //ADDED (3)
 161  0
             List<? extends Column> columns = components.getColumns();//getColumns();
 162  0
             List<KeyValueSort> keyValues = aResult.getResultContainers();
 163  0
             for (int i = 0; i < columns.size(); i++)
 164  
             {
 165  
 
 166  
 //            for (Iterator iterator = columns.iterator(); iterator.hasNext();) {
 167  
 
 168  
 //                Column col = (Column) iterator.next();
 169  
 //ADDED 3
 170  0
                 Column col = (Column) columns.get(i);
 171  0
                 KeyValueSort keyValue = keyValues.get(i);
 172  
 //Set values from keyvalue on column
 173  0
                 col.setPropertyValue(keyValue.getUserDisplayValue());
 174  
 
 175  0
                 Formatter formatter = col.getFormatter();
 176  
 
 177  
                 // pick off result column from result list, do formatting
 178  0
                 String propValue = KNSConstants.EMPTY_STRING;
 179  
 //                Object prop = ObjectUtils.getPropertyValue(element, col.getPropertyName());
 180  
 //ADDED
 181  0
                 Object prop = col.getPropertyValue();
 182  
 
 183  
                 // set comparator and formatter based on property type
 184  0
                 Class propClass = propertyTypes.get(col.getPropertyName());
 185  0
                 if (propClass == null)
 186  
                 {
 187  
                     try
 188  
                     {
 189  
 //                            propClass = ObjectUtils.getPropertyType( element, col.getPropertyName(), getPersistenceStructureService() );
 190  
 //                            propertyTypes.put( col.getPropertyName(), propClass );
 191  
                     } catch (Exception e)
 192  
                     {
 193  
 //                        throw new RuntimeException("Cannot access PropertyType for property " + "'" + col.getPropertyName() + "' " + " on an instance of '" + element.getClass().getName() + "'.", e);
 194  0
                     }
 195  
                 }
 196  
 
 197  
                 // formatters
 198  0
                 if (prop != null)
 199  
                 {
 200  
                     // for Booleans, always use BooleanFormatter
 201  0
                     if (prop instanceof Boolean)
 202  
                     {
 203  0
                         formatter = new BooleanFormatter();
 204  
                     }
 205  
 
 206  
                     // for Dates, always use DateFormatter
 207  0
                     if (prop instanceof Date)
 208  
                     {
 209  0
                         formatter = new DateFormatter();
 210  
                     }
 211  
 
 212  
                     // for collection, use the list formatter if a formatter hasn't been defined yet
 213  0
                     if (prop instanceof Collection && formatter == null)
 214  
                     {
 215  0
                         formatter = new CollectionFormatter();
 216  
                     }
 217  
 
 218  0
                     if (formatter != null)
 219  
                     {
 220  0
                         propValue = (String) formatter.format(prop);
 221  
                     } else
 222  
                     {
 223  0
                         propValue = prop.toString();
 224  
                     }
 225  
                 }
 226  
 
 227  
                 // comparator
 228  0
                 col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass));
 229  0
                 col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass));
 230  
 
 231  
 //                propValue = maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions);
 232  
 
 233  0
                 col.setPropertyValue(propValue);
 234  
 
 235  0
                 if (StringUtils.isNotBlank(propValue))
 236  
                 {
 237  
 //                    col.setColumnAnchor(getInquiryUrl(element, col.getPropertyName()));
 238  
 //ADDED (3 lines)
 239  0
                     AnchorHtmlData anchor = new AnchorHtmlData(KNSConstants.EMPTY_STRING, KNSConstants.EMPTY_STRING);
 240  
                     //TODO: change to grab URL from config variable
 241  0
                     if (StringUtils.isNotEmpty(keyValue.getValue()) && StringUtils.equals("routeHeaderId", keyValue.getkey()))
 242  
                     {
 243  0
                         anchor.setHref(StringUtils.getNestedString(keyValue.getValue(), "<a href=\"", "docId=") + "docId=" + keyValue.getUserDisplayValue());
 244  0
                         col.setMaxLength(100); //for now force this
 245  
                     }
 246  
 
 247  0
                     col.setColumnAnchor(anchor);
 248  
                 }
 249  
             }
 250  
 
 251  0
             ResultRow row = new ResultRow((List<Column>) columns, returnUrl.constructCompleteHtmlTag(), actionUrls);
 252  0
             row.setRowId(returnUrl.getName());
 253  
             // because of concerns of the BO being cached in session on the ResultRow,
 254  
             // let's only attach it when needed (currently in the case of export)
 255  0
             if (getBusinessObjectDictionaryService().isExportable(getBusinessObjectClass()))
 256  
             {
 257  
 //                    row.setBusinessObject(element);
 258  
             }
 259  
 
 260  
 //            boolean rowReturnable = isResultReturnable(element);
 261  
 //ADDED
 262  0
             boolean rowReturnable = true;
 263  0
             row.setRowReturnable(rowReturnable);
 264  0
             if (rowReturnable)
 265  
             {
 266  0
                 hasReturnableRow = true;
 267  
             }
 268  0
             resultTable.add(row);
 269  0
         }
 270  
 
 271  0
         lookupForm.setHasReturnableRow(hasReturnableRow);
 272  
 
 273  0
         return displayList;
 274  
                 //####END COPIED CODE#########
 275  
         }
 276  
 
 277  
 
 278  
         /**
 279  
          * This method ...
 280  
          *
 281  
          * @param lookupForm
 282  
          * @return
 283  
          */
 284  
         private DocSearchCriteriaDTO constructCriteria(Map<String,String> fieldsForLookup) {
 285  
                 //TODO: move this into adapter
 286  0
             DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO();
 287  0
             Map<String,String> fieldsToSet = new HashMap<String,String>();
 288  0
                 for (String formKey : fieldsForLookup.keySet()) {
 289  0
                         if(!(formKey.equalsIgnoreCase(KNSConstants.BACK_LOCATION) ||
 290  
                            formKey.equalsIgnoreCase(KNSConstants.DOC_FORM_KEY)) && StringUtils.isNotEmpty(fieldsForLookup.get(formKey))) {
 291  0
                                 fieldsToSet.put(formKey, fieldsForLookup.get(formKey));
 292  
                         }
 293  
                 }
 294  
                 //if we use DocSearchCriteriaDTO as object we shouldn't need this conversion stuff
 295  0
             for (String fieldToSet : fieldsToSet.keySet()) {
 296  
                         //need translation code here for certain fields
 297  0
                     String valueToSet = fieldsToSet.get(fieldToSet);
 298  
                         try {
 299  
                                 //TODO: temporary work around until is a criteria
 300  0
                                 if(fieldToSet.equals("documentType.name")) {
 301  0
                                         fieldToSet = "docTypeFullName";
 302  
                                 }
 303  0
                                 PropertyUtils.setNestedProperty(criteria, fieldToSet, valueToSet);
 304  0
                         } catch (IllegalAccessException e) {
 305  0
                                 e.printStackTrace();
 306  0
                         } catch (InvocationTargetException e) {
 307  0
                                 e.printStackTrace();
 308  0
                         } catch (NoSuchMethodException e) {
 309  0
                                 e.printStackTrace();
 310  0
                         }
 311  0
                 }
 312  0
                 return criteria;
 313  
         }
 314  
 
 315  
 
 316  
         /**
 317  
          * This overridden method ...
 318  
          *
 319  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject, java.lang.String)
 320  
          */
 321  
         @Override
 322  
         public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
 323  
                 //FIXME: ctk - make sure and check that it's ok to do this here.  I may move this out to the doc search processor
 324  0
                 if(KEWPropertyConstants.DOC_SEARCH_RESULT_PROPERTY_NAME_ROUTE_HEADER_ID.equals(propertyName)) {
 325  
 
 326  0
                         AnchorHtmlData link = new AnchorHtmlData();
 327  0
                         DocumentRouteHeaderValue doc = (DocumentRouteHeaderValue)bo;
 328  
                         //if !superuser
 329  0
                         Long routeHeaderId = doc.getRouteHeaderId();
 330  0
                         link.setDisplayText(routeHeaderId+"");
 331  
                         
 332  0
                         String href = ConfigContext.getCurrentContextConfig().getKRBaseURL()+"/"+
 333  
                         KEWConstants.DOC_HANDLER_REDIRECT_PAGE + "?" + KEWConstants.COMMAND_PARAMETER + "=" + 
 334  
                         KEWConstants.DOCSEARCH_COMMAND + "&" + KEWConstants.ROUTEHEADER_ID_PARAMETER + "=" + routeHeaderId;
 335  0
                         link.setHref(href);
 336  
 
 337  0
                         return link;
 338  
                 }
 339  
 
 340  0
                 return super.getInquiryUrl(bo, propertyName);
 341  
         }
 342  
 
 343  
 
 344  
         /**
 345  
          * This overridden method ...
 346  
          *
 347  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#setRows()
 348  
          */
 349  
         @Override
 350  
         protected void setRows() {
 351  
                 // TODO chris - this method should call the criteria processor adapter which will
 352  
                 //call the criteria processor (either standard or custom) and massage the data into the proper format
 353  
                 //this is called by setbo in super(which is called by form) so should be called when the page needs refreshing
 354  
 
 355  
                 //TODO: move over code that checks for doctype (actually should that be in the refresh, since that's where the doc type will be coming back to?)
 356  
 
 357  
 
 358  
                 //###START LOOKUP ROW CODE Not sure if we need these but they may be valuable for eventually forcing all standard field customization in the xml
 359  0
                 super.setRows();
 360  0
                 List<Row> lookupRows = new ArrayList<Row>();
 361  
                 //copy the current rows
 362  0
                 for (Row row : super.getRows()) {
 363  0
                         lookupRows.add(row);
 364  
                 }
 365  
                 //clear out
 366  0
                 super.getRows().clear();
 367  
                 //###END LOOKUP ROW CODE TODO: do something with lookupRows or delete above code
 368  
 
 369  
 
 370  0
         processor = new DocumentLookupCriteriaProcessorKEWAdapter();
 371  
 
 372  
 
 373  
 
 374  
                 //TODO: get this from parameters (or bo?)
 375  0
                 DocumentType docType = null;
 376  0
                 if(processor != null) {
 377  
                         //TODO: same Doc type
 378  
 //                        if(docType==oldDocType) {
 379  0
                         ((DocumentLookupCriteriaProcessorKEWAdapter)processor).setCriteriaProcessor(new StandardDocumentSearchCriteriaProcessor());
 380  
 //                        } else {
 381  
 
 382  
 //                        }
 383  
                 } else {
 384  
                         //TODO: same Doc type
 385  0
                         if(docType==null) {
 386  0
                                 ((DocumentLookupCriteriaProcessorKEWAdapter)processor).setCriteriaProcessor(new StandardDocumentSearchCriteriaProcessor());
 387  
                         }// else {
 388  
 
 389  
 //                        }
 390  
                 }
 391  
 
 392  
                 //call get rows
 393  0
                 List<Row> rows = processor.getRows(docType,lookupRows,false,false);
 394  0
                 super.getRows().addAll(rows);
 395  
 
 396  
                 //are we in basic or detailed, are we in super.
 397  
                 //TODO: ctk Add this code back in when KNS impacting changes are worked back
 398  
 
 399  
 
 400  
 
 401  
 
 402  
 
 403  0
         }
 404  
 
 405  
 
 406  
         /**
 407  
          * This overridden method allows for overriding what the clear logic does.
 408  
          *
 409  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#performClear()
 410  
          */
 411  
         @Override
 412  
         public void performClear(LookupForm lookupForm) {
 413  0
                 Map<String,String> fieldsToClear = new HashMap<String,String>();
 414  
 
 415  0
                 for (Row row : this.getRows()) {
 416  0
                         for (Field field : row.getFields()) {
 417  0
                                 fieldsToClear.put(field.getPropertyName(), field.getPropertyValue());
 418  
                         }
 419  
                 }
 420  
                 //TODO: also check if standard here (maybe from object if use criteria)
 421  0
                 if(this.processor==null) {
 422  0
                         super.performClear(lookupForm);
 423  
                 } else {
 424  0
                         DocSearchCriteriaDTO docCriteria = constructCriteria(fieldsToClear);
 425  0
                         if(StringUtils.isNotEmpty("documentType.name")) {
 426  
                                 //TODO: move these strings to constants (probably after moving this to criteria
 427  0
                                 getValidDocumentType(fieldsToClear.get("documentType.name")).getDocumentSearchGenerator().clearSearch(docCriteria);;
 428  
                         }
 429  
                 }
 430  0
         }
 431  
         /**
 432  
          *
 433  
          * This method is taken from DocSearch to retrieve a document type
 434  
          *
 435  
          * @param docTypeName
 436  
          * @return
 437  
          */
 438  
     private static DocumentType getValidDocumentType(String docTypeName) {
 439  0
         DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName);
 440  0
         if (documentType == null) {
 441  0
             throw new RuntimeException("Document Type invalid : " + docTypeName);
 442  
         }
 443  0
         return documentType;
 444  
     }
 445  
 }