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.io.Serializable;
21  import java.time.OffsetDateTime;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.Optional;
25  import java.util.UUID;
26  
27  import org.eclipse.microprofile.openapi.annotations.media.Schema;
28  
29  import com.fasterxml.jackson.annotation.JsonIgnore;
30  import com.fasterxml.jackson.annotation.JsonProperty;
31  
32  import jakarta.validation.constraints.NotNull;
33  
34  /**
35   * <p>Metadata -- .</p>
36   *
37   * @author rlichti {@literal <rlichti@kaiserpfalz-edv.de>}
38   * @since 1.0.0  2023-01-19
39   */
40  public interface Metadata extends Serializable, Cloneable {
41      /**
42       * @return The display name of the resource.
43       */
44      @Schema(
45              name = "selfLink",
46              description = "The local part of the URL to retrieve the resource.",
47              nullable = true,
48              readOnly = true,
49              example = "/api/resource/v1/default/name",
50              pattern = "/api/" + HasName.VALID_NAME_PATTERN
51                      + "/" + HasApiVersion.VALID_VERSION_PATTERN
52                      + "/" + HasName.VALID_NAME_PATTERN
53                      + "/" + HasName.VALID_NAME_PATTERN,
54              minLength = 19,
55              maxLength = 318
56      )
57      @JsonProperty(value = "selfLink", access = JsonProperty.Access.READ_ONLY)
58      default String getSelfLink() {
59          return String.format("/api/%s/%s/%s/%s",
60                  this.getKind().toLowerCase(Locale.getDefault()), this.getApiVersion().toLowerCase(Locale.getDefault()),
61                  this.getNameSpace(), this.getName()
62          );
63      }
64  
65      @JsonIgnore
66      Optional<OffsetDateTime> getDeletionTimestamp();
67  
68      @JsonIgnore
69      Optional<Pointer> getOwningResource();
70  
71      /**
72       * Checks if there is an annotation for this name.
73       *
74       * @param name the name of the annotation.
75       * @return If there is an annotation for this name.
76       */
77      @JsonIgnore
78      default boolean isAnnotated(@NotNull final String name) {
79          return this.getAnnotations().containsKey(name);
80      }
81  
82      /**
83       * Returns the value of the annotation.
84       *
85       * @param name Annotation name to retrieve
86       * @return The value of the annotation.
87       */
88      @JsonIgnore
89      default Optional<String> getAnnotation(@NotNull final String name) {
90          return Optional.ofNullable(this.getAnnotations().get(name));
91      }
92  
93      /**
94       * Checks if there is a label with a special name.
95       *
96       * @param name The name of the label.
97       * @return If the label is there.
98       */
99      @JsonIgnore
100     default boolean isLabeled(final String name) {
101         return this.getLabels().containsKey(name);
102     }
103 
104     /**
105      * Returns the value of the label.
106      *
107      * @param name Label name to retrieve.
108      * @return The value of the label.
109      */
110 
111     @JsonIgnore
112     default Optional<String> getLabel(@NotNull final String name) {
113         return Optional.ofNullable(this.getLabels().get(name));
114     }
115 
116     Metadata increaseGeneration();
117 
118     @JsonIgnore
119     default String getKind() {
120         return this.getIdentity().getKind();
121     }
122 
123     @JsonIgnore
124     default String getApiVersion() {
125         return this.getIdentity().getApiVersion();
126     }
127 
128     @JsonIgnore
129     default String getNameSpace() {
130         return this.getIdentity().getNameSpace();
131     }
132 
133     @JsonIgnore
134     default String getName() {
135         return this.getIdentity().getName();
136     }
137 
138     Pointer getIdentity();
139 
140     UUID getUid();
141 
142     Integer getGeneration();
143 
144     Pointer getOwner();
145 
146     OffsetDateTime getCreated();
147 
148     OffsetDateTime getModified();
149 
150     OffsetDateTime getDeleted();
151 
152     Map<String, String> getAnnotations();
153 
154     Map<String, String> getLabels();
155 }