1 /**
2 * Copyright 2005-2016 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.datadictionary;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.service.DataDictionaryService;
20 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21 import org.springframework.beans.MutablePropertyValues;
22 import org.springframework.beans.PropertyValue;
23 import org.springframework.beans.factory.config.BeanDefinition;
24 import org.springframework.beans.factory.config.BeanDefinitionHolder;
25 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26 import org.springframework.beans.factory.config.TypedStringValue;
27
28 /**
29 * Base class for dictionary bean processors that provides utility methods
30 *
31 * @author Kuali Rice Team (rice.collab@kuali.org)
32 */
33 public abstract class DictionaryBeanProcessorBase implements DictionaryBeanProcessor {
34
35 /**
36 * Retrieves the class for the object that will be created from the bean definition. Since the class might not
37 * be configured on the bean definition, but by a parent, each parent bean definition is recursively checked for
38 * a class until one is found
39 *
40 * @param beanDefinition bean definition to get class for
41 * @param beanFactory bean factory that contains the bean definition
42 * @return Class<?> class configured for the bean definition, or null
43 */
44 protected Class<?> getBeanClass(BeanDefinition beanDefinition, ConfigurableListableBeanFactory beanFactory) {
45 if (StringUtils.isNotBlank(beanDefinition.getBeanClassName())) {
46 try {
47 return Class.forName(beanDefinition.getBeanClassName());
48 } catch (ClassNotFoundException e) {
49 // swallow exception and return null so bean is not processed
50 return null;
51 }
52 } else if (StringUtils.isNotBlank(beanDefinition.getParentName())) {
53 BeanDefinition parentBeanDefinition = beanFactory.getBeanDefinition(beanDefinition.getParentName());
54 if (parentBeanDefinition != null) {
55 return getBeanClass(parentBeanDefinition, beanFactory);
56 }
57 }
58
59 return null;
60 }
61
62 /**
63 * Determines whether the given value is of String type and if so returns the string value
64 *
65 * @param value object value to check
66 * @return String string value for object or null if object is not a string type
67 */
68 protected String getStringValue(Object value) {
69 if (value instanceof TypedStringValue) {
70 TypedStringValue typedStringValue = (TypedStringValue) value;
71 return typedStringValue.getValue();
72 } else if (value instanceof String) {
73 return (String) value;
74 }
75
76 return null;
77 }
78
79 /**
80 * Applies the given property name and value to the bean definition
81 *
82 * @param propertyPath name of the property to add value for
83 * @param propertyValue value for the property
84 * @param beanDefinition bean definition to add property value to
85 */
86 protected void applyPropertyValueToBean(String propertyPath, String propertyValue, BeanDefinition beanDefinition) {
87 applyPropertyValueToBean(propertyPath, propertyValue, beanDefinition.getPropertyValues());
88 }
89
90 /**
91 * Applies the given property name and value to given property values
92 *
93 * @param propertyPath name of the property to add value for
94 * @param propertyValue value for the property
95 * @param pvs property values to add property to
96 */
97 protected void applyPropertyValueToBean(String propertyPath, String propertyValue, MutablePropertyValues pvs) {
98 pvs.addPropertyValue(propertyPath, propertyValue);
99 }
100
101 /**
102 * Determines if the given property value is a bean definition or bean definition holder and if
103 * so returns the value as a bean definintion
104 *
105 * @param propertyValue property value to get bean definition from
106 * @return property value as a bean definition or null if value does not contain a bean definition
107 */
108 protected BeanDefinition getPropertyValueBeanDefinition(PropertyValue propertyValue) {
109 BeanDefinition beanDefinition = null;
110
111 Object value = propertyValue.getValue();
112 if ((value instanceof BeanDefinition) || (value instanceof BeanDefinitionHolder)) {
113 if (propertyValue instanceof BeanDefinition) {
114 beanDefinition = (BeanDefinition) propertyValue;
115 } else {
116 beanDefinition = ((BeanDefinitionHolder) value).getBeanDefinition();
117 }
118 }
119
120 return beanDefinition;
121 }
122
123 /**
124 * Indicates whether the given bean name was generated by spring
125 *
126 * @param beanName bean name to check
127 * @return boolean true if bean name is generated, false if not
128 */
129 protected boolean isGeneratedBeanName(String beanName) {
130 return StringUtils.isNotBlank(beanName) && (StringUtils.contains(beanName, "$") || StringUtils.contains(
131 beanName, "#"));
132 }
133
134 /**
135 * Returns an instance of the data dictionary service
136 *
137 * @return DataDictionaryService instance
138 */
139 protected DataDictionaryService getDataDictionaryService() {
140 return KRADServiceLocatorWeb.getDataDictionaryService();
141 }
142 }