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 | |
|
14 | 0 | public class TransformationManager { |
15 | 0 | final Logger LOG = Logger.getLogger(TransformationManager.class); |
16 | |
|
17 | |
private MetadataServiceImpl metadataService; |
18 | 0 | private DataBeanMapper mapper = new DefaultDataBeanMapper(); |
19 | 0 | private List<TransformFilter> filterList = new ArrayList<TransformFilter>(); |
20 | |
|
21 | 0 | private static ThreadLocal<Metadata> metadataCache = new ThreadLocal<Metadata>(); |
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 = null; |
87 | 0 | metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), filterProperties)); |
88 | 0 | metadataCache.set(metadata); |
89 | 0 | applyInboundFilters(clazz.getName(), value, filterProperties,metadata); |
90 | 0 | Object dtoValue = mapper.convertFromData(value, clazz,metadata); |
91 | 0 | applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata); |
92 | 0 | return dtoValue; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{ |
104 | 0 | for (TransformFilter filter:filterList){ |
105 | 0 | if (filter.getType().isInstance(value)){ |
106 | 0 | if (filter instanceof AbstractDataFilter) { |
107 | 0 | ((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties); |
108 | |
} else { |
109 | 0 | ((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties); |
110 | |
} |
111 | 0 | LOG.info(filter.getClass().getName() + ": Filter Applied"); |
112 | |
} |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{ |
125 | 0 | Metadata metadata = metadataCache.get(); |
126 | |
|
127 | 0 | for (TransformFilter filter:filterList){ |
128 | 0 | if (filter.getType().isInstance(value)){ |
129 | 0 | if (filter instanceof AbstractDataFilter) { |
130 | 0 | metadata = (metadata != null ? metadata:getMetadata(dtoName, properties)); |
131 | 0 | ((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties); |
132 | |
} else { |
133 | 0 | ((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties); |
134 | |
} |
135 | 0 | LOG.info(filter.getClass().getName() + ": Filter Applied"); |
136 | |
} |
137 | |
} |
138 | 0 | } |
139 | |
|
140 | |
public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){ |
141 | 0 | String state = (String)filterProperties.get(DtoConstants.DTO_STATE); |
142 | 0 | String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE); |
143 | 0 | String workflowNode = (String)filterProperties.get(DtoConstants.DTO_WORKFLOW_NODE); |
144 | |
|
145 | |
Metadata metadata; |
146 | 0 | if (workflowNode == null || workflowNode.isEmpty()){ |
147 | 0 | metadata = metadataService.getMetadata(dtoName, null, state, nextState); |
148 | |
} else { |
149 | 0 | metadata = metadataService.getMetadataByWorkflowNode(dtoName, workflowNode); |
150 | |
} |
151 | |
|
152 | 0 | applyMetadataFilters(dtoName, metadata, filterProperties); |
153 | 0 | return metadata; |
154 | |
} |
155 | |
|
156 | |
public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){ |
157 | 0 | for (TransformFilter filter:filterList){ |
158 | 0 | if (filter instanceof MetadataFilter){ |
159 | 0 | ((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties); |
160 | |
} |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
public Metadata getUnfilteredMetadata(String dtoName){ |
165 | 0 | Metadata metadata = metadataService.getMetadata(dtoName); |
166 | 0 | return metadata; |
167 | |
} |
168 | |
|
169 | |
public void setMetadataService(MetadataServiceImpl metadataService) { |
170 | 0 | this.metadataService = metadataService; |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
public DataBeanMapper getMapper() { |
175 | 0 | return mapper; |
176 | |
} |
177 | |
|
178 | |
public void setMapper(DataBeanMapper mapper) { |
179 | 0 | this.mapper = mapper; |
180 | 0 | } |
181 | |
|
182 | |
public void addFilter(TransformFilter filter){ |
183 | 0 | filterList.add(filter); |
184 | 0 | } |
185 | |
|
186 | |
public void setFilters(List<TransformFilter> filters){ |
187 | 0 | filterList.addAll(filters); |
188 | 0 | } |
189 | |
|
190 | |
public void removeFilter(TransformFilter filter){ |
191 | 0 | filterList.remove(filter); |
192 | 0 | } |
193 | |
|
194 | |
} |