View Javadoc
1   /*
2    * Copyright (C) 2023 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.persistence;
7   
8   import jakarta.persistence.Column;
9   import jakarta.persistence.Entity;
10  import jakarta.persistence.EntityListeners;
11  import jakarta.persistence.Id;
12  import jakarta.persistence.ManyToMany;
13  import jakarta.persistence.PreRemove;
14  import jakarta.persistence.Table;
15  import jakarta.persistence.Version;
16  import jakarta.validation.constraints.Pattern;
17  import java.util.ArrayList;
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  import org.hibernate.annotations.Type;
22  import org.tailormap.api.persistence.json.AdminAdditionalProperty;
23  import org.tailormap.api.persistence.listener.EntityEventPublisher;
24  import org.tailormap.api.util.Constants;
25  
26  @Entity
27  @Table(name = "groups")
28  @EntityListeners(EntityEventPublisher.class)
29  public class Group {
30    // Group to make authorization rules for anonymous users
31    public static final String ANONYMOUS = "anonymous";
32    // Group to make authorization rules for authenticated users
33    public static final String AUTHENTICATED = "authenticated";
34    public static final String ADMIN = "admin";
35    public static final String ACTUATOR = "actuator";
36  
37    @Id
38    @Pattern(regexp = Constants.NAME_REGEX, message = "Group " + Constants.NAME_REGEX_INVALID_MESSAGE)
39    private String name;
40  
41    @Version private Long version;
42  
43    private boolean systemGroup;
44  
45    private String description;
46  
47    @Column(columnDefinition = "text")
48    private String notes;
49  
50    @ManyToMany(mappedBy = "groups")
51    private Set<User> members = new HashSet<>();
52  
53    /**
54     * Generic additional properties which can be set on a group. A viewer admin frontend extension
55     * component can define attributes for the purposes of the extension and the viewer admin UI will
56     * show a control to edit the attribute in the group detail form.
57     */
58    @Type(value = io.hypersistence.utils.hibernate.type.json.JsonBinaryType.class)
59    @Column(columnDefinition = "jsonb")
60    private List<AdminAdditionalProperty> additionalProperties = new ArrayList<>();
61  
62    public String getName() {
63      return name;
64    }
65  
66    public Group setName(String name) {
67      this.name = name;
68      return this;
69    }
70  
71    public Long getVersion() {
72      return version;
73    }
74  
75    public Group setVersion(Long version) {
76      this.version = version;
77      return this;
78    }
79  
80    public boolean isSystemGroup() {
81      return systemGroup;
82    }
83  
84    public Group setSystemGroup(boolean systemGroup) {
85      this.systemGroup = systemGroup;
86      return this;
87    }
88  
89    public String getDescription() {
90      return description;
91    }
92  
93    public Group setDescription(String title) {
94      this.description = title;
95      return this;
96    }
97  
98    public String getNotes() {
99      return notes;
100   }
101 
102   public Group setNotes(String notes) {
103     this.notes = notes;
104     return this;
105   }
106 
107   public Set<User> getMembers() {
108     return members;
109   }
110 
111   public Group setMembers(Set<User> members) {
112     this.members = members;
113     return this;
114   }
115 
116   public List<AdminAdditionalProperty> getAdditionalProperties() {
117     return additionalProperties;
118   }
119 
120   public Group setAdditionalProperties(List<AdminAdditionalProperty> additionalProperties) {
121     this.additionalProperties = additionalProperties;
122     return this;
123   }
124 
125   @PreRemove
126   @SuppressWarnings("PMD.UnusedPrivateMethod")
127   private void removeMembers() {
128     for (User user : this.members) {
129       user.getGroups().remove(this);
130     }
131   }
132 }