View Javadoc
1   package de.dlr.shepard.common.neo4j.entities;
2   
3   import de.dlr.shepard.auth.users.entities.User;
4   import de.dlr.shepard.common.util.HasId;
5   import de.dlr.shepard.common.util.HasNeo4jId;
6   import de.dlr.shepard.common.util.Neo4jLabels;
7   import java.util.Date;
8   import java.util.Objects;
9   import lombok.Getter;
10  import lombok.NoArgsConstructor;
11  import lombok.Setter;
12  import lombok.ToString;
13  import org.neo4j.ogm.annotation.GeneratedValue;
14  import org.neo4j.ogm.annotation.Id;
15  import org.neo4j.ogm.annotation.Index;
16  import org.neo4j.ogm.annotation.Relationship;
17  import org.neo4j.ogm.annotation.typeconversion.DateLong;
18  
19  /*
20   * Abstract base class for most entities that
21   * - have an id
22   * - have a deleted flag
23   * - have meta data for create and update
24   */
25  @Getter
26  @Setter
27  @ToString
28  @NoArgsConstructor
29  public abstract class AbstractEntity implements HasId, Deletable, HasProvenance, HasNeo4jId {
30  
31    @Id
32    @GeneratedValue
33    protected Long id;
34  
35    @Index
36    protected boolean deleted = false;
37  
38    @DateLong
39    protected Date createdAt;
40  
41    @ToString.Exclude
42    @Relationship(type = Neo4jLabels.CREATED_BY)
43    protected User createdBy;
44  
45    @DateLong
46    protected Date updatedAt;
47  
48    @ToString.Exclude
49    @Relationship(type = Neo4jLabels.UPDATED_BY)
50    protected User updatedBy;
51  
52    /**
53     * For testing purposes only
54     *
55     * @param id identifies the entity
56     */
57    public AbstractEntity(long id) {
58      this.id = id;
59    }
60  
61    @Override
62    public int hashCode() {
63      return Objects.hash(this.getClass().getName(), id);
64    }
65  
66    @Override
67    public boolean equals(Object o) {
68      if (this == o) return true;
69      if (o == null) return false;
70      if (this.getClass() != o.getClass()) return false;
71      AbstractEntity entity = (AbstractEntity) o;
72      return (
73        id.equals(entity.id) &&
74        deleted == entity.deleted &&
75        Objects.equals(createdAt, entity.createdAt) &&
76        Objects.equals(createdBy, entity.createdBy) &&
77        Objects.equals(updatedAt, entity.updatedAt) &&
78        Objects.equals(updatedBy, entity.updatedBy)
79      );
80    }
81  
82    @Override
83    public String getUniqueId() {
84      return id.toString();
85    }
86  }