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 { private final GenericDAO dao; private final Function notFoundException; private final Function alreadyExistsException; public ManagerDaoAdapter(GenericDAO dao, Function notFoundException, Function alreadyExistsException) { this.dao = dao; this.notFoundException = notFoundException; this.alreadyExistsException = alreadyExistsException; } public void modify(T object, Function permissionCheck, AroundHandler beforeUpdate, AroundHandler afterUpdate) throws E { 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 notFoundException.apply(object); } } public T create(T newObject, Supplier permissionCheck, AroundHandler beforeCreate, AroundHandler 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; } @FunctionalInterface public interface AroundHandler { void handle(T notModified) throws E; } }