View Javadoc

1   /**
2    * Copyright 2005-2011 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.kns.datadictionary.validation;
17  
18  import org.apache.commons.beanutils.PropertyUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.util.type.TypeUtils;
22  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
23  import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
24  import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
25  import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
26  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
27  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
28  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
29  import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
30  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31  import org.kuali.rice.krad.service.PersistenceStructureService;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  import java.beans.PropertyDescriptor;
35  import java.lang.reflect.InvocationTargetException;
36  import java.lang.reflect.Method;
37  import java.util.HashMap;
38  import java.util.LinkedList;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * This class provides legacy processing for maintenance documents in the dictionary validation service implementation. 
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   * @deprecated As of release 2.0
47   */
48  @Deprecated
49  public class MaintenanceDocumentAttributeValueReader extends DictionaryObjectAttributeValueReader {
50  
51  	protected Map<String, Class<?>> attributeTypeMap;
52  	protected Map<String, Object> attributeValueMap;
53  	//protected Map<String, PropertyDescriptor> beanInfo;
54  	
55  	private final static Logger LOG = Logger.getLogger(MaintenanceDocumentAttributeValueReader.class);
56  	
57  	private List<Constrainable> attributeDefinitions;
58  	private Map<String, AttributeDefinition> attributeDefinitionMap;
59  	
60  	public MaintenanceDocumentAttributeValueReader(Object object, String entryName, MaintenanceDocumentEntry entry, PersistenceStructureService persistenceStructureService) {
61  		super(object, entryName, entry);
62  		
63  		//if (object != null)
64  		//	this.beanInfo = getBeanInfo(object.getClass());
65  		
66  		this.attributeTypeMap = new HashMap<String, Class<?>>();
67  		this.attributeValueMap = new HashMap<String, Object>();		
68  		
69  		this.attributeDefinitions = new LinkedList<Constrainable>();
70  		this.attributeDefinitionMap = new HashMap<String, AttributeDefinition>();
71  		for (MaintainableSectionDefinition sectionDefinition : entry.getMaintainableSections()) {
72  			List<? extends MaintainableItemDefinition> itemDefinitions = sectionDefinition.getMaintainableItems();
73  			
74  			for (MaintainableItemDefinition itemDefinition : itemDefinitions) {
75  				if (itemDefinition instanceof MaintainableFieldDefinition) {
76  					String itemDefinitionName = itemDefinition.getName();
77  					AttributeDefinition attributeDefinition = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeDefinition(object.getClass().getName(), itemDefinitionName);
78  						
79  						//entry.getAttributeDefinition(attributeName);
80  					boolean isAttributeDefined = attributeDefinition != null;
81  						//getDataDictionaryService().isAttributeDefined(businessObject.getClass(), itemDefinition.getName());
82  			        if (isAttributeDefined) {
83  			        	attributeDefinitions.add(attributeDefinition);
84  			        	attributeDefinitionMap.put(itemDefinitionName, attributeDefinition);
85                          LOG.info("itemDefName: " + itemDefinitionName);
86                          PropertyDescriptor propertyDescriptor = null;
87                          try {
88                              propertyDescriptor = PropertyUtils.getPropertyDescriptor(object, itemDefinitionName);
89                          } catch (IllegalAccessException e) {
90                              LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
91                          } catch (InvocationTargetException e) {
92                              LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
93                          } catch (NoSuchMethodException e) {
94                              LOG.warn("Failed to find property description on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
95                          }
96  
97  			    		Method readMethod = propertyDescriptor.getReadMethod();
98  			    		
99  						try {
100 							Object attributeValue = readMethod.invoke(object);
101 							
102 							if (attributeValue != null && StringUtils.isNotBlank(attributeValue.toString())) {
103 				    			Class<?> propertyType = ObjectUtils.getPropertyType(object, itemDefinitionName, persistenceStructureService);
104 				    			attributeTypeMap.put(itemDefinitionName, propertyType);
105                                 if (TypeUtils.isStringClass(propertyType) || TypeUtils.isIntegralClass(propertyType) ||
106                                         TypeUtils.isDecimalClass(propertyType) ||
107                                         TypeUtils.isTemporalClass(propertyType) ||
108                                         TypeUtils.isBooleanClass(propertyType)) {
109                                     // check value format against dictionary
110                                     if (!TypeUtils.isTemporalClass(propertyType)) {
111                                         attributeValueMap.put(itemDefinitionName, attributeValue);
112                                     }
113                                 }
114 				    		}
115 						} catch (IllegalArgumentException e) {
116 							LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
117 						} catch (IllegalAccessException e) {
118 							LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);
119 						} catch (InvocationTargetException e) {
120 							LOG.warn("Failed to invoke read method on object when looking for " + itemDefinitionName + " as a field of " + entry.getDocumentTypeName(), e);						
121 			        	}
122 			    		
123 			        }
124 				}
125 			}
126 		}
127 	}
128 	
129 	/**
130 	 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getDefinition(java.lang.String)
131 	 */
132 	@Override
133 	public Constrainable getDefinition(String attributeName) {
134 		return attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null;
135 	}
136 
137 	/**
138 	 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getDefinitions()
139 	 */
140 	@Override
141 	public List<Constrainable> getDefinitions() {
142 		return attributeDefinitions;
143 	}
144 
145 	@Override
146 	public String getLabel(String attributeName) {
147 		AttributeDefinition attributeDefinition = attributeDefinitionMap != null ? attributeDefinitionMap.get(attributeName) : null;
148 		return attributeDefinition != null ? attributeDefinition.getLabel()  : attributeName;
149 	}
150 	
151 	/**
152 	 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getType(java.lang.String)
153 	 */
154 	@Override
155 	public Class<?> getType(String attributeName) {
156 		return attributeTypeMap != null ? attributeTypeMap.get(attributeName) : null;
157 	}
158 
159 	/**
160 	 * @see org.kuali.rice.krad.datadictionary.validation.AttributeValueReader#getValue(java.lang.String)
161 	 */
162 	@Override
163 	public <X> X getValue(String attributeName) throws AttributeValidationException {
164 		return (X) attributeValueMap.get(attributeName);
165 	}
166 
167 	//private Map<String, PropertyDescriptor> getBeanInfo(Class<?> clazz) {
168 	//	final Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
169 	//	for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(clazz)) {
170 	//		properties.put(propertyDescriptor.getName(), propertyDescriptor);
171 	//	}
172 	//	return properties;
173 	//}
174 	
175 }