Search in sources :

Example 51 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method update.

@Override
public UserEntity update(UpdateUserEntity updateUserEntity) {
    try {
        LOGGER.debug("Updating {}", updateUserEntity);
        Optional<User> checkUser = userRepository.findByUsername(updateUserEntity.getUsername());
        if (!checkUser.isPresent()) {
            throw new UserNotFoundException(updateUserEntity.getUsername());
        }
        User user = checkUser.get();
        User previousUser = new User(user);
        // Set date fields
        user.setUpdatedAt(new Date());
        // Set variant fields
        user.setPicture(updateUserEntity.getPicture());
        user.setFirstname(updateUserEntity.getFirstname());
        user.setLastname(updateUserEntity.getLastname());
        User updatedUser = userRepository.update(user);
        auditService.createPortalAuditLog(Collections.singletonMap(USER, user.getUsername()), User.AuditEvent.USER_UPDATED, user.getUpdatedAt(), previousUser, user);
        return convert(updatedUser, true);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update {}", updateUserEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying update " + updateUserEntity, ex);
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 52 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class UserServiceImpl method connect.

@Override
public UserEntity connect(String userId) {
    try {
        LOGGER.debug("Connection of {}", userId);
        Optional<User> checkUser = userRepository.findById(userId);
        if (!checkUser.isPresent()) {
            throw new UserNotFoundException(userId);
        }
        User user = checkUser.get();
        User previousUser = new User(user);
        // First connection: create default application for user
        if (defaultApplicationForFirstConnection && user.getLastConnectionAt() == null) {
            LOGGER.debug("Create a default application for {}", userId);
            NewApplicationEntity defaultApp = new NewApplicationEntity();
            defaultApp.setName("Default application");
            defaultApp.setDescription("My default application");
            applicationService.create(defaultApp, userId);
        }
        // Set date fields
        user.setLastConnectionAt(new Date());
        user.setUpdatedAt(user.getLastConnectionAt());
        User updatedUser = userRepository.update(user);
        auditService.createPortalAuditLog(Collections.singletonMap(USER, userId), User.AuditEvent.USER_CONNECTED, user.getUpdatedAt(), previousUser, user);
        return convert(updatedUser, true);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to connect {}", userId, ex);
        throw new TechnicalManagementException("An error occurs while trying to connect " + userId, ex);
    }
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) User(io.gravitee.repository.management.model.User) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 53 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class ViewServiceImpl method delete.

@Override
public void delete(final String viewId) {
    if (View.ALL_ID.equals(viewId)) {
        LOGGER.error("Delete the default view is forbidden");
        throw new TechnicalManagementException("Delete the default view is forbidden");
    }
    try {
        Optional<View> viewOptional = viewRepository.findById(viewId);
        if (viewOptional.isPresent()) {
            viewRepository.delete(viewId);
            auditService.createPortalAuditLog(Collections.singletonMap(VIEW, viewId), VIEW_DELETED, new Date(), null, viewOptional.get());
            // delete all reference on APIs
            apiService.deleteViewFromAPIs(viewId);
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete view {}", viewId, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete view " + viewId, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 54 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class ViewServiceImpl method create.

@Override
public List<ViewEntity> create(final List<NewViewEntity> viewEntities) {
    // First we prevent the duplicate view name
    final List<String> viewNames = viewEntities.stream().map(NewViewEntity::getName).collect(Collectors.toList());
    final Optional<ViewEntity> optionalView = findAll().stream().filter(view -> viewNames.contains(view.getName())).findAny();
    if (optionalView.isPresent()) {
        throw new DuplicateViewNameException(optionalView.get().getName());
    }
    final List<ViewEntity> savedViews = new ArrayList<>(viewEntities.size());
    viewEntities.forEach(viewEntity -> {
        try {
            View view = convert(viewEntity);
            savedViews.add(convert(viewRepository.create(view)));
            auditService.createPortalAuditLog(Collections.singletonMap(VIEW, view.getId()), VIEW_CREATED, new Date(), null, view);
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to create view {}", viewEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to create view " + viewEntity.getName(), ex);
        }
    });
    return savedViews;
}
Also used : ViewEntity(io.gravitee.management.model.ViewEntity) java.util(java.util) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) Logger(org.slf4j.Logger) ApiService(io.gravitee.management.service.ApiService) ViewService(io.gravitee.management.service.ViewService) VIEW_DELETED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_DELETED) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) VIEW(io.gravitee.repository.management.model.Audit.AuditProperties.VIEW) VIEW_UPDATED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_UPDATED) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) View(io.gravitee.repository.management.model.View) Autowired(org.springframework.beans.factory.annotation.Autowired) NewViewEntity(io.gravitee.management.model.NewViewEntity) AuditService(io.gravitee.management.service.AuditService) Collectors(java.util.stream.Collectors) IdGenerator(io.gravitee.common.utils.IdGenerator) Component(org.springframework.stereotype.Component) VIEW_CREATED(io.gravitee.repository.management.model.View.AuditEvent.VIEW_CREATED) ViewRepository(io.gravitee.repository.management.api.ViewRepository) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ViewEntity(io.gravitee.management.model.ViewEntity) UpdateViewEntity(io.gravitee.management.model.UpdateViewEntity) NewViewEntity(io.gravitee.management.model.NewViewEntity) DuplicateViewNameException(io.gravitee.management.service.exceptions.DuplicateViewNameException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 55 with TechnicalException

use of io.gravitee.repository.exceptions.TechnicalException in project gravitee-management-rest-api by gravitee-io.

the class ViewServiceImpl method createDefaultView.

@Override
public void createDefaultView() {
    View view = new View();
    view.setId(View.ALL_ID);
    view.setName("All");
    view.setDefaultView(true);
    view.setOrder(0);
    view.setCreatedAt(new Date());
    view.setUpdatedAt(view.getCreatedAt());
    try {
        viewRepository.create(view);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create view {}", view.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create view " + view.getName(), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) View(io.gravitee.repository.management.model.View) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)102 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)80 Logger (org.slf4j.Logger)22 LoggerFactory (org.slf4j.LoggerFactory)22 Autowired (org.springframework.beans.factory.annotation.Autowired)22 Component (org.springframework.stereotype.Component)20 java.util (java.util)18 Collectors (java.util.stream.Collectors)18 io.gravitee.management.model (io.gravitee.management.model)16 AuditService (io.gravitee.management.service.AuditService)12 UUID (io.gravitee.common.utils.UUID)11 Date (java.util.Date)11 IdGenerator (io.gravitee.common.utils.IdGenerator)9 IOException (java.io.IOException)9 io.gravitee.management.service (io.gravitee.management.service)8 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 Metadata (io.gravitee.repository.management.model.Metadata)8 Rating (io.gravitee.repository.management.model.Rating)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7