Created
June 29, 2017 11:49
-
-
Save JeromeGuyon/2dece07a5f4256955d121b2e41ea3395 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Base document for versionning an entity | |
| * | |
| * @param <T> | |
| */ | |
| public abstract class AbstractVersionnedDocument<T extends AbstractVersionnedEntity> { | |
| @Id | |
| protected String id; | |
| @Version | |
| @Field("_optimisticLock") | |
| protected Long optimisticLock; | |
| @CreatedBy | |
| @Field("audit.createdBy") | |
| protected String createdBy; | |
| @CreatedDate | |
| @Field("audit.createdDate") | |
| protected LocalDateTime createdDate; //todo : check | |
| @LastModifiedBy | |
| @Field("audit.lastModifiedBy") | |
| protected String lastModifiedBy; | |
| @LastModifiedDate | |
| @Field("audit.lastModifiedDate") | |
| protected LocalDateTime lastModifiedDate; | |
| /** | |
| * Current entity | |
| */ | |
| @Field("current") | |
| protected T current = null; | |
| /** | |
| * All previous versions | |
| */ | |
| protected List<T> previous = new ArrayList<>(); | |
| public T createNewVersion() { | |
| T t = createNewInstance(); | |
| Long newVersion = 0l; | |
| if (current != null) { | |
| //got a previous version, time to archive it | |
| previous.add(current); | |
| newVersion = current.getVersion() + 1; | |
| } | |
| current = t; | |
| current.setVersion(newVersion); | |
| return getCurrentVersion(); | |
| } | |
| public T getCurrentVersion() { | |
| return current; | |
| } | |
| /** | |
| * Propagate audit infos to entity version | |
| */ | |
| public void updateAuditInfosOntoCurrentVersion() { | |
| if (current != null) { | |
| current.setCreatedBy(lastModifiedBy); | |
| current.setCreatedDate(lastModifiedDate); | |
| } | |
| } | |
| public List<T> listAllVersions() { | |
| List<T> res = new ArrayList<>(); | |
| res.addAll(previous); | |
| res.add(current); | |
| return Collections.unmodifiableList(res); | |
| } | |
| public List<T> getPrevious() { | |
| return previous; | |
| } | |
| protected abstract T createNewInstance(); | |
| public String getId() { | |
| return id; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment