View Javadoc
1   /*
2    * Copyright (c) 2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
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   * <p>Metadata -- .</p>
34   *
35   * @author rlichti {@literal <rlichti@kaiserpfalz-edv.de>}
36   * @since 1.0.0  2023-01-19
37   */
38  public interface Metadata extends HasUid, HasTimestamps, Cloneable {
39      /**
40       * @return The display name of the resource.
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       * Checks if there is an annotation for this name.
88       *
89       * @param name the name of the annotation.
90       * @return If there is an annotation for this name.
91       */
92      @JsonIgnore
93      default boolean isAnnotated(@NotNull final String name) {
94          return this.getAnnotations().containsKey(name);
95      }
96  
97      /**
98       * Returns the value of the annotation.
99       *
100      * @param name Annotation name to retrieve
101      * @return The value of the annotation.
102      */
103     @JsonIgnore
104     default Optional<String> getAnnotation(@NotNull final String name) {
105         return Optional.ofNullable(this.getAnnotations().get(name));
106     }
107 
108     /**
109      * Checks if there is a label with a special name.
110      *
111      * @param name The name of the label.
112      * @return If the label is there.
113      */
114     @JsonIgnore
115     default boolean isLabeled(final String name) {
116         return this.getLabels().containsKey(name);
117     }
118 
119     /**
120      * Returns the value of the label.
121      *
122      * @param name Label name to retrieve.
123      * @return The value of the label.
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 }