io.coodoo.framework.jpa.control.JpaRevisionService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coodoo-jpa-essentials Show documentation
Show all versions of coodoo-jpa-essentials Show documentation
Essential JPA entities and functionality
package io.coodoo.framework.jpa.control;
import java.time.LocalDateTime;
import java.time.ZoneId;
import javax.ejb.Stateless;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import io.coodoo.framework.jpa.boundary.RevisionUser;
import io.coodoo.framework.jpa.boundary.entity.AbstractRevisionDatesEntity;
import io.coodoo.framework.jpa.boundary.entity.AbstractRevisionDeleteMarkerEntity;
import io.coodoo.framework.jpa.boundary.entity.AbstractRevisionEntity;
/**
* Fills the entites revision attributes
*
*
*
* Entity
* Creation Date
* Creation User
* Update Date
* Update User
* Delete Date
* Delete User
*
*
* AbstractRevisionDatesEntity
* X
*
* X
*
*
*
*
*
* AbstractRevisionDatesDeleteMarkerEntity
* X
*
* X
*
* X
*
*
*
* AbstractRevisionEntity
* X
* X
* X
* X
*
*
*
*
* AbstractRevisionDeleteMarkerEntity
* X
* X
* X
* X
* X
* X
*
*
*
*
* @author coodoo GmbH (coodoo.io)
*/
@Stateless
public class JpaRevisionService {
@Inject
Instance revisionUserInstance;
public void markCreation(AbstractRevisionDatesEntity entity) {
entity.setCreatedAt(now());
if (entity instanceof AbstractRevisionEntity) {
RevisionUser revisionUser = revisionUserInstance.get();
((AbstractRevisionEntity) entity).setCreatedBy(revisionUser.getUserId());
}
}
public void markUpdate(AbstractRevisionDatesEntity entity) {
if (entity instanceof AbstractRevisionDeleteMarkerEntity
// triggered by a set deletion marker field
&& (((AbstractRevisionDeleteMarkerEntity) entity).getDeletedAt() != null
|| ((AbstractRevisionDeleteMarkerEntity) entity).getDeletedBy() != null)) {
RevisionUser revisionUser = revisionUserInstance.get();
((AbstractRevisionDeleteMarkerEntity) entity).setDeletedAt(now());
((AbstractRevisionDeleteMarkerEntity) entity).setDeletedBy(revisionUser.getUserId());
}
entity.setUpdatedAt(now());
if (entity instanceof AbstractRevisionEntity) {
RevisionUser revisionUser = revisionUserInstance.get();
((AbstractRevisionEntity) entity).setUpdatedBy(revisionUser.getUserId());
}
}
private LocalDateTime now() {
return LocalDateTime.now(ZoneId.of("UTC"));
}
}