1 package de.dlr.shepard.neo4Core.io;
2
3 import de.dlr.shepard.neo4Core.entities.Permissions;
4 import de.dlr.shepard.neo4Core.entities.User;
5 import de.dlr.shepard.neo4Core.entities.UserGroup;
6 import de.dlr.shepard.util.PermissionType;
7 import jakarta.validation.constraints.NotNull;
8 import lombok.Data;
9 import lombok.NoArgsConstructor;
10 import org.apache.commons.lang3.ArrayUtils;
11 import org.eclipse.microprofile.openapi.annotations.media.Schema;
12
13 @Data
14 @NoArgsConstructor
15 @Schema(name = "Permissions")
16 public class PermissionsIO {
17
18 @Schema(readOnly = true, required = true)
19 private long entityId;
20
21 private String owner;
22
23 @Schema(required = true)
24 private PermissionType permissionType;
25
26 @NotNull
27 @Schema(required = true)
28 private String[] reader;
29
30 @NotNull
31 @Schema(required = true)
32 private String[] writer;
33
34 private long[] readerGroupIds = {};
35
36 private long[] writerGroupIds = {};
37
38 @NotNull
39 @Schema(required = true)
40 private String[] manager;
41
42 public PermissionsIO(Permissions permissions) {
43 this.entityId = permissions.getEntities().get(0).getNumericId();
44 this.permissionType = permissions.getPermissionType();
45 this.owner = permissions.getOwner() != null ? permissions.getOwner().getUsername() : null;
46 this.reader = permissions.getReader().stream().map(User::getUsername).toArray(String[]::new);
47 this.writer = permissions.getWriter().stream().map(User::getUsername).toArray(String[]::new);
48 this.readerGroupIds = ArrayUtils.toPrimitive(
49 permissions.getReaderGroups().stream().map(UserGroup::getId).toArray(Long[]::new)
50 );
51 this.writerGroupIds = ArrayUtils.toPrimitive(
52 permissions.getWriterGroups().stream().map(UserGroup::getId).toArray(Long[]::new)
53 );
54 this.manager = permissions.getManager().stream().map(User::getUsername).toArray(String[]::new);
55 }
56 }