Coverage Report - org.kuali.rice.krad.lookup.keyvalues.PersistableBusinessObjectValuesFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
PersistableBusinessObjectValuesFinder
0%
0/45
0%
0/6
1.818
 
 1  
 /*
 2  
  * Copyright 2006-2007 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.krad.lookup.keyvalues;
 17  
 
 18  
 import java.lang.reflect.InvocationTargetException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.beanutils.PropertyUtils;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.kuali.rice.core.util.KeyValue;
 27  
 import org.kuali.rice.core.util.ConcreteKeyValue;
 28  
 import org.kuali.rice.krad.bo.PersistableBusinessObject;
 29  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 30  
 import org.kuali.rice.krad.service.KeyValuesService;
 31  
 import org.springframework.transaction.annotation.Transactional;
 32  
 
 33  
 /**
 34  
  * This class is a Generic ValuesFinder that builds the list of KeyValuePairs it returns
 35  
  * in getKeyValues() based on a BO along with a keyAttributeName and labelAttributeName
 36  
  * that are specified.
 37  
  */
 38  
 @Transactional
 39  0
 public class PersistableBusinessObjectValuesFinder <T extends PersistableBusinessObject> extends KeyValuesBase {
 40  
 
 41  0
     private static final Log LOG = LogFactory.getLog(PersistableBusinessObjectValuesFinder.class);
 42  
 
 43  
     private Class<T> businessObjectClass;
 44  
     private String keyAttributeName;
 45  
     private String labelAttributeName;
 46  0
     private boolean includeKeyInDescription = false;
 47  0
     private boolean includeBlankRow = false;
 48  
 
 49  
     /**
 50  
      * Build the list of KeyValues using the key (keyAttributeName) and
 51  
      * label (labelAttributeName) of the list of all business objects found
 52  
      * for the BO class specified.
 53  
      *
 54  
      * @see org.kuali.keyvalues.KeyValuesFinder#getKeyValues()
 55  
      */
 56  
     @Override
 57  
         public List<KeyValue> getKeyValues() {
 58  0
             List<KeyValue> labels = new ArrayList<KeyValue>();
 59  
 
 60  
             try {
 61  0
                 KeyValuesService boService = KRADServiceLocator.getKeyValuesService();
 62  0
             Collection<T> objects = boService.findAll(businessObjectClass);
 63  0
             if(includeBlankRow) {
 64  0
                     labels.add(new ConcreteKeyValue("", ""));
 65  
             }
 66  0
             for (T object : objects) {
 67  0
                     Object key = PropertyUtils.getProperty(object, keyAttributeName);
 68  0
                     String label = (String)PropertyUtils.getProperty(object, labelAttributeName);
 69  0
                     if (includeKeyInDescription) {
 70  0
                         label = key + " - " + label;
 71  
                     }
 72  0
                     labels.add(new ConcreteKeyValue(key.toString(), label));
 73  0
                 }
 74  0
             } catch (IllegalAccessException e) {
 75  0
             LOG.debug(e.getMessage(), e);
 76  0
             LOG.error(e.getMessage());
 77  0
             throw new RuntimeException("IllegalAccessException occurred while trying to build keyValues List. businessObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
 78  0
             } catch (InvocationTargetException e) {
 79  0
             LOG.debug(e.getMessage(), e);
 80  0
             LOG.error(e.getMessage());
 81  0
             throw new RuntimeException("InvocationTargetException occurred while trying to build keyValues List. businessObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
 82  0
             } catch (NoSuchMethodException e) {
 83  0
             LOG.debug(e.getMessage(), e);
 84  0
             LOG.error(e.getMessage());
 85  0
             throw new RuntimeException("NoSuchMethodException occurred while trying to build keyValues List. businessObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
 86  0
             }
 87  
 
 88  0
         return labels;
 89  
     }
 90  
 
 91  
     /**
 92  
      * @return the businessObjectClass
 93  
      */
 94  
     public Class<T> getBusinessObjectClass() {
 95  0
         return this.businessObjectClass;
 96  
     }
 97  
 
 98  
     /**
 99  
      * @param businessObjectClass the businessObjectClass to set
 100  
      */
 101  
     public void setBusinessObjectClass(Class<T> businessObjectClass) {
 102  0
         this.businessObjectClass = businessObjectClass;
 103  0
     }
 104  
 
 105  
     /**
 106  
      * @return the includeKeyInDescription
 107  
      */
 108  
     public boolean isIncludeKeyInDescription() {
 109  0
         return this.includeKeyInDescription;
 110  
     }
 111  
 
 112  
     /**
 113  
      * @param includeKeyInDescription the includeKeyInDescription to set
 114  
      */
 115  
     public void setIncludeKeyInDescription(boolean includeKeyInDescription) {
 116  0
         this.includeKeyInDescription = includeKeyInDescription;
 117  0
     }
 118  
 
 119  
     /**
 120  
      * @return the keyAttributeName
 121  
      */
 122  
     public String getKeyAttributeName() {
 123  0
         return this.keyAttributeName;
 124  
     }
 125  
 
 126  
     /**
 127  
      * @param keyAttributeName the keyAttributeName to set
 128  
      */
 129  
     public void setKeyAttributeName(String keyAttributeName) {
 130  0
         this.keyAttributeName = keyAttributeName;
 131  0
     }
 132  
 
 133  
     /**
 134  
      * @return the labelAttributeName
 135  
      */
 136  
     public String getLabelAttributeName() {
 137  0
         return this.labelAttributeName;
 138  
     }
 139  
 
 140  
     /**
 141  
      * @param labelAttributeName the labelAttributeName to set
 142  
      */
 143  
     public void setLabelAttributeName(String labelAttributeName) {
 144  0
         this.labelAttributeName = labelAttributeName;
 145  0
     }
 146  
 
 147  
         /**
 148  
          * @return the includeBlankRow
 149  
          */
 150  
         public boolean isIncludeBlankRow() {
 151  0
                 return this.includeBlankRow;
 152  
         }
 153  
 
 154  
         /**
 155  
          * @param includeBlankRow the includeBlankRow to set
 156  
          */
 157  
         public void setIncludeBlankRow(boolean includeBlankRow) {
 158  0
                 this.includeBlankRow = includeBlankRow;
 159  0
         }
 160  
 
 161  
 }