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
26
27
28
29
30
31
32
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
59
60
61
62
63
64
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
78
79
80
81
82
83
84
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
96
97
98
99
100
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
117
118
119
120
121
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
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
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
184
185
186
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 }