View Javadoc

1   /**
2    * Copyright 2005-2013 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.BeanUtils;
19  import org.apache.commons.beanutils.PropertyUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.log4j.Logger;
22  import org.kuali.rice.core.api.util.type.TypeUtils;
23  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
24  import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
25  import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
26  import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
27  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
28  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
29  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
30  import org.kuali.rice.krad.datadictionary.validation.capability.Constrainable;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.service.PersistenceStructureService;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  
35  import java.beans.PropertyDescriptor;
36  import java.lang.reflect.InvocationTargetException;
37  import java.lang.reflect.Method;
38  import java.util.HashMap;
39  import java.util.LinkedList;
40  import java.util.List;
41  import java.util.Map;
42  
43  /**
44   * This class provides legacy processing for maintenance documents in the dictionary validation service implementation. 
45   * 
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   * @deprecated As of release 2.0
48   */
49  @Deprecated
50  public class MaintenanceDocumentAttributeValueReader extends DictionaryObjectAttributeValueReader {
51  
52  	protected Map<String, Class<?>> attributeTypeMap;
53  	protected Map<String, Object> attributeValueMap;
54  	//protected Map<String, PropertyDescriptor> beanInfo;
55  	
56  	private final static Logger LOG = Logger.getLogger(MaintenanceDocumentAttributeValueReader.class);
57  	
58  	private List<Constrainable> attributeDefinitions;
59  	private Map<String, AttributeDefinition> attributeDefinitionMap;
60  	
61  	public MaintenanceDocumentAttributeValueReader(Object object, String entryName, MaintenanceDocumentEntry entry, PersistenceStructureService persistenceStructureService) {
62  		super(object, entryName, entry);
63  		
64  		//if (object != null)
65  		//	this.beanInfo = getBeanInfo(object.getClass());
66  		
67  		this.attributeTypeMap = new HashMap<String, Class<?>>();
68  		this.attributeValueMap = new HashMap<String, Object>();		
69  		
70  		this.attributeDefinitions = new LinkedList<Constrainable>();
71  		this.attributeDefinitionMap = new HashMap<String, AttributeDefinition>();
72  		for (MaintainableSectionDefinition sectionDefinition : entry.getMaintainableSections()) {
73  			List<? extends MaintainableItemDefinition> itemDefinitions = sectionDefinition.getMaintainableItems();
74  			
75  			for (MaintainableItemDefinition itemDefinition : itemDefinitions) {
76  				if (itemDefinition instanceof MaintainableFieldDefinition) {
77  					String itemDefinitionName = itemDefinition.getName();
78  					AttributeDefinition attributeDefinition = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeDefinition(object.getClass().getName(), itemDefinitionName);
79  						
80  						//entry.getAttributeDefinition(attributeName);
81  					boolean isAttributeDefined = attributeDefinition != null;
82  						//getDataDictionaryService().isAttributeDefined(businessObject.getClass(), itemDefinition.getName());
83  			        if (isAttributeDefined) {
84  			        	attributeDefinitions.add(attributeDefinition);
85  			        	attributeDefinitionMap.put(itemDefinitionName, attributeDefinition);
86                          LOG.info("itemDefName: " + itemDefinitionName);
87  
88  						try {
89                              Object  attributeValue = PropertyUtils.getNestedProperty(object, itemDefinitionName);
90  
91  							if (attributeValue != null && StringUtils.isNotBlank(attributeValue.toString())) {
92  				    			Class<?> propertyType = ObjectUtils.getPropertyType(object, itemDefinitionName, persistenceStructureService);
93  				    			attributeTypeMap.put(itemDefinitionName, propertyType);
94                                  if (TypeUtils.isStringClass(propertyType) || TypeUtils.isIntegralClass(propertyType) ||
95                                          TypeUtils.isDecimalClass(propertyType) ||
96                                          TypeUtils.isTemporalClass(propertyType) ||
97                                          TypeUtils.isBooleanClass(propertyType)) {
98                                      // check value format against dictionary
99                                      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 }