1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.kaiserpfalzedv.commons.api.resources;
19
20 import java.time.OffsetDateTime;
21 import java.util.Locale;
22 import java.util.Map;
23 import java.util.Optional;
24
25 import org.eclipse.microprofile.openapi.annotations.media.Schema;
26
27 import com.fasterxml.jackson.annotation.JsonIgnore;
28 import com.fasterxml.jackson.annotation.JsonProperty;
29
30 import jakarta.validation.constraints.NotNull;
31
32
33
34
35
36
37
38 public interface Metadata extends HasUid, HasTimestamps, Cloneable {
39
40
41
42 @Schema(
43 name = "selfLink",
44 description = "The local part of the URL to retrieve the resource.",
45 nullable = true,
46 readOnly = true,
47 pattern = "/<prefix (default: api)>/" + HasName.VALID_NAME_PATTERN
48 + "/" + HasApiVersion.VALID_VERSION_PATTERN
49 + "/" + HasName.VALID_NAME_PATTERN
50 + "/" + HasName.VALID_NAME_PATTERN,
51 minLength = 19,
52 maxLength = 318
53 )
54 @NotNull
55 @JsonProperty(value = "selfLink", access = JsonProperty.Access.READ_ONLY)
56 default String getSelfLink() {
57 String prefix = removeTrailingSlash(getSelfLinkPrefix());
58
59 return String.format("%s/%s/%s/%s/%s",
60 prefix,
61 this.getKind().toLowerCase(Locale.getDefault()), this.getApiVersion().toLowerCase(Locale.getDefault()),
62 this.getNameSpace(), this.getName()
63 );
64 }
65
66 @NotNull
67 @JsonIgnore
68 default String removeTrailingSlash(@NotNull final String prefix) {
69 return prefix.endsWith("/")
70 ? prefix.substring(1, prefix.length() - 1)
71 : prefix;
72 }
73
74 @NotNull
75 @JsonIgnore
76 default String getSelfLinkPrefix() {
77 return "/api";
78 }
79
80 @JsonIgnore
81 Optional<OffsetDateTime> getDeletionTimestamp();
82
83 @JsonIgnore
84 Optional<Pointer> getOwningResource();
85
86
87
88
89
90
91
92 @JsonIgnore
93 default boolean isAnnotated(@NotNull final String name) {
94 return this.getAnnotations().containsKey(name);
95 }
96
97
98
99
100
101
102
103 @JsonIgnore
104 default Optional<String> getAnnotation(@NotNull final String name) {
105 return Optional.ofNullable(this.getAnnotations().get(name));
106 }
107
108
109
110
111
112
113
114 @JsonIgnore
115 default boolean isLabeled(final String name) {
116 return this.getLabels().containsKey(name);
117 }
118
119
120
121
122
123
124
125
126 @JsonIgnore
127 default Optional<String> getLabel(@NotNull final String name) {
128 return Optional.ofNullable(this.getLabels().get(name));
129 }
130
131 Metadata increaseGeneration();
132
133 @JsonIgnore
134 default String getKind() {
135 return this.getIdentity().getKind();
136 }
137
138 @JsonIgnore
139 default String getApiVersion() {
140 return this.getIdentity().getApiVersion();
141 }
142
143 @JsonIgnore
144 default String getNameSpace() {
145 return this.getIdentity().getNameSpace();
146 }
147
148 @JsonIgnore
149 default String getName() {
150 return this.getIdentity().getName();
151 }
152
153 Pointer getIdentity();
154
155 Integer getGeneration();
156
157 Pointer getOwner();
158
159 Map<String, String> getAnnotations();
160
161 Map<String, String> getLabels();
162 }