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