|
1
|
|
|
package es.webbeta.serializer; |
|
2
|
|
|
|
|
3
|
|
|
import es.webbeta.serializer.base.MetadataProperty; |
|
4
|
|
|
import es.webbeta.serializer.base.MetadataAccessor; |
|
5
|
|
|
import es.webbeta.serializer.base.ParentFieldData; |
|
6
|
|
|
import es.webbeta.serializer.base.SerializerMetadataProvider; |
|
7
|
|
|
import es.webbeta.serializer.metadata.*; |
|
8
|
|
|
import es.webbeta.serializer.type.FieldAccessType; |
|
9
|
|
|
import org.yaml.snakeyaml.Yaml; |
|
10
|
|
|
|
|
11
|
|
|
import java.util.*; |
|
12
|
|
|
|
|
13
|
|
|
public class SerializerYamlMetadataProvider implements SerializerMetadataProvider { |
|
14
|
|
|
|
|
15
|
|
|
private MetadataAccessor metadataAccessor; |
|
16
|
|
|
|
|
17
|
|
|
private Map<String, Metadata> metadatas; |
|
18
|
|
|
|
|
19
|
|
|
public SerializerYamlMetadataProvider(MetadataAccessor metadataAccessor) { |
|
20
|
|
|
this.metadataAccessor = metadataAccessor; |
|
21
|
|
|
|
|
22
|
|
|
metadatas = new HashMap<>(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private void initializeMetadata(Class<?> klass) { |
|
26
|
|
|
if (metadatas.containsKey(klass.getCanonicalName())) |
|
27
|
|
|
return; |
|
28
|
|
|
|
|
29
|
|
|
Yaml yaml = new Yaml(); |
|
30
|
|
|
|
|
31
|
|
|
if (!metadataAccessor.hasMetadata(klass)) { |
|
32
|
|
|
putNullMetadata(klass); |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
String yamlFileContents = metadataAccessor.getMetadataContent(klass); |
|
37
|
|
|
Metadata metadata = MetadataConstructor.build(yaml.loadAs(yamlFileContents, Map.class)); |
|
38
|
|
|
|
|
39
|
|
|
if (metadata == null || !metadata.appliesTo(klass.getCanonicalName())) { |
|
40
|
|
|
putNullMetadata(klass); |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
this.metadatas.put(klass.getCanonicalName(), metadata); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private void putNullMetadata(Class<?> klass) { |
|
48
|
|
|
this.metadatas.put(klass.getCanonicalName(), null); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private Boolean containsMetadata(Class<?> klass) { |
|
52
|
|
|
return metadatas.containsKey(klass.getCanonicalName()) && |
|
53
|
|
|
metadatas.get(klass.getCanonicalName()) != null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private Metadata getMetadata(Class<?> klass) { |
|
57
|
|
|
return metadatas.get(klass.getCanonicalName()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
@Override |
|
61
|
|
|
public Boolean canProvide(Class<?> klass) { |
|
62
|
|
|
initializeMetadata(klass); |
|
63
|
|
|
|
|
64
|
|
|
return metadataAccessor.hasMetadata(klass) && |
|
65
|
|
|
containsMetadata(klass); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
@Override |
|
69
|
|
|
public String[] getPropertiesByGroup(Class<?> klass, ParentFieldData parentData, String... group) { |
|
70
|
|
|
initializeMetadata(klass); |
|
71
|
|
|
|
|
72
|
|
|
if (!containsMetadata(klass)) |
|
73
|
|
|
return new String[0]; |
|
74
|
|
|
|
|
75
|
|
|
List<String> properties = new ArrayList<>(); |
|
76
|
|
|
|
|
77
|
|
|
Metadata metadata = getMetadata(klass); |
|
78
|
|
|
|
|
79
|
|
|
if (metadata.hasProperties()) { |
|
80
|
|
|
for (es.webbeta.serializer.metadata.MetadataProperty property : metadata.getProperties()) { |
|
81
|
|
|
if (parentData != null && parentData.isRecursive(klass, property.getPropertyName())) |
|
82
|
|
|
continue; |
|
83
|
|
|
|
|
84
|
|
|
if (!property.hasGroups()) |
|
85
|
|
|
continue; |
|
86
|
|
|
|
|
87
|
|
|
if (property.appliesToGroups(Arrays.asList(group))) |
|
88
|
|
|
properties.add(property.getPropertyName()); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (metadata.hasVirtualProperties()) { |
|
93
|
|
|
for (MetadataVirtualProperty property : metadata.getVirtualProperties()) { |
|
94
|
|
|
if (parentData != null && parentData.isRecursive(klass, property.getPropertyName())) |
|
95
|
|
|
continue; |
|
96
|
|
|
|
|
97
|
|
|
if (!property.hasGroups()) |
|
98
|
|
|
continue; |
|
99
|
|
|
|
|
100
|
|
|
if (property.appliesToGroups(Arrays.asList(group))) |
|
101
|
|
|
properties.add(property.getPropertyName()); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return properties.toArray(new String[0]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
@Override |
|
109
|
|
|
public String[] getGroupsByFieldName(Class<?> klass, String fieldName) { |
|
110
|
|
|
Metadata metadata = getMetadata(klass); |
|
111
|
|
|
MetadataProperty property = metadata.getMixedProperty(fieldName); |
|
112
|
|
|
return property.getGroups().toArray(new String[0]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
@Override |
|
116
|
|
|
public Boolean hasAccessType(Class<?> klass) { |
|
117
|
|
|
Metadata metadata = getMetadata(klass); |
|
118
|
|
|
return metadata.hasAccessType(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
@Override |
|
122
|
|
|
public FieldAccessType getAccessType(Class<?> klass) { |
|
123
|
|
|
Metadata metadata = getMetadata(klass); |
|
124
|
|
|
return metadata.getAccessType(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
@Override |
|
128
|
|
|
public Boolean hasPropertyAccessType(Class<?> klass, String propertyName) { |
|
129
|
|
|
Metadata metadata = getMetadata(klass); |
|
130
|
|
|
MetadataProperty property = metadata.getMixedProperty(propertyName); |
|
131
|
|
|
return property.hasAccessType(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
@Override |
|
135
|
|
|
public FieldAccessType getPropertyAccessType(Class<?> klass, String propertyName) { |
|
136
|
|
|
Metadata metadata = getMetadata(klass); |
|
137
|
|
|
MetadataProperty property = metadata.getMixedProperty(propertyName); |
|
138
|
|
|
return property.getAccessType(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
@Override |
|
142
|
|
|
public String getPropertyCustomGetterName(Class<?> klass, String propertyName) { |
|
143
|
|
|
Metadata metadata = getMetadata(klass); |
|
144
|
|
|
MetadataProperty property = metadata.getMixedProperty(propertyName); |
|
145
|
|
|
MetadataPropertyAccessor accessor = property.getAccessor(); |
|
146
|
|
|
if (accessor == null || !accessor.hasGetter()) |
|
147
|
|
|
return null; |
|
148
|
|
|
else |
|
149
|
|
|
return accessor.getGetter(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
@Override |
|
153
|
|
|
public Boolean hasPropertySerializedName(Class<?> klass, String propertyName) { |
|
154
|
|
|
Metadata metadata = getMetadata(klass); |
|
155
|
|
|
MetadataProperty property = metadata.getMixedProperty(propertyName); |
|
156
|
|
|
return property.hasSerializedName(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
@Override |
|
160
|
|
|
public String getPropertySerializedName(Class<?> klass, String propertyName) { |
|
161
|
|
|
Metadata metadata = getMetadata(klass); |
|
162
|
|
|
MetadataProperty property = metadata.getMixedProperty(propertyName); |
|
163
|
|
|
return property.getSerializedName(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
@Override |
|
167
|
|
|
public Boolean isVirtualProperty(Class<?> klass, String propertyName) { |
|
168
|
|
|
Metadata metadata = getMetadata(klass); |
|
169
|
|
|
MetadataProperty property = metadata.getMixedProperty(propertyName); |
|
170
|
|
|
return property instanceof MetadataVirtualProperty; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
} |