Search in sources :

Example 1 with WorkbasketResource

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

the class WorkbasketController method updateWorkbasket.

@PutMapping(path = "/{workbasketId}")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<WorkbasketResource> updateWorkbasket(@PathVariable(value = "workbasketId") String workbasketId, @RequestBody WorkbasketResource workbasketResource) throws InvalidWorkbasketException, WorkbasketNotFoundException, NotAuthorizedException {
    ResponseEntity<WorkbasketResource> result;
    if (workbasketId.equals(workbasketResource.workbasketId)) {
        Workbasket workbasket = workbasketMapper.toModel(workbasketResource);
        workbasket = workbasketService.updateWorkbasket(workbasket);
        result = ResponseEntity.ok(workbasketMapper.toResource(workbasket));
    } else {
        throw new InvalidWorkbasketException("Target-WB-ID('" + workbasketId + "') is not identical with the WB-ID of to object which should be updated. ID=('" + workbasketResource.getId() + "')");
    }
    return result;
}
Also used : InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource) Workbasket(pro.taskana.Workbasket) PutMapping(org.springframework.web.bind.annotation.PutMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with WorkbasketResource

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

the class WorkbasketController method getWorkbasket.

@GetMapping(path = "/{workbasketId}")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<WorkbasketResource> getWorkbasket(@PathVariable(value = "workbasketId") String workbasketId) throws WorkbasketNotFoundException, NotAuthorizedException {
    ResponseEntity<WorkbasketResource> result;
    Workbasket workbasket = workbasketService.getWorkbasket(workbasketId);
    result = new ResponseEntity<>(workbasketMapper.toResource(workbasket), HttpStatus.OK);
    return result;
}
Also used : WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource) Workbasket(pro.taskana.Workbasket) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with WorkbasketResource

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

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

the class WorkbasketMapper method toResource.

public WorkbasketResource toResource(Workbasket wb) throws NotAuthorizedException, WorkbasketNotFoundException {
    WorkbasketResource resource = new WorkbasketResource();
    BeanUtils.copyProperties(wb, resource);
    // need to be set by hand, since name or type is different
    resource.setWorkbasketId(wb.getId());
    resource.setModified(wb.getModified().toString());
    resource.setCreated(wb.getCreated().toString());
    return addLinks(resource, wb);
}
Also used : WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource)

Example 5 with WorkbasketResource

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

the class WorkbasketMapperTest method workbasketToResource.

@Test
public void workbasketToResource() throws NotAuthorizedException, WorkbasketNotFoundException {
    // given
    Workbasket workbasket = workbasketService.newWorkbasket("1", "DOMAIN_A");
    ((WorkbasketImpl) workbasket).setId("ID");
    workbasket.setType(WorkbasketType.PERSONAL);
    workbasket.setName("Testbasket");
    workbasket.setOrgLevel1("Org1");
    workbasket.setOrgLevel2("Org2");
    workbasket.setOrgLevel3("Org3");
    workbasket.setOrgLevel4("Org4");
    workbasket.setDescription("A test workbasket");
    workbasket.setCustom1("1");
    workbasket.setCustom2("2");
    workbasket.setCustom3("3");
    workbasket.setCustom4("4");
    workbasket.setOwner("Lars");
    ((WorkbasketImpl) workbasket).setCreated(Instant.parse("2010-01-01T12:00:00Z"));
    ((WorkbasketImpl) workbasket).setModified(Instant.parse("2010-01-01T12:00:00Z"));
    // when
    WorkbasketResource workbasketResource = workbasketMapper.toResource(workbasket);
    // then
    testEquality(workbasket, workbasketResource);
}
Also used : WorkbasketImpl(pro.taskana.impl.WorkbasketImpl) Workbasket(pro.taskana.Workbasket) WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource) Test(org.junit.Test)

Aggregations

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