View Javadoc
1   package de.dlr.shepard.neo4Core.io;
2   
3   import com.fasterxml.jackson.annotation.JsonFormat;
4   import de.dlr.shepard.neo4Core.entities.BasicEntity;
5   import de.dlr.shepard.neo4Core.entities.VersionableEntity;
6   import de.dlr.shepard.util.HasId;
7   import jakarta.validation.constraints.NotBlank;
8   import java.util.Date;
9   import java.util.List;
10  import lombok.Data;
11  import lombok.NoArgsConstructor;
12  import org.eclipse.microprofile.openapi.annotations.media.Schema;
13  
14  @Data
15  @NoArgsConstructor
16  @Schema(name = "BasicEntity")
17  public class BasicEntityIO implements HasId {
18  
19    @Schema(readOnly = true, required = true)
20    private Long id;
21  
22    @JsonFormat(shape = JsonFormat.Shape.STRING)
23    @Schema(readOnly = true, required = true, format = "date-time", example = "2024-08-15T11:18:44.632+00:00")
24    private Date createdAt;
25  
26    @Schema(readOnly = true, required = true)
27    private String createdBy;
28  
29    @JsonFormat(shape = JsonFormat.Shape.STRING)
30    @Schema(
31      readOnly = true,
32      nullable = true,
33      required = true,
34      format = "date-time",
35      example = "2024-08-15T11:18:44.632+00:00"
36    )
37    private Date updatedAt;
38  
39    @Schema(readOnly = true, nullable = true, required = true)
40    private String updatedBy;
41  
42    @NotBlank
43    @Schema(required = true)
44    private String name;
45  
46    public BasicEntityIO(BasicEntity entity) {
47      this.id = entity.getId();
48      this.createdAt = entity.getCreatedAt();
49      this.createdBy = entity.getCreatedBy() != null ? entity.getCreatedBy().getUsername() : null;
50      this.updatedAt = entity.getUpdatedAt();
51      this.updatedBy = entity.getUpdatedBy() != null ? entity.getUpdatedBy().getUsername() : null;
52      this.name = entity.getName();
53    }
54  
55    public BasicEntityIO(VersionableEntity entity) {
56      this.id = entity.getShepardId();
57      this.createdAt = entity.getCreatedAt();
58      this.createdBy = entity.getCreatedBy() != null ? entity.getCreatedBy().getUsername() : null;
59      this.updatedAt = entity.getUpdatedAt();
60      this.updatedBy = entity.getUpdatedBy() != null ? entity.getUpdatedBy().getUsername() : null;
61      this.name = entity.getName();
62    }
63  
64    protected static long[] extractIds(List<? extends BasicEntity> entities) {
65      var result = entities.stream().map(BasicEntity::getId).mapToLong(Long::longValue).toArray();
66      return result;
67    }
68  
69    protected static long[] extractShepardIds(List<? extends VersionableEntity> entities) {
70      var result = entities.stream().map(VersionableEntity::getShepardId).mapToLong(Long::longValue).toArray();
71      return result;
72    }
73  
74    @Override
75    public String getUniqueId() {
76      return id.toString();
77    }
78  }