2018-07-12 10:15:32 +02:00
|
|
|
package sonia.scm;
|
|
|
|
|
|
|
|
|
|
import com.github.sdorra.ssp.PermissionCheck;
|
|
|
|
|
import sonia.scm.util.AssertUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
|
|
public class ManagerDaoAdapter<T extends ModelObject, E extends Exception> {
|
|
|
|
|
|
|
|
|
|
private final GenericDAO<T> dao;
|
2018-07-12 10:20:16 +02:00
|
|
|
private final Function<T, E> notFoundException;
|
2018-07-12 10:15:32 +02:00
|
|
|
private final Function<T, E> alreadyExistsException;
|
|
|
|
|
|
2018-07-12 10:20:16 +02:00
|
|
|
public ManagerDaoAdapter(GenericDAO<T> dao, Function<T, E> notFoundException, Function<T, E> alreadyExistsException) {
|
2018-07-12 10:15:32 +02:00
|
|
|
this.dao = dao;
|
|
|
|
|
this.notFoundException = notFoundException;
|
|
|
|
|
this.alreadyExistsException = alreadyExistsException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void modify(T object, Function<T, PermissionCheck> permissionCheck, AroundHandler<T, E> beforeUpdate, AroundHandler<T, E> afterUpdate) throws E {
|
2018-07-12 10:20:16 +02:00
|
|
|
T notModified = dao.get(object.getId());
|
2018-07-12 10:15:32 +02:00
|
|
|
if (notModified != null) {
|
|
|
|
|
permissionCheck.apply(notModified).check();
|
|
|
|
|
AssertUtil.assertIsValid(object);
|
|
|
|
|
|
|
|
|
|
beforeUpdate.handle(notModified);
|
|
|
|
|
|
|
|
|
|
object.setLastModified(System.currentTimeMillis());
|
|
|
|
|
object.setCreationDate(notModified.getCreationDate());
|
|
|
|
|
|
|
|
|
|
dao.modify(object);
|
|
|
|
|
|
|
|
|
|
afterUpdate.handle(notModified);
|
|
|
|
|
} else {
|
2018-07-12 10:20:16 +02:00
|
|
|
throw notFoundException.apply(object);
|
2018-07-12 10:15:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T create(T newObject, Supplier<PermissionCheck> permissionCheck, AroundHandler<T, E> beforeCreate, AroundHandler<T, E> afterCreate) throws E {
|
|
|
|
|
permissionCheck.get().check();
|
|
|
|
|
AssertUtil.assertIsValid(newObject);
|
|
|
|
|
if (dao.contains(newObject)) {
|
|
|
|
|
throw alreadyExistsException.apply(newObject);
|
|
|
|
|
}
|
|
|
|
|
newObject.setCreationDate(System.currentTimeMillis());
|
|
|
|
|
beforeCreate.handle(newObject);
|
|
|
|
|
dao.add(newObject);
|
|
|
|
|
afterCreate.handle(newObject);
|
|
|
|
|
return newObject;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 10:30:33 +02:00
|
|
|
public void delete(T toDelete, Supplier<PermissionCheck> permissionCheck, AroundHandler<T, E> beforeDelete, AroundHandler<T, E> afterDelete) throws E {
|
|
|
|
|
permissionCheck.get().check();
|
|
|
|
|
if (dao.contains(toDelete)) {
|
|
|
|
|
beforeDelete.handle(toDelete);
|
|
|
|
|
dao.delete(toDelete);
|
|
|
|
|
afterDelete.handle(toDelete);
|
|
|
|
|
} else {
|
|
|
|
|
throw notFoundException.apply(toDelete);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 10:15:32 +02:00
|
|
|
@FunctionalInterface
|
|
|
|
|
public interface AroundHandler<T extends ModelObject, E extends Exception> {
|
|
|
|
|
void handle(T notModified) throws E;
|
|
|
|
|
}
|
|
|
|
|
}
|