001 /** 002 * Copyright 2005-2013 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 */ 016 package org.kuali.rice.kns.datadictionary.validation; 017 018 import org.apache.commons.beanutils.BeanUtils; 019 import org.apache.commons.beanutils.PropertyUtils; 020 import org.apache.commons.lang.StringUtils; 021 import org.apache.log4j.Logger; 022 import org.kuali.rice.core.api.util.type.TypeUtils; 023 import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition; 024 import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition; 025 import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition; 026 import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry; 027 import org.kuali.rice.krad.datadictionary.AttributeDefinition; 028 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException; 029 import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader; 030 import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable; 031 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 032 import org.kuali.rice.krad.service.PersistenceStructureService; 033 import org.kuali.rice.krad.util.ObjectUtils; 034 035 import java.beans.PropertyDescriptor; 036 import java.lang.reflect.InvocationTargetException; 037 import java.lang.reflect.Method; 038 import java.util.HashMap; 039 import java.util.LinkedList; 040 import java.util.List; 041 import java.util.Map; 042 043 /** 044 * This class provides legacy processing for maintenance documents in the dictionary validation service implementation. 045 * 046 * @author Kuali Rice Team (rice.collab@kuali.org) 047 * @deprecated As of release 2.0 048 */ 049 @Deprecated 050 public class MaintenanceDocumentAttributeValueReader extends DictionaryObjectAttributeValueReader { 051 052 protected Map<String, Class<?>> attributeTypeMap; 053 protected Map<String, Object> attributeValueMap; 054 //protected Map<String, PropertyDescriptor> beanInfo; 055 056 private final static Logger LOG = Logger.getLogger(MaintenanceDocumentAttributeValueReader.class); 057 058 private List<Constrainable> attributeDefinitions; 059 private Map<String, AttributeDefinition> attributeDefinitionMap; 060 061 public MaintenanceDocumentAttributeValueReader(Object object, String entryName, MaintenanceDocumentEntry entry, PersistenceStructureService persistenceStructureService) { 062 super(object, entryName, entry); 063 064 //if (object != null) 065 // this.beanInfo = getBeanInfo(object.getClass()); 066 067 this.attributeTypeMap = new HashMap<String, Class<?>>(); 068 this.attributeValueMap = new HashMap<String, Object>(); 069 070 this.attributeDefinitions = new LinkedList<Constrainable>(); 071 this.attributeDefinitionMap = new HashMap<String, AttributeDefinition>(); 072 for (MaintainableSectionDefinition sectionDefinition : entry.getMaintainableSections()) { 073 List<? extends MaintainableItemDefinition> itemDefinitions = sectionDefinition.getMaintainableItems(); 074 075 for (MaintainableItemDefinition itemDefinition : itemDefinitions) { 076 if (itemDefinition instanceof MaintainableFieldDefinition) { 077 String itemDefinitionName = itemDefinition.getName(); 078 AttributeDefinition attributeDefinition = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeDefinition(object.getClass().getName(), itemDefinitionName); 079 080 //entry.getAttributeDefinition(attributeName); 081 boolean isAttributeDefined = attributeDefinition != null; 082 //getDataDictionaryService().isAttributeDefined(businessObject.getClass(), itemDefinition.getName()); 083 if (isAttributeDefined) { 084 attributeDefinitions.add(attributeDefinition); 085 attributeDefinitionMap.put(itemDefinitionName, attributeDefinition); 086 LOG.info("itemDefName: " + itemDefinitionName); 087 088 try { 089 Object attributeValue = PropertyUtils.getNestedProperty(object, itemDefinitionName); 090 091 if (attributeValue != null && StringUtils.isNotBlank(attributeValue.toString())) { 092 Class<?> propertyType = ObjectUtils.getPropertyType(object, itemDefinitionName, persistenceStructureService); 093 attributeTypeMap.put(itemDefinitionName, propertyType); 094 if (TypeUtils.isStringClass(propertyType) || TypeUtils.isIntegralClass(propertyType) || 095 TypeUtils.isDecimalClass(propertyType) || 096 TypeUtils.isTemporalClass(propertyType) || 097 TypeUtils.isBooleanClass(propertyType)) { 098 // check value format against dictionary 099 if (!TypeUtils.isTemporalClass(propertyType)) { 100 attributeValueMap.put(itemDefinitionName, attributeValue); 101 } 102 } 103 } 104 } catch (IllegalArgumentException e) { 105 LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); 106 } catch (IllegalAccessException e) { 107 LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); 108 } catch (InvocationTargetException e) { 109 LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); 110 } catch (NoSuchMethodException e) { 111 LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e); 112 } 113 114 } 115 } 116 } 117 } 118 } 119 120 /** 121 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getDefinition(java.lang.String) 122 */ 123 @Override 124 public Constrainable getDefinition(String attributeName) { 125 return attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null; 126 } 127 128 /** 129 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getDefinitions() 130 */ 131 @Override 132 public List<Constrainable> getDefinitions() { 133 return attributeDefinitions; 134 } 135 136 @Override 137 public String getLabel(String attributeName) { 138 AttributeDefinition attributeDefinition = attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null; 139 return attributeDefinition != null ? attributeDefinition.getLabel() : attributeName; 140 } 141 142 /** 143 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getType(java.lang.String) 144 */ 145 @Override 146 public Class<?> getType(String attributeName) { 147 return attributeTypeMap != null ? attributeTypeMap.get(attributeName) : null; 148 } 149 150 /** 151 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getValue(java.lang.String) 152 */ 153 @Override 154 public <X> X getValue(String attributeName) throws AttributeValidationException { 155 return (X) attributeValueMap.get(attributeName); 156 } 157 158 //private Map<String, PropertyDescriptor> getBeanInfo(Class<?> clazz) { 159 // final Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(); 160 // for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(clazz)) { 161 // properties.put(propertyDescriptor.getName(), propertyDescriptor); 162 // } 163 // return properties; 164 //} 165 166 }