View Javadoc

1   /**
2    * Copyright 2005-2014 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.keyvalues;
17  
18  import org.apache.commons.beanutils.PropertyUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.kuali.rice.core.api.util.ConcreteKeyValue;
22  import org.kuali.rice.core.api.util.KeyValue;
23  import org.kuali.rice.krad.bo.PersistableBusinessObject;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.service.KeyValuesService;
26  import org.springframework.transaction.annotation.Transactional;
27  
28  import java.lang.reflect.InvocationTargetException;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.List;
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  public class PersistableBusinessObjectValuesFinder <T extends PersistableBusinessObject> extends KeyValuesBase {
40  
41      private static final Log LOG = LogFactory.getLog(PersistableBusinessObjectValuesFinder.class);
42  
43      private Class<T> businessObjectClass;
44      private String keyAttributeName;
45      private String labelAttributeName;
46      private boolean includeKeyInDescription = false;
47      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      	List<KeyValue> labels = new ArrayList<KeyValue>();
59  
60      	try {
61      	    KeyValuesService boService = KRADServiceLocator.getKeyValuesService();
62              Collection<T> objects = boService.findAll(businessObjectClass);
63              if(includeBlankRow) {
64              	labels.add(new ConcreteKeyValue("", ""));
65              }
66              for (T object : objects) {
67              	Object key = PropertyUtils.getProperty(object, keyAttributeName);
68              	String label = (String)PropertyUtils.getProperty(object, labelAttributeName);
69              	if (includeKeyInDescription) {
70              	    label = key + " - " + label;
71              	}
72              	labels.add(new ConcreteKeyValue(key.toString(), label));
73      	    }
74      	} catch (IllegalAccessException e) {
75              LOG.debug(e.getMessage(), e);
76              LOG.error(e.getMessage());
77              throw new RuntimeException("IllegalAccessException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
78      	} catch (InvocationTargetException e) {
79              LOG.debug(e.getMessage(), e);
80              LOG.error(e.getMessage());
81              throw new RuntimeException("InvocationTargetException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
82      	} catch (NoSuchMethodException e) {
83              LOG.debug(e.getMessage(), e);
84              LOG.error(e.getMessage());
85              throw new RuntimeException("NoSuchMethodException occurred while trying to build keyValues List. dataObjectClass: " + businessObjectClass + "; keyAttributeName: " + keyAttributeName + "; labelAttributeName: " + labelAttributeName + "; includeKeyInDescription: " + includeKeyInDescription, e);
86      	}
87  
88          return labels;
89      }
90  
91      /**
92       * @return the dataObjectClass
93       */
94      public Class<T> getBusinessObjectClass() {
95          return this.businessObjectClass;
96      }
97  
98      /**
99       * @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 }