@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Document
public class BaseEntity {
@Id
private String id;
@Field(name = "createdAt")
private LocalDateTime createdAt;
@Field(name = "createdBy")
private String createdBy;
@Field(name = "updatedAt")
private LocalDateTime updatedAt;
@Field(name = "updatedBy")
private String updatedBy;
@PrePersist
public void prePersist() {
this.createdBy = Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.map(Authentication::getPrincipal)
.filter(user -> !"anonymousUser".equals(user))
.map(Jwt.class::cast)
.map(jwt -> jwt.getClaim(TokenClaims.USER_EMAIL.getValue()).toString())
.orElse("anonymousUser");
this.createdAt = LocalDateTime.now();
}
@PreUpdate
public void preUpdate() {
this.updatedBy = Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.map(Authentication::getPrincipal)
.filter(user -> !"anonymousUser".equals(user))
.map(Jwt.class::cast)
.map(jwt -> jwt.getClaim(TokenClaims.USER_EMAIL.getValue()).toString())
.orElse("anonymousUser");
this.updatedAt = LocalDateTime.now();
}
}
@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Document
public class BaseEntity {
@Id
private String id;
@Field(name = "createdAt")
private LocalDateTime createdAt;
@Field(name = "createdBy")
private String createdBy;
@Field(name = "updatedAt")
private LocalDateTime updatedAt;
@Field(name = "updatedBy")
private String updatedBy;
@PrePersist
public void prePersist() {
this.createdBy = Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.map(Authentication::getPrincipal)
.filter(user -> !"anonymousUser".equals(user))
.map(Jwt.class::cast)
.map(jwt -> jwt.getClaim(TokenClaims.USER_EMAIL.getValue()).toString())
.orElse("anonymousUser");
this.createdAt = LocalDateTime.now();
}
@PreUpdate
public void preUpdate() {
this.updatedBy = Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.map(Authentication::getPrincipal)
.filter(user -> !"anonymousUser".equals(user))
.map(Jwt.class::cast)
.map(jwt -> jwt.getClaim(TokenClaims.USER_EMAIL.getValue()).toString())
.orElse("anonymousUser");
this.updatedAt = LocalDateTime.now();
}
}