001/** 002 * Copyright 2005-2014 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 java.util.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.List; 022 023import org.apache.commons.beanutils.PropertyUtils; 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.kuali.rice.core.api.util.ConcreteKeyValue; 027import org.kuali.rice.core.api.util.KeyValue; 028import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 029 030/** 031 * This class is a Generic ValuesFinder that builds the list of KeyValuePairs it returns 032 * in getKeyValues() based on a BO along with a keyAttributeName and labelAttributeName 033 * that are specified. 034 * 035 * @param <T> business object type 036 */ 037public class PersistableBusinessObjectValuesFinder extends KeyValuesBase { 038 private static final Log LOG = LogFactory.getLog(PersistableBusinessObjectValuesFinder.class); 039 private static final long serialVersionUID = 1L; 040 041 protected Class<?> businessObjectClass; 042 protected String keyAttributeName; 043 protected String labelAttributeName; 044 protected boolean includeKeyInDescription = false; 045 protected boolean includeBlankRow = false; 046 047 /** 048 * Build the list of KeyValues using the key (keyAttributeName) and 049 * label (labelAttributeName) of the list of all business objects found 050 * for the BO class specified. 051 */ 052 @Override 053 public List<KeyValue> getKeyValues() { 054 try { 055 @SuppressWarnings("deprecation") 056 Collection<?> objects = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(businessObjectClass, Collections.<String, String>emptyMap()); 057 List<KeyValue> labels = new ArrayList<KeyValue>(objects.size()); 058 if(includeBlankRow) { 059 labels.add(new ConcreteKeyValue("", "")); 060 } 061 for (Object object : objects) { 062 Object key = PropertyUtils.getProperty(object, keyAttributeName); 063 String label = (String)PropertyUtils.getProperty(object, labelAttributeName); 064 if (includeKeyInDescription) { 065 label = key + " - " + label; 066 } 067 labels.add(new ConcreteKeyValue(key.toString(), label)); 068 } 069 return labels; 070 } catch (Exception e) { 071 LOG.error("Exception occurred while trying to build keyValues List: " + this, e); 072 throw new RuntimeException("Exception occurred while trying to build keyValues List: " + this, e); 073 } 074 } 075 076 public void setBusinessObjectClass(Class<?> businessObjectClass) { 077 this.businessObjectClass = businessObjectClass; 078 } 079 080 public void setIncludeKeyInDescription(boolean includeKeyInDescription) { 081 this.includeKeyInDescription = includeKeyInDescription; 082 } 083 084 public void setKeyAttributeName(String keyAttributeName) { 085 this.keyAttributeName = keyAttributeName; 086 } 087 088 public void setLabelAttributeName(String labelAttributeName) { 089 this.labelAttributeName = labelAttributeName; 090 } 091 092 public void setIncludeBlankRow(boolean includeBlankRow) { 093 this.includeBlankRow = includeBlankRow; 094 } 095 096 @Override 097 public String toString() { 098 StringBuilder builder = new StringBuilder(); 099 builder.append("PersistableBusinessObjectValuesFinder [businessObjectClass=").append(this.businessObjectClass) 100 .append(", keyAttributeName=").append(this.keyAttributeName).append(", labelAttributeName=") 101 .append(this.labelAttributeName).append(", includeKeyInDescription=") 102 .append(this.includeKeyInDescription).append(", includeBlankRow=").append(this.includeBlankRow) 103 .append("]"); 104 return builder.toString(); 105 } 106 107}