|
16 | 16 |
|
17 | 17 | package org.springframework.data.ebean.domain; |
18 | 18 |
|
| 19 | +import io.ebean.annotation.CreatedTimestamp; |
| 20 | +import io.ebean.annotation.UpdatedTimestamp; |
| 21 | +import io.ebean.annotation.WhoCreated; |
| 22 | +import io.ebean.annotation.WhoModified; |
| 23 | +import org.springframework.data.domain.Auditable; |
19 | 24 | import org.springframework.data.domain.Persistable; |
20 | 25 | import org.springframework.util.ClassUtils; |
21 | 26 |
|
22 | 27 | import javax.persistence.Id; |
23 | 28 | import javax.persistence.MappedSuperclass; |
24 | 29 | import javax.persistence.Transient; |
| 30 | +import java.time.LocalDateTime; |
| 31 | +import java.util.Optional; |
25 | 32 |
|
26 | 33 | /** |
27 | | - * Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements |
| 34 | + * Abstract base class for entities. |
28 | 35 | * {@link #equals(Object)} and {@link #hashCode()} based on that id. |
29 | 36 | * |
30 | 37 | * @author Xuegui Yuan |
31 | 38 | */ |
32 | 39 | @MappedSuperclass |
33 | | -public abstract class AbstractEntity implements Persistable<Long> { |
| 40 | +public abstract class AbstractEntity implements Persistable<Long>, Auditable<String, Long, LocalDateTime> { |
34 | 41 |
|
35 | | - private static final long serialVersionUID = -5554308939380869754L; |
| 42 | + private static final long serialVersionUID = -5554308939380869754L; |
36 | 43 |
|
37 | 44 | @Id |
38 | | - protected Long id; |
39 | | - |
40 | | - /* |
41 | | - * (non-Javadoc) |
42 | | - * |
43 | | - * @see java.lang.Object#hashCode() |
44 | | - */ |
45 | | - @Override |
46 | | - public int hashCode() { |
47 | | - |
48 | | - int hashCode = 17; |
49 | | - |
50 | | - hashCode += null == getId() ? 0 : getId().hashCode() * 31; |
51 | | - |
52 | | - return hashCode; |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public Long getId() { |
57 | | - return id; |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Sets the id of the entity. |
62 | | - * |
63 | | - * @param id the id to set |
64 | | - */ |
65 | | - public void setId(final Long id) { |
66 | | - this.id = id; |
67 | | - } |
68 | | - |
69 | | - @Transient |
70 | | - @Override |
71 | | - public boolean isNew() { |
72 | | - return null == getId(); |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public boolean equals(Object obj) { |
77 | | - |
78 | | - if (null == obj) { |
79 | | - return false; |
| 45 | + protected Long id; |
| 46 | + |
| 47 | + @WhoCreated |
| 48 | + String createdBy; |
| 49 | + |
| 50 | + @CreatedTimestamp |
| 51 | + LocalDateTime createdDate; |
| 52 | + |
| 53 | + @WhoModified |
| 54 | + String lastModifiedBy; |
| 55 | + |
| 56 | + @UpdatedTimestamp |
| 57 | + LocalDateTime lastModifiedDate; |
| 58 | + |
| 59 | + @Override |
| 60 | + public Long getId() { |
| 61 | + return id; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the id of the entity. |
| 66 | + * |
| 67 | + * @param id the id to set |
| 68 | + */ |
| 69 | + public void setId(final Long id) { |
| 70 | + this.id = id; |
| 71 | + } |
| 72 | + |
| 73 | + @Transient |
| 74 | + @Override |
| 75 | + public boolean isNew() { |
| 76 | + return null == getId(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Optional<String> getCreatedBy() { |
| 81 | + return Optional.ofNullable(createdBy); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void setCreatedBy(String createdBy) { |
| 86 | + this.createdBy = createdBy; |
80 | 87 | } |
81 | 88 |
|
82 | | - if (this == obj) { |
83 | | - return true; |
| 89 | + @Override |
| 90 | + public Optional<LocalDateTime> getCreatedDate() { |
| 91 | + return Optional.ofNullable(createdDate); |
84 | 92 | } |
85 | 93 |
|
86 | | - if (!getClass().equals(ClassUtils.getUserClass(obj))) { |
87 | | - return false; |
| 94 | + @Override |
| 95 | + public void setCreatedDate(LocalDateTime createdDate) { |
| 96 | + this.createdDate = createdDate; |
88 | 97 | } |
89 | 98 |
|
90 | | - AbstractEntity that = (AbstractEntity) obj; |
| 99 | + @Override |
| 100 | + public Optional<String> getLastModifiedBy() { |
| 101 | + return Optional.ofNullable(lastModifiedBy); |
| 102 | + } |
91 | 103 |
|
92 | | - return null == this.getId() ? false : this.getId().equals(that.getId()); |
93 | | - } |
| 104 | + @Override |
| 105 | + public void setLastModifiedBy(String lastModifiedBy) { |
| 106 | + this.lastModifiedBy = lastModifiedBy; |
| 107 | + } |
94 | 108 |
|
95 | | - /* |
96 | | - * (non-Javadoc) |
97 | | - * |
98 | | - * @see java.lang.Object#toString() |
99 | | - */ |
100 | | - @Override |
101 | | - public String toString() { |
102 | | - return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId()); |
103 | | - } |
| 109 | + @Override |
| 110 | + public Optional<LocalDateTime> getLastModifiedDate() { |
| 111 | + return Optional.ofNullable(lastModifiedDate); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void setLastModifiedDate(LocalDateTime lastModifiedDate) { |
| 116 | + this.lastModifiedDate = lastModifiedDate; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /* |
| 121 | + * (non-Javadoc) |
| 122 | + * |
| 123 | + * @see java.lang.Object#hashCode() |
| 124 | + */ |
| 125 | + @Override |
| 126 | + public int hashCode() { |
| 127 | + int hashCode = 17; |
| 128 | + |
| 129 | + hashCode += null == getId() ? 0 : getId().hashCode() * 31; |
| 130 | + |
| 131 | + return hashCode; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean equals(Object obj) { |
| 136 | + if (null == obj) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + if (this == obj) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + if (!getClass().equals(ClassUtils.getUserClass(obj))) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + AbstractEntity that = (AbstractEntity) obj; |
| 149 | + |
| 150 | + return null == this.getId() ? false : this.getId().equals(that.getId()); |
| 151 | + } |
| 152 | + |
| 153 | + /* |
| 154 | + * (non-Javadoc) |
| 155 | + * |
| 156 | + * @see java.lang.Object#toString() |
| 157 | + */ |
| 158 | + @Override |
| 159 | + public String toString() { |
| 160 | + return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId()); |
| 161 | + } |
104 | 162 |
|
105 | 163 |
|
106 | 164 | } |
0 commit comments