1 | |
package org.kuali.student.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.common.assembly.data.Data; |
10 | |
import org.kuali.student.common.assembly.data.Metadata; |
11 | |
import org.kuali.student.common.assembly.dictionary.MetadataServiceImpl; |
12 | |
import org.kuali.student.common.dto.DtoConstants; |
13 | |
import org.kuali.student.common.rice.StudentWorkflowConstants; |
14 | |
|
15 | 0 | public class TransformationManager { |
16 | 0 | final Logger LOG = Logger.getLogger(TransformationManager.class); |
17 | |
|
18 | |
private MetadataServiceImpl metadataService; |
19 | 0 | private DataBeanMapper mapper = new DefaultDataBeanMapper(); |
20 | 0 | private List<TransformFilter> inboundFilterList = new ArrayList<TransformFilter>(); |
21 | 0 | private List<TransformFilter> outboundFilterList = new ArrayList<TransformFilter>(); |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public Data transform(Object value, String objectType) throws Exception{ |
34 | 0 | Metadata metadata = null; |
35 | 0 | metadata = (metadata != null ? metadata:getMetadata(objectType, new HashMap<String,Object>())); |
36 | |
|
37 | 0 | applyOutboundFilters(value.getClass().getName(), value, new HashMap<String,Object>()); |
38 | 0 | Data dataValue = mapper.convertFromBean(value, metadata); |
39 | 0 | applyOutboundFilters(value.getClass().getName(), dataValue, new HashMap<String,Object>()); |
40 | |
|
41 | 0 | return dataValue; |
42 | |
} |
43 | |
|
44 | |
public Data transform(Object value, String objectType, Map<String,Object> filterProperties) throws Exception{ |
45 | 0 | Metadata metadata = null; |
46 | 0 | metadata = (metadata != null ? metadata:getMetadata(objectType, new HashMap<String,Object>())); |
47 | |
|
48 | 0 | applyOutboundFilters(value.getClass().getName(), value, filterProperties); |
49 | 0 | Data dataValue = mapper.convertFromBean(value, metadata); |
50 | 0 | applyOutboundFilters(value.getClass().getName(), dataValue, filterProperties); |
51 | |
|
52 | 0 | return dataValue; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public Object transform(Data value, Class<?> clazz) throws Exception{ |
66 | 0 | Metadata metadata = null; |
67 | 0 | metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), new HashMap<String,Object>())); |
68 | 0 | applyInboundFilters(clazz.getName(), value, new HashMap<String,Object>(),metadata); |
69 | 0 | Object dtoValue = mapper.convertFromData(value, clazz,metadata); |
70 | 0 | applyInboundFilters(clazz.getName(), dtoValue, new HashMap<String,Object>(),metadata); |
71 | |
|
72 | 0 | return dtoValue; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Object transform(Data value, Class<?> clazz, Map<String,Object> filterProperties) throws Exception{ |
86 | 0 | Metadata metadata = getMetadata(clazz.getName(), filterProperties); |
87 | 0 | applyInboundFilters(clazz.getName(), value, filterProperties,metadata); |
88 | 0 | Object dtoValue = mapper.convertFromData(value, clazz,metadata); |
89 | 0 | applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata); |
90 | 0 | return dtoValue; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{ |
102 | 0 | for (TransformFilter filter:inboundFilterList){ |
103 | 0 | if (filter.getType().isInstance(value)){ |
104 | 0 | if (filter instanceof AbstractDataFilter) { |
105 | 0 | ((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties); |
106 | |
} else { |
107 | 0 | ((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties); |
108 | |
} |
109 | 0 | LOG.info(filter.getClass().getName() + ": Filter Applied"); |
110 | |
} |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{ |
123 | 0 | for (TransformFilter filter:outboundFilterList){ |
124 | 0 | if (filter.getType().isInstance(value)){ |
125 | 0 | if (filter instanceof AbstractDataFilter) { |
126 | |
|
127 | 0 | Metadata metadata = getMetadata(dtoName, properties); |
128 | 0 | ((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties); |
129 | 0 | } else { |
130 | 0 | ((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties); |
131 | |
} |
132 | 0 | LOG.info(filter.getClass().getName() + ": Filter Applied"); |
133 | |
} |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){ |
138 | 0 | String state = (String)filterProperties.get(DtoConstants.DTO_STATE); |
139 | 0 | String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE); |
140 | 0 | String workflowNode = (String)filterProperties.get(DtoConstants.DTO_WORKFLOW_NODE); |
141 | |
|
142 | 0 | String documentTypeName = (String)filterProperties.get(StudentWorkflowConstants.WORKFLOW_DOCUMENT_TYPE); |
143 | |
|
144 | |
Metadata metadata; |
145 | 0 | if (workflowNode == null || workflowNode.isEmpty()){ |
146 | 0 | metadata = metadataService.getMetadata(dtoName, null, state, nextState, documentTypeName); |
147 | |
} else { |
148 | 0 | metadata = metadataService.getMetadataByWorkflowNode(dtoName, workflowNode, documentTypeName); |
149 | |
} |
150 | |
|
151 | 0 | applyMetadataFilters(dtoName, metadata, filterProperties); |
152 | 0 | return metadata; |
153 | |
} |
154 | |
|
155 | |
public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){ |
156 | 0 | for (TransformFilter filter:outboundFilterList){ |
157 | 0 | if (filter instanceof MetadataFilter){ |
158 | 0 | ((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties); |
159 | |
} |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | |
public Metadata getUnfilteredMetadata(String dtoName){ |
164 | 0 | Metadata metadata = metadataService.getMetadata(dtoName); |
165 | 0 | return metadata; |
166 | |
} |
167 | |
|
168 | |
public void setMetadataService(MetadataServiceImpl metadataService) { |
169 | 0 | this.metadataService = metadataService; |
170 | 0 | } |
171 | |
|
172 | |
|
173 | |
public DataBeanMapper getMapper() { |
174 | 0 | return mapper; |
175 | |
} |
176 | |
|
177 | |
public void setMapper(DataBeanMapper mapper) { |
178 | 0 | this.mapper = mapper; |
179 | 0 | } |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
@Deprecated |
188 | |
public void setFilters(List<TransformFilter> filters){ |
189 | 0 | inboundFilterList.addAll(filters); |
190 | 0 | outboundFilterList.addAll(filters); |
191 | 0 | } |
192 | |
|
193 | |
public void setInboundFilters(List<TransformFilter> filters){ |
194 | 0 | inboundFilterList.addAll(filters); |
195 | 0 | } |
196 | |
|
197 | |
public void setOutboundFilters(List<TransformFilter> filters){ |
198 | 0 | outboundFilterList.addAll(filters); |
199 | 0 | } |
200 | |
|
201 | |
} |