| 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.api.type.KimTypeService; |
| 27 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
| 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 | 0 | public class KimAttributeValuesFinder extends KeyValuesBase { |
| 41 | |
|
| 42 | 0 | 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 | 0 | if ( dataDictionaryService == null ) { |
| 50 | 0 | dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService(); |
| 51 | |
} |
| 52 | 0 | return this.dataDictionaryService; |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public List<KeyValue> getKeyValues() { |
| 60 | 0 | KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId); |
| 61 | 0 | if ( kimType != null ) { |
| 62 | 0 | KimTypeService service = KIMServiceLocatorWeb.getKimTypeService(kimType); |
| 63 | 0 | if ( service != null ) { |
| 64 | 0 | return getAttributeValidValues(kimTypeId,kimAttributeName); |
| 65 | |
} |
| 66 | 0 | LOG.error( "Unable to get type service " + kimType.getServiceName() ); |
| 67 | 0 | } else { |
| 68 | 0 | LOG.error( "Unable to obtain KIM type for kimTypeId=" + kimTypeId ); |
| 69 | |
} |
| 70 | 0 | return Collections.emptyList(); |
| 71 | |
} |
| 72 | |
|
| 73 | |
private List<KeyValue> getAttributeValidValues(String kimTypeId, String attributeName) { |
| 74 | 0 | if ( LOG.isDebugEnabled() ) { |
| 75 | 0 | LOG.debug( "getAttributeValidValues(" + kimTypeId + "," + attributeName + ")"); |
| 76 | |
} |
| 77 | 0 | KimTypeAttribute attrib = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(attributeName); |
| 78 | 0 | if ( LOG.isDebugEnabled() ) { |
| 79 | 0 | LOG.debug( "Found Attribute definition: " + attrib ); |
| 80 | |
} |
| 81 | 0 | List<KeyValue> pairs = null; |
| 82 | 0 | if ( StringUtils.isNotBlank(attrib.getKimAttribute().getComponentName()) ) { |
| 83 | |
try { |
| 84 | 0 | Class.forName(attrib.getKimAttribute().getComponentName()); |
| 85 | |
try { |
| 86 | 0 | pairs = getLocalDataDictionaryAttributeValues(attrib); |
| 87 | 0 | } catch ( ClassNotFoundException ex ) { |
| 88 | 0 | 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 | 0 | return Collections.emptyList(); |
| 90 | 0 | } |
| 91 | 0 | } catch ( ClassNotFoundException ex ) { |
| 92 | 0 | 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 | 0 | } |
| 94 | |
} else { |
| 95 | 0 | pairs = getCustomValueFinderValues(attrib); |
| 96 | |
} |
| 97 | 0 | return pairs; |
| 98 | |
} |
| 99 | |
|
| 100 | |
protected List<KeyValue> getCustomValueFinderValues(KimTypeAttribute attrib) { |
| 101 | 0 | return Collections.emptyList(); |
| 102 | |
} |
| 103 | |
|
| 104 | |
protected List<KeyValue> getLocalDataDictionaryAttributeValues(KimTypeAttribute attr) throws ClassNotFoundException { |
| 105 | |
|
| 106 | 0 | BusinessObjectEntry entry = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(attr.getKimAttribute().getComponentName()); |
| 107 | 0 | if ( entry == null ) { |
| 108 | 0 | LOG.warn( "Unable to obtain BusinessObjectEntry for component name: " + attr.getKimAttribute().getComponentName() ); |
| 109 | 0 | return Collections.emptyList(); |
| 110 | |
} |
| 111 | 0 | AttributeDefinition definition = entry.getAttributeDefinition(attr.getKimAttribute().getAttributeName()); |
| 112 | 0 | if ( definition == null ) { |
| 113 | 0 | LOG.warn( "No attribute named " + attr.getKimAttribute().getAttributeName() + " found on BusinessObjectEntry for: " + attr.getKimAttribute().getComponentName() ); |
| 114 | 0 | return Collections.emptyList(); |
| 115 | |
} |
| 116 | |
|
| 117 | 0 | List<KeyValue> pairs = new ArrayList<KeyValue>(); |
| 118 | 0 | String keyValuesFinderName = definition.getControl().getValuesFinderClass(); |
| 119 | 0 | if ( StringUtils.isNotBlank(keyValuesFinderName)) { |
| 120 | |
try { |
| 121 | 0 | KeyValuesFinder finder = (KeyValuesFinder)Class.forName(keyValuesFinderName).newInstance(); |
| 122 | 0 | if (finder instanceof PersistableBusinessObjectValuesFinder) { |
| 123 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass( |
| 124 | |
ClassLoaderUtils.getClass(definition.getControl().getBusinessObjectClass())); |
| 125 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName(definition.getControl().getKeyAttribute()); |
| 126 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName(definition.getControl().getLabelAttribute()); |
| 127 | 0 | if (definition.getControl().getIncludeBlankRow() != null) { |
| 128 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(definition.getControl().getIncludeBlankRow()); |
| 129 | |
} |
| 130 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(definition.getControl().getIncludeKeyInLabel()); |
| 131 | |
} |
| 132 | |
|
| 133 | 0 | for (KeyValue pair : finder.getKeyValues()) { |
| 134 | 0 | pairs.add(new ConcreteKeyValue(pair)); |
| 135 | |
} |
| 136 | |
|
| 137 | 0 | } catch ( ClassNotFoundException ex ) { |
| 138 | 0 | LOG.info( "Unable to find class: " + keyValuesFinderName + " in the current context." ); |
| 139 | 0 | throw ex; |
| 140 | 0 | } catch (Exception e) { |
| 141 | 0 | LOG.error("Unable to build a KeyValuesFinder for " + attr.getKimAttribute().getAttributeName(), e); |
| 142 | 0 | } |
| 143 | |
} else { |
| 144 | 0 | LOG.warn( "No values finder class defined on the control definition (" + definition.getControl() + ") on BO / attr = " + attr.getKimAttribute().getComponentName() + " / " + attr.getKimAttribute().getAttributeName() ); |
| 145 | |
} |
| 146 | 0 | return pairs; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public String getKimAttributeName() { |
| 153 | 0 | return this.kimAttributeName; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public void setKimAttributeName(String kimAttributeName) { |
| 160 | 0 | this.kimAttributeName = kimAttributeName; |
| 161 | 0 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public String getKimTypeId() { |
| 167 | 0 | return this.kimTypeId; |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public void setKimTypeId(String kimTypeId) { |
| 174 | 0 | this.kimTypeId = kimTypeId; |
| 175 | 0 | } |
| 176 | |
|
| 177 | |
} |