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