Search in sources :

Example 1 with Dashboard

use of io.gravitee.repository.management.model.Dashboard in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method create.

@Override
public DashboardEntity create(final NewDashboardEntity dashboardEntity) {
    try {
        final List<Dashboard> dashboards = dashboardRepository.findByReferenceType(dashboardEntity.getReferenceType().name());
        Dashboard dashboard = dashboardRepository.create(convert(dashboardEntity, dashboards));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(DASHBOARD, dashboard.getId()), DASHBOARD_CREATED, dashboard.getCreatedAt(), null, dashboard);
        return convert(dashboard);
    } catch (TechnicalException ex) {
        final String error = "An error occurred while trying to create dashboard " + dashboardEntity;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with Dashboard

use of io.gravitee.repository.management.model.Dashboard in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method reorderAndSaveDashboards.

private DashboardEntity reorderAndSaveDashboards(final Dashboard dashboardToReorder, final boolean deleted) throws TechnicalException {
    final Collection<Dashboard> dashboards = dashboardRepository.findByReferenceType(dashboardToReorder.getReferenceType());
    final Dashboard[] dashboardsToReorder = dashboards.stream().filter(d -> !Objects.equals(d.getId(), dashboardToReorder.getId())).toArray(Dashboard[]::new);
    if (dashboardToReorder.getOrder() < 1) {
        dashboardToReorder.setOrder(1);
    } else if (dashboardToReorder.getOrder() > dashboardsToReorder.length + 1) {
        dashboardToReorder.setOrder(dashboardsToReorder.length + 1);
    }
    try {
        for (int i = 0; i < dashboardsToReorder.length; i++) {
            if (deleted) {
                if (dashboardsToReorder[i].getOrder() > dashboardToReorder.getOrder()) {
                    dashboardsToReorder[i].setOrder(dashboardsToReorder[i].getOrder() - 1);
                    dashboardRepository.update(dashboardsToReorder[i]);
                }
            } else {
                int newOrder = (i + 1) < dashboardToReorder.getOrder() ? (i + 1) : (i + 2);
                if (dashboardsToReorder[i].getOrder() != newOrder) {
                    dashboardsToReorder[i].setOrder(newOrder);
                    dashboardRepository.update(dashboardsToReorder[i]);
                }
            }
        }
        if (deleted) {
            return null;
        }
        return convert(dashboardRepository.update(dashboardToReorder));
    } catch (final TechnicalException ex) {
        final String error = "An error occurs while trying to update dashboard " + dashboardToReorder.getId();
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : Charset.defaultCharset(java.nio.charset.Charset.defaultCharset) java.util(java.util) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) DASHBOARD(io.gravitee.repository.management.model.Audit.AuditProperties.DASHBOARD) Value(org.springframework.beans.factory.annotation.Value) AuditEvent(io.gravitee.repository.management.model.Dashboard.AuditEvent) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) JsonNode(com.fasterxml.jackson.databind.JsonNode) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardService(io.gravitee.rest.api.service.DashboardService) Comparator.comparing(java.util.Comparator.comparing) DashboardRepository(io.gravitee.repository.management.api.DashboardRepository) UuidString(io.gravitee.rest.api.service.common.UuidString) Comparator.comparingInt(java.util.Comparator.comparingInt) Logger(org.slf4j.Logger) Dashboard(io.gravitee.repository.management.model.Dashboard) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AuditService(io.gravitee.rest.api.service.AuditService) IOException(java.io.IOException) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) DashboardNotFoundException(io.gravitee.rest.api.service.exceptions.DashboardNotFoundException) DashboardReferenceType(io.gravitee.repository.management.model.DashboardReferenceType) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with Dashboard

use of io.gravitee.repository.management.model.Dashboard in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method delete.

@Override
public void delete(final String dashboardId) {
    try {
        Optional<Dashboard> dashboardOptional = dashboardRepository.findById(dashboardId);
        if (dashboardOptional.isPresent()) {
            dashboardRepository.delete(dashboardId);
            reorderAndSaveDashboards(dashboardOptional.get(), true);
            auditService.createEnvironmentAuditLog(Collections.singletonMap(DASHBOARD, dashboardId), DASHBOARD_DELETED, new Date(), null, dashboardOptional.get());
        }
    } catch (TechnicalException ex) {
        final String error = "An error occurs while trying to delete dashboard " + dashboardId;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with Dashboard

use of io.gravitee.repository.management.model.Dashboard in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method convert.

private Dashboard convert(final UpdateDashboardEntity dashboardEntity) {
    final Dashboard dashboard = new Dashboard();
    dashboard.setId(dashboardEntity.getId());
    dashboard.setReferenceId(dashboardEntity.getReferenceId());
    dashboard.setReferenceType(dashboardEntity.getReferenceType().name());
    dashboard.setName(dashboardEntity.getName());
    dashboard.setQueryFilter(dashboardEntity.getQueryFilter());
    dashboard.setOrder(dashboardEntity.getOrder());
    dashboard.setEnabled(dashboardEntity.isEnabled());
    dashboard.setDefinition(dashboardEntity.getDefinition());
    dashboard.setUpdatedAt(new Date());
    return dashboard;
}
Also used : Dashboard(io.gravitee.repository.management.model.Dashboard)

Example 5 with Dashboard

use of io.gravitee.repository.management.model.Dashboard in project gravitee-management-rest-api by gravitee-io.

the class DashboardServiceImpl method update.

@Override
public DashboardEntity update(UpdateDashboardEntity dashboardEntity) {
    try {
        final Optional<Dashboard> dashboardOptional = dashboardRepository.findById(dashboardEntity.getId());
        if (dashboardOptional.isPresent()) {
            final Dashboard dashboard = convert(dashboardEntity);
            final DashboardEntity savedDashboard;
            if (dashboard.getOrder() != dashboardOptional.get().getOrder()) {
                savedDashboard = reorderAndSaveDashboards(dashboard, false);
            } else {
                savedDashboard = convert(dashboardRepository.update(dashboard));
            }
            auditService.createEnvironmentAuditLog(Collections.singletonMap(DASHBOARD, dashboard.getId()), DASHBOARD_UPDATED, new Date(), dashboardOptional.get(), dashboard);
            return savedDashboard;
        } else {
            throw new DashboardNotFoundException(dashboardEntity.getId());
        }
    } catch (TechnicalException ex) {
        final String error = "An error occurred while trying to update dashboard " + dashboardEntity;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : DashboardNotFoundException(io.gravitee.rest.api.service.exceptions.DashboardNotFoundException) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UpdateDashboardEntity(io.gravitee.rest.api.model.UpdateDashboardEntity) DashboardEntity(io.gravitee.rest.api.model.DashboardEntity) NewDashboardEntity(io.gravitee.rest.api.model.NewDashboardEntity) Dashboard(io.gravitee.repository.management.model.Dashboard) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

Dashboard (io.gravitee.repository.management.model.Dashboard)13 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)7 DashboardEntity (io.gravitee.rest.api.model.DashboardEntity)7 NewDashboardEntity (io.gravitee.rest.api.model.NewDashboardEntity)7 UpdateDashboardEntity (io.gravitee.rest.api.model.UpdateDashboardEntity)7 Date (java.util.Date)6 Test (org.junit.Test)6 UuidString (io.gravitee.rest.api.service.common.UuidString)5 DashboardNotFoundException (io.gravitee.rest.api.service.exceptions.DashboardNotFoundException)5 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)5 DashboardRepository (io.gravitee.repository.management.api.DashboardRepository)3 DASHBOARD (io.gravitee.repository.management.model.Audit.AuditProperties.DASHBOARD)3 DashboardReferenceType (io.gravitee.repository.management.model.DashboardReferenceType)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 PLATFORM (io.gravitee.rest.api.model.DashboardReferenceType.PLATFORM)2 DashboardServiceImpl (io.gravitee.rest.api.service.impl.DashboardServiceImpl)2 Collections.singleton (java.util.Collections.singleton)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Optional.empty (java.util.Optional.empty)2