Skip to content
Snippets Groups Projects
Commit 01785180 authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Add finalize methods which are run after create/update

These methods allow additonal application logic based on an created/
updated entity.
parent b60ab6c2
1 merge request!89Foundation for development of version 3.0
...@@ -69,9 +69,12 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService ...@@ -69,9 +69,12 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService
throw new IllegalArgumentException("Entity is not new."); throw new IllegalArgumentException("Entity is not new.");
} }
entity.setCreationTimestamp(new Date()); entity.setCreationTimestamp(new Date());
prepareCreate(entity); prepareCreate(entity);
final T createdEntity = repository.save(entity);
finalizeCreate(entity);
return repository.save(entity); return createdEntity;
} }
/** /**
...@@ -83,6 +86,15 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService ...@@ -83,6 +86,15 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService
} }
/**
* This method can be overridden by subclasses to modify the entity after creation.
*
* @param entity The entity which has been created
*/
protected void finalizeCreate(final T entity) {
}
public T update(final T entity) { public T update(final T entity) {
return update(repository.findOne(entity.getId()), entity); return update(repository.findOne(entity.getId()), entity);
} }
...@@ -92,9 +104,12 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService ...@@ -92,9 +104,12 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService
public T update(final T oldEntity, final T newEntity) { public T update(final T oldEntity, final T newEntity) {
newEntity.setId(oldEntity.getId()); newEntity.setId(oldEntity.getId());
newEntity.setUpdateTimestamp(new Date()); newEntity.setUpdateTimestamp(new Date());
prepareUpdate(newEntity); prepareUpdate(newEntity);
final T updatedEntity = repository.save(newEntity);
finalizeUpdate(updatedEntity);
return repository.save(newEntity); return updatedEntity;
} }
/** /**
...@@ -106,6 +121,15 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService ...@@ -106,6 +121,15 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService
} }
/**
* This method can be overridden by subclasses to modify the entity after updating.
*
* @param entity The entity which has been updated
*/
protected void finalizeUpdate(final T entity) {
}
@Override @Override
public T patch(final T entity, final Map<String, Object> changes) throws IOException { public T patch(final T entity, final Map<String, Object> changes) throws IOException {
return patch(entity, changes, Function.identity()); return patch(entity, changes, Function.identity());
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment