Coverage Report - org.kuali.rice.kns.lookup.DataDictionaryLookupResultsSupportStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
DataDictionaryLookupResultsSupportStrategy
0%
0/81
0%
0/42
4.545
 
 1  
 /*
 2  
  * Copyright 2007-2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.lookup;
 17  
 
 18  
 import java.lang.reflect.InvocationTargetException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.beanutils.PropertyUtils;
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.kuali.rice.core.web.format.Formatter;
 29  
 import org.kuali.rice.kns.bo.BusinessObject;
 30  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 31  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 32  
 import org.kuali.rice.kns.service.DataDictionaryService;
 33  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 34  
 
 35  
 /**
 36  
  * LookupResults support strategy which uses the primary keys and lookupable defined in a business object's data dictionary file to support the multivalue lookup 
 37  
  * 
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  *
 40  
  */
 41  0
 public class DataDictionaryLookupResultsSupportStrategy implements
 42  
                 LookupResultsSupportStrategyService {
 43  
         
 44  
         private DataDictionaryService dataDictionaryService;
 45  
 
 46  
         /**
 47  
          * Builds a lookup id for the given business object
 48  
          * @see org.kuali.rice.kns.lookup.LookupResultsSupportStrategyService#getLookupIdForBusinessObject(org.kuali.rice.kns.bo.BusinessObject)
 49  
          */
 50  
         public String getLookupIdForBusinessObject(BusinessObject businessObject) {
 51  0
                 final List<String> pkFieldNames = getPrimaryKeyFieldsForBusinessObject(businessObject.getClass());
 52  0
                 return convertPKFieldMapToLookupId(pkFieldNames, businessObject);
 53  
         }
 54  
 
 55  
         /**
 56  
          * Determines if both the primary keys and Lookupable are present in the data dictionary definition; if so, the BO qualifies, but if either are missing, it does not
 57  
          * @see org.kuali.rice.kns.lookup.LookupResultsSupportStrategyService#qualifiesForStrategy(java.lang.Class)
 58  
          */
 59  
         public boolean qualifiesForStrategy(Class<? extends BusinessObject> boClass) {
 60  0
                 if (getLookupableForBusinessObject(boClass) == null) {
 61  0
                         return false; // this probably isn't going to happen, but still...
 62  
                 }
 63  0
                 final List<String> pkFields = getPrimaryKeyFieldsForBusinessObject(boClass);
 64  0
                 return pkFields != null && pkFields.size() > 0;
 65  
         }
 66  
 
 67  
         /**
 68  
          * Uses the Lookupable associated with the given BusinessObject to search for business objects
 69  
          * @see org.kuali.rice.kns.lookup.LookupResultsSupportStrategyService#retrieveSelectedResultBOs(java.lang.String, java.lang.Class, java.lang.String, org.kuali.rice.kns.lookup.LookupResultsService)
 70  
          */
 71  
         public <T extends BusinessObject> Collection<T> retrieveSelectedResultBOs(Class<T> boClass, Set<String> lookupIds) throws Exception {
 72  
         
 73  0
         List<T> retrievedBusinessObjects = new ArrayList<T>();
 74  0
         final Lookupable lookupable = getLookupableForBusinessObject(boClass);
 75  0
         for (String lookupId : lookupIds) {
 76  0
                 final Map<String, String> lookupKeys = convertLookupIdToPKFieldMap(lookupId, boClass);
 77  0
                 List<BusinessObject> bos = lookupable.getSearchResults(lookupKeys);
 78  
                 
 79  
                 // we should only get one business object...but let's put them all in...
 80  0
                 for (BusinessObject bo : bos) {
 81  0
                         retrievedBusinessObjects.add((T)bo);
 82  
                 }
 83  0
         }
 84  
                 
 85  0
                 return retrievedBusinessObjects;
 86  
         }
 87  
         
 88  
         /**
 89  
          * Retrieves the Lookupable for the given business object class
 90  
          * 
 91  
          * @param businessObjectClass the class to find the Lookupable for
 92  
          * @return the Lookupable, or null if nothing could be found
 93  
          */
 94  
         protected Lookupable getLookupableForBusinessObject(Class<? extends BusinessObject> businessObjectClass) {
 95  0
                 final BusinessObjectEntry boEntry = getBusinessObjectEntry(businessObjectClass);
 96  0
                 if (boEntry == null) {
 97  0
                         return null;
 98  
                 }
 99  
                 
 100  0
                 final String lookupableId = boEntry.getLookupDefinition().getLookupableID();
 101  0
                 return KNSServiceLocatorWeb.getLookupable(lookupableId);
 102  
         }
 103  
         
 104  
         /**
 105  
          * Returns the data dictionary defined primary keys for the given BusinessObject
 106  
          * 
 107  
          * @param businessObjectClass the business object to get DataDictionary defined primary keys for
 108  
          * @return the List of primary key property names, or null if nothing could be found
 109  
          */
 110  
         protected List<String> getPrimaryKeyFieldsForBusinessObject(Class<? extends BusinessObject> businessObjectClass) {
 111  0
                 final BusinessObjectEntry boEntry = getBusinessObjectEntry(businessObjectClass);
 112  0
                 if (boEntry == null) {
 113  0
                         return null;
 114  
                 }
 115  0
                 return boEntry.getPrimaryKeys();
 116  
         }
 117  
         
 118  
         /**
 119  
          * Converts a lookup id into a PK field map to use to search in a lookupable
 120  
          * 
 121  
          * @param lookupId the id returned by the lookup
 122  
          * @param businessObjectClass the class of the business object getting the primary key
 123  
          * @return a Map of field names and values which can be profitably used to search for matching business objects
 124  
          */
 125  
         protected Map<String, String> convertLookupIdToPKFieldMap(String lookupId, Class<? extends BusinessObject> businessObjectClass) {
 126  0
                 Map<String, String> pkFields = new HashMap<String, String>();
 127  0
                 if (!StringUtils.isBlank(lookupId)) {
 128  0
                         final String[] pkValues = lookupId.split("\\|");
 129  0
                         for (String pkValue : pkValues) {
 130  0
                                 if (!StringUtils.isBlank(pkValue)) {
 131  0
                                         final String[] pkPieces = pkValue.split("-");
 132  0
                                         if (!StringUtils.isBlank(pkPieces[0]) && !StringUtils.isBlank(pkPieces[1])) {
 133  0
                                                 pkFields.put(pkPieces[0], pkPieces[1]);
 134  
                                         }
 135  
                                 }
 136  
                         }
 137  
                 }
 138  0
                 return pkFields;
 139  
         }
 140  
         
 141  
         /**
 142  
          * Converts a Map of PKFields into a String lookup ID
 143  
          * @param pkFieldNames the name of the PK fields, which should be converted to the given lookupId
 144  
          * @param businessObjectClass the class of the business object getting the primary key
 145  
          * @return the String lookup id
 146  
          */
 147  
         protected String convertPKFieldMapToLookupId(List<String> pkFieldNames, BusinessObject businessObject) {
 148  0
                 StringBuilder lookupId = new StringBuilder();
 149  0
                 for (String pkFieldName : pkFieldNames) {
 150  
                         try {
 151  0
                                 final Object value = PropertyUtils.getProperty(businessObject, pkFieldName);
 152  
                                 
 153  0
                                 if (value != null) {
 154  0
                                         lookupId.append(pkFieldName);
 155  0
                                         lookupId.append("-");
 156  0
                                         final Formatter formatter = retrieveBestFormatter(pkFieldName, businessObject.getClass());
 157  0
                                         final String formattedValue = (formatter != null) ? formatter.format(value).toString() : value.toString();
 158  
                                         
 159  0
                                         lookupId.append(formattedValue);
 160  
                                 }
 161  0
                                 lookupId.append("|");
 162  0
                         } catch (IllegalAccessException iae) {
 163  0
                                 throw new RuntimeException("Could not retrieve pk field value "+pkFieldName+" from business object "+businessObject.getClass().getName(), iae);
 164  0
                         } catch (InvocationTargetException ite) {
 165  0
                                 throw new RuntimeException("Could not retrieve pk field value "+pkFieldName+" from business object "+businessObject.getClass().getName(), ite);
 166  0
                         } catch (NoSuchMethodException nsme) {
 167  0
                                 throw new RuntimeException("Could not retrieve pk field value "+pkFieldName+" from business object "+businessObject.getClass().getName(), nsme);
 168  0
                         }
 169  
                 }
 170  0
                 return lookupId.substring(0, lookupId.length() - 1); // kill the last "|"
 171  
         }
 172  
         
 173  
         /**
 174  
          * Like when you're digging through your stuff drawer, you know the one in the kitchen with all the batteries and lint in it, this method
 175  
          * goes through the stuff drawer of KNS formatters and attempts to return you a good one
 176  
          * 
 177  
          * @param propertyName the name of the property to retrieve
 178  
          * @param boClass the class of the BusinessObject the property is on
 179  
          * @return a Formatter, or null if we were unsuccessful in finding
 180  
          */
 181  
         protected Formatter retrieveBestFormatter(String propertyName, Class<? extends BusinessObject> boClass) {
 182  0
                 Formatter formatter = null;
 183  
                 
 184  
                 try {
 185  0
                         Class<? extends Formatter> formatterClass = null;
 186  
                         
 187  0
                         final BusinessObjectEntry boEntry = getBusinessObjectEntry(boClass);
 188  0
                         if (boEntry != null) {
 189  0
                                 final AttributeDefinition attributeDefinition = boEntry.getAttributeDefinition(propertyName);
 190  0
                                 if (attributeDefinition != null && attributeDefinition.hasFormatterClass()) {
 191  0
                                         formatterClass = (Class<? extends Formatter>)Class.forName(attributeDefinition.getFormatterClass());
 192  
                                 }
 193  
                         }
 194  0
                         if (formatterClass == null) {
 195  0
                                 final java.lang.reflect.Field propertyField = boClass.getDeclaredField(propertyName);
 196  0
                                 if (propertyField != null) {
 197  0
                                         formatterClass = Formatter.findFormatter(propertyField.getType());
 198  
                                 }
 199  
                         }
 200  
                         
 201  0
                         if (formatterClass != null) {
 202  0
                                 formatter = formatterClass.newInstance();
 203  
                         }
 204  0
                 } catch (SecurityException se) {
 205  0
                         throw new RuntimeException("Could not retrieve good formatter", se);
 206  0
                 } catch (ClassNotFoundException cnfe) {
 207  0
                         throw new RuntimeException("Could not retrieve good formatter", cnfe);
 208  0
                 } catch (NoSuchFieldException nsfe) {
 209  0
                         throw new RuntimeException("Could not retrieve good formatter", nsfe);
 210  0
                 } catch (IllegalAccessException iae) {
 211  0
                         throw new RuntimeException("Could not retrieve good formatter", iae);
 212  0
                 } catch (InstantiationException ie) {
 213  0
                         throw new RuntimeException("Could not retrieve good formatter", ie);
 214  0
                 }
 215  
                 
 216  0
                 return formatter;
 217  
         }
 218  
         
 219  
         /**
 220  
          * Looks up the DataDictionary BusinessObjectEntry for the given class
 221  
          * 
 222  
          * @param boClass the class of the BusinessObject to find a BusinessObjectEntry for
 223  
          * @return the entry from the data dictionary, or null if nothing was found
 224  
          */
 225  
         protected BusinessObjectEntry getBusinessObjectEntry(Class<? extends BusinessObject> boClass) {
 226  0
                 return getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(boClass.getName());
 227  
         }
 228  
 
 229  
         /**
 230  
          * @return the dataDictionaryService
 231  
          */
 232  
         public DataDictionaryService getDataDictionaryService() {
 233  0
                 return this.dataDictionaryService;
 234  
         }
 235  
 
 236  
         /**
 237  
          * @param dataDictionaryService the dataDictionaryService to set
 238  
          */
 239  
         public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
 240  0
                 this.dataDictionaryService = dataDictionaryService;
 241  0
         }
 242  
 }