package sonia.scm; import com.github.sdorra.ssp.PermissionCheck; import sonia.scm.util.AssertUtil; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; public class ManagerDaoAdapter { private final GenericDAO dao; public ManagerDaoAdapter(GenericDAO dao) { this.dao = dao; } public void modify(T object, Function permissionCheck, AroundHandler beforeUpdate, AroundHandler afterUpdate) { T notModified = dao.get(object.getId()); 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 { throw new NotFoundException(object.getClass(), object.getId()); } } public T create(T newObject, Supplier permissionCheck, AroundHandler beforeCreate, AroundHandler afterCreate) { return create(newObject, permissionCheck, beforeCreate, afterCreate, o -> { if (dao.contains(o)) { throw new AlreadyExistsException(newObject); } }); } public T create(T newObject, Supplier permissionCheck, AroundHandler beforeCreate, AroundHandler afterCreate, Consumer existsCheck) { permissionCheck.get().check(); AssertUtil.assertIsValid(newObject); existsCheck.accept(newObject); newObject.setCreationDate(System.currentTimeMillis()); beforeCreate.handle(newObject); dao.add(newObject); afterCreate.handle(newObject); return newObject; } public void delete(T toDelete, Supplier permissionCheck, AroundHandler beforeDelete, AroundHandler afterDelete) { permissionCheck.get().check(); if (dao.contains(toDelete)) { beforeDelete.handle(toDelete); dao.delete(toDelete); afterDelete.handle(toDelete); } else { throw new NotFoundException(toDelete.getClass(), toDelete.getId()); } } @FunctionalInterface public interface AroundHandler { void handle(T notModified); } }