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