Search in sources :

Example 11 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException in project taskana by Taskana.

the class AbstractPagingController method initPageMetadata.

protected PageMetadata initPageMetadata(String pagesizeParam, String pageParam, long totalElements) throws InvalidArgumentException {
    long pagesize;
    long page;
    try {
        pagesize = Long.valueOf(pagesizeParam);
        page = Long.valueOf(pageParam);
    } catch (NumberFormatException e) {
        throw new InvalidArgumentException("page and pagesize must be a integer value.");
    }
    PageMetadata pageMetadata = new PageMetadata(pagesize, page, totalElements);
    if (pageMetadata.getNumber() > pageMetadata.getTotalPages()) {
        // unfortunately no setter for number
        pageMetadata = new PageMetadata(pagesize, pageMetadata.getTotalPages(), totalElements);
    }
    return pageMetadata;
}
Also used : PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException)

Example 12 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException in project taskana by Taskana.

the class WorkbasketController method getWorkbaskets.

@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<PagedResources<WorkbasketSummaryResource>> getWorkbaskets(@RequestParam(value = "sortBy", defaultValue = "name", required = false) String sortBy, @RequestParam(value = "order", defaultValue = "asc", required = false) String order, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "nameLike", required = false) String nameLike, @RequestParam(value = "key", required = false) String key, @RequestParam(value = "keyLike", required = false) String keyLike, @RequestParam(value = "descLike", required = false) String descLike, @RequestParam(value = "owner", required = false) String owner, @RequestParam(value = "ownerLike", required = false) String ownerLike, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "requiredPermission", required = false) String requiredPermission, @RequestParam(value = "page", required = false) String page, @RequestParam(value = "pagesize", required = false) String pageSize) throws InvalidArgumentException {
    WorkbasketQuery query = workbasketService.createWorkbasketQuery();
    addSortingToQuery(query, sortBy, order);
    addAttributeFilter(query, name, nameLike, key, keyLike, descLike, owner, ownerLike, type);
    addAuthorizationFilter(query, requiredPermission);
    PageMetadata pageMetadata = null;
    List<WorkbasketSummary> workbasketSummaries = null;
    if (page != null && pageSize != null) {
        // paging
        long totalElements = query.count();
        pageMetadata = initPageMetadata(pageSize, page, totalElements);
        workbasketSummaries = query.listPage((int) pageMetadata.getNumber(), (int) pageMetadata.getSize());
    } else if (page == null && pageSize == null) {
        // not paging
        workbasketSummaries = query.list();
    } else {
        throw new InvalidArgumentException("Paging information is incomplete.");
    }
    WorkbasketSummaryResourcesAssembler assembler = new WorkbasketSummaryResourcesAssembler();
    PagedResources<WorkbasketSummaryResource> pagedResources = assembler.toResources(workbasketSummaries, pageMetadata);
    return new ResponseEntity<>(pagedResources, HttpStatus.OK);
}
Also used : PageMetadata(org.springframework.hateoas.PagedResources.PageMetadata) ResponseEntity(org.springframework.http.ResponseEntity) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) WorkbasketSummaryResourcesAssembler(pro.taskana.rest.resource.mapper.WorkbasketSummaryResourcesAssembler) WorkbasketQuery(pro.taskana.WorkbasketQuery) WorkbasketSummaryResource(pro.taskana.rest.resource.WorkbasketSummaryResource) WorkbasketSummary(pro.taskana.WorkbasketSummary) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException in project taskana by Taskana.

the class WorkbasketDefinitionController method importWorkbaskets.

/**
 * This method imports a <b>list of {@link WorkbasketDefinition}</b>. This does not exactly match the REST norm, but
 * we want to have an option to import all settings at once. When a logical equal (key and domain are equal)
 * workbasket already exists an update will be executed. Otherwise a new workbasket will be created.
 *
 * @param definitions the list of workbasket definitions which will be imported to the current system.
 * @return Return answer is determined by the status code: 200 - all good 400 - list state error (referring to non
 * existing id's) 401 - not authorized
 */
@PostMapping(path = "/import")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<String> importWorkbaskets(@RequestBody List<WorkbasketDefinition> definitions) {
    try {
        // key: logical ID
        // value: system ID (in database)
        Map<String, String> systemIds = workbasketService.createWorkbasketQuery().list().stream().collect(Collectors.toMap(this::logicalId, WorkbasketSummary::getId));
        // key: old system ID
        // value: system ID
        Map<String, String> idConversion = new HashMap<>();
        // STEP 1: update or create workbaskets from the import
        for (WorkbasketDefinition definition : definitions) {
            WorkbasketResource res = definition.workbasketResource;
            Workbasket workbasket;
            String oldId = res.workbasketId;
            if (systemIds.containsKey(logicalId(res))) {
                res.workbasketId = systemIds.get(logicalId(res));
                workbasket = workbasketService.updateWorkbasket(workbasketMapper.toModel(res));
            } else {
                res.workbasketId = null;
                workbasket = workbasketService.createWorkbasket(workbasketMapper.toModel(res));
            }
            res.workbasketId = oldId;
            // simply delete all existing accessItems and create new ones.
            for (WorkbasketAccessItem accessItem : workbasketService.getWorkbasketAccessItems(workbasket.getId())) {
                workbasketService.deleteWorkbasketAccessItem(accessItem.getId());
            }
            for (WorkbasketAccessItemResource authorization : definition.authorizations) {
                workbasketService.createWorkbasketAccessItem(workbasketAccessItemMapper.toModel(authorization));
            }
            idConversion.put(definition.workbasketResource.workbasketId, workbasket.getId());
        }
        // This can not be done in step 1 because the system IDs are only known after step 1
        for (WorkbasketDefinition definition : definitions) {
            List<String> distributionTargets = new ArrayList<>();
            for (String oldId : definition.distributionTargets) {
                if (idConversion.containsKey(oldId)) {
                    distributionTargets.add(idConversion.get(oldId));
                } else {
                    throw new InvalidWorkbasketException(String.format("invalid import state: Workbasket '%s' does not exist in the given import list", oldId));
                }
            }
            workbasketService.setDistributionTargets(// no verification necessary since the workbasket was already imported in step 1.
            idConversion.get(definition.workbasketResource.workbasketId), distributionTargets);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    } catch (WorkbasketNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (InvalidWorkbasketException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } catch (NotAuthorizedException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    } catch (InvalidArgumentException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
    } catch (WorkbasketAlreadyExistException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    } catch (DomainNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
}
Also used : WorkbasketDefinition(pro.taskana.rest.resource.WorkbasketDefinition) WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource) HashMap(java.util.HashMap) WorkbasketAlreadyExistException(pro.taskana.exceptions.WorkbasketAlreadyExistException) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) ArrayList(java.util.ArrayList) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) DomainNotFoundException(pro.taskana.exceptions.DomainNotFoundException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) ResponseEntity(org.springframework.http.ResponseEntity) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource) Workbasket(pro.taskana.Workbasket) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException in project taskana by Taskana.

the class ClassificationServiceImplIntExplicitTest method testInsertClassification.

@Test
public void testInsertClassification() throws SQLException, ClassificationNotFoundException, ClassificationAlreadyExistException, NotAuthorizedException, DomainNotFoundException, InvalidArgumentException {
    Connection connection = dataSource.getConnection();
    taskanaEngineImpl.setConnection(connection);
    final String domain = "DOMAIN_A";
    final String key = "dummy-key";
    ClassificationImpl expectedClassification;
    Classification actualClassification;
    Classification actualClassification2;
    // empty classification (root)
    expectedClassification = (ClassificationImpl) this.createNewClassificationWithUniqueKey("", "TASK");
    expectedClassification = (ClassificationImpl) classificationService.createClassification(expectedClassification);
    connection.commit();
    actualClassification = classificationService.getClassification(expectedClassification.getKey(), expectedClassification.getDomain());
    assertThat(actualClassification, not(equalTo(null)));
    assertThat(actualClassification.getCreated(), not(equalTo(null)));
    assertThat(actualClassification.getId(), not(equalTo(null)));
    assertThat(actualClassification.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    // specific to domain + root
    expectedClassification = (ClassificationImpl) this.createNewClassificationWithUniqueKey(domain, "TASK");
    expectedClassification.setKey(key);
    expectedClassification = (ClassificationImpl) classificationService.createClassification(expectedClassification);
    connection.commit();
    actualClassification = classificationService.getClassification(expectedClassification.getKey(), expectedClassification.getDomain());
    actualClassification2 = classificationService.getClassification(expectedClassification.getKey(), "");
    assertThat(actualClassification, not(equalTo(null)));
    assertThat(actualClassification.getCreated(), not(equalTo(null)));
    assertThat(actualClassification.getId(), not(equalTo(null)));
    assertThat(actualClassification.getKey(), equalTo(key));
    assertThat(actualClassification.getDomain(), equalTo(domain));
    assertThat(actualClassification.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    assertThat(actualClassification2, not(equalTo(null)));
    assertThat(actualClassification2.getCreated(), not(equalTo(null)));
    assertThat(actualClassification2.getId(), not(equalTo(null)));
    assertThat(actualClassification2.getId(), not(equalTo(actualClassification.getId())));
    assertThat(actualClassification2.getKey(), equalTo(key));
    assertThat(actualClassification2.getDomain(), equalTo(""));
    assertThat(actualClassification2.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    // does exist already
    try {
        expectedClassification = (ClassificationImpl) this.createNewClassificationWithUniqueKey(domain, "TASK");
        expectedClassification.setKey(key);
        classificationService.createClassification(expectedClassification);
        connection.commit();
        fail("Should have thrown 'ClassificationAlreadyExistException' here.");
    } catch (ClassificationAlreadyExistException e) {
    }
    // new classification but root existing
    expectedClassification = (ClassificationImpl) this.createNewClassificationWithUniqueKey("", "TASK");
    expectedClassification.setKey(key);
    expectedClassification.setDomain("DOMAIN_B");
    classificationService.createClassification(expectedClassification);
    connection.commit();
    actualClassification = classificationService.getClassification(key, "DOMAIN_B");
    assertThat(actualClassification, not(equalTo(null)));
    assertThat(actualClassification.getCreated(), not(equalTo(null)));
    assertThat(actualClassification.getId(), not(equalTo(null)));
    assertThat(actualClassification.getKey(), equalTo(key));
    assertThat(actualClassification.getDomain(), equalTo("DOMAIN_B"));
    assertThat(actualClassification.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    Classification rootResult = classificationService.getClassification(key, "");
    assertThat(rootResult, not(equalTo(null)));
    // invalid serviceLevel
    try {
        expectedClassification = (ClassificationImpl) this.createNewClassificationWithUniqueKey("", "TASK");
        expectedClassification.setDomain(domain);
        expectedClassification.setKey("");
        expectedClassification.setServiceLevel("ASAP");
        classificationService.createClassification(expectedClassification);
        connection.commit();
        fail("Should have thrown IllegalArgumentException, because ServiceLevel is invalid.");
    } catch (InvalidArgumentException e) {
    }
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) Classification(pro.taskana.Classification) ClassificationAlreadyExistException(pro.taskana.exceptions.ClassificationAlreadyExistException) Connection(java.sql.Connection) ClassificationImpl(pro.taskana.impl.ClassificationImpl) TaskanaEngineConfigurationTest(pro.taskana.impl.configuration.TaskanaEngineConfigurationTest) Test(org.junit.Test)

Example 15 with InvalidArgumentException

use of pro.taskana.exceptions.InvalidArgumentException in project taskana by Taskana.

the class ClassificationServiceImplIntAutoCommitTest method testInsertClassifications.

@Test
public void testInsertClassifications() throws Exception {
    final String domain = "DOMAIN_A";
    final String key = "dummy-key";
    ClassificationImpl expectedClassification;
    Classification actualClassification;
    Classification actualClassification2;
    // empty classification (root)
    expectedClassification = (ClassificationImpl) this.createDummyClassificationWithUniqueKey("", "TASK");
    expectedClassification = (ClassificationImpl) classificationService.createClassification(expectedClassification);
    actualClassification = classificationService.getClassification(expectedClassification.getKey(), expectedClassification.getDomain());
    assertThat(actualClassification, not(equalTo(null)));
    assertThat(actualClassification.getCreated(), not(equalTo(null)));
    assertThat(actualClassification.getId(), not(equalTo(null)));
    assertThat(actualClassification.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    // specific to domain + root
    expectedClassification = (ClassificationImpl) this.createDummyClassificationWithUniqueKey(domain, "TASK");
    expectedClassification.setKey(key);
    expectedClassification = (ClassificationImpl) classificationService.createClassification(expectedClassification);
    actualClassification = classificationService.getClassification(expectedClassification.getKey(), expectedClassification.getDomain());
    actualClassification2 = classificationService.getClassification(expectedClassification.getKey(), "");
    assertThat(actualClassification, not(equalTo(null)));
    assertThat(actualClassification.getCreated(), not(equalTo(null)));
    assertThat(actualClassification.getId(), not(equalTo(null)));
    assertThat(actualClassification.getKey(), equalTo(key));
    assertThat(actualClassification.getDomain(), equalTo(domain));
    assertThat(actualClassification.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    assertThat(actualClassification2, not(equalTo(null)));
    assertThat(actualClassification2.getCreated(), not(equalTo(null)));
    assertThat(actualClassification2.getId(), not(equalTo(null)));
    assertThat(actualClassification2.getId(), not(equalTo(actualClassification.getId())));
    assertThat(actualClassification2.getKey(), equalTo(key));
    assertThat(actualClassification2.getDomain(), equalTo(""));
    assertThat(actualClassification2.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    // does exist already
    try {
        expectedClassification = (ClassificationImpl) this.createDummyClassificationWithUniqueKey(domain, "TASK");
        expectedClassification.setKey(key);
        classificationService.createClassification(expectedClassification);
        fail("Should have thrown 'ClassificationAlreadyExistException' here.");
    } catch (ClassificationAlreadyExistException e) {
    }
    // new classification but root existing
    expectedClassification = (ClassificationImpl) this.createDummyClassificationWithUniqueKey("DOMAIN_B", "TASK");
    expectedClassification.setKey(key);
    classificationService.createClassification(expectedClassification);
    actualClassification = classificationService.getClassification(key, "DOMAIN_B");
    assertThat(actualClassification, not(equalTo(null)));
    assertThat(actualClassification.getCreated(), not(equalTo(null)));
    assertThat(actualClassification.getId(), not(equalTo(null)));
    assertThat(actualClassification.getKey(), equalTo(key));
    assertThat(actualClassification.getDomain(), equalTo("DOMAIN_B"));
    assertThat(actualClassification.getId(), startsWith(ID_PREFIX_CLASSIFICATION));
    // verify that
    Classification rootResults = classificationService.getClassification(key, "");
    assertThat(rootResults, not(equalTo(null)));
    // invalid serviceLevel
    try {
        expectedClassification = (ClassificationImpl) this.createDummyClassificationWithUniqueKey(domain, "TASK");
        expectedClassification.setKey("");
        expectedClassification.setServiceLevel("ASAP");
        classificationService.createClassification(expectedClassification);
        fail("Should have thrown IllegalArgumentException, because ServiceLevel is invalid.");
    } catch (InvalidArgumentException e) {
    }
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) Classification(pro.taskana.Classification) ClassificationAlreadyExistException(pro.taskana.exceptions.ClassificationAlreadyExistException) ClassificationImpl(pro.taskana.impl.ClassificationImpl) TaskanaEngineConfigurationTest(pro.taskana.impl.configuration.TaskanaEngineConfigurationTest) Test(org.junit.Test)

Aggregations

InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)41 Test (org.junit.Test)24 AbstractAccTest (acceptance.AbstractAccTest)21 WithAccessId (pro.taskana.security.WithAccessId)19 TaskService (pro.taskana.TaskService)18 TaskSummary (pro.taskana.TaskSummary)16 TaskanaEngineProxyForTest (pro.taskana.impl.TaskanaEngineProxyForTest)12 NotAuthorizedException (pro.taskana.exceptions.NotAuthorizedException)11 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Task (pro.taskana.Task)7 Workbasket (pro.taskana.Workbasket)7 Classification (pro.taskana.Classification)6 ClassificationNotFoundException (pro.taskana.exceptions.ClassificationNotFoundException)6 SQLException (java.sql.SQLException)5 ClassificationSummary (pro.taskana.ClassificationSummary)5 WorkbasketAccessItem (pro.taskana.WorkbasketAccessItem)5 WorkbasketService (pro.taskana.WorkbasketService)5 Instant (java.time.Instant)4 Collectors (java.util.stream.Collectors)4