View Javadoc

1   package org.kuali.student.r1.common.assembly.transform;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.apache.log4j.Logger;
9   import org.kuali.student.r1.common.assembly.data.Data;
10  import org.kuali.student.r1.common.assembly.data.Metadata;
11  import org.kuali.student.r1.common.assembly.dictionary.MetadataServiceImpl;
12  import org.kuali.student.r2.common.dto.DtoConstants;
13  import org.kuali.student.r1.common.rice.StudentWorkflowConstants;
14  
15  public class TransformationManager {
16  	final Logger LOG = Logger.getLogger(TransformationManager.class);
17  
18  	private MetadataServiceImpl metadataService;
19  	private DataBeanMapper mapper = new DefaultDataBeanMapper();
20  	private List<TransformFilter> inboundFilterList = new ArrayList<TransformFilter>();
21  	private List<TransformFilter> outboundFilterList = new ArrayList<TransformFilter>();	
22  
23  	/**
24  	 * This is an outbound transform request which will convert incoming DTO objects
25  	 * to a Data map and apply outbound filter operation to both the data object
26  	 * and the converted DTO object.
27  	 *
28  	 * @param value
29  	 * @param objectType TODO
30  	 * @return
31  	 * @throws Exception
32  	 */
33  	public Data transform(Object value, String objectType) throws Exception{
34  		Metadata metadata = null;
35  		metadata = (metadata != null ? metadata:getMetadata(objectType, new HashMap<String,Object>()));
36  		
37  		applyOutboundFilters(value.getClass().getName(), value, new HashMap<String,Object>());
38  		Data dataValue = mapper.convertFromBean(value, metadata);
39  		applyOutboundFilters(value.getClass().getName(), dataValue, new HashMap<String,Object>());
40  
41  		return dataValue;
42  	}
43  
44  	public Data transform(Object value, String objectType, Map<String,Object> filterProperties) throws Exception{
45  		Metadata metadata = null;
46  		metadata = (metadata != null ? metadata:getMetadata(objectType, new HashMap<String,Object>()));
47  
48  		applyOutboundFilters(value.getClass().getName(), value, filterProperties);
49  		Data dataValue = mapper.convertFromBean(value, metadata);
50  		applyOutboundFilters(value.getClass().getName(), dataValue, filterProperties);
51  
52  		return dataValue;
53  	}
54  
55  
56  	/**
57  	 * This is an inbound transform request which will convert incoming Data objects
58  	 * to the corresponding DTO object and apply inbound filter operations to both the data object
59  	 * and the converted DTO object.
60  	 *
61  	 * @param value The incoming Data value to transform to DTO object
62  	 * @param clazz The DTO object class represented by the Data value
63  	 * @return The converted DTO with both inbound Data and DTO filters applied.
64  	 */
65  	public Object transform(Data value, Class<?> clazz) throws Exception{
66  		Metadata metadata = null;
67  		metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), new HashMap<String,Object>()));
68  		applyInboundFilters(clazz.getName(), value, new HashMap<String,Object>(),metadata);
69  		Object dtoValue = mapper.convertFromData(value, clazz,metadata);
70  		applyInboundFilters(clazz.getName(), dtoValue, new HashMap<String,Object>(),metadata);
71  
72  		return dtoValue;
73  	}
74  
75  	/**
76  	 * This is an inbound transform request which will convert incoming Data objects
77  	 * to the corresponding DTO object and apply inbound filters to both the data object
78  	 * and the converted DTO object.
79  	 *
80  	 * @param value The incoming Data value to transform to DTO object
81  	 * @param clazz The DTO object class represented by the Data value
82  	 * @param filterProperties properties that can be consumed by the filters
83  	 * @return The converted DTO with both inbound Data and DTO filters applied.
84  	 */
85  	public Object transform(Data value, Class<?> clazz, Map<String,Object> filterProperties) throws Exception{
86  		Metadata metadata = getMetadata(clazz.getName(), filterProperties);
87  		applyInboundFilters(clazz.getName(), value, filterProperties,metadata);
88  		Object dtoValue = mapper.convertFromData(value, clazz,metadata);
89  		applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata);
90  		return dtoValue;
91  	}
92  
93  	/**
94  	 * This method applies all inbound filters known to his transformation manager.
95  	 *
96  	 * @param dtoName The name of the dto represented by a Data value, for Dto values,
97  	 *  this can simply be the name of the dto class
98  	 * @param value The dto or data object to apply filters to
99  	 * @throws Exception
100 	 */
101 	public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{
102 		for (TransformFilter filter:inboundFilterList){
103 			if (filter.getType().isInstance(value)){
104 				if (filter instanceof AbstractDataFilter) {
105 					((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties);
106 				} else {
107 					((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties);
108 				}
109 				LOG.info(filter.getClass().getName() + ": Filter Applied");
110 			}
111 		}
112 	}
113 
114 	/**
115 	 * This method applies all outbound filters known to his transformation manager.
116 	 *
117 	 * @param dtoName The name of the dto represented by a Data value, for Dto values,
118 	 *  this can simply be the name of the dto class
119 	 * @param value The dto or data object to apply filters to
120 	 * @throws Exception
121 	 */
122 	public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{
123 		for (TransformFilter filter:outboundFilterList){
124 			if (filter.getType().isInstance(value)){
125 				if (filter instanceof AbstractDataFilter) {
126 					//FIXME: It might be more efficient to getMetadata outside of the for loop (unless metadata might be different)
127 					Metadata metadata = getMetadata(dtoName, properties);
128 					((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties);
129 				} else {
130 					((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties);
131 				}
132 				LOG.info(filter.getClass().getName() + ": Filter Applied");
133 			}
134 		}
135 	}
136 
137 	public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){
138 		String state = (String)filterProperties.get(DtoConstants.DTO_STATE);
139 		String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE);
140 		String workflowNode = (String)filterProperties.get(DtoConstants.DTO_WORKFLOW_NODE);
141 		//The docTypeName is actually set with ProposalWorkflowFilter.WORKFLOW_DOC_TYPE, however it is not visible in this project.
142 		String documentTypeName = (String)filterProperties.get(StudentWorkflowConstants.WORKFLOW_DOCUMENT_TYPE);
143 
144 		Metadata metadata;
145 		if (workflowNode == null || workflowNode.isEmpty()){
146 			metadata = metadataService.getMetadata(dtoName, null, state, nextState, documentTypeName);
147 		} else {
148 			metadata = metadataService.getMetadataByWorkflowNode(dtoName, workflowNode, documentTypeName);
149 		}		 
150 
151 		applyMetadataFilters(dtoName, metadata, filterProperties);
152 		return metadata;
153 	}
154 
155 	public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){
156 		for (TransformFilter filter:outboundFilterList){
157 			if (filter instanceof MetadataFilter){
158 				((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties);
159 			}
160 		}
161 	}
162 
163 	public Metadata getUnfilteredMetadata(String dtoName){
164 		Metadata metadata = metadataService.getMetadata(dtoName);
165 		return metadata;
166 	}
167 
168 	public void setMetadataService(MetadataServiceImpl metadataService) {
169 		this.metadataService = metadataService;
170 	}
171 	
172 	
173 	public DataBeanMapper getMapper() {
174 		return mapper;
175 	}
176 
177 	public void setMapper(DataBeanMapper mapper) {
178 		this.mapper = mapper;
179 	}
180 
181 	/**
182 	 * Use setInboundFilters and setOutboundFilters instead. This sets both
183 	 * the inbound and outbound filter chain to be the same.
184 	 *  
185 	 * @param filters
186 	 */
187 	@Deprecated
188 	public void setFilters(List<TransformFilter> filters){
189 		inboundFilterList.addAll(filters);
190 		outboundFilterList.addAll(filters);
191 	}	
192 
193 	public void setInboundFilters(List<TransformFilter> filters){
194 		inboundFilterList.addAll(filters);
195 	}	
196 		
197 	public void setOutboundFilters(List<TransformFilter> filters){
198 		outboundFilterList.addAll(filters);
199 	}	
200 
201 }