Search in sources :

Example 1 with WorkbasketAccessItemResource

use of pro.taskana.rest.resource.WorkbasketAccessItemResource 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 2 with WorkbasketAccessItemResource

use of pro.taskana.rest.resource.WorkbasketAccessItemResource in project taskana by Taskana.

the class WorkbasketAccessItemMapper method toResource.

public WorkbasketAccessItemResource toResource(WorkbasketAccessItem wbAccItem) throws NotAuthorizedException, WorkbasketNotFoundException {
    WorkbasketAccessItemResource resource = new WorkbasketAccessItemResource();
    BeanUtils.copyProperties(wbAccItem, resource);
    // property is named different, so it needs to be set by hand
    resource.setAccessItemId(wbAccItem.getId());
    return addLinks(resource, wbAccItem);
}
Also used : WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource)

Example 3 with WorkbasketAccessItemResource

use of pro.taskana.rest.resource.WorkbasketAccessItemResource in project taskana by Taskana.

the class WorkbasketAccessItemMapperTest method workBasketAccessItemToModelPropertiesEqual.

@Test
public void workBasketAccessItemToModelPropertiesEqual() {
    // given
    WorkbasketAccessItemResource resource = new WorkbasketAccessItemResource();
    resource.setAccessId("10");
    resource.setAccessItemId("120");
    resource.setWorkbasketId("1");
    resource.setPermRead(true);
    resource.setPermAppend(false);
    resource.setPermDistribute(false);
    resource.setPermOpen(false);
    resource.setPermTransfer(true);
    resource.setPermCustom1(false);
    resource.setPermCustom2(false);
    resource.setPermCustom3(false);
    resource.setPermCustom4(false);
    resource.setPermCustom5(true);
    resource.setPermCustom6(false);
    resource.setPermCustom7(false);
    resource.setPermCustom8(false);
    resource.setPermCustom9(false);
    resource.setPermCustom10(false);
    resource.setPermCustom11(true);
    resource.setPermCustom12(false);
    // when
    WorkbasketAccessItem accessItem = workbasketAccessItemMapper.toModel(resource);
    // then
    testEquality(accessItem, resource);
}
Also used : WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) Test(org.junit.Test)

Example 4 with WorkbasketAccessItemResource

use of pro.taskana.rest.resource.WorkbasketAccessItemResource in project taskana by Taskana.

the class WorkbasketAccessItemMapperTest method workBasketAccessItemToResourcePropertiesEqual.

@Test
public void workBasketAccessItemToResourcePropertiesEqual() throws NotAuthorizedException, WorkbasketNotFoundException {
    // given
    WorkbasketAccessItem accessItem = workbasketService.newWorkbasketAccessItem("1", "2");
    accessItem.setPermDistribute(false);
    accessItem.setPermOpen(true);
    accessItem.setPermAppend(false);
    accessItem.setPermRead(false);
    accessItem.setPermTransfer(true);
    accessItem.setPermCustom1(false);
    accessItem.setPermCustom2(false);
    accessItem.setPermCustom3(true);
    accessItem.setPermCustom4(true);
    accessItem.setPermCustom5(true);
    accessItem.setPermCustom6(true);
    accessItem.setPermCustom7(true);
    accessItem.setPermCustom8(true);
    accessItem.setPermCustom9(true);
    accessItem.setPermCustom10(true);
    accessItem.setPermCustom11(true);
    accessItem.setPermCustom12(true);
    // when
    WorkbasketAccessItemResource resource = workbasketAccessItemMapper.toResource(accessItem);
    // then
    testEquality(accessItem, resource);
}
Also used : WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) Test(org.junit.Test)

Example 5 with WorkbasketAccessItemResource

use of pro.taskana.rest.resource.WorkbasketAccessItemResource in project taskana by Taskana.

the class WorkbasketAccessItemListMapper method toResource.

public Resources<WorkbasketAccessItemResource> toResource(String workbasketId, Collection<WorkbasketAccessItem> accessItems) throws NotAuthorizedException, WorkbasketNotFoundException {
    List<WorkbasketAccessItemResource> resourceList = new ArrayList<>();
    for (WorkbasketAccessItem accessItem : accessItems) {
        resourceList.add(workbasketAccessItemMapper.toResource(accessItem));
    }
    Resources<WorkbasketAccessItemResource> accessItemListResource = new Resources<>(resourceList);
    accessItemListResource.add(linkTo(methodOn(WorkbasketController.class).getWorkbasketAccessItems(workbasketId)).withSelfRel());
    accessItemListResource.add(linkTo(methodOn(WorkbasketController.class).getWorkbasket(workbasketId)).withRel("workbasket"));
    return accessItemListResource;
}
Also used : WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) ArrayList(java.util.ArrayList) Resources(org.springframework.hateoas.Resources)

Aggregations

WorkbasketAccessItemResource (pro.taskana.rest.resource.WorkbasketAccessItemResource)6 WorkbasketAccessItem (pro.taskana.WorkbasketAccessItem)5 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)2 WorkbasketDefinition (pro.taskana.rest.resource.WorkbasketDefinition)2 HashMap (java.util.HashMap)1 Resources (org.springframework.hateoas.Resources)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 Workbasket (pro.taskana.Workbasket)1 DomainNotFoundException (pro.taskana.exceptions.DomainNotFoundException)1 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)1 InvalidWorkbasketException (pro.taskana.exceptions.InvalidWorkbasketException)1 NotAuthorizedException (pro.taskana.exceptions.NotAuthorizedException)1 WorkbasketAlreadyExistException (pro.taskana.exceptions.WorkbasketAlreadyExistException)1 WorkbasketNotFoundException (pro.taskana.exceptions.WorkbasketNotFoundException)1 WorkbasketResource (pro.taskana.rest.resource.WorkbasketResource)1