1 | |
package org.kuali.student.core.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.core.assembly.data.Data; |
10 | |
import org.kuali.student.core.assembly.data.Metadata; |
11 | |
import org.kuali.student.core.assembly.dictionary.MetadataServiceImpl; |
12 | |
import org.kuali.student.core.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 | |
public Data transform(Object value) throws Exception{ |
33 | 0 | applyOutboundFilters(value.getClass().getName(), value, new HashMap<String,Object>()); |
34 | 0 | Data dataValue = mapper.convertFromBean(value); |
35 | 0 | applyOutboundFilters(value.getClass().getName(), dataValue, new HashMap<String,Object>()); |
36 | |
|
37 | 0 | return dataValue; |
38 | |
} |
39 | |
|
40 | |
public Data transform(Object value, Map<String,Object> filterProperties) throws Exception{ |
41 | 0 | applyOutboundFilters(value.getClass().getName(), value, filterProperties); |
42 | 0 | Data dataValue = mapper.convertFromBean(value); |
43 | 0 | applyOutboundFilters(value.getClass().getName(), dataValue, filterProperties); |
44 | |
|
45 | 0 | return dataValue; |
46 | |
} |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public Object transform(Data value, Class<?> clazz) throws Exception{ |
59 | 0 | Metadata metadata = null; |
60 | 0 | metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), new HashMap<String,Object>())); |
61 | 0 | applyInboundFilters(clazz.getName(), value, new HashMap<String,Object>(),metadata); |
62 | 0 | Object dtoValue = mapper.convertFromData(value, clazz,metadata); |
63 | 0 | applyInboundFilters(clazz.getName(), dtoValue, new HashMap<String,Object>(),metadata); |
64 | |
|
65 | 0 | return dtoValue; |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public Object transform(Data value, Class<?> clazz, Map<String,Object> filterProperties) throws Exception{ |
79 | 0 | Metadata metadata = null; |
80 | 0 | metadata = (metadata != null ? metadata:getMetadata(clazz.getName(), filterProperties)); |
81 | 0 | metadataCache.set(metadata); |
82 | 0 | applyInboundFilters(clazz.getName(), value, filterProperties,metadata); |
83 | 0 | Object dtoValue = mapper.convertFromData(value, clazz,metadata); |
84 | 0 | applyInboundFilters(clazz.getName(), dtoValue, filterProperties,metadata); |
85 | 0 | return dtoValue; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void applyInboundFilters(String dtoName, Object value, Map<String,Object> properties, Metadata metadata) throws Exception{ |
97 | 0 | for (TransformFilter filter:filterList){ |
98 | 0 | if (filter.getType().isInstance(value)){ |
99 | 0 | if (filter instanceof AbstractDataFilter) { |
100 | 0 | ((AbstractDataFilter)filter).applyInboundDataFilter((Data)value, metadata, properties); |
101 | |
} else { |
102 | 0 | ((AbstractDTOFilter)filter).applyInboundDtoFilter(value, properties); |
103 | |
} |
104 | 0 | LOG.info(filter.getClass().getName() + ": Filter Applied"); |
105 | |
} |
106 | |
} |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void applyOutboundFilters(String dtoName, Object value, Map<String,Object> properties) throws Exception{ |
118 | 0 | Metadata metadata = metadataCache.get(); |
119 | |
|
120 | 0 | for (TransformFilter filter:filterList){ |
121 | 0 | if (filter.getType().isInstance(value)){ |
122 | 0 | if (filter instanceof AbstractDataFilter) { |
123 | 0 | metadata = (metadata != null ? metadata:getMetadata(dtoName, properties)); |
124 | 0 | ((AbstractDataFilter)filter).applyOutboundDataFilter((Data)value, metadata, properties); |
125 | |
} else { |
126 | 0 | ((AbstractDTOFilter)filter).applyOutboundDtoFilter(value, properties); |
127 | |
} |
128 | 0 | LOG.info(filter.getClass().getName() + ": Filter Applied"); |
129 | |
} |
130 | |
} |
131 | 0 | } |
132 | |
|
133 | |
public Metadata getMetadata(String dtoName, Map<String,Object> filterProperties){ |
134 | 0 | String state = (String)filterProperties.get(DtoConstants.DTO_STATE); |
135 | 0 | String nextState = (String)filterProperties.get(DtoConstants.DTO_NEXT_STATE); |
136 | |
|
137 | 0 | Metadata metadata = metadataService.getMetadata(dtoName, null, state, nextState); |
138 | |
|
139 | 0 | applyMetadataFilters(dtoName, metadata, filterProperties); |
140 | 0 | return metadata; |
141 | |
} |
142 | |
|
143 | |
public void applyMetadataFilters(String dtoName, Metadata metadata, Map<String, Object> filterProperties){ |
144 | 0 | for (TransformFilter filter:filterList){ |
145 | 0 | if (filter instanceof MetadataFilter){ |
146 | 0 | ((MetadataFilter) filter).applyMetadataFilter(dtoName, metadata, filterProperties); |
147 | |
} |
148 | |
} |
149 | 0 | } |
150 | |
|
151 | |
public Metadata getUnfilteredMetadata(String dtoName){ |
152 | 0 | Metadata metadata = metadataService.getMetadata(dtoName); |
153 | 0 | return metadata; |
154 | |
} |
155 | |
|
156 | |
public void setMetadataService(MetadataServiceImpl metadataService) { |
157 | 0 | this.metadataService = metadataService; |
158 | 0 | } |
159 | |
|
160 | |
public void addFilter(TransformFilter filter){ |
161 | 0 | filterList.add(filter); |
162 | 0 | } |
163 | |
|
164 | |
public void setFilters(List<TransformFilter> filters){ |
165 | 0 | filterList.addAll(filters); |
166 | 0 | } |
167 | |
|
168 | |
public void removeFilter(TransformFilter filter){ |
169 | 0 | filterList.remove(filter); |
170 | 0 | } |
171 | |
} |