mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
Introduce ManagerDaoAdapter for create and modify
This commit is contained in:
@@ -53,5 +53,11 @@ public interface ModelObject
|
|||||||
*
|
*
|
||||||
* @return unique id
|
* @return unique id
|
||||||
*/
|
*/
|
||||||
public String getId();
|
String getId();
|
||||||
|
|
||||||
|
void setLastModified(Long timestamp);
|
||||||
|
|
||||||
|
Long getCreationDate();
|
||||||
|
|
||||||
|
void setCreationDate(Long timestamp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,15 +42,9 @@ package sonia.scm.group;
|
|||||||
public class GroupAlreadyExistsException extends GroupException
|
public class GroupAlreadyExistsException extends GroupException
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private static final long serialVersionUID = 4042878550219750430L;
|
private static final long serialVersionUID = 4042878550219750430L;
|
||||||
|
|
||||||
/**
|
public GroupAlreadyExistsException(Group group) {
|
||||||
* Constructs a new instance.
|
super(group.getName() + " group already exists");
|
||||||
*
|
|
||||||
* @param name The name (aka id) of the group
|
|
||||||
*/
|
|
||||||
public GroupAlreadyExistsException(String name) {
|
|
||||||
super(name + " group already exists");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,6 @@ public class GroupNotFoundException extends GroupException
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public GroupNotFoundException() {
|
public GroupNotFoundException() {
|
||||||
super("group does not exists");
|
super("group does not exist");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,17 +52,11 @@ public class RepositoryNotFoundException extends RepositoryException
|
|||||||
* error detail message.
|
* error detail message.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public RepositoryNotFoundException() {}
|
public RepositoryNotFoundException() {
|
||||||
|
super("repository does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public RepositoryNotFoundException(String repositoryId) {
|
||||||
* Constructs a new {@link RepositoryNotFoundException} with the specified
|
super("repository with id " + repositoryId + " does not exist");
|
||||||
* error detail message.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param message error detail message
|
|
||||||
*/
|
|
||||||
public RepositoryNotFoundException(String message)
|
|
||||||
{
|
|
||||||
super(message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,20 +37,19 @@ package sonia.scm.repository.api;
|
|||||||
|
|
||||||
import com.github.legman.ReferenceType;
|
import com.github.legman.ReferenceType;
|
||||||
import com.github.legman.Subscribe;
|
import com.github.legman.Subscribe;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.HandlerEventType;
|
import sonia.scm.HandlerEventType;
|
||||||
import sonia.scm.cache.Cache;
|
import sonia.scm.cache.Cache;
|
||||||
import sonia.scm.cache.CacheManager;
|
import sonia.scm.cache.CacheManager;
|
||||||
import sonia.scm.config.ScmConfiguration;
|
import sonia.scm.config.ScmConfiguration;
|
||||||
|
import sonia.scm.event.ScmEventBus;
|
||||||
|
import sonia.scm.repository.ClearRepositoryCacheEvent;
|
||||||
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
|
import sonia.scm.repository.PostReceiveRepositoryHookEvent;
|
||||||
import sonia.scm.repository.PreProcessorUtil;
|
import sonia.scm.repository.PreProcessorUtil;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
@@ -63,11 +62,9 @@ import sonia.scm.repository.spi.RepositoryServiceProvider;
|
|||||||
import sonia.scm.repository.spi.RepositoryServiceResolver;
|
import sonia.scm.repository.spi.RepositoryServiceResolver;
|
||||||
import sonia.scm.security.ScmSecurityException;
|
import sonia.scm.security.ScmSecurityException;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import sonia.scm.event.ScmEventBus;
|
|
||||||
import sonia.scm.repository.ClearRepositoryCacheEvent;
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link RepositoryServiceFactory} is the entrypoint of the repository api.
|
* The {@link RepositoryServiceFactory} is the entrypoint of the repository api.
|
||||||
@@ -179,8 +176,7 @@ public final class RepositoryServiceFactory
|
|||||||
|
|
||||||
if (repository == null)
|
if (repository == null)
|
||||||
{
|
{
|
||||||
throw new RepositoryNotFoundException(
|
throw new RepositoryNotFoundException(repositoryId);
|
||||||
"could not find a repository with id ".concat(repositoryId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return create(repository);
|
return create(repository);
|
||||||
|
|||||||
@@ -41,19 +41,11 @@ package sonia.scm.user;
|
|||||||
public class UserAlreadyExistsException extends UserException
|
public class UserAlreadyExistsException extends UserException
|
||||||
{
|
{
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private static final long serialVersionUID = 9182294539718090814L;
|
private static final long serialVersionUID = 9182294539718090814L;
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
//~--- constructors ---------------------------------------------------------
|
||||||
|
|
||||||
/**
|
public UserAlreadyExistsException(User user) {
|
||||||
* Constructs a new instance.
|
super(user.getName() + " user already exists");
|
||||||
*
|
|
||||||
* @param name The name (aka id) of the user
|
|
||||||
* @since 1.5
|
|
||||||
*/
|
|
||||||
public UserAlreadyExistsException(String name)
|
|
||||||
{
|
|
||||||
super(name + " user already exists");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,6 @@ public class UserNotFoundException extends UserException
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public UserNotFoundException() {
|
public UserNotFoundException() {
|
||||||
super("user does not exists");
|
super("user does not exist");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
59
scm-webapp/src/main/java/sonia/scm/ManagerDaoAdapter.java
Normal file
59
scm-webapp/src/main/java/sonia/scm/ManagerDaoAdapter.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
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;
|
||||||
|
private final Supplier<E> notFoundException;
|
||||||
|
private final Function<T, E> alreadyExistsException;
|
||||||
|
|
||||||
|
public ManagerDaoAdapter(GenericDAO<T> dao, Supplier<E> notFoundException, Function<T, E> alreadyExistsException) {
|
||||||
|
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 {
|
||||||
|
String name = object.getId();
|
||||||
|
|
||||||
|
T notModified = dao.get(name);
|
||||||
|
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.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface AroundHandler<T extends ModelObject, E extends Exception> {
|
||||||
|
void handle(T notModified) throws E;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,6 +43,7 @@ import com.google.inject.Singleton;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.HandlerEventType;
|
import sonia.scm.HandlerEventType;
|
||||||
|
import sonia.scm.ManagerDaoAdapter;
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
import sonia.scm.TransformFilter;
|
import sonia.scm.TransformFilter;
|
||||||
import sonia.scm.search.SearchRequest;
|
import sonia.scm.search.SearchRequest;
|
||||||
@@ -84,6 +85,10 @@ public class DefaultGroupManager extends AbstractGroupManager
|
|||||||
public DefaultGroupManager(GroupDAO groupDAO)
|
public DefaultGroupManager(GroupDAO groupDAO)
|
||||||
{
|
{
|
||||||
this.groupDAO = groupDAO;
|
this.groupDAO = groupDAO;
|
||||||
|
this.managerDaoAdapter = new ManagerDaoAdapter<>(
|
||||||
|
groupDAO,
|
||||||
|
GroupNotFoundException::new,
|
||||||
|
GroupAlreadyExistsException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -101,46 +106,23 @@ public class DefaultGroupManager extends AbstractGroupManager
|
|||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param group
|
|
||||||
*
|
|
||||||
* @throws GroupException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public Group create(Group group) throws GroupException
|
public Group create(Group group) throws GroupException {
|
||||||
{
|
|
||||||
String type = group.getType();
|
String type = group.getType();
|
||||||
|
if (Util.isEmpty(type)) {
|
||||||
if (Util.isEmpty(type))
|
|
||||||
{
|
|
||||||
group.setType(groupDAO.getType());
|
group.setType(groupDAO.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
String name = group.getName();
|
logger.info("create group {} of type {}", group.getName(), group.getType());
|
||||||
|
|
||||||
if (logger.isInfoEnabled())
|
|
||||||
{
|
|
||||||
logger.info("create group {} of type {}", name,
|
|
||||||
group.getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
GroupPermissions.create().check();
|
|
||||||
|
|
||||||
if (groupDAO.contains(name))
|
|
||||||
{
|
|
||||||
throw new GroupAlreadyExistsException(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
removeDuplicateMembers(group);
|
removeDuplicateMembers(group);
|
||||||
group.setCreationDate(System.currentTimeMillis());
|
|
||||||
fireEvent(HandlerEventType.BEFORE_CREATE, group);
|
return managerDaoAdapter.create(
|
||||||
groupDAO.add(group);
|
group,
|
||||||
fireEvent(HandlerEventType.CREATE, group);
|
GroupPermissions::create,
|
||||||
return group;
|
newGroup -> fireEvent(HandlerEventType.BEFORE_CREATE, newGroup),
|
||||||
|
newGroup -> fireEvent(HandlerEventType.CREATE, newGroup)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -195,31 +177,18 @@ public class DefaultGroupManager extends AbstractGroupManager
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void modify(Group group) throws GroupException
|
public void modify(Group group) throws GroupException {
|
||||||
{
|
logger.info("modify group {} of type {}", group.getName(), group.getType());
|
||||||
if (logger.isInfoEnabled())
|
|
||||||
{
|
|
||||||
logger.info("modify group {} of type {}", group.getName(),
|
|
||||||
group.getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = group.getName();
|
managerDaoAdapter.modify(
|
||||||
GroupPermissions.modify().check(name);
|
group,
|
||||||
|
GroupPermissions::modify,
|
||||||
Group notModified = groupDAO.get(name);
|
notModified -> {
|
||||||
if (notModified != null)
|
|
||||||
{
|
|
||||||
removeDuplicateMembers(group);
|
removeDuplicateMembers(group);
|
||||||
fireEvent(HandlerEventType.BEFORE_MODIFY, group, notModified);
|
fireEvent(HandlerEventType.BEFORE_MODIFY, group, notModified);
|
||||||
group.setLastModified(System.currentTimeMillis());
|
},
|
||||||
group.setCreationDate(notModified.getCreationDate());
|
notModified -> fireEvent(HandlerEventType.MODIFY, group, notModified)
|
||||||
groupDAO.modify(group);
|
);
|
||||||
fireEvent(HandlerEventType.MODIFY, group, notModified);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new GroupNotFoundException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -458,4 +427,5 @@ public class DefaultGroupManager extends AbstractGroupManager
|
|||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private GroupDAO groupDAO;
|
private GroupDAO groupDAO;
|
||||||
|
private final ManagerDaoAdapter<Group, GroupException> managerDaoAdapter;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import sonia.scm.ArgumentIsInvalidException;
|
import sonia.scm.ArgumentIsInvalidException;
|
||||||
import sonia.scm.ConfigurationException;
|
import sonia.scm.ConfigurationException;
|
||||||
import sonia.scm.HandlerEventType;
|
import sonia.scm.HandlerEventType;
|
||||||
|
import sonia.scm.ManagerDaoAdapter;
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
import sonia.scm.Type;
|
import sonia.scm.Type;
|
||||||
import sonia.scm.config.ScmConfiguration;
|
import sonia.scm.config.ScmConfiguration;
|
||||||
@@ -90,6 +91,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
private final Set<Type> types;
|
private final Set<Type> types;
|
||||||
private RepositoryMatcher repositoryMatcher;
|
private RepositoryMatcher repositoryMatcher;
|
||||||
private NamespaceStrategy namespaceStrategy;
|
private NamespaceStrategy namespaceStrategy;
|
||||||
|
private final ManagerDaoAdapter<Repository, RepositoryException> managerDaoAdapter;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -116,6 +118,10 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
for (RepositoryHandler handler : handlerSet) {
|
for (RepositoryHandler handler : handlerSet) {
|
||||||
addHandler(contextProvider, handler);
|
addHandler(contextProvider, handler);
|
||||||
}
|
}
|
||||||
|
managerDaoAdapter = new ManagerDaoAdapter<>(
|
||||||
|
repositoryDAO,
|
||||||
|
RepositoryNotFoundException::new,
|
||||||
|
RepositoryAlreadyExistsException::create);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -128,55 +134,28 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Method description
|
public Repository create(Repository repository) throws RepositoryException {
|
||||||
*
|
return create(repository, true);
|
||||||
*
|
|
||||||
* @param repository
|
|
||||||
* @param initRepository
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
public Repository create(Repository repository, boolean initRepository)
|
|
||||||
throws RepositoryException {
|
|
||||||
logger.info("create repository {} of type {}", repository.getName(),
|
|
||||||
repository.getType());
|
|
||||||
|
|
||||||
RepositoryPermissions.create().check();
|
|
||||||
AssertUtil.assertIsValid(repository);
|
|
||||||
|
|
||||||
if (repositoryDAO.contains(repository)) {
|
|
||||||
throw RepositoryAlreadyExistsException.create(repository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Repository create(Repository repository, boolean initRepository) throws RepositoryException {
|
||||||
repository.setId(keyGenerator.createKey());
|
repository.setId(keyGenerator.createKey());
|
||||||
repository.setCreationDate(System.currentTimeMillis());
|
|
||||||
repository.setNamespace(namespaceStrategy.getNamespace());
|
repository.setNamespace(namespaceStrategy.getNamespace());
|
||||||
|
|
||||||
|
logger.info("create repository {} of type {} in namespace {}", repository.getName(), repository.getType(), repository.getNamespace());
|
||||||
|
|
||||||
|
return managerDaoAdapter.create(
|
||||||
|
repository,
|
||||||
|
RepositoryPermissions::create,
|
||||||
|
newRepository -> {
|
||||||
if (initRepository) {
|
if (initRepository) {
|
||||||
getHandler(repository).create(repository);
|
getHandler(newRepository).create(newRepository);
|
||||||
}
|
}
|
||||||
|
fireEvent(HandlerEventType.BEFORE_CREATE, newRepository);
|
||||||
fireEvent(HandlerEventType.BEFORE_CREATE, repository);
|
},
|
||||||
repositoryDAO.add(repository);
|
newRepository -> fireEvent(HandlerEventType.CREATE, newRepository)
|
||||||
fireEvent(HandlerEventType.CREATE, repository);
|
);
|
||||||
return repository;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param repository
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Repository create(Repository repository)
|
|
||||||
throws RepositoryException {
|
|
||||||
return create(repository, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -209,8 +188,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
repositoryDAO.delete(repository);
|
repositoryDAO.delete(repository);
|
||||||
fireEvent(HandlerEventType.DELETE, repository);
|
fireEvent(HandlerEventType.DELETE, repository);
|
||||||
} else {
|
} else {
|
||||||
throw new RepositoryNotFoundException(
|
throw new RepositoryNotFoundException();
|
||||||
"repository ".concat(repository.getName()).concat(" not found"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,30 +227,18 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
* @throws RepositoryException
|
* @throws RepositoryException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void modify(Repository repository)
|
public void modify(Repository repository) throws RepositoryException {
|
||||||
throws RepositoryException {
|
logger.info("modify repository {} of type {}", repository.getName(), repository.getType());
|
||||||
if (logger.isInfoEnabled()) {
|
|
||||||
logger.info("modify repository {} of type {}", repository.getName(),
|
|
||||||
repository.getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
AssertUtil.assertIsValid(repository);
|
managerDaoAdapter.modify(
|
||||||
|
repository,
|
||||||
Repository oldRepository = repositoryDAO.get(repository.getType(),
|
RepositoryPermissions::modify,
|
||||||
repository.getName());
|
notModified -> {
|
||||||
|
fireEvent(HandlerEventType.BEFORE_MODIFY, repository, notModified);
|
||||||
if (oldRepository != null) {
|
|
||||||
RepositoryPermissions.modify(oldRepository).check();
|
|
||||||
fireEvent(HandlerEventType.BEFORE_MODIFY, repository, oldRepository);
|
|
||||||
repository.setLastModified(System.currentTimeMillis());
|
|
||||||
repository.setCreationDate(oldRepository.getCreationDate());
|
|
||||||
getHandler(repository).modify(repository);
|
getHandler(repository).modify(repository);
|
||||||
repositoryDAO.modify(repository);
|
},
|
||||||
fireEvent(HandlerEventType.MODIFY, repository, oldRepository);
|
notModified -> fireEvent(HandlerEventType.MODIFY, repository, notModified)
|
||||||
} else {
|
);
|
||||||
throw new RepositoryNotFoundException(
|
|
||||||
"repository ".concat(repository.getName()).concat(" not found"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -296,8 +262,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
if (fresh != null) {
|
if (fresh != null) {
|
||||||
fresh.copyProperties(repository);
|
fresh.copyProperties(repository);
|
||||||
} else {
|
} else {
|
||||||
throw new RepositoryNotFoundException(
|
throw new RepositoryNotFoundException();
|
||||||
"repository ".concat(repository.getName()).concat(" not found"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -638,5 +603,4 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
|
|||||||
|
|
||||||
return handler;
|
return handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,11 +41,11 @@ import com.google.inject.Singleton;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.HandlerEventType;
|
import sonia.scm.HandlerEventType;
|
||||||
|
import sonia.scm.ManagerDaoAdapter;
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
import sonia.scm.TransformFilter;
|
import sonia.scm.TransformFilter;
|
||||||
import sonia.scm.search.SearchRequest;
|
import sonia.scm.search.SearchRequest;
|
||||||
import sonia.scm.search.SearchUtil;
|
import sonia.scm.search.SearchUtil;
|
||||||
import sonia.scm.util.AssertUtil;
|
|
||||||
import sonia.scm.util.CollectionAppender;
|
import sonia.scm.util.CollectionAppender;
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
import sonia.scm.util.Util;
|
import sonia.scm.util.Util;
|
||||||
@@ -96,6 +96,10 @@ public class DefaultUserManager extends AbstractUserManager
|
|||||||
public DefaultUserManager(UserDAO userDAO)
|
public DefaultUserManager(UserDAO userDAO)
|
||||||
{
|
{
|
||||||
this.userDAO = userDAO;
|
this.userDAO = userDAO;
|
||||||
|
this.managerDaoAdapter = new ManagerDaoAdapter<>(
|
||||||
|
userDAO,
|
||||||
|
UserNotFoundException::new,
|
||||||
|
UserAlreadyExistsException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -137,33 +141,20 @@ public class DefaultUserManager extends AbstractUserManager
|
|||||||
* @throws UserException
|
* @throws UserException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public User create(User user) throws UserException
|
public User create(User user) throws UserException {
|
||||||
{
|
|
||||||
String type = user.getType();
|
String type = user.getType();
|
||||||
|
if (Util.isEmpty(type)) {
|
||||||
if (Util.isEmpty(type))
|
|
||||||
{
|
|
||||||
user.setType(userDAO.getType());
|
user.setType(userDAO.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isInfoEnabled())
|
|
||||||
{
|
|
||||||
logger.info("create user {} of type {}", user.getName(), user.getType());
|
logger.info("create user {} of type {}", user.getName(), user.getType());
|
||||||
}
|
|
||||||
|
|
||||||
UserPermissions.create().check();
|
return managerDaoAdapter.create(
|
||||||
|
user,
|
||||||
if (userDAO.contains(user.getName()))
|
UserPermissions::create,
|
||||||
{
|
newUser -> fireEvent(HandlerEventType.BEFORE_CREATE, newUser),
|
||||||
throw new UserAlreadyExistsException(user.getName());
|
newUser -> fireEvent(HandlerEventType.CREATE, newUser)
|
||||||
}
|
);
|
||||||
|
|
||||||
AssertUtil.assertIsValid(user);
|
|
||||||
user.setCreationDate(System.currentTimeMillis());
|
|
||||||
fireEvent(HandlerEventType.BEFORE_CREATE, user);
|
|
||||||
userDAO.add(user);
|
|
||||||
fireEvent(HandlerEventType.CREATE, user);
|
|
||||||
return user;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -226,28 +217,14 @@ public class DefaultUserManager extends AbstractUserManager
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void modify(User user) throws UserException
|
public void modify(User user) throws UserException
|
||||||
{
|
|
||||||
String name = user.getName();
|
|
||||||
if (logger.isInfoEnabled())
|
|
||||||
{
|
{
|
||||||
logger.info("modify user {} of type {}", user.getName(), user.getType());
|
logger.info("modify user {} of type {}", user.getName(), user.getType());
|
||||||
}
|
|
||||||
|
|
||||||
UserPermissions.modify(user).check();
|
managerDaoAdapter.modify(
|
||||||
User notModified = userDAO.get(name);
|
user,
|
||||||
if (notModified != null)
|
UserPermissions::modify,
|
||||||
{
|
notModified -> fireEvent(HandlerEventType.BEFORE_MODIFY, user, notModified),
|
||||||
AssertUtil.assertIsValid(user);
|
notModified -> fireEvent(HandlerEventType.MODIFY, user, notModified));
|
||||||
fireEvent(HandlerEventType.BEFORE_MODIFY, user, notModified);
|
|
||||||
user.setLastModified(System.currentTimeMillis());
|
|
||||||
user.setCreationDate(notModified.getCreationDate());
|
|
||||||
userDAO.modify(user);
|
|
||||||
fireEvent(HandlerEventType.MODIFY, user, notModified);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new UserNotFoundException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -497,6 +474,6 @@ public class DefaultUserManager extends AbstractUserManager
|
|||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
|
||||||
private final UserDAO userDAO;
|
private final UserDAO userDAO;
|
||||||
|
private final ManagerDaoAdapter<User, UserException> managerDaoAdapter;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,21 @@ public class AbstractManagerResourceTest {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLastModified(Long timestamp) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Long getCreationDate() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCreationDate(Long timestamp) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getLastModified() {
|
public Long getLastModified() {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user