-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathV3ApiUtil.java
287 lines (265 loc) · 12.9 KB
/
V3ApiUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package io.apicurio.registry.rest.v3;
import io.apicurio.common.apps.config.DynamicConfigPropertyDef;
import io.apicurio.common.apps.config.DynamicConfigPropertyDto;
import io.apicurio.registry.rest.v3.beans.ArtifactMetaData;
import io.apicurio.registry.rest.v3.beans.ArtifactReference;
import io.apicurio.registry.rest.v3.beans.ArtifactSearchResults;
import io.apicurio.registry.rest.v3.beans.BranchMetaData;
import io.apicurio.registry.rest.v3.beans.BranchSearchResults;
import io.apicurio.registry.rest.v3.beans.Comment;
import io.apicurio.registry.rest.v3.beans.ConfigurationProperty;
import io.apicurio.registry.rest.v3.beans.GroupMetaData;
import io.apicurio.registry.rest.v3.beans.GroupSearchResults;
import io.apicurio.registry.rest.v3.beans.RoleMapping;
import io.apicurio.registry.rest.v3.beans.RoleMappingSearchResults;
import io.apicurio.registry.rest.v3.beans.SearchedArtifact;
import io.apicurio.registry.rest.v3.beans.SearchedBranch;
import io.apicurio.registry.rest.v3.beans.SearchedGroup;
import io.apicurio.registry.rest.v3.beans.SearchedVersion;
import io.apicurio.registry.rest.v3.beans.SortOrder;
import io.apicurio.registry.rest.v3.beans.VersionMetaData;
import io.apicurio.registry.rest.v3.beans.VersionSearchResults;
import io.apicurio.registry.storage.dto.ArtifactMetaDataDto;
import io.apicurio.registry.storage.dto.ArtifactReferenceDto;
import io.apicurio.registry.storage.dto.ArtifactSearchResultsDto;
import io.apicurio.registry.storage.dto.ArtifactVersionMetaDataDto;
import io.apicurio.registry.storage.dto.BranchMetaDataDto;
import io.apicurio.registry.storage.dto.BranchSearchResultsDto;
import io.apicurio.registry.storage.dto.CommentDto;
import io.apicurio.registry.storage.dto.EditableArtifactMetaDataDto;
import io.apicurio.registry.storage.dto.GroupMetaDataDto;
import io.apicurio.registry.storage.dto.GroupSearchResultsDto;
import io.apicurio.registry.storage.dto.RoleMappingDto;
import io.apicurio.registry.storage.dto.RoleMappingSearchResultsDto;
import io.apicurio.registry.storage.dto.VersionSearchResultsDto;
import io.apicurio.registry.types.RoleType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public final class V3ApiUtil {
private V3ApiUtil() {
}
/**
* Creates a jax-rs meta-data entity from the id, type, and artifactStore meta-data.
*
* @param dto
*/
public static ArtifactMetaData dtoToArtifactMetaData(ArtifactMetaDataDto dto) {
ArtifactMetaData metaData = new ArtifactMetaData();
metaData.setOwner(dto.getOwner());
metaData.setCreatedOn(new Date(dto.getCreatedOn()));
metaData.setDescription(dto.getDescription());
metaData.setGroupId(dto.getGroupId());
metaData.setArtifactId(dto.getArtifactId());
metaData.setModifiedBy(dto.getModifiedBy());
metaData.setModifiedOn(new Date(dto.getModifiedOn()));
metaData.setName(dto.getName());
metaData.setArtifactType(dto.getArtifactType());
metaData.setLabels(dto.getLabels());
return metaData;
}
/**
* Creates a jax-rs version meta-data entity from the id, type, and artifactStore meta-data.
*
* @param dto
*/
public static VersionMetaData dtoToVersionMetaData(ArtifactVersionMetaDataDto dto) {
VersionMetaData metaData = new VersionMetaData();
metaData.setGroupId(dto.getGroupId());
metaData.setArtifactId(dto.getArtifactId());
metaData.setOwner(dto.getOwner());
metaData.setCreatedOn(new Date(dto.getCreatedOn()));
metaData.setModifiedBy(dto.getModifiedBy());
metaData.setModifiedOn(new Date(dto.getModifiedOn()));
metaData.setDescription(dto.getDescription());
metaData.setName(dto.getName());
metaData.setArtifactType(dto.getArtifactType());
metaData.setVersion(dto.getVersion());
metaData.setGlobalId(dto.getGlobalId());
metaData.setContentId(dto.getContentId());
metaData.setState(dto.getState());
metaData.setLabels(dto.getLabels());
return metaData;
}
/**
* Sets values from the EditableArtifactMetaDataDto into the ArtifactMetaDataDto.
*
* @param dto
* @param editableArtifactMetaData
* @return the updated ArtifactMetaDataDto object
*/
public static ArtifactMetaDataDto setEditableMetaDataInArtifact(ArtifactMetaDataDto dto,
EditableArtifactMetaDataDto editableArtifactMetaData) {
if (editableArtifactMetaData.getName() != null) {
dto.setName(editableArtifactMetaData.getName());
}
if (editableArtifactMetaData.getDescription() != null) {
dto.setDescription(editableArtifactMetaData.getDescription());
}
if (editableArtifactMetaData.getLabels() != null && !editableArtifactMetaData.getLabels().isEmpty()) {
dto.setLabels(editableArtifactMetaData.getLabels());
}
return dto;
}
public static Comparator<ArtifactMetaDataDto> comparator(SortOrder sortOrder) {
return (id1, id2) -> compare(sortOrder, id1, id2);
}
public static int compare(SortOrder sortOrder, ArtifactMetaDataDto metaDataDto1,
ArtifactMetaDataDto metaDataDto2) {
String name1 = metaDataDto1.getName();
if (name1 == null) {
name1 = metaDataDto1.getArtifactId();
}
String name2 = metaDataDto2.getName();
if (name2 == null) {
name2 = metaDataDto2.getArtifactId();
}
return sortOrder == SortOrder.desc ? name2.compareToIgnoreCase(name1)
: name1.compareToIgnoreCase(name2);
}
public static ArtifactSearchResults dtoToSearchResults(ArtifactSearchResultsDto dto) {
ArtifactSearchResults results = new ArtifactSearchResults();
results.setCount((int) dto.getCount());
results.setArtifacts(new ArrayList<>(dto.getArtifacts().size()));
dto.getArtifacts().forEach(artifact -> {
SearchedArtifact sa = new SearchedArtifact();
sa.setOwner(artifact.getOwner());
sa.setCreatedOn(artifact.getCreatedOn());
sa.setDescription(artifact.getDescription());
sa.setArtifactId(artifact.getArtifactId());
sa.setGroupId(artifact.getGroupId());
sa.setModifiedBy(artifact.getModifiedBy());
sa.setModifiedOn(artifact.getModifiedOn());
sa.setName(artifact.getName());
sa.setArtifactType(artifact.getArtifactType());
sa.setLabels(artifact.getLabels());
results.getArtifacts().add(sa);
});
return results;
}
public static GroupSearchResults dtoToSearchResults(GroupSearchResultsDto dto) {
GroupSearchResults results = new GroupSearchResults();
results.setCount(dto.getCount());
results.setGroups(new ArrayList<>(dto.getGroups().size()));
dto.getGroups().forEach(group -> {
SearchedGroup sg = new SearchedGroup();
sg.setOwner(group.getOwner());
sg.setCreatedOn(group.getCreatedOn());
sg.setDescription(group.getDescription());
sg.setGroupId(group.getId());
sg.setModifiedBy(group.getModifiedBy());
sg.setModifiedOn(group.getModifiedOn());
sg.setLabels(group.getLabels());
results.getGroups().add(sg);
});
return results;
}
public static BranchSearchResults dtoToSearchResults(BranchSearchResultsDto dto) {
BranchSearchResults results = new BranchSearchResults();
results.setCount(dto.getCount());
results.setBranches(new ArrayList<>(dto.getBranches().size()));
dto.getBranches().forEach(branch -> {
SearchedBranch searchedBranch = new SearchedBranch();
searchedBranch.setOwner(branch.getOwner());
searchedBranch.setCreatedOn(new Date(branch.getCreatedOn()));
searchedBranch.setDescription(branch.getDescription());
searchedBranch.setSystemDefined(branch.isSystemDefined());
searchedBranch.setBranchId(branch.getBranchId());
searchedBranch.setModifiedBy(branch.getModifiedBy());
searchedBranch.setModifiedOn(new Date(branch.getModifiedOn()));
results.getBranches().add(searchedBranch);
});
return results;
}
public static VersionSearchResults dtoToSearchResults(VersionSearchResultsDto dto) {
VersionSearchResults results = new VersionSearchResults();
results.setCount((int) dto.getCount());
results.setVersions(new ArrayList<>(dto.getVersions().size()));
dto.getVersions().forEach(version -> {
SearchedVersion sv = new SearchedVersion();
sv.setGroupId(version.getGroupId());
sv.setArtifactId(version.getArtifactId());
sv.setVersion(version.getVersion());
sv.setOwner(version.getOwner());
sv.setCreatedOn(version.getCreatedOn());
sv.setModifiedBy(version.getModifiedBy());
sv.setModifiedOn(version.getModifiedOn());
sv.setDescription(version.getDescription());
sv.setGlobalId(version.getGlobalId());
sv.setContentId(version.getContentId());
sv.setName(version.getName());
sv.setState(version.getState());
sv.setArtifactType(version.getArtifactType());
sv.setLabels(version.getLabels());
results.getVersions().add(sv);
});
return results;
}
public static ArtifactReferenceDto referenceToDto(ArtifactReference reference) {
final ArtifactReferenceDto artifactReference = new ArtifactReferenceDto();
artifactReference.setGroupId(reference.getGroupId());
artifactReference.setName(reference.getName());
artifactReference.setVersion(reference.getVersion());
artifactReference.setArtifactId(reference.getArtifactId());
return artifactReference;
}
public static List<ArtifactReference> referenceDtosToReferences(List<ArtifactReferenceDto> dtos) {
return dtos.stream().map(dto -> referenceDtoToReference(dto)).collect(Collectors.toList());
}
public static ArtifactReference referenceDtoToReference(ArtifactReferenceDto reference) {
final ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setGroupId(reference.getGroupId());
artifactReference.setName(reference.getName());
artifactReference.setVersion(reference.getVersion());
artifactReference.setArtifactId(reference.getArtifactId());
return artifactReference;
}
public static GroupMetaData groupDtoToGroup(GroupMetaDataDto dto) {
GroupMetaData group = new GroupMetaData();
group.setGroupId(dto.getGroupId());
group.setDescription(dto.getDescription());
group.setOwner(dto.getOwner());
group.setModifiedBy(dto.getModifiedBy());
group.setCreatedOn(new Date(dto.getCreatedOn()));
group.setModifiedOn(new Date(dto.getModifiedOn()));
group.setLabels(dto.getLabels());
return group;
}
public static Comment commentDtoToComment(CommentDto dto) {
return Comment.builder().commentId(dto.getCommentId()).owner(dto.getOwner())
.createdOn(new Date(dto.getCreatedOn())).value(dto.getValue()).build();
}
public static RoleMapping dtoToRoleMapping(RoleMappingDto dto) {
RoleMapping mapping = new RoleMapping();
mapping.setPrincipalId(dto.getPrincipalId());
mapping.setRole(RoleType.valueOf(dto.getRole()));
mapping.setPrincipalName(dto.getPrincipalName());
return mapping;
}
public static RoleMappingSearchResults dtoToRoleMappingSearchResults(RoleMappingSearchResultsDto dto) {
RoleMappingSearchResults results = new RoleMappingSearchResults();
results.setCount((int) dto.getCount());
results.setRoleMappings(dto.getRoleMappings().stream().map(rm -> {
return dtoToRoleMapping(rm);
}).collect(Collectors.toList()));
return results;
}
public static ConfigurationProperty dtoToConfigurationProperty(DynamicConfigPropertyDef def,
DynamicConfigPropertyDto dto) {
ConfigurationProperty rval = new ConfigurationProperty();
rval.setName(def.getName());
rval.setValue(dto.getValue());
rval.setType(def.getType().getName());
rval.setLabel(def.getLabel());
rval.setDescription(def.getDescription());
return rval;
}
public static BranchMetaData dtoToBranchMetaData(BranchMetaDataDto branch) {
return BranchMetaData.builder().groupId(branch.getGroupId()).artifactId(branch.getArtifactId())
.branchId(branch.getBranchId()).description(branch.getDescription()).owner(branch.getOwner())
.systemDefined(branch.isSystemDefined()).createdOn(new Date(branch.getCreatedOn()))
.modifiedBy(branch.getModifiedBy()).modifiedOn(new Date(branch.getModifiedOn())).build();
}
}