Coverage Report - org.kuali.rice.krad.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.krad.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.krad.bo.BusinessObject;
 42  
 import org.kuali.rice.krad.bo.BusinessObjectRelationship;
 43  
 import org.kuali.rice.krad.datadictionary.RelationshipDefinition;
 44  
 import org.kuali.rice.krad.datadictionary.control.ControlDefinition;
 45  
 import org.kuali.rice.krad.datadictionary.exception.UnknownBusinessClassAttributeException;
 46  
 import org.kuali.rice.krad.exception.ClassNotPersistableException;
 47  
 import org.kuali.rice.krad.service.BusinessObjectDictionaryService;
 48  
 import org.kuali.rice.krad.service.BusinessObjectMetaDataService;
 49  
 import org.kuali.rice.krad.service.DataDictionaryService;
 50  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 51  
 import org.kuali.rice.krad.service.PersistenceStructureService;
 52  
 import org.kuali.rice.krad.util.KRADConstants;
 53  
 import org.kuali.rice.krad.util.KRADPropertyConstants;
 54  
 import org.kuali.rice.krad.util.ObjectUtils;
 55  
 import org.kuali.rice.krad.web.comparator.NullValueComparator;
 56  
 import org.kuali.rice.krad.web.ui.Field;
 57  
 import org.kuali.rice.krad.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(KRADConstants.KRAD_NAMESPACE, KRADConstants.DetailTypes.LOOKUP_PARM_DETAIL_TYPE, KRADConstants.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.KRADConstants.SystemGroupParameterNames#MULTIPLE_VALUE_LOOKUP_RESULTS_PER_PAGE
 241  
      * @return
 242  
      */
 243  
     public static Integer getApplicationMaximumSearchResulsPerPageForMultipleValueLookups() {
 244  0
         String limitString = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KRADConstants.KRAD_NAMESPACE, KRADConstants.DetailTypes.LOOKUP_PARM_DETAIL_TYPE, KRADConstants.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 = KRADConstants.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 = KRADConstants.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 = KRADServiceLocator.getKualiConfigurationService();
 426  0
             if ( isMultipleValue ) {
 427  0
                     if ( BASE_MULTIPLE_VALUE_LOOKUP_ACTION_URL == null ) {
 428  0
                             String lookupUrl = kualiConfigurationService.getPropertyString(KRADConstants.APPLICATION_URL_KEY);
 429  0
                             if (!lookupUrl.endsWith("/")) {
 430  0
                                     lookupUrl = lookupUrl + "/";
 431  
                             }
 432  0
                                 lookupUrl += "kr/" + KRADConstants.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(KRADConstants.APPLICATION_URL_KEY);
 439  0
                             if (!lookupUrl.endsWith("/")) {
 440  0
                                     lookupUrl = lookupUrl + "/";
 441  
                             }
 442  0
                                 lookupUrl += "kr/" + KRADConstants.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  
                                     KRADServiceLocator.getKualiConfigurationService().getPropertyString(KRADConstants.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( KRADConstants.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/" + KRADConstants.LOOKUP_ACTION, "kr/" + KRADConstants.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(
 482  
                     KRADConstants.KRAD_NAMESPACE, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KRADConstants.SystemGroupParameterNames.ENABLE_DIRECT_INQUIRIES_IND);
 483  0
             if (directInquiriesEnabled) {
 484  0
                 if (StringUtils.isNotBlank(field.getFieldConversions())) {
 485  0
                     String fieldConversions = field.getFieldConversions();
 486  0
                     String newInquiryParameters = KRADConstants.EMPTY_STRING;
 487  0
                     String[] conversions = StringUtils.split(fieldConversions, KRADConstants.FIELD_CONVERSIONS_SEPARATOR);
 488  
 
 489  0
                     for (int l = 0; l < conversions.length; l++) {
 490  0
                         String conversion = conversions[l];
 491  
                         //String[] conversionPair = StringUtils.split(conversion, KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR);
 492  0
                         String[] conversionPair = StringUtils.split(conversion, KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR, 2);
 493  0
                         String conversionFrom = conversionPair[0];
 494  0
                         String conversionTo = conversionPair[1];
 495  0
                         newInquiryParameters += (conversionTo + KRADConstants.FIELD_CONVERSION_PAIR_SEPARATOR + conversionFrom);
 496  
 
 497  0
                         if (l < conversions.length - 1) {
 498  0
                             newInquiryParameters += KRADConstants.FIELD_CONVERSIONS_SEPARATOR;
 499  
                         }
 500  
                     }
 501  
 
 502  0
                     field.setInquiryParameters(newInquiryParameters);
 503  
                 }
 504  
             }
 505  0
             field.setFieldDirectInquiryEnabled(directInquiriesEnabled);
 506  0
         }
 507  
         else {
 508  0
             field.setFieldDirectInquiryEnabled(false);
 509  
         }
 510  0
     }
 511  
 
 512  
     /**
 513  
      *
 514  
      * @param field
 515  
      * @return the altered Field object
 516  
      */
 517  
     public static Field setFieldDirectInquiry(BusinessObject businessObject, String attributeName, Field field)
 518  
     {
 519  0
                 if (businessObject == null)
 520  
                 {
 521  0
             return field;
 522  
         }
 523  
 
 524  0
         Boolean noDirectInquiry = businessObjectDictionaryService.noDirectInquiryFieldLookup(businessObject.getClass(), attributeName);
 525  
         //check if noDirectInquiry is present and true, but if it's not set in existing data dictionary definitions, don't create a direct inquiry
 526  0
         if (noDirectInquiry != null && noDirectInquiry.booleanValue() || noDirectInquiry == null) {
 527  0
             return field;
 528  
         }
 529  
 
 530  0
         setFieldDirectInquiry(field);
 531  
 
 532  0
         return field;
 533  
     }
 534  
 
 535  0
     private static Map<Class,Map<String,Map>> referencesForForeignKey = new HashMap<Class, Map<String,Map>>();
 536  
 
 537  
     @Deprecated
 538  
     public static Map getPrimitiveReference(BusinessObject businessObject, String attributeName) {
 539  0
         Map chosenReferenceByKeySize = new HashMap();
 540  0
         Map chosenReferenceByFieldName = new HashMap();
 541  
 
 542  0
         Map referenceClasses = null;
 543  
 
 544  
         try {
 545  
             // add special caching of these relationships since the Spring caching is so expensive
 546  0
             Map<String,Map> propMap = referencesForForeignKey.get(businessObject.getClass());
 547  0
             if ( propMap == null ) {
 548  0
                 propMap = new HashMap<String, Map>();
 549  0
                 referencesForForeignKey.put(businessObject.getClass(), propMap);
 550  
             }
 551  0
             if ( propMap.containsKey(attributeName) ) {
 552  0
                 referenceClasses = propMap.get( attributeName );
 553  
             } else {
 554  
                     //KFSMI-709: Make Inquiry Framework use BusinessObjectMetadataService instead of just PersistenceStructureService
 555  0
                     referenceClasses = businessObjectMetaDataService.getReferencesForForeignKey(businessObject, attributeName);
 556  0
                     if(referenceClasses==null || referenceClasses.isEmpty()) {
 557  0
                         if ( persistenceStructureService.isPersistable(businessObject.getClass()) ) {
 558  0
                             referenceClasses = persistenceStructureService.getReferencesForForeignKey(businessObject.getClass(), attributeName);
 559  
                         }
 560  
                     }
 561  0
                 propMap.put(attributeName, referenceClasses);
 562  
             }
 563  0
         } catch ( ClassNotPersistableException ex ) {
 564  
             // do nothing, there is no quickfinder
 565  0
             Map<String,Map> propMap = referencesForForeignKey.get(businessObject.getClass());
 566  0
             propMap.put(attributeName, null);
 567  0
         }
 568  
 
 569  
         // if field is not fk to any reference class, return field object w no quickfinder
 570  0
         if (referenceClasses == null || referenceClasses.isEmpty()) {
 571  0
             return chosenReferenceByKeySize;
 572  
         }
 573  
 
 574  
         /*
 575  
          * if field is fk to more than one reference, take the class with the least # of pk fields, this should give the correct
 576  
          * grain for the attribute
 577  
          */
 578  0
         int minKeys = Integer.MAX_VALUE;
 579  0
         for (Iterator iter = referenceClasses.keySet().iterator(); iter.hasNext();) {
 580  0
             String attr = (String) iter.next();
 581  0
             Class clazz = (Class) referenceClasses.get(attr);
 582  0
             List pkNames = businessObjectMetaDataService.listPrimaryKeyFieldNames(clazz);
 583  
 
 584  
             // Compare based on key size.
 585  0
             if (pkNames.size() < minKeys) {
 586  0
                 minKeys = pkNames.size();
 587  0
                 chosenReferenceByKeySize.clear();
 588  0
                 chosenReferenceByKeySize.put(attr, clazz);
 589  
             }
 590  
 
 591  
             // Compare based on field name.
 592  0
             if (attributeName.startsWith(attr)) {
 593  0
                 chosenReferenceByFieldName.clear();
 594  0
                 chosenReferenceByFieldName.put(attr, clazz);
 595  
             }
 596  0
         }
 597  
 
 598  
         // If a compatible key was found based on field names, prefer it, otherwise use choice by key size.
 599  0
         return chosenReferenceByFieldName.isEmpty() ? chosenReferenceByKeySize : chosenReferenceByFieldName;
 600  
     }
 601  
 
 602  
     /**
 603  
      *
 604  
      * This method walks through the nested attribute and finds the last business object in the chain and returns it (excluding the
 605  
      * last parameter which is the actual attribute)
 606  
      *
 607  
      * @param attributeName
 608  
      * @return
 609  
      */
 610  
     public static BusinessObject getNestedBusinessObject(BusinessObject bo, String attributeName) {
 611  0
         String[] nestedAttributes = StringUtils.split(attributeName, ".");
 612  
 
 613  0
         BusinessObject childBO = null;
 614  0
         String attributeRefName = "";
 615  0
         Class clazz = null;
 616  0
         if (nestedAttributes.length > 1) {
 617  0
             String attributeStringSoFar = "";
 618  0
             for (int i = 0; i < nestedAttributes.length - 1; i++) {
 619  
                 // we need to build a string of the attribute names depending on which iteration we're in.
 620  
                 // so if the original attributeName string we're using is "a.b.c.d.e", then first iteration would use
 621  
                 // "a", 2nd "a.b", 3rd "a.b.c", etc.
 622  0
                 if (i != 0) {
 623  0
                     attributeStringSoFar = attributeStringSoFar + ".";
 624  
                 }
 625  0
                 attributeStringSoFar = attributeStringSoFar + nestedAttributes[i];
 626  
 
 627  0
                 clazz = ObjectUtils.getPropertyType( bo, attributeStringSoFar, persistenceStructureService );
 628  
 
 629  0
                 if (clazz != null && BusinessObject.class.isAssignableFrom(clazz)) {
 630  
                     try {
 631  0
                             childBO = (BusinessObject) ObjectUtils.createNewObjectFromClass(clazz);
 632  
                     }
 633  0
                     catch (Exception e) {
 634  0
                         return null;
 635  0
                     }
 636  
                 }
 637  
             }
 638  
         }
 639  0
         return childBO;
 640  
     }
 641  
 
 642  
     public static Class getNestedReferenceClass(BusinessObject businessObject, String attributeName) {
 643  0
         BusinessObject bo = getNestedBusinessObject(businessObject, attributeName);
 644  0
         return null == bo ? null : bo.getClass();
 645  
     }
 646  
 
 647  
     @Deprecated
 648  
     private static String generateFieldConversions(BusinessObject businessObject, String collectionName, BusinessObjectRelationship relationship, String propertyPrefix, List displayedFieldNames, String nestedObjectPrefix) {
 649  0
         String fieldConversions = "";
 650  
 
 651  0
         if ( LOG.isDebugEnabled() ) {
 652  0
             LOG.debug( "generateFieldConversions(" + businessObject.getClass().getName() + "," + collectionName + ",\n" + relationship + "\n," + propertyPrefix + "," + displayedFieldNames + "," + nestedObjectPrefix + ")" );
 653  
         }
 654  
 
 655  
         // get the references for the given property
 656  0
         for ( Map.Entry<String,String> entry : relationship.getParentToChildReferences().entrySet() ) {
 657  0
             String fromField = entry.getValue();
 658  0
             String toField = entry.getKey();
 659  
 
 660  
             // find the displayed to field mapping
 661  0
             if (!displayedFieldNames.contains(toField)) {
 662  0
                 toField = translateToDisplayedField(businessObject.getClass(), toField, displayedFieldNames);
 663  
             }
 664  
 
 665  0
             if (StringUtils.isNotBlank(fieldConversions)) {
 666  0
                 fieldConversions += ",";
 667  
             }
 668  
 
 669  0
             if ( StringUtils.isNotEmpty( propertyPrefix ) ) {
 670  0
                 toField = propertyPrefix + "." + toField;
 671  
             }
 672  
 
 673  0
             if ( StringUtils.isNotEmpty( collectionName ) ) {
 674  0
                 toField = collectionName + toField;
 675  
             }
 676  
 
 677  0
             fieldConversions += fromField + ":" + toField;
 678  0
         }
 679  
 
 680  0
         return fieldConversions;
 681  
     }
 682  
 
 683  
     @Deprecated
 684  
     private static String generateLookupParameters(BusinessObject businessObject, String collectionName, BusinessObjectRelationship relationship, String propertyPrefix, List displayedFieldNames, String nestedObjectPrefix) {
 685  
 
 686  0
         String lookupParameters = "";
 687  
 
 688  0
         List displayedQFFieldNames = businessObjectDictionaryService.getLookupFieldNames(relationship.getRelatedClass());
 689  0
         for ( Map.Entry<String,String> entry : relationship.getParentToChildReferences().entrySet() ) {
 690  0
             String fromField = entry.getKey();
 691  0
             String toField = entry.getValue();
 692  
 
 693  0
             if ( relationship.getUserVisibleIdentifierKey() == null || relationship.getUserVisibleIdentifierKey().equals( fromField ) ) {
 694  
                 // find the displayed from field mapping
 695  0
                 if (!displayedFieldNames.contains(fromField)) {
 696  0
                     fromField = translateToDisplayedField(businessObject.getClass(), fromField, displayedFieldNames);
 697  
                 }
 698  
 
 699  
                 // translate to field
 700  0
                 if (displayedQFFieldNames != null && !displayedQFFieldNames.contains(toField)) {
 701  0
                     toField = translateToDisplayedField(relationship.getRelatedClass(), toField, displayedQFFieldNames);
 702  
                 }
 703  
 
 704  0
                 if (StringUtils.isNotBlank(lookupParameters)) {
 705  0
                     lookupParameters += ",";
 706  
                 }
 707  
 
 708  0
                 if (propertyPrefix != null && !propertyPrefix.equals("")) {
 709  0
                     fromField = propertyPrefix + "." + fromField;
 710  
                 }
 711  
 
 712  0
                 if ( StringUtils.isNotEmpty( collectionName ) ) {
 713  0
                     fromField = collectionName + fromField;
 714  
                 }
 715  
 
 716  0
                 lookupParameters += fromField + ":" + toField;
 717  
             }
 718  0
         }
 719  
 
 720  0
         return lookupParameters;
 721  
     }
 722  
 
 723  
     @Deprecated
 724  
     private static String translateToDisplayedField(Class businessObjectClass, String fieldName, List displayedFieldNames) {        
 725  0
         if ( persistenceStructureService.isPersistable(businessObjectClass) ) {
 726  0
             Map nestedFkMap = persistenceStructureService.getNestedForeignKeyMap(businessObjectClass);
 727  
 
 728  
             // translate to primitive fk if nested
 729  
             /*
 730  
              * if (ObjectUtils.isNestedAttribute(fieldName) && nestedFkMap.containsKey(fieldName)) { fieldName = (String)
 731  
              * nestedFkMap.get(fieldName); }
 732  
              */
 733  
 
 734  0
             if (!displayedFieldNames.contains(fieldName)) {
 735  0
                 for (Iterator iterator = displayedFieldNames.iterator(); iterator.hasNext();) {
 736  0
                     String dispField = (String) iterator.next();
 737  
 
 738  0
                     if (nestedFkMap.containsKey(dispField) && nestedFkMap.get(dispField).equals(fieldName)) {
 739  0
                         fieldName = dispField;
 740  
                     }
 741  0
                 }
 742  
             }
 743  
         }
 744  
 
 745  0
         return fieldName;
 746  
     }
 747  
 
 748  
     public static String convertReferencesToSelectCollectionToString(Collection<String> referencesToRefresh) {
 749  0
         StringBuilder buf = new StringBuilder();
 750  0
         for (String reference : referencesToRefresh) {
 751  0
             buf.append(reference).append(KRADConstants.REFERENCES_TO_REFRESH_SEPARATOR);
 752  
         }
 753  0
         if (!referencesToRefresh.isEmpty()) {
 754  
             // we appended one too many separators, remove it
 755  0
             buf.delete(buf.length() - KRADConstants.REFERENCES_TO_REFRESH_SEPARATOR.length(), buf.length());
 756  
         }
 757  0
         return buf.toString();
 758  
     }
 759  
 
 760  
     public static String convertSetOfObjectIdsToString(Set<String> objectIds) {
 761  0
         if (objectIds.isEmpty()) {
 762  0
             return "";
 763  
         }
 764  0
         StringBuilder buf = new StringBuilder();
 765  0
         for (String objectId : objectIds) {
 766  0
             if (objectId.contains(KRADConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR)) {
 767  0
                 throw new RuntimeException("object ID " + objectId + " contains the selected obj ID separator");
 768  
             }
 769  0
             buf.append(objectId).append(KRADConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR);
 770  
         }
 771  
         // added one extra separator, remove it
 772  0
         buf.delete(buf.length() - KRADConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR.length(), buf.length());
 773  
 
 774  0
         return buf.toString();
 775  
     }
 776  
 
 777  
     public static Set<String> convertStringOfObjectIdsToSet(String objectIdsString) {
 778  0
         Set<String> set = new HashSet<String>();
 779  
 
 780  0
         if (StringUtils.isNotBlank(objectIdsString)) {
 781  0
             String[] objectIds = StringUtils.splitByWholeSeparator(objectIdsString, KRADConstants.MULTIPLE_VALUE_LOOKUP_OBJ_IDS_SEPARATOR);
 782  0
             for (String objectId : objectIds) {
 783  0
                 set.add(objectId);
 784  
             }
 785  
         }
 786  0
         return set;
 787  
     }
 788  
 
 789  
     /**
 790  
      * Given a list of results from a lookup, determines the best comparator to use on the String values of each of these columns
 791  
      *
 792  
      * 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,
 793  
      * so we gotta go thru the whole list and determine the best comparator to use
 794  
      *
 795  
      * @param resultsTable
 796  
      * @param column
 797  
      * @return
 798  
      */
 799  
     public static Comparator findBestValueComparatorForColumn(List<ResultRow> resultTable, int column) {
 800  
         // BIG HACK
 801  0
         Comparator comp = NullValueComparator.getInstance();
 802  0
         for (ResultRow row : resultTable) {
 803  0
             Comparator tempComp = row.getColumns().get(column).getValueComparator();
 804  0
             if (tempComp != null && !NullValueComparator.class.equals(tempComp.getClass())) {
 805  0
                 return tempComp;
 806  
             }
 807  0
         }
 808  0
         return comp;
 809  
     }
 810  
 
 811  
     /**
 812  
      * Given 3 sets of object IDs: the set of selected object IDs before rendering the current page,
 813  
      * the set of object IDs rendered on the page, and the set of object IDs selected on the page, computes
 814  
      * the total set of selected object IDs.
 815  
      *
 816  
      * Instead of storing it in a set, returns it in a map with the selected object ID as both the key and value
 817  
      * @param previouslySelectedObjectIds
 818  
      * @param displayedObjectIds
 819  
      * @param selectedObjectIds
 820  
      * @return
 821  
      */
 822  
     public static Map<String, String> generateCompositeSelectedObjectIds(Set<String> previouslySelectedObjectIds, Set<String> displayedObjectIds, Set<String> selectedObjectIds) {
 823  0
         Map<String, String> tempMap = new HashMap<String, String>();
 824  
         // Equivalent to the set operation:
 825  
         // (P - D) union C, where - is the set difference operator
 826  
         // 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
 827  
         // since HTML does not pass a value for non-selected dcheckboxes
 828  
 
 829  
         // first build a map w/ all the previouslySelectedObjectIds as keys
 830  0
         for (String previouslySelectedObjectId : previouslySelectedObjectIds) {
 831  0
             tempMap.put(previouslySelectedObjectId, previouslySelectedObjectId);
 832  
         }
 833  
         // then remove all the displayed elements (any selected displayed elements will be added back in the next loop)
 834  0
         for (String displayedObjectId : displayedObjectIds) {
 835  0
             tempMap.remove(displayedObjectId);
 836  
         }
 837  
         // put back the selected IDs
 838  0
         for (String selectedObjectId : selectedObjectIds) {
 839  0
             tempMap.put(selectedObjectId, selectedObjectId);
 840  
         }
 841  0
         return tempMap;
 842  
     }
 843  
 
 844  
     /**
 845  
      * Removes fields identified in the data dictionary as hidden from the lookup field values.
 846  
      * (This will remove Universal User ID and Person name from search requests when a user ID is entered.)
 847  
      *
 848  
      * @param fieldValues
 849  
      */
 850  
     public static void removeHiddenCriteriaFields( Class businessObjectClass, Map fieldValues ) {
 851  0
         List<String> lookupFieldAttributeList = businessObjectMetaDataService.getLookupableFieldNames(businessObjectClass);
 852  0
         if (lookupFieldAttributeList != null) {
 853  0
             for (Iterator iter = lookupFieldAttributeList.iterator(); iter.hasNext();) {
 854  0
                 String attributeName = (String) iter.next();
 855  0
                 if (fieldValues.containsKey(attributeName)) {
 856  0
                     ControlDefinition controlDef = dataDictionaryService.getAttributeControlDefinition(businessObjectClass, attributeName);
 857  0
                     if (controlDef != null && controlDef.isHidden() ) {
 858  0
                         fieldValues.remove(attributeName);
 859  
                     }
 860  
                 }
 861  0
             }
 862  
         }
 863  0
         }
 864  
 
 865  
     /**
 866  
      * Determines what Timestamp should be used for active queries on effective dated records. Determination made as
 867  
      * follows:
 868  
      * <ul>
 869  
      *   <li>Use activeAsOfDate value from search values Map if value is not empty</li>
 870  
      *   <li>If search value given, try to convert to sql date, if conversion fails, try to convert to Timestamp</li>
 871  
      *   <li>If search value empty, use current Date</li>
 872  
      *   <li>If Timestamp value not given, create Timestamp from given Date setting the time as 1 second before midnight
 873  
      * </ul>
 874  
      * 
 875  
      * @param searchValues - Map containing search key/value pairs
 876  
      * @return Timestamp to be used for active criteria
 877  
      */
 878  
         public static Timestamp getActiveDateTimestampForCriteria(Map searchValues) {
 879  0
                 Date activeDate = dateTimeService.getCurrentSqlDate();
 880  0
                 Timestamp activeTimestamp = null;
 881  0
                 if (searchValues.containsKey(KRADPropertyConstants.ACTIVE_AS_OF_DATE)) {
 882  0
                         String activeAsOfDate = (String) searchValues.get(KRADPropertyConstants.ACTIVE_AS_OF_DATE);
 883  0
                         if (StringUtils.isNotBlank(activeAsOfDate)) {
 884  
                                 try {
 885  0
                                         activeDate = dateTimeService.convertToSqlDate(ObjectUtils.clean(activeAsOfDate));
 886  0
                                 } catch (ParseException e) {
 887  
                                         // try to parse as timestamp
 888  
                                         try {
 889  0
                                                 activeTimestamp = dateTimeService.convertToSqlTimestamp(ObjectUtils.clean(activeAsOfDate));
 890  0
                                         } catch (ParseException e1) {
 891  0
                                                 throw new RuntimeException("Unable to convert date: " + ObjectUtils.clean(activeAsOfDate));
 892  0
                                         }
 893  0
                                 }
 894  
                         }
 895  
                 }
 896  
 
 897  
                 // if timestamp not given set to 1 second before midnight on the given date
 898  0
                 if (activeTimestamp == null) {
 899  0
                         Calendar cal = Calendar.getInstance();
 900  0
                         cal.setTime(activeDate);
 901  0
                         cal.set(Calendar.HOUR, cal.getMaximum(Calendar.HOUR));
 902  0
                         cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
 903  0
                         cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
 904  
 
 905  0
                         activeTimestamp = new Timestamp(cal.getTime().getTime());
 906  
                 }
 907  
 
 908  0
                 return activeTimestamp;
 909  
         }
 910  
 }