Search in sources :

Example 1 with ConcurrencyException

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

the class ClassificationDefinitionController method importClassifications.

@PostMapping(path = "/import")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<String> importClassifications(@RequestBody List<ClassificationResource> classificationResources) throws InvalidArgumentException {
    Map<String, String> systemIds = classificationService.createClassificationQuery().list().stream().collect(Collectors.toMap(i -> i.getKey() + "|" + i.getDomain(), ClassificationSummary::getId));
    try {
        for (ClassificationResource classificationResource : classificationResources) {
            if (systemIds.containsKey(classificationResource.key + "|" + classificationResource.domain)) {
                classificationService.updateClassification(classificationMapper.toModel(classificationResource));
            } else {
                classificationResource.classificationId = null;
                classificationService.createClassification(classificationMapper.toModel(classificationResource));
            }
        }
    } catch (NotAuthorizedException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    } catch (ClassificationNotFoundException | DomainNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (ClassificationAlreadyExistException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    // TODO why is this occuring???
    } catch (ConcurrencyException e) {
    }
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DomainNotFoundException(pro.taskana.exceptions.DomainNotFoundException) ClassificationResource(pro.taskana.rest.resource.ClassificationResource) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) ClassificationAlreadyExistException(pro.taskana.exceptions.ClassificationAlreadyExistException) ClassificationService(pro.taskana.ClassificationService) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) ClassificationSummary(pro.taskana.ClassificationSummary) PostMapping(org.springframework.web.bind.annotation.PostMapping) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) TransactionInterceptor(org.springframework.transaction.interceptor.TransactionInterceptor) MediaType(org.springframework.http.MediaType) Classification(pro.taskana.Classification) ClassificationMapper(pro.taskana.rest.resource.mapper.ClassificationMapper) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) ResponseEntity(org.springframework.http.ResponseEntity) ClassificationQuery(pro.taskana.ClassificationQuery) Transactional(org.springframework.transaction.annotation.Transactional) ResponseEntity(org.springframework.http.ResponseEntity) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) ClassificationResource(pro.taskana.rest.resource.ClassificationResource) ClassificationAlreadyExistException(pro.taskana.exceptions.ClassificationAlreadyExistException) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) DomainNotFoundException(pro.taskana.exceptions.DomainNotFoundException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ConcurrencyException

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

the class ClassificationServiceImpl method updateClassification.

@Override
public Classification updateClassification(Classification classification) throws NotAuthorizedException, ConcurrencyException, ClassificationNotFoundException, InvalidArgumentException {
    LOGGER.debug("entry to updateClassification(Classification = {})", classification);
    taskanaEngine.checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN);
    ClassificationImpl classificationImpl = null;
    try {
        taskanaEngine.openConnection();
        classificationImpl = (ClassificationImpl) classification;
        // Check if current object is based on the newest (by modified)
        Classification oldClassification = this.getClassification(classificationImpl.getKey(), classificationImpl.getDomain());
        if (!oldClassification.getModified().equals(classificationImpl.getModified())) {
            throw new ConcurrencyException("The current Classification has been modified while editing. The values can not be updated. Classification=" + classificationImpl.toString());
        }
        classificationImpl.setModified(Instant.now());
        this.initDefaultClassificationValues(classificationImpl);
        // Update classification fields used by tasks
        if (oldClassification.getCategory() != classificationImpl.getCategory()) {
            List<TaskSummary> taskSumamries = taskanaEngine.getTaskService().createTaskQuery().classificationIdIn(oldClassification.getId()).list();
            boolean categoryChanged = !(oldClassification.getCategory() == null ? classification.getCategory() == null : oldClassification.getCategory().equals(classification.getCategory()));
            if (!taskSumamries.isEmpty() && categoryChanged) {
                List<String> taskIds = new ArrayList<>();
                taskSumamries.stream().forEach(ts -> taskIds.add(ts.getTaskId()));
                taskMapper.updateClassificationCategoryOnChange(taskIds, classificationImpl.getCategory());
            }
        }
        // Check if parentId changed and object does exist
        if (!oldClassification.getParentId().equals(classificationImpl.getParentId())) {
            if (classificationImpl.getParentId() != null && !classificationImpl.getParentId().isEmpty()) {
                this.getClassification(classificationImpl.getParentId());
            }
        }
        classificationMapper.update(classificationImpl);
        boolean priorityChanged = oldClassification.getPriority() != classification.getPriority();
        boolean serviceLevelChanged = oldClassification.getServiceLevel() != classification.getServiceLevel();
        if (priorityChanged || serviceLevelChanged) {
            Map<String, String> args = new HashMap<>();
            args.put(TaskUpdateOnClassificationChangeExecutor.CLASSIFICATION_ID, classificationImpl.getId());
            args.put(TaskUpdateOnClassificationChangeExecutor.PRIORITY_CHANGED, String.valueOf(priorityChanged));
            args.put(TaskUpdateOnClassificationChangeExecutor.SERVICE_LEVEL_CHANGED, String.valueOf(serviceLevelChanged));
            Job job = new Job();
            job.setCreated(Instant.now());
            job.setState(Job.State.READY);
            job.setExecutor(TaskUpdateOnClassificationChangeExecutor.class.getName());
            job.setArguments(args);
            taskanaEngine.getSqlSession().getMapper(JobMapper.class).insertJob(job);
        }
        LOGGER.debug("Method updateClassification() updated the classification {}.", classificationImpl);
        return classification;
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from updateClassification().");
    }
}
Also used : JobMapper(pro.taskana.mappings.JobMapper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) Classification(pro.taskana.Classification) TaskSummary(pro.taskana.TaskSummary)

Example 3 with ConcurrencyException

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

the class UpdateTaskAccTest method testThrowsExceptionIfTaskHasAlreadyBeenUpdated.

@WithAccessId(userName = "user_1_1", groupNames = { "group_1" })
@Test
public void testThrowsExceptionIfTaskHasAlreadyBeenUpdated() throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException, ConcurrencyException, AttachmentPersistenceException {
    TaskService taskService = taskanaEngine.getTaskService();
    Task task = taskService.getTask("TKI:000000000000000000000000000000000000");
    Task task2 = taskService.getTask("TKI:000000000000000000000000000000000000");
    task.setCustomAttribute("1", "willi");
    Task updatedTask = null;
    updatedTask = taskService.updateTask(task);
    updatedTask = taskService.getTask(updatedTask.getId());
    task2.setCustomAttribute("2", "Walter");
    try {
        updatedTask = taskService.updateTask(task2);
    } catch (ConcurrencyException ex) {
        assertEquals("The task has already been updated by another user", ex.getMessage());
    }
}
Also used : Task(pro.taskana.Task) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) TaskService(pro.taskana.TaskService) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Aggregations

ConcurrencyException (pro.taskana.exceptions.ConcurrencyException)3 ArrayList (java.util.ArrayList)2 Classification (pro.taskana.Classification)2 AbstractAccTest (acceptance.AbstractAccTest)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 ResponseEntity (org.springframework.http.ResponseEntity)1 Transactional (org.springframework.transaction.annotation.Transactional)1 TransactionInterceptor (org.springframework.transaction.interceptor.TransactionInterceptor)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 RequestBody (org.springframework.web.bind.annotation.RequestBody)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1