Search in sources :

Example 1 with Workflow

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

the class TaskServiceImpl method getMetadata.

@Override
public Metadata getMetadata(List<TaskEntity> tasks) {
    final Metadata metadata = new Metadata();
    tasks.forEach(task -> {
        final Object data = task.getData();
        if (data instanceof SubscriptionEntity) {
            final SubscriptionEntity subscription = (SubscriptionEntity) data;
            if (!metadata.containsKey(subscription.getApplication())) {
                ApplicationEntity applicationEntity = applicationService.findById(subscription.getApplication());
                metadata.put(subscription.getApplication(), "name", applicationEntity.getName());
            }
            if (!metadata.containsKey(subscription.getPlan())) {
                PlanEntity planEntity = planService.findById(subscription.getPlan());
                String apiId = planEntity.getApi();
                ApiEntity api = apiService.findById(apiId);
                metadata.put(subscription.getPlan(), "name", planEntity.getName());
                metadata.put(subscription.getPlan(), "api", apiId);
                metadata.put(apiId, "name", api.getName());
            }
        } else if (data instanceof Workflow) {
            final Workflow workflow = (Workflow) data;
            if (API.name().equals(workflow.getReferenceType()) && !metadata.containsKey(workflow.getReferenceId())) {
                ApiEntity api = apiService.findById(workflow.getReferenceId());
                metadata.put(api.getId(), "name", api.getName());
            }
        }
    });
    return metadata;
}
Also used : Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Workflow(io.gravitee.repository.management.model.Workflow)

Example 2 with Workflow

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

the class TaskServiceImpl method findAll.

@Override
public List<TaskEntity> findAll(String userId) {
    if (userId == null) {
        throw new UnauthorizedAccessException();
    }
    try {
        // because Tasks only consists on subscriptions, we can optimize the search by only look for apis where
        // the user has a SUBSCRIPTION_UPDATE permission
        // search for PENDING subscriptions
        Set<String> apiIds = getApisForAPermission(userId, SUBSCRIPTION.getName());
        final List<TaskEntity> tasks;
        if (apiIds.isEmpty()) {
            tasks = new ArrayList<>();
        } else {
            SubscriptionQuery query = new SubscriptionQuery();
            query.setStatuses(singleton(PENDING));
            query.setApis(apiIds);
            tasks = subscriptionService.search(query).stream().map(this::convert).collect(toList());
        }
        // search for PENDING user registration
        final Page<UserEntity> pendingUsers = userService.search(new UserCriteria.Builder().statuses(UserStatus.PENDING).build(), new PageableImpl(1, NUMBER_OF_PENDING_USERS_TO_SEARCH));
        if (pendingUsers.getContent() != null && !pendingUsers.getContent().isEmpty()) {
            tasks.addAll(pendingUsers.getContent().stream().map(this::convert).collect(toList()));
        }
        // search for IN_REVIEW apis
        apiIds = getApisForAPermission(userId, REVIEWS.getName());
        if (!apiIds.isEmpty()) {
            apiIds.forEach(apiId -> {
                final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
                if (workflows != null && !workflows.isEmpty()) {
                    final Workflow currentWorkflow = workflows.get(0);
                    if (WorkflowState.IN_REVIEW.name().equals(currentWorkflow.getState())) {
                        tasks.add(convert(currentWorkflow));
                    }
                }
            });
        }
        // search for REQUEST_FOR_CHANGES apis
        apiIds = getApisForAPermission(userId, DEFINITION.getName());
        if (!apiIds.isEmpty()) {
            apiIds.forEach(apiId -> {
                final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
                if (workflows != null && !workflows.isEmpty()) {
                    final Workflow currentWorkflow = workflows.get(0);
                    if (WorkflowState.REQUEST_FOR_CHANGES.name().equals(currentWorkflow.getState())) {
                        tasks.add(convert(currentWorkflow));
                    }
                }
            });
        }
        // search for TO_BE_VALIDATED promotions
        tasks.addAll(promotionTasksService.getPromotionTasks(GraviteeContext.getCurrentOrganization()));
        return tasks;
    } catch (TechnicalException e) {
        LOGGER.error("Error retrieving user tasks {}", e.getMessage());
        throw new TechnicalManagementException("Error retreiving user tasks", e);
    }
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Workflow(io.gravitee.repository.management.model.Workflow) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with Workflow

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

the class WorkflowServiceImpl method create.

@Override
public Workflow create(final WorkflowReferenceType referenceType, final String referenceId, final WorkflowType type, final String user, final WorkflowState state, final String comment) {
    final Workflow workflow = new Workflow();
    workflow.setId(UuidString.generateRandom());
    workflow.setReferenceType(referenceType.name());
    workflow.setReferenceId(referenceId);
    workflow.setType(type.name());
    workflow.setUser(user);
    workflow.setState(state.name());
    workflow.setComment(comment);
    workflow.setCreatedAt(new Date());
    try {
        return workflowRepository.create(workflow);
    } catch (TechnicalException ex) {
        final String message = "An error occurs while trying to create workflow of type " + workflow.getType();
        LOGGER.error(message, ex);
        throw new TechnicalManagementException(message, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Workflow(io.gravitee.repository.management.model.Workflow) UuidString(io.gravitee.rest.api.service.common.UuidString) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with Workflow

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

the class ApiService_UpdateTest method shouldNotChangeLifecycleStateFromCreatedInReview.

@Test
public void shouldNotChangeLifecycleStateFromCreatedInReview() throws TechnicalException {
    prepareUpdate();
    when(parameterService.findAsBoolean(Key.API_REVIEW_ENABLED, "DEFAULT", ParameterReferenceType.ENVIRONMENT)).thenReturn(true);
    final Workflow workflow = new Workflow();
    workflow.setState("IN_REVIEW");
    when(workflowService.findByReferenceAndType(API, API_ID, REVIEW)).thenReturn(singletonList(workflow));
    assertUpdate(ApiLifecycleState.CREATED, CREATED, false);
    assertUpdate(ApiLifecycleState.CREATED, PUBLISHED, true);
    assertUpdate(ApiLifecycleState.CREATED, UNPUBLISHED, true);
    assertUpdate(ApiLifecycleState.CREATED, DEPRECATED, true);
}
Also used : Workflow(io.gravitee.repository.management.model.Workflow) Test(org.junit.Test)

Example 5 with Workflow

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

the class ApiService_UpdateTest method prepareReviewAuditTest.

private void prepareReviewAuditTest() throws TechnicalException {
    when(api.getDefinition()).thenReturn(API_DEFINITION);
    when(apiRepository.findById(API_ID)).thenReturn(Optional.of(api));
    final MembershipEntity membership = new MembershipEntity();
    membership.setMemberId(USER_NAME);
    when(membershipService.getPrimaryOwner(MembershipReferenceType.API, API_ID)).thenReturn(membership);
    when(userService.findById(USER_NAME)).thenReturn(mock(UserEntity.class));
    final Workflow workflow = new Workflow();
    workflow.setState(WorkflowState.REQUEST_FOR_CHANGES.name());
    when(workflowService.create(any(), any(), any(), any(), any(), any())).thenReturn(workflow);
}
Also used : Workflow(io.gravitee.repository.management.model.Workflow)

Aggregations

Workflow (io.gravitee.repository.management.model.Workflow)5 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 UserCriteria (io.gravitee.repository.management.api.search.UserCriteria)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)1 Metadata (io.gravitee.rest.api.model.pagedresult.Metadata)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 UnauthorizedAccessException (io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException)1 Date (java.util.Date)1 Test (org.junit.Test)1