1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.keyvalues;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.util.ClassLoaderUtils;
21 import org.kuali.rice.core.api.util.ConcreteKeyValue;
22 import org.kuali.rice.core.api.util.KeyValue;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.kim.api.type.KimType;
25 import org.kuali.rice.kim.api.type.KimTypeAttribute;
26 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
27 import org.kuali.rice.kim.framework.type.KimTypeService;
28 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
29 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
30 import org.kuali.rice.krad.service.DataDictionaryService;
31 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36
37
38
39
40 public class KimAttributeValuesFinder extends KeyValuesBase {
41
42 private static final Logger LOG = Logger.getLogger( KimAttributeValuesFinder.class );
43
44 protected String kimTypeId;
45 protected String kimAttributeName;
46 private DataDictionaryService dataDictionaryService;
47
48 protected DataDictionaryService getDataDictionaryService() {
49 if ( dataDictionaryService == null ) {
50 dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
51 }
52 return this.dataDictionaryService;
53 }
54
55
56
57
58 @Override
59 public List<KeyValue> getKeyValues() {
60 KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId);
61 if ( kimType != null ) {
62 KimTypeService service = KimFrameworkServiceLocator.getKimTypeService(kimType);
63 if ( service != null ) {
64 return getAttributeValidValues(kimTypeId,kimAttributeName);
65 }
66 LOG.error( "Unable to get type service " + kimType.getServiceName() );
67 } else {
68 LOG.error( "Unable to obtain KIM type for kimTypeId=" + kimTypeId );
69 }
70 return Collections.emptyList();
71 }
72
73 private List<KeyValue> getAttributeValidValues(String kimTypeId, String attributeName) {
74 if ( LOG.isDebugEnabled() ) {
75 LOG.debug( "getAttributeValidValues(" + kimTypeId + "," + attributeName + ")");
76 }
77 KimTypeAttribute attrib = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(attributeName);
78 if ( LOG.isDebugEnabled() ) {
79 LOG.debug( "Found Attribute definition: " + attrib );
80 }
81 List<KeyValue> pairs = null;
82 if ( StringUtils.isNotBlank(attrib.getKimAttribute().getComponentName()) ) {
83 try {
84 Class.forName(attrib.getKimAttribute().getComponentName());
85 try {
86 pairs = getLocalDataDictionaryAttributeValues(attrib);
87 } catch ( ClassNotFoundException ex ) {
88 LOG.error( "Got a ClassNotFoundException resolving a values finder - since this should have been executing in the context of the host system - this should not happen.");
89 return Collections.emptyList();
90 }
91 } catch ( ClassNotFoundException ex ) {
92 LOG.error( "Got a ClassNotFoundException resolving a component name (" + attrib.getKimAttribute().getComponentName() + ") - since this should have been executing in the context of the host system - this should not happen.");
93 }
94 } else {
95 pairs = getCustomValueFinderValues(attrib);
96 }
97 return pairs;
98 }
99
100 protected List<KeyValue> getCustomValueFinderValues(KimTypeAttribute attrib) {
101 return Collections.emptyList();
102 }
103
104 protected List<KeyValue> getLocalDataDictionaryAttributeValues(KimTypeAttribute attr) throws ClassNotFoundException {
105
106 BusinessObjectEntry entry = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(attr.getKimAttribute().getComponentName());
107 if ( entry == null ) {
108 LOG.warn( "Unable to obtain BusinessObjectEntry for component name: " + attr.getKimAttribute().getComponentName() );
109 return Collections.emptyList();
110 }
111 AttributeDefinition definition = entry.getAttributeDefinition(attr.getKimAttribute().getAttributeName());
112 if ( definition == null ) {
113 LOG.warn( "No attribute named " + attr.getKimAttribute().getAttributeName() + " found on BusinessObjectEntry for: " + attr.getKimAttribute().getComponentName() );
114 return Collections.emptyList();
115 }
116
117 List<KeyValue> pairs = new ArrayList<KeyValue>();
118 String keyValuesFinderName = definition.getControl().getValuesFinderClass();
119 if ( StringUtils.isNotBlank(keyValuesFinderName)) {
120 try {
121 KeyValuesFinder finder = (KeyValuesFinder)Class.forName(keyValuesFinderName).newInstance();
122 if (finder instanceof PersistableBusinessObjectValuesFinder) {
123 ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(
124 ClassLoaderUtils.getClass(definition.getControl().getBusinessObjectClass()));
125 ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName(definition.getControl().getKeyAttribute());
126 ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName(definition.getControl().getLabelAttribute());
127 if (definition.getControl().getIncludeBlankRow() != null) {
128 ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(definition.getControl().getIncludeBlankRow());
129 }
130 ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(definition.getControl().getIncludeKeyInLabel());
131 }
132
133 for (KeyValue pair : finder.getKeyValues()) {
134 pairs.add(new ConcreteKeyValue(pair));
135 }
136
137 } catch ( ClassNotFoundException ex ) {
138 LOG.info( "Unable to find class: " + keyValuesFinderName + " in the current context." );
139 throw ex;
140 } catch (Exception e) {
141 LOG.error("Unable to build a KeyValuesFinder for " + attr.getKimAttribute().getAttributeName(), e);
142 }
143 } else {
144 LOG.warn( "No values finder class defined on the control definition (" + definition.getControl() + ") on BO / attr = " + attr.getKimAttribute().getComponentName() + " / " + attr.getKimAttribute().getAttributeName() );
145 }
146 return pairs;
147 }
148
149
150
151
152 public String getKimAttributeName() {
153 return this.kimAttributeName;
154 }
155
156
157
158
159 public void setKimAttributeName(String kimAttributeName) {
160 this.kimAttributeName = kimAttributeName;
161 }
162
163
164
165
166 public String getKimTypeId() {
167 return this.kimTypeId;
168 }
169
170
171
172
173 public void setKimTypeId(String kimTypeId) {
174 this.kimTypeId = kimTypeId;
175 }
176
177 }