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