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