View Javadoc
1   package de.dlr.shepard.common.neo4j.io;
2   
3   import com.fasterxml.jackson.annotation.JsonFormat;
4   import de.dlr.shepard.common.neo4j.entities.BasicEntity;
5   import de.dlr.shepard.common.util.HasId;
6   import de.dlr.shepard.context.version.entities.VersionableEntity;
7   import jakarta.validation.constraints.NotBlank;
8   import java.util.Date;
9   import java.util.List;
10  import java.util.Objects;
11  import lombok.Data;
12  import lombok.NoArgsConstructor;
13  import org.eclipse.microprofile.openapi.annotations.media.Schema;
14  
15  @Data
16  @NoArgsConstructor
17  @Schema(name = "BasicEntity")
18  public class BasicEntityIO implements HasId {
19  
20    @Schema(readOnly = true, required = true)
21    private Long id;
22  
23    @JsonFormat(shape = JsonFormat.Shape.STRING)
24    @Schema(readOnly = true, required = true, format = "date-time", example = "2024-08-15T11:18:44.632+00:00")
25    private Date createdAt;
26  
27    @Schema(readOnly = true, required = true)
28    private String createdBy;
29  
30    @JsonFormat(shape = JsonFormat.Shape.STRING)
31    @Schema(
32      readOnly = true,
33      nullable = true,
34      required = true,
35      format = "date-time",
36      example = "2024-08-15T11:18:44.632+00:00"
37    )
38    private Date updatedAt;
39  
40    @Schema(readOnly = true, nullable = true, required = true)
41    private String updatedBy;
42  
43    @NotBlank
44    @Schema(required = true)
45    private String name;
46  
47    public BasicEntityIO(BasicEntity entity) {
48      this.id = entity.getId();
49      this.createdAt = entity.getCreatedAt();
50      this.createdBy = entity.getCreatedBy() != null ? entity.getCreatedBy().getUsername() : null;
51      this.updatedAt = entity.getUpdatedAt();
52      this.updatedBy = entity.getUpdatedBy() != null ? entity.getUpdatedBy().getUsername() : null;
53      this.name = entity.getName();
54    }
55  
56    public BasicEntityIO(BasicEntityIO entity) {
57      this.id = entity.getId();
58      this.createdAt = entity.getCreatedAt();
59      this.createdBy = entity.getCreatedBy() != null ? entity.getCreatedBy() : null;
60      this.updatedAt = entity.getUpdatedAt();
61      this.updatedBy = entity.getUpdatedBy() != null ? entity.getUpdatedBy() : null;
62      this.name = entity.getName();
63    }
64  
65    public BasicEntityIO(VersionableEntity entity) {
66      this.id = entity.getShepardId();
67      this.createdAt = entity.getCreatedAt();
68      this.createdBy = entity.getCreatedBy() != null ? entity.getCreatedBy().getUsername() : null;
69      this.updatedAt = entity.getUpdatedAt();
70      this.updatedBy = entity.getUpdatedBy() != null ? entity.getUpdatedBy().getUsername() : null;
71      this.name = entity.getName();
72    }
73  
74    protected static long[] extractIds(List<? extends BasicEntity> entities) {
75      var result = entities.stream().map(BasicEntity::getId).mapToLong(Long::longValue).toArray();
76      return result;
77    }
78  
79    protected static long[] extractShepardIds(List<? extends VersionableEntity> entities) {
80      var result = entities.stream().map(VersionableEntity::getShepardId).mapToLong(Long::longValue).toArray();
81      return result;
82    }
83  
84    @Override
85    public String getUniqueId() {
86      return id.toString();
87    }
88  
89    @Override
90    public boolean equals(Object o) {
91      if (this == o) return true;
92      if (o == null) return false;
93      if (this.getClass() != o.getClass()) return false;
94      BasicEntityIO other = (BasicEntityIO) o;
95      return (
96        Objects.equals(id, other.id) &&
97        Objects.equals(createdAt, other.createdAt) &&
98        Objects.equals(createdBy, other.createdBy) &&
99        Objects.equals(updatedAt, other.updatedAt) &&
100       Objects.equals(updatedBy, other.updatedBy) &&
101       Objects.equals(name, other.name)
102     );
103   }
104 
105   @Override
106   public int hashCode() {
107     final int prime = 31;
108     int result = 0;
109     result = prime * result + Objects.hash(id, createdAt, createdBy, updatedAt, updatedBy, name);
110     return result;
111   }
112 }