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 com.fasterxml.jackson.annotation.JsonIgnore;
21  import de.kaiserpfalzedv.commons.api.store.StoreService;
22  
23  import java.io.Serializable;
24  import java.util.*;
25  
26  /**
27   * <p>DefaultResourceSpec -- .</p>
28   *
29   * @author rlichti {@literal <rlichti@kaiserpfalz-edv.de>}
30   * @since 1.0.0  2023-01-19
31   */
32  public interface DefaultResourceSpec extends Serializable, Cloneable {
33      /**
34       * Returns a property.
35       *
36       * @param key The unique key of the property within the user dataset.
37       * @return The property saved with the user.
38       */
39      @JsonIgnore
40      default Optional<String> getProperty(final String key) {
41          return Optional.ofNullable(getProperties().get(key));
42      }
43  
44  
45      /**
46       * Returns an array of property names which should be saved by a {@link StoreService}
47       * implementation. You should really overwrite it when needed.
48       *
49       * @return the names of the default properties of this resource.
50       */
51      @JsonIgnore
52      default String[] getDefaultProperties() {
53          throw new UnsupportedOperationException();
54      }
55  
56      /**
57       * Reads a resource pointer from a property.
58       *
59       * @param key The name of the property.
60       * @return The resource pointer.
61       * @throws IllegalStateException    If the property can't be converted.
62       * @throws IllegalArgumentException If the UUID of the pointer can't be read from the property.
63       * @throws NoSuchElementException   There is no such property.
64       */
65      @JsonIgnore
66      default Optional<Pointer> getResourcePointer(String key) {
67          try {
68              String property = getProperty(key).orElseThrow();
69  
70              return Optional.of(convertStringToResourcePointer(property));
71          } catch (NoSuchElementException e) {
72              return Optional.empty();
73          }
74      }
75  
76      @JsonIgnore
77      default List<Pointer> getResourcePointers(String key) {
78          try {
79              String property = getProperty(key).orElseThrow();
80  
81              String[] data = property.split(",");
82  
83              ArrayList<Pointer> result = new ArrayList<>(data.length);
84              for (String p : data) {
85                  result.add(convertStringToResourcePointer(p));
86              }
87  
88              return result;
89          } catch (NoSuchElementException e) {
90              return new ArrayList<>();
91          }
92      }
93  
94  
95      @JsonIgnore
96      Pointer convertStringToResourcePointer(String property);
97  
98      /**
99       * Saves a resource pointer as property.
100      *
101      * @param key     The name of the property.
102      * @param pointer the pointer to save.
103      */
104     @JsonIgnore
105     default void saveResourcePointer(String key, Pointer pointer) {
106         if (pointer != null) {
107             String data = convertResourcePointerToString(pointer);
108 
109             getProperties().put(key, data);
110         }
111     }
112 
113     default String convertResourcePointerToString(Pointer pointer) {
114         return new StringJoiner("/")
115                 .add(pointer.getKind())
116                 .add(pointer.getApiVersion())
117                 .add(pointer.getNameSpace())
118                 .add(pointer.getName())
119                 .toString();
120     }
121 
122     @JsonIgnore
123     void saveResourcePointers(String key, Collection<Pointer> pointers);
124 
125     Map<String, String> getProperties();
126 }