Coverage Report - org.kuali.rice.kns.lookup.LookupUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupUtils
0%
0/358
0%
0/224
4.564
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.kns.lookup;
 18  
 
 19  
 import java.sql.Date;
 20  
 import java.sql.Timestamp;
 21  
 import java.text.ParseException;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Calendar;
 24  
 import java.util.Collection;
 25  
 import java.util.Comparator;
 26  
 import java.util.HashMap;
 27  
 import java.util.HashSet;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 import java.util.Set;
 32  
 import java.util.StringTokenizer;
 33  
 
 34  
 import org.apache.commons.lang.StringUtils;
 35  
 import org.apache.ojb.broker.query.Criteria;
 36  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 37  
 import org.kuali.rice.core.api.datetime.DateTimeService;
 38  
 import org.kuali.rice.core.api.encryption.EncryptionService;
 39  
 import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
 40  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 41  
 import org.kuali.rice.kns.bo.BusinessObject;
 42  
 import org.kuali.rice.kns.bo.BusinessObjectRelationship;
 43  
 import org.kuali.rice.kns.datadictionary.RelationshipDefinition;
 44  
 import org.kuali.rice.kns.datadictionary.control.ControlDefinition;
 45  
 import org.kuali.rice.kns.datadictionary.exception.UnknownBusinessClassAttributeException;
 46  
 import org.kuali.rice.kns.exception.ClassNotPersistableException;
 47  
 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
 48  
 import org.kuali.rice.kns.service.BusinessObjectMetaDataService;
 49  
 import org.kuali.rice.kns.service.DataDictionaryService;
 50  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 51  
 import org.kuali.rice.kns.service.PersistenceStructureService;
 52  
 import org.kuali.rice.kns.util.KNSConstants;
 53  
 import org.kuali.rice.kns.util.KNSPropertyConstants;
 54  
 import org.kuali.rice.kns.util.ObjectUtils;
 55  
 import org.kuali.rice.kns.web.comparator.NullValueComparator;
 56  
 import org.kuali.rice.kns.web.ui.Field;
 57  
 import org.kuali.rice.kns.web.ui.ResultRow;
 58  
 
 59  
 /**
 60  
  * Not a static utility class for Lookup related utilities and helper methods.
 61  
  */
 62  
 public class LookupUtils {
 63  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupUtils.class);
 64  
 
 65  
     private static DataDictionaryService dataDictionaryService;
 66  
     private static PersistenceStructureService persistenceStructureService;
 67  
     private static BusinessObjectDictionaryService businessObjectDictionaryService;
 68  
     private static BusinessObjectMetaDataService businessObjectMetaDataService;
 69  
     private static DateTimeService dateTimeService;
 70  
 
 71  0
     public LookupUtils() {
 72  
         // default constructor for Spring to call to start up initialization process
 73  0
     }
 74  
 
 75  
     public void setBusinessObjectDictionaryService(BusinessObjectDictionaryService businessObjectDictionaryService) {
 76  0
         LookupUtils.businessObjectDictionaryService = businessObjectDictionaryService;
 77  0
     }
 78  
 
 79  
     public void setDataDictionaryService(DataDictionaryService ddService) {
 80  0
         LookupUtils.dataDictionaryService = ddService;
 81  0
     }
 82  
 
 83  
     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
 84  0
         LookupUtils.persistenceStructureService = persistenceStructureService;
 85  0
     }
 86  
 
 87  
     public void setDateTimeService(DateTimeService dateTimeService) {
 88  0
                 LookupUtils.dateTimeService = dateTimeService;
 89  0
         }
 90  
 
 91  
         /**
 92  
      * Sets the businessObjectMetaDataService attribute value.
 93  
      * @param businessObjectMetaDataService The businessObjectMetaDataService to set.
 94  
      */
 95  
     public void setBusinessObjectMetaDataService(BusinessObjectMetaDataService businessObjectMetaDataService) {
 96  0
         LookupUtils.businessObjectMetaDataService = businessObjectMetaDataService;
 97  0
     }
 98  
 
 99  
     /**
 100  
     * Uses the DataDictionary to determine whether to force uppercase the value, and if it should, then it does the
 101  
     * uppercase, and returns the upper-cased value.
 102  
     *
 103  
     * @param dataObjectClass Parent DO class that the fieldName is a member of.
 104  
     * @param fieldName Name of the field to be forced to uppercase.
 105  
     * @param fieldValue Value of the field that may be uppercased.
 106  
     * @return The correctly uppercased fieldValue if it should be uppercased, otherwise fieldValue is returned unchanged.
 107  
     *
 108  
     */
 109  
    public static String forceUppercase(Class<?> dataObjectClass, String fieldName, String fieldValue) {
 110  
 
 111  
        // short-circuit to exit if there isnt enough information to do the forceUppercase
 112  0
        if (StringUtils.isBlank(fieldValue)) {
 113  0
            return fieldValue;
 114  
        }
 115  
 
 116  
        // parameter validation
 117  0
        if (dataObjectClass == null) {
 118  0
            throw new IllegalArgumentException("Parameter dataObjectClass passed in with null value.");
 119  
        }
 120  
        
 121  0
        if (StringUtils.isBlank(fieldName)) {
 122  0
            throw new IllegalArgumentException("Parameter fieldName passed in with empty value.");
 123  
        }
 124  
 
 125  0
        if (!dataDictionaryService.isAttributeDefined(dataObjectClass, fieldName).booleanValue()) {
 126  0
            return fieldValue;
 127  
        }
 128  
 
 129  
 
 130  0
        boolean forceUpperCase = false;
 131  
        try {
 132  0
            forceUpperCase = dataDictionaryService.getAttributeForceUppercase(dataObjectClass, fieldName).booleanValue();
 133  
        }
 134  0
        catch (UnknownBusinessClassAttributeException ubae) {
 135  
            // do nothing, don't alter the fieldValue
 136  0
        }
 137  0
        if (forceUpperCase && !fieldValue.endsWith(EncryptionService.ENCRYPTION_POST_PREFIX)) {
 138  0
            return fieldValue.toUpperCase();
 139  
        }
 140  
        
 141  0
        return fieldValue;
 142  
    }
 143  
 
 144  
    /**
 145  
     * Uses the DataDictionary to determine whether to force uppercase the values, and if it should, then it does the
 146  
     * uppercase, and returns the upper-cased Map of fieldname/fieldValue pairs.
 147  
     *
 148  
     * @param dataObjectClass Parent DO class that the fieldName is a member of.
 149  
     * @param fieldValues A Map<String,String> where the key is the fieldName and the value is the fieldValue.
 150  
     * @return The same Map is returned, with the appropriate values uppercased (if any).
 151  
     *
 152  
     */
 153  
    public static Map<String, String> forceUppercase(Class<?> dataObjectClass, Map<String, String> fieldValues) {
 154  0
        if (dataObjectClass == null) {
 155  0
            throw new IllegalArgumentException("Parameter boClass passed in with null value.");
 156  
        }
 157  
        
 158  0
        if (fieldValues == null) {
 159  0
            throw new IllegalArgumentException("Parameter fieldValues passed in with null value.");
 160  
        }
 161  
 
 162  0
        for (String fieldName : fieldValues.keySet()) {
 163  0
            fieldValues.put(fieldName, LookupUtils.forceUppercase(dataObjectClass, fieldName, (String) fieldValues.get(fieldName)));
 164  
        }
 165  
        
 166  0
        return fieldValues;
 167  
    }
 168  
 
 169  
     /**
 170  
      * This method applies the search results limit to the search criteria for this BO
 171  
      *
 172  
      * @param businessObjectClass BO class to search on / get limit for
 173  
      * @param criteria search criteria
 174  
      * @param platform database platform
 175  
      */
 176  
     public static void applySearchResultsLimit(Class businessObjectClass, Criteria criteria, DatabasePlatform platform) {
 177  0
         Integer limit = getSearchResultsLimit(businessObjectClass);
 178  0
         if (limit != null) {
 179  0
             platform.applyLimit(limit, criteria);
 180  
         }
 181  0
     }
 182  
 
 183  
     /**
 184  
      * This method applies the search results limit to the search criteria for this BO (JPA)
 185  
      *
 186  
      * @param businessObjectClass BO class to search on / get limit for
 187  
      * @param criteria search criteria
 188  
      */
 189  
     public static void applySearchResultsLimit(Class businessObjectClass, org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria) {
 190  0
         Integer limit = getSearchResultsLimit(businessObjectClass);
 191  0
         if (limit != null) {
 192  0
                 criteria.setSearchLimit(limit);
 193  
                 }
 194  0
     }
 195  
 
 196  
     /**
 197  
      * This method parses and returns the lookup result set limit, checking first for the limit
 198  
      * for the BO being looked up, and then the global application limit if there isn't a limit
 199  
      * specific to this BO.
 200  
      *
 201  
      * @param businessObjectClass BO class to search on / get limit for.  If the passed in type is not of type
 202  
      * {@link BusinessObject}, then the application-wide default limit is used.
 203  
      * @return result set limit (or null if there isn't one)
 204  
      */
 205  
     public static Integer getSearchResultsLimit(Class businessObjectClass) {
 206  0
         Integer limit = null;
 207  0
         if (BusinessObject.class.isAssignableFrom(businessObjectClass)) {
 208  0
             limit = getBusinessObjectSearchResultsLimit(businessObjectClass);
 209  
         }
 210  0
         if (limit == null) {
 211  0
             limit = getApplicationSearchResultsLimit();
 212  
         }
 213  0
         return limit;
 214  
     }
 215  
 
 216  
     /**
 217  
      *
 218  
      */
 219  
     private static Integer getApplicationSearchResultsLimit() {
 220  0
         String limitString = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KNSConstants.KNS_NAMESPACE, KNSConstants.DetailTypes.LOOKUP_PARM_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.LOOKUP_RESULTS_LIMIT);
 221  0
         if (limitString != null) {
 222  0
             return Integer.valueOf(limitString);
 223  
         }
 224  0
         return null;
 225  
     }
 226  
 
 227  
     /**
 228  
      * This method parses and returns the lookup result set limit for the passed in BO (if one exists)
 229  
      *
 230  
      * @param businessObjectClass
 231  
      * @return result set limit for this BO (or null if the BO doesn't have a limit)
 232  
      */
 233  
     private static Integer getBusinessObjectSearchResultsLimit(Class businessObjectClass) {
 234  0
         return businessObjectDictionaryService.getLookupResultSetLimit(businessObjectClass);
 235  
     }
 236  
 
 237  
     /**
 238  
      * This method the maximum rows per page in a multiple value lookup
 239  
      *
 240  
      * @see org.kuali.KNSConstants.SystemGroupParameterNames#MULTIPLE_VALUE_LOOKUP_RESULTS_PER_PAGE
 241  
      * @return
 242  
      */
 243  
     public static Integer getApplicationMaximumSearchResulsPerPageForMultipleValueLookups() {
 244  0
         String limitString = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KNSConstants.KNS_NAMESPACE, KNSConstants.DetailTypes.LOOKUP_PARM_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.MULTIPLE_VALUE_LOOKUP_RESULTS_PER_PAGE);
 245  0
         if (limitString != null) {
 246  0
             return Integer.valueOf(limitString);
 247  
         }
 248  0
         return null;
 249  
     }
 250  
 
 251  
     /**
 252  
      * This makes a , delimited String list of fields separated by a , into a List of target --> lookup readOnlyFieldsList.
 253  
      *
 254  
      * @param readOnlyFields
 255  
      * @return the List representation of the readOnlyFields  String provided.
 256  
      */
 257  
     public static List<String> translateReadOnlyFieldsToList(String readOnlyFieldsString) {
 258  0
         List<String> readOnlyFieldsList = new ArrayList<String>();
 259  0
         if (StringUtils.isNotEmpty(readOnlyFieldsString)) {
 260  0
             if (readOnlyFieldsString.indexOf(",") > 0) {
 261  0
                 StringTokenizer token = new StringTokenizer(readOnlyFieldsString, ",");
 262  0
                 while (token.hasMoreTokens()) {
 263  0
                     String element = token.nextToken();
 264  0
                     readOnlyFieldsList.add(element);
 265  0
                 }
 266  0
             }
 267  
             else {
 268  0
                 readOnlyFieldsList.add(readOnlyFieldsString);
 269  
             }
 270  
         }
 271  0
       return readOnlyFieldsList;
 272  
     }
 273  
 
 274  
     /**
 275  
      * This translates a , delimited list of pairs separated by a : into a Map of target --> lookup field conversions.
 276  
      *
 277  
      * @param conversionFields
 278  
      * @return the Map representation of the fieldConversions String provided.
 279  
      */
 280  
     public static Map translateFieldConversions(String fieldConversionsString) {
 281  0
         Map fieldConversionsMap = new HashMap();
 282  0
         if (StringUtils.isNotEmpty(fieldConversionsString)) {
 283  0
             if (fieldConversionsString.indexOf(",") > 0) {
 284  0
                 StringTokenizer token = new StringTokenizer(fieldConversionsString, ",");
 285  0
                 while (token.hasMoreTokens()) {
 286  0
                     String element = token.nextToken();
 287  0
                     fieldConversionsMap.put(element.substring(0, element.indexOf(":")), element.substring(element.indexOf(":") + 1));
 288  0
                 }
 289  0
             }
 290  
             else {
 291  0
                 fieldConversionsMap.put(fieldConversionsString.substring(0, fieldConversionsString.indexOf(":")), fieldConversionsString.substring(fieldConversionsString.indexOf(":") + 1));
 292  
             }
 293  
         }
 294  0
         return fieldConversionsMap;
 295  
     }
 296  
 
 297  
     @Deprecated
 298  
     public static Field setFieldQuickfinder(BusinessObject businessObject,
 299  
             String attributeName, Field field, List displayedFieldNames) {
 300  0
         return setFieldQuickfinder( businessObject, (String)null, false, 0, attributeName, field, displayedFieldNames );
 301  
     }
 302  
 
 303  
     @Deprecated
 304  
     public static Field setFieldQuickfinder(BusinessObject businessObject,
 305  
             String attributeName, Field field, List displayedFieldNames, SelectiveReferenceRefresher srr) {
 306  0
         return setFieldQuickfinder( businessObject, (String)null, false, 0, attributeName, field, displayedFieldNames, srr );
 307  
     }
 308  
 
 309  
     /**
 310  
      * Sets a fields quickfinder class and field conversions for an attribute.
 311  
      */
 312  
     @Deprecated
 313  
     public static Field setFieldQuickfinder(BusinessObject businessObject, String collectionName, boolean addLine, int index,
 314  
             String attributeName, Field field, List displayedFieldNames, SelectiveReferenceRefresher srr) {
 315  0
         field = setFieldQuickfinder(businessObject, collectionName, addLine, index, attributeName, field, displayedFieldNames);
 316  0
         if (srr != null) {
 317  0
             String collectionPrefix = "";
 318  0
             if ( collectionName != null ) {
 319  0
                 if (addLine) {
 320  0
                     collectionPrefix = KNSConstants.MAINTENANCE_ADD_PREFIX + collectionName + ".";
 321  
                 }
 322  
                 else {
 323  0
                     collectionPrefix = collectionName + "[" + index + "].";
 324  
                 }
 325  
             }
 326  0
             field.setReferencesToRefresh(convertReferencesToSelectCollectionToString(
 327  
                     srr.getAffectedReferencesFromLookup(businessObject, attributeName, collectionPrefix)));
 328  
         }
 329  0
         return field;
 330  
     }
 331  
 
 332  
     /**
 333  
      * Sets a fields quickfinder class and field conversions for an attribute.
 334  
      */
 335  
     @Deprecated
 336  
     public static Field setFieldQuickfinder(BusinessObject businessObject, String collectionName, boolean addLine, int index,
 337  
                                             String attributeName, Field field, List displayedFieldNames) {
 338  0
         boolean noLookup = false;
 339  0
         if (businessObject == null) {
 340  0
             return field;
 341  
         }
 342  
 
 343  0
         Boolean noLookupField = businessObjectDictionaryService.noLookupFieldLookup(businessObject.getClass(), attributeName);
 344  0
         if (noLookupField != null && noLookupField) {
 345  0
             noLookup = true;
 346  
         }
 347  
 
 348  0
          return setFieldQuickfinder(businessObject, collectionName, addLine, index, attributeName, field, displayedFieldNames, noLookup);
 349  
 
 350  
     }
 351  
 
 352  
     @Deprecated
 353  
     public static Field setFieldQuickfinder(BusinessObject businessObject, String collectionName, boolean addLine, int index, String attributeName, Field field, List displayedFieldNames, boolean noLookupField)
 354  
     {
 355  0
          if (businessObject == null) {
 356  0
             return field;
 357  
         }
 358  
 
 359  0
         if (noLookupField) {
 360  0
             return field;
 361  
         }
 362  0
         BusinessObjectRelationship relationship = null;
 363  0
         if ( LOG.isDebugEnabled() ) {
 364  0
             LOG.debug( "setFieldQuickfinder("+businessObject.getClass().getName()+","+attributeName+","+field+","+displayedFieldNames+")" );
 365  
         }
 366  
 
 367  0
         relationship = businessObjectMetaDataService.getBusinessObjectRelationship(businessObject, businessObject.getClass(), attributeName, "", false);
 368  
 
 369  0
         String collectionPrefix = "";
 370  0
         if ( collectionName != null ) {
 371  0
             if (addLine) {
 372  0
                 collectionPrefix = KNSConstants.MAINTENANCE_ADD_PREFIX + collectionName + ".";
 373  
             }
 374  
             else {
 375  0
                 collectionPrefix = collectionName + "[" + index + "].";
 376  
             }
 377  
         }
 378  
 
 379  0
         if (relationship == null) {
 380  0
             Class c = ObjectUtils.getPropertyType(businessObject, attributeName, persistenceStructureService);
 381  
 
 382  0
             if(c!=null) {
 383  0
                 if (attributeName.contains(".")) {
 384  0
                     attributeName = StringUtils.substringBeforeLast( attributeName, "." );
 385  
                 }
 386  
 
 387  0
                 RelationshipDefinition ddReference = businessObjectMetaDataService.getBusinessObjectRelationshipDefinition(businessObject, attributeName);
 388  0
                 relationship = businessObjectMetaDataService.getBusinessObjectRelationship(ddReference, businessObject, businessObject.getClass(), attributeName, "", false);
 389  0
                 if(relationship!=null) {
 390  0
                     field.setQuickFinderClassNameImpl(relationship.getRelatedClass().getName());
 391  0
                     field.setFieldConversions(generateFieldConversions( businessObject, collectionPrefix, relationship, field.getPropertyPrefix(), displayedFieldNames, null));
 392  0
                     field.setLookupParameters(generateLookupParameters( businessObject, collectionPrefix, relationship, field.getPropertyPrefix(), displayedFieldNames, null));
 393  0
                     field.setBaseLookupUrl(LookupUtils.getBaseLookupUrl(false));
 394  0
                     field.setImageSrc(businessObjectDictionaryService.getSearchIconOverride(businessObject.getClass()));
 395  
                 }
 396  
             }
 397  
 
 398  0
             return field;
 399  
         }
 400  0
         if (ObjectUtils.isNestedAttribute(attributeName)) {
 401  
             //first determine the prefix and the attribute we are referring to
 402  0
             String nestedAttributePrefix = StringUtils.substringBeforeLast(attributeName, ".");
 403  
 
 404  0
             field.setQuickFinderClassNameImpl(relationship.getRelatedClass().getName());
 405  0
             field.setFieldConversions( generateFieldConversions( businessObject, collectionPrefix, relationship, field.getPropertyPrefix(), displayedFieldNames, nestedAttributePrefix ) );
 406  0
             field.setLookupParameters( generateLookupParameters( businessObject, collectionPrefix, relationship, field.getPropertyPrefix(), displayedFieldNames, nestedAttributePrefix ) );
 407  0
             field.setBaseLookupUrl(LookupUtils.getBaseLookupUrl(false));
 408  0
         } else {
 409  0
             field.setQuickFinderClassNameImpl(relationship.getRelatedClass().getName());
 410  0
             field.setFieldConversions( generateFieldConversions( businessObject, collectionPrefix, relationship, field.getPropertyPrefix(), displayedFieldNames, null ) );
 411  0
             field.setLookupParameters( generateLookupParameters( businessObject, collectionPrefix, relationship, field.getPropertyPrefix(), displayedFieldNames, null ) );
 412  0
             field.setBaseLookupUrl(LookupUtils.getBaseLookupUrl(false));
 413  
         }
 414  0
         field.setImageSrc(businessObjectDictionaryService.getSearchIconOverride(businessObject.getClass()));
 415  
 
 416  0
         return field;
 417  
     }
 418  
 
 419  0
     private static String BASE_LOOKUP_ACTION_URL = null;
 420  0
     private static String BASE_MULTIPLE_VALUE_LOOKUP_ACTION_URL = null;
 421  0
     private static String BASE_INQUIRY_ACTION_URL = null;
 422  
     
 423  
     @Deprecated
 424  
     public static String getBaseLookupUrl(boolean isMultipleValue) {
 425  0
         ConfigurationService kualiConfigurationService = KNSServiceLocator.getKualiConfigurationService();
 426  0
             if ( isMultipleValue ) {
 427  0
                     if ( BASE_MULTIPLE_VALUE_LOOKUP_ACTION_URL == null ) {
 428  0
                             String lookupUrl = kualiConfigurationService.getPropertyString(KNSConstants.APPLICATION_URL_KEY);
 429  0
                             if (!lookupUrl.endsWith("/")) {
 430  0
                                     lookupUrl = lookupUrl + "/";
 431  
                             }
 432  0
                                 lookupUrl += "kr/" + KNSConstants.MULTIPLE_VALUE_LOOKUP_ACTION;
 433  0
                                 BASE_MULTIPLE_VALUE_LOOKUP_ACTION_URL = lookupUrl;
 434  
                     }
 435  0
                     return BASE_MULTIPLE_VALUE_LOOKUP_ACTION_URL;
 436  
             } else {
 437  0
                     if ( BASE_LOOKUP_ACTION_URL == null ) {
 438  0
                             String lookupUrl = kualiConfigurationService.getPropertyString(KNSConstants.APPLICATION_URL_KEY);
 439  0
                             if (!lookupUrl.endsWith("/")) {
 440  0
                                     lookupUrl = lookupUrl + "/";
 441  
                             }
 442  0
                                 lookupUrl += "kr/" + KNSConstants.LOOKUP_ACTION;
 443  0
                                 BASE_LOOKUP_ACTION_URL = lookupUrl;
 444  
                     }
 445  0
                     return BASE_LOOKUP_ACTION_URL;
 446  
             }
 447  
     }
 448  
 
 449  
     @Deprecated
 450  
     public static String getBaseInquiryUrl() {
 451  0
             if ( BASE_INQUIRY_ACTION_URL == null ) {
 452  0
                     StringBuffer inquiryUrl = new StringBuffer( 
 453  
                                     KNSServiceLocator.getKualiConfigurationService().getPropertyString(KNSConstants.APPLICATION_URL_KEY) );
 454  0
                         if (inquiryUrl.charAt(inquiryUrl.length()-1) != '/' ) {
 455  0
                                 inquiryUrl.append( '/' );
 456  
                         }
 457  0
                         inquiryUrl.append("kr/");
 458  0
                         inquiryUrl.append( KNSConstants.INQUIRY_ACTION );
 459  0
                         BASE_INQUIRY_ACTION_URL = inquiryUrl.toString();
 460  
             }
 461  0
             return BASE_INQUIRY_ACTION_URL;
 462  
     }
 463  
 
 464  
     public static String transformLookupUrlToMultiple(String lookupUrl) {
 465  0
             return lookupUrl.replace("kr/" + KNSConstants.LOOKUP_ACTION, "kr/" + KNSConstants.MULTIPLE_VALUE_LOOKUP_ACTION);
 466  
     }
 467  
 
 468  
     /**
 469  
      * Sets whether a field should have direct inquiries enabled.  The direct inquiry is the functionality on a page such that if the primary key for
 470  
      * a quickfinder is filled in and the direct inquiry button is pressed, then a new window will popup showing an inquiry page without going through
 471  
      * the lookup first.
 472  
      *
 473  
      * For this method to work properly, it must be called after setFieldQuickfinder
 474  
      * //TODO: chb: that should not be the case -- the relationship object the two rely upon should be established outside of the lookup/quickfinder code
 475  
      *
 476  
      *
 477  
      * @param field
 478  
      */
 479  
     private static void setFieldDirectInquiry(Field field) {
 480  0
         if (StringUtils.isNotBlank(field.getFieldConversions())) {
 481  0
             boolean directInquiriesEnabled = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(KNSConstants.KNS_NAMESPACE, KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.ENABLE_DIRECT_INQUIRIES_IND);
 482  0
             if (directInquiriesEnabled) {
 483  0
                 if (StringUtils.isNotBlank(field.getFieldConversions())) {
 484  0
                     String fieldConversions = field.getFieldConversions();
 485  0
                     String newInquiryParameters = KNSConstants.EMPTY_STRING;
 486  0
                     String[] conversions = StringUtils.split(fieldConversions, KNSConstants.FIELD_CONVERSIONS_SEPARATOR);
 487  
 
 488  0
                     for (int l = 0; l < conversions.length; l++) {
 489  0
                         String conversion = conversions[l];
 490  
                         //String[] conversionPair = StringUtils.split(conversion, KNSConstants.FIELD_CONVERSION_PAIR_SEPARATOR);
 491  0
                         String[] conversionPair = StringUtils.split(conversion, KNSConstants.FIELD_CONVERSION_PAIR_SEPARATOR, 2);
 492  0
                         String conversionFrom = conversionPair[0];
 493  0
                         String conversionTo = conversionPair[1];
 494  0
                         newInquiryParameters += (conversionTo + KNSConstants.FIELD_CONVERSION_PAIR_SEPARATOR + conversionFrom);
 495  
 
 496  0
                         if (l < conversions.length - 1) {
 497  0
                             newInquiryParameters += KNSConstants.FIELD_CONVERSIONS_SEPARATOR;
 498  
                         }
 499  
                     }
 500  
 
 501  0
                     field.setInquiryParameters(newInquiryParameters);
 502  
                 }
 503  
             }
 504  0
             field.setFieldDirectInquiryEnabled(directInquiriesEnabled);
 505  0
         }
 506  
         else {
 507  0
             field.setFieldDirectInquiryEnabled(false);
 508  
         }
 509  0
     }
 510  
 
 511  
     /**
 512  
      *
 513  
      * @param field
 514  
      * @return the altered Field object
 515  
      */
 516  
     public static Field setFieldDirectInquiry(BusinessObject businessObject, String attributeName, Field field)
 517  
     {
 518  0
                 if (businessObject == null)
 519  
                 {
 520  0
             return field;
 521  
         }
 522  
 
 523  0
         Boolean noDirectInquiry = businessObjectDictionaryService.noDirectInquiryFieldLookup(businessObject.getClass(), attributeName);
 524  
         //check if noDirectInquiry is present and true, but if it's not set in existing data dictionary definitions, don't create a direct inquiry
 525  0
         if (noDirectInquiry != null && noDirectInquiry.booleanValue() || noDirectInquiry == null) {
 526  0
             return field;
 527  
         }
 528  
 
 529  0
         setFieldDirectInquiry(field);
 530  
 
 531  0
         return field;
 532  
     }
 533  
 
 534  0
     private static Map<Class,Map<String,Map>> referencesForForeignKey = new HashMap<Class, Map<String,Map>>();
 535  
 
 536  
     @Deprecated
 537  
     public static Map getPrimitiveReference(BusinessObject businessObject, String attributeName) {
 538  0
         Map chosenReferenceByKeySize = new HashMap();
 539  0
         Map chosenReferenceByFieldName = new HashMap();
 540  
 
 541  0
         Map referenceClasses = null;
 542  
 
 543  
         try {
 544  
             // add special caching of these relationships since the Spring caching is so expensive
 545  0
             Map<String,Map> propMap = referencesForForeignKey.get(businessObject.getClass());
 546  0
             if ( propMap == null ) {
 547  0
                 propMap = new HashMap<String, Map>();
 548  0
                 referencesForForeignKey.put(businessObject.getClass(), propMap);
 549  
             }
 550  0
             if ( propMap.containsKey(attributeName) ) {
 551  0
                 referenceClasses = propMap.get( attributeName );
 552  
             } else {
 553  
                     //KFSMI-709: Make Inquiry Framework use BusinessObjectMetadataService instead of just PersistenceStructureService
 554  0
                     referenceClasses = businessObjectMetaDataService.getReferencesForForeignKey(businessObject, attributeName);
 555  0
                     if(referenceClasses==null || referenceClasses.isEmpty()) {
 556  0
                         if ( persistenceStructureService.isPersistable(businessObject.getClass()) ) {
 557  0
                             referenceClasses = persistenceStructureService.getReferencesForForeignKey(businessObject.getClass(), attributeName);
 558  
                         }
 559  
                     }
 560  0
                 propMap.put(attributeName, referenceClasses);
 561  
             }
 562  0
         } catch ( ClassNotPersistableException ex ) {
 563  
             // do nothing, there is no quickfinder
 564  0
             Map<String,Map> propMap = referencesForForeignKey.get(businessObject.getClass());
 565  0
             propMap.put(attributeName, null);
 566  0
         }
 567  
 
 568  
         // if field is not fk to any reference class, return field object w no quickfinder
 569  0
         if (referenceClasses == null || referenceClasses.isEmpty()) {
 570  0
             return chosenReferenceByKeySize;
 571  
         }
 572  
 
 573  
         /*
 574  
          * if field is fk to more than one reference, take the class with the least # of pk fields, this should give the correct
 575  
          * grain for the attribute
 576  
          */
 577  0
         int minKeys = Integer.MAX_VALUE;
 578  0
         for (Iterator iter = referenceClasses.keySet().iterator(); iter.hasNext();) {
 579  0
             String attr = (String) iter.next();
 580  0
             Class clazz = (Class) referenceClasses.get(attr);
 581  0
             List pkNames = businessObjectMetaDataService.listPrimaryKeyFieldNames(clazz);
 582  
 
 583  
             // Compare based on key size.
 584  0
             if (pkNames.size() < minKeys) {
 585  0
                 minKeys = pkNames.size();
 586  0
                 chosenReferenceByKeySize.clear();
 587  0
                 chosenReferenceByKeySize.put(attr, clazz);
 588  
             }
 589  
 
 590  
             // Compare based on field name.
 591  0
             if (attributeName.startsWith(attr)) {
 592  0
                 chosenReferenceByFieldName.clear();
 593  0
                 chosenReferenceByFieldName.put(attr, clazz);
 594  
             }
 595  0
         }
 596  
 
 597  
         // If a compatible key was found based on field names, prefer it, otherwise use choice by key size.
 598  0
         return chosenReferenceByFieldName.isEmpty() ? chosenReferenceByKeySize : chosenReferenceByFieldName;
 599  
     }
 600  
 
 601  
     /**
 602  
      *
 603  
      * This method walks through the nested attribute and finds the last business object in the chain and returns it (excluding the
 604  
      * last parameter which is the actual attribute)
 605  
      *
 606  
      * @param attributeName
 607  
      * @return
 608  
      */
 609  
     public static BusinessObject getNestedBusinessObject(BusinessObject bo, String attributeName) {
 610  0
         String[] nestedAttributes = StringUtils.split(attributeName, ".");
 611  
 
 612  0
         BusinessObject childBO = null;
 613  0
         String attributeRefName = "";
 614  0
         Class clazz = null;
 615  0
         if (nestedAttributes.length > 1) {
 616  0
             String attributeStringSoFar = "";
 617  0
             for (int i = 0; i < nestedAttributes.length - 1; i++) {
 618  
                 // we need to build a string of the attribute names depending on which iteration we're in.
 619  
                 // so if the original attributeName string we're using is "a.b.c.d.e", then first iteration would use
 620  
                 // "a", 2nd "a.b", 3rd "a.b.c", etc.
 621  0
                 if (i != 0) {
 622  0
                     attributeStringSoFar = attributeStringSoFar + ".";
 623  
                 }
 624  0
                 attributeStringSoFar = attributeStringSoFar + nestedAttributes[i];
 625  
 
 626  0
                 clazz = ObjectUtils.getPropertyType( bo, attributeStringSoFar, persistenceStructureService );
 627  
 
 628  0
                 if (clazz != null && BusinessObject.class.isAssignableFrom(clazz)) {
 629  
                     try {
 630  0
                             childBO = (BusinessObject) ObjectUtils.createNewObjectFromClass(clazz);
 631  
                     }
 632  0
                     catch (Exception e) {
 633  0
                         return null;
 634  0
                     }
 635  
                 }
 636  
             }
 637  
         }
 638  0
         return childBO;
 639  
     }
 640  
 
 641  
     public static Class getNestedReferenceClass(BusinessObject businessObject, String attributeName) {
 642  0
         BusinessObject bo = getNestedBusinessObject(businessObject, attributeName);
 643  0
         return null == bo ? null : bo.getClass();
 644  
     }
 645  
 
 646  
     @Deprecated
 647  
     private static String generateFieldConversions(BusinessObject businessObject, String collectionName, BusinessObjectRelationship relationship, String propertyPrefix, List displayedFieldNames, String nestedObjectPrefix) {
 648  0
         String fieldConversions = "";
 649  
 
 650  0
         if ( LOG.isDebugEnabled() ) {
 651  0
             LOG.debug( "generateFieldConversions(" + businessObject.getClass().getName() + "," + collectionName + ",\n" + relationship + "\n," + propertyPrefix + "," + displayedFieldNames + "," + nestedObjectPrefix + ")" );
 652  
         }
 653  
 
 654  
         // get the references for the given property
 655  0
         for ( Map.Entry<String,String> entry : relationship.getParentToChildReferences().entrySet() ) {
 656  0
             String fromField = entry.getValue();
 657  0
             String toField = entry.getKey();
 658  
 
 659  
             // find the displayed to field mapping
 660  0
             if (!displayedFieldNames.contains(toField)) {
 661  0
                 toField = translateToDisplayedField(businessObject.getClass(), toField, displayedFieldNames);
 662  
             }
 663  
 
 664  0
             if (StringUtils.isNotBlank(fieldConversions)) {
 665  0
                 fieldConversions += ",";
 666  
             }
 667  
 
 668  0
             if ( StringUtils.isNotEmpty( propertyPrefix ) ) {
 669  0
                 toField = propertyPrefix + "." + toField;
 670  
             }
 671  
 
 672  0
             if ( StringUtils.isNotEmpty( collectionName ) ) {
 673  0
                 toField = collectionName + toField;
 674  
             }
 675  
 
 676  0
             fieldConversions += fromField + ":" + toField;
 677  0
         }
 678  
 
 679  0
         return fieldConversions;
 680  
     }
 681  
 
 682  
     @Deprecated
 683  
     private static String generateLookupParameters(BusinessObject businessObject, String collectionName, BusinessObjectRelationship relationship, String propertyPrefix, List displayedFieldNames, String nestedObjectPrefix) {
 684  
 
 685  0
         String lookupParameters = "";
 686  
 
 687  0
         List displayedQFFieldNames = businessObjectDictionaryService.getLookupFieldNames(relationship.getRelatedClass());
 688  0
         for ( Map.Entry<String,String> entry : relationship.getParentToChildReferences().entrySet() ) {
 689  0
             String fromField = entry.getKey();
 690  0
             String toField = entry.getValue();
 691  
 
 692  0
             if ( relationship.getUserVisibleIdentifierKey() == null || relationship.getUserVisibleIdentifierKey().equals( fromField ) ) {
 693  
                 // find the displayed from field mapping
 694  0
                 if (!displayedFieldNames.contains(fromField)) {
 695  0
                     fromField = translateToDisplayedField(businessObject.getClass(), fromField, displayedFieldNames);
 696  
                 }
 697  
 
 698  
                 // translate to field
 699  0
                 if (displayedQFFieldNames != null && !displayedQFFieldNames.contains(toField)) {
 700  0
                     toField = translateToDisplayedField(relationship.getRelatedClass(), toField, displayedQFFieldNames);
 701  
                 }
 702  
 
 703  0
                 if (StringUtils.isNotBlank(lookupParameters)) {
 704  0
                     lookupParameters += ",";
 705  
                 }
 706  
 
 707  0
                 if (propertyPrefix != null && !propertyPrefix.equals("")) {
 708  0
                     fromField = propertyPrefix + "." + fromField;
 709  
                 }
 710  
 
 711  0
                 if ( StringUtils.isNotEmpty( collectionName ) ) {
 712  0
                     fromField = collectionName + fromField;
 713  
                 }
 714  
 
 715  0
                 lookupParameters += fromField + ":" + toField;
 716  
             }
 717  0
         }
 718  
 
 719  0
         return lookupParameters;
 720  
     }
 721  
 
 722  
     @Deprecated
 723  
     private static String translateToDisplayedField(Class businessObjectClass, String fieldName, List displayedFieldNames) {        
 724  0
         if ( persistenceStructureService.isPersistable(businessObjectClass) ) {
 725  0
             Map nestedFkMap = persistenceStructureService.getNestedForeignKeyMap(businessObjectClass);
 726  
 
 727  
             // translate to primitive fk if nested
 728  
             /*
 729  
              * if (ObjectUtils.isNestedAttribute(fieldName) && nestedFkMap.containsKey(fieldName)) { fieldName = (String)
 730  
              * nestedFkMap.get(fieldName); }
 731  
              */
 732  
 
 733  0
             if (!displayedFieldNames.contains(fieldName)) {
 734  0
                 for (Iterator iterator = displayedFieldNames.iterator(); iterator.hasNext();) {
 735  0
                     String dispField = (String) iterator.next();
 736  
 
 737  0
                     if (nestedFkMap.containsKey(dispField) && nestedFkMap.get(dispField).equals(fieldName)) {
 738  0
                         fieldName = dispField;
 739  
                     }
 740  0
                 }
 741  
             }
 742  
         }
 743  
 
 744  0
         return fieldName;
 745  
     }
 746  
 
 747  
     public static String convertReferencesToSelectCollectionToString(Collection<String> referencesToRefresh) {
 748  0
         StringBuilder buf = new StringBuilder();
 749  0
         for (String reference : referencesToRefresh) {
 750  0
             buf.append(reference).append(KNSConstants.REFERENCES_TO_REFRESH_SEPARATOR);
 751  
         }
 752  0
         if (!referencesToRefresh.isEmpty()) {
 753  
             // we appended one too many separators, remove it
 754  0
             buf.delete(buf.length() - KNSConstants.REFERENCES_TO_REFRESH_SEPARATOR.length(), buf.length());
 755  
         }
 756  0
         return buf.toString();
 757  
     }
 758  
 
 759  
     public static String convertSetOfObjectIdsToString(Set<String> objectIds) {
 760  0
         if (objectIds.isEmpty()) {
 761  0
             return "";
 762  
         }
 763  0
         StringBuilder buf = new StringBuilder();
 764  0
         for (String objectId : objectIds) {
 765  0
             if (objectId.contains(KNSConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR)) {
 766  0
                 throw new RuntimeException("object ID " + objectId + " contains the selected obj ID separator");
 767  
             }
 768  0
             buf.append(objectId).append(KNSConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR);
 769  
         }
 770  
         // added one extra separator, remove it
 771  0
         buf.delete(buf.length() - KNSConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR.length(), buf.length());
 772  
 
 773  0
         return buf.toString();
 774  
     }
 775  
 
 776  
     public static Set<String> convertStringOfObjectIdsToSet(String objectIdsString) {
 777  0
         Set<String> set = new HashSet<String>();
 778  
 
 779  0
         if (StringUtils.isNotBlank(objectIdsString)) {
 780  0
             String[] objectIds = StringUtils.splitByWholeSeparator(objectIdsString, KNSConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR);
 781  0
             for (String objectId : objectIds) {
 782  0
                 set.add(objectId);
 783  
             }
 784  
         }
 785  0
         return set;
 786  
     }
 787  
 
 788  
     /**
 789  
      * Given a list of results from a lookup, determines the best comparator to use on the String values of each of these columns
 790  
      *
 791  
      * This method exists because each cell (represented by the Column object) lists the comparator that should be used within it based on the property value class,
 792  
      * so we gotta go thru the whole list and determine the best comparator to use
 793  
      *
 794  
      * @param resultsTable
 795  
      * @param column
 796  
      * @return
 797  
      */
 798  
     public static Comparator findBestValueComparatorForColumn(List<ResultRow> resultTable, int column) {
 799  
         // BIG HACK
 800  0
         Comparator comp = NullValueComparator.getInstance();
 801  0
         for (ResultRow row : resultTable) {
 802  0
             Comparator tempComp = row.getColumns().get(column).getValueComparator();
 803  0
             if (tempComp != null && !NullValueComparator.class.equals(tempComp.getClass())) {
 804  0
                 return tempComp;
 805  
             }
 806  0
         }
 807  0
         return comp;
 808  
     }
 809  
 
 810  
     /**
 811  
      * Given 3 sets of object IDs: the set of selected object IDs before rendering the current page,
 812  
      * the set of object IDs rendered on the page, and the set of object IDs selected on the page, computes
 813  
      * the total set of selected object IDs.
 814  
      *
 815  
      * Instead of storing it in a set, returns it in a map with the selected object ID as both the key and value
 816  
      * @param previouslySelectedObjectIds
 817  
      * @param displayedObjectIds
 818  
      * @param selectedObjectIds
 819  
      * @return
 820  
      */
 821  
     public static Map<String, String> generateCompositeSelectedObjectIds(Set<String> previouslySelectedObjectIds, Set<String> displayedObjectIds, Set<String> selectedObjectIds) {
 822  0
         Map<String, String> tempMap = new HashMap<String, String>();
 823  
         // Equivalent to the set operation:
 824  
         // (P - D) union C, where - is the set difference operator
 825  
         // P is the list of object IDs previously passed in, D is the set of displayed object IDs, and C is the set of checked obj IDs
 826  
         // since HTML does not pass a value for non-selected dcheckboxes
 827  
 
 828  
         // first build a map w/ all the previouslySelectedObjectIds as keys
 829  0
         for (String previouslySelectedObjectId : previouslySelectedObjectIds) {
 830  0
             tempMap.put(previouslySelectedObjectId, previouslySelectedObjectId);
 831  
         }
 832  
         // then remove all the displayed elements (any selected displayed elements will be added back in the next loop)
 833  0
         for (String displayedObjectId : displayedObjectIds) {
 834  0
             tempMap.remove(displayedObjectId);
 835  
         }
 836  
         // put back the selected IDs
 837  0
         for (String selectedObjectId : selectedObjectIds) {
 838  0
             tempMap.put(selectedObjectId, selectedObjectId);
 839  
         }
 840  0
         return tempMap;
 841  
     }
 842  
 
 843  
     /**
 844  
      * Removes fields identified in the data dictionary as hidden from the lookup field values.
 845  
      * (This will remove Universal User ID and Person name from search requests when a user ID is entered.)
 846  
      *
 847  
      * @param fieldValues
 848  
      */
 849  
     public static void removeHiddenCriteriaFields( Class businessObjectClass, Map fieldValues ) {
 850  0
         List<String> lookupFieldAttributeList = businessObjectMetaDataService.getLookupableFieldNames(businessObjectClass);
 851  0
         if (lookupFieldAttributeList != null) {
 852  0
             for (Iterator iter = lookupFieldAttributeList.iterator(); iter.hasNext();) {
 853  0
                 String attributeName = (String) iter.next();
 854  0
                 if (fieldValues.containsKey(attributeName)) {
 855  0
                     ControlDefinition controlDef = dataDictionaryService.getAttributeControlDefinition(businessObjectClass, attributeName);
 856  0
                     if (controlDef != null && controlDef.isHidden() ) {
 857  0
                         fieldValues.remove(attributeName);
 858  
                     }
 859  
                 }
 860  0
             }
 861  
         }
 862  0
         }
 863  
 
 864  
     /**
 865  
      * Determines what Timestamp should be used for active queries on effective dated records. Determination made as
 866  
      * follows:
 867  
      * <ul>
 868  
      *   <li>Use activeAsOfDate value from search values Map if value is not empty</li>
 869  
      *   <li>If search value given, try to convert to sql date, if conversion fails, try to convert to Timestamp</li>
 870  
      *   <li>If search value empty, use current Date</li>
 871  
      *   <li>If Timestamp value not given, create Timestamp from given Date setting the time as 1 second before midnight
 872  
      * </ul>
 873  
      * 
 874  
      * @param searchValues - Map containing search key/value pairs
 875  
      * @return Timestamp to be used for active criteria
 876  
      */
 877  
         public static Timestamp getActiveDateTimestampForCriteria(Map searchValues) {
 878  0
                 Date activeDate = dateTimeService.getCurrentSqlDate();
 879  0
                 Timestamp activeTimestamp = null;
 880  0
                 if (searchValues.containsKey(KNSPropertyConstants.ACTIVE_AS_OF_DATE)) {
 881  0
                         String activeAsOfDate = (String) searchValues.get(KNSPropertyConstants.ACTIVE_AS_OF_DATE);
 882  0
                         if (StringUtils.isNotBlank(activeAsOfDate)) {
 883  
                                 try {
 884  0
                                         activeDate = dateTimeService.convertToSqlDate(ObjectUtils.clean(activeAsOfDate));
 885  0
                                 } catch (ParseException e) {
 886  
                                         // try to parse as timestamp
 887  
                                         try {
 888  0
                                                 activeTimestamp = dateTimeService.convertToSqlTimestamp(ObjectUtils.clean(activeAsOfDate));
 889  0
                                         } catch (ParseException e1) {
 890  0
                                                 throw new RuntimeException("Unable to convert date: " + ObjectUtils.clean(activeAsOfDate));
 891  0
                                         }
 892  0
                                 }
 893  
                         }
 894  
                 }
 895  
 
 896  
                 // if timestamp not given set to 1 second before midnight on the given date
 897  0
                 if (activeTimestamp == null) {
 898  0
                         Calendar cal = Calendar.getInstance();
 899  0
                         cal.setTime(activeDate);
 900  0
                         cal.set(Calendar.HOUR, cal.getMaximum(Calendar.HOUR));
 901  0
                         cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
 902  0
                         cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
 903  
 
 904  0
                         activeTimestamp = new Timestamp(cal.getTime().getTime());
 905  
                 }
 906  
 
 907  0
                 return activeTimestamp;
 908  
         }
 909  
 }