Skip to content

Instantly share code, notes, and snippets.

@JeromeGuyon
Created June 29, 2017 11:49
Show Gist options
  • Select an option

  • Save JeromeGuyon/2dece07a5f4256955d121b2e41ea3395 to your computer and use it in GitHub Desktop.

Select an option

Save JeromeGuyon/2dece07a5f4256955d121b2e41ea3395 to your computer and use it in GitHub Desktop.
/**
* 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