001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.keyvalues; 017 018import org.apache.commons.beanutils.PropertyUtils; 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.kuali.rice.core.api.util.ConcreteKeyValue; 022import org.kuali.rice.core.api.util.KeyValue; 023import org.kuali.rice.krad.bo.PersistableBusinessObject; 024import org.kuali.rice.krad.service.KRADServiceLocator; 025import org.kuali.rice.krad.service.KeyValuesService; 026import org.springframework.transaction.annotation.Transactional; 027 028import java.lang.reflect.InvocationTargetException; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.List; 032 033/** 034 * This class is a Generic ValuesFinder that builds the list of KeyValuePairs it returns 035 * in getKeyValues() based on a BO along with a keyAttributeName and labelAttributeName 036 * that are specified. 037 */ 038@Transactional 039public class PersistableBusinessObjectValuesFinder <T extends PersistableBusinessObject> extends KeyValuesBase { 040 041 private static final Log LOG = LogFactory.getLog(PersistableBusinessObjectValuesFinder.class); 042 043 private Class<T> businessObjectClass; 044 private String keyAttributeName; 045 private String labelAttributeName; 046 private boolean includeKeyInDescription = false; 047 private boolean includeBlankRow = false; 048 049 /** 050 * Build the list of KeyValues using the key (keyAttributeName) and 051 * label (labelAttributeName) of the list of all business objects found 052 * for the BO class specified. 053 * 054 * @see org.kuali.keyvalues.KeyValuesFinder#getKeyValues() 055 */ 056 @Override 057 public List<KeyValue> getKeyValues() { 058 List<KeyValue> labels = new ArrayList<KeyValue>(); 059 060 try { 061 KeyValuesService boService = KRADServiceLocator.getKeyValuesService(); 062 Collection<T> objects = boService.findAll(businessObjectClass); 063 if(includeBlankRow) { 064 labels.add(new ConcreteKeyValue("", "")); 065 } 066 for (T object : objects) { 067 Object key = PropertyUtils.getProperty(object, keyAttributeName); 068 String label = (String)PropertyUtils.getProperty(object, labelAttributeName); 069 if (includeKeyInDescription) { 070 label = key + " - " + label; 071 } 072 labels.add(new ConcreteKeyValue(key.toString(), label)); 073 } 074 } catch (IllegalAccessException e) { 075 LOG.debug(e.getMessage(), e); 076 LOG.error(e.getMessage()); 077 throw new RuntimeException("IllegalAccessException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e); 078 } catch (InvocationTargetException e) { 079 LOG.debug(e.getMessage(), e); 080 LOG.error(e.getMessage()); 081 throw new RuntimeException("InvocationTargetException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e); 082 } catch (NoSuchMethodException e) { 083 LOG.debug(e.getMessage(), e); 084 LOG.error(e.getMessage()); 085 throw new RuntimeException("NoSuchMethodException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e); 086 } 087 088 return labels; 089 } 090 091 /** 092 * @return the dataObjectClass 093 */ 094 public Class<T> getBusinessObjectClass() { 095 return this.businessObjectClass; 096 } 097 098 /** 099 * @param businessObjectClass the dataObjectClass to set 100 */ 101 public void setBusinessObjectClass(Class<T> businessObjectClass) { 102 this.businessObjectClass = businessObjectClass; 103 } 104 105 /** 106 * @return the includeKeyInDescription 107 */ 108 public boolean isIncludeKeyInDescription() { 109 return this.includeKeyInDescription; 110 } 111 112 /** 113 * @param includeKeyInDescription the includeKeyInDescription to set 114 */ 115 public void setIncludeKeyInDescription(boolean includeKeyInDescription) { 116 this.includeKeyInDescription = includeKeyInDescription; 117 } 118 119 /** 120 * @return the keyAttributeName 121 */ 122 public String getKeyAttributeName() { 123 return this.keyAttributeName; 124 } 125 126 /** 127 * @param keyAttributeName the keyAttributeName to set 128 */ 129 public void setKeyAttributeName(String keyAttributeName) { 130 this.keyAttributeName = keyAttributeName; 131 } 132 133 /** 134 * @return the labelAttributeName 135 */ 136 public String getLabelAttributeName() { 137 return this.labelAttributeName; 138 } 139 140 /** 141 * @param labelAttributeName the labelAttributeName to set 142 */ 143 public void setLabelAttributeName(String labelAttributeName) { 144 this.labelAttributeName = labelAttributeName; 145 } 146 147 /** 148 * @return the includeBlankRow 149 */ 150 public boolean isIncludeBlankRow() { 151 return this.includeBlankRow; 152 } 153 154 /** 155 * @param includeBlankRow the includeBlankRow to set 156 */ 157 public void setIncludeBlankRow(boolean includeBlankRow) { 158 this.includeBlankRow = includeBlankRow; 159 } 160 161}