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      return entities.stream().map(BasicEntity::getId).mapToLong(Long::longValue).toArray();
76    }
77  
78    protected static long[] extractShepardIds(List<? extends VersionableEntity> entities) {
79      return entities.stream().map(VersionableEntity::getShepardId).mapToLong(Long::longValue).toArray();
80    }
81  
82    @Override
83    public String getUniqueId() {
84      return id.toString();
85    }
86  
87    @Override
88    public boolean equals(Object o) {
89      if (this == o) return true;
90      if (o == null) return false;
91      if (this.getClass() != o.getClass()) return false;
92      BasicEntityIO other = (BasicEntityIO) o;
93      return (
94        Objects.equals(id, other.id) &&
95        Objects.equals(createdAt, other.createdAt) &&
96        Objects.equals(createdBy, other.createdBy) &&
97        Objects.equals(updatedAt, other.updatedAt) &&
98        Objects.equals(updatedBy, other.updatedBy) &&
99        Objects.equals(name, other.name)
100     );
101   }
102 
103   @Override
104   public int hashCode() {
105     final int prime = 31;
106     int result = 0;
107     result = prime * result + Objects.hash(id, createdAt, createdBy, updatedAt, updatedBy, name);
108     return result;
109   }
110 }