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