Search in sources :

Example 1 with ClassificationInUseException

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

the class DeleteClassificationAccTest method testThrowExceptionWhenChildClassificationIsInUseAndRollback.

@WithAccessId(userName = "teamlead_1", groupNames = { "group_1", "businessadmin" })
@Test
public void testThrowExceptionWhenChildClassificationIsInUseAndRollback() throws ClassificationInUseException, NotAuthorizedException, ClassificationNotFoundException {
    boolean classificationInUse = false;
    try {
        classificationService.deleteClassification("L11010", "DOMAIN_A");
    } catch (ClassificationInUseException e) {
        classificationInUse = true;
    }
    Classification rollback = classificationService.getClassification("L11010", "DOMAIN_A");
    assertTrue(classificationInUse);
    assertEquals("DOMAIN_A", rollback.getDomain());
    classificationInUse = false;
    try {
        classificationService.deleteClassification("L11010", "");
    } catch (ClassificationInUseException e) {
        classificationInUse = true;
    }
    Classification rollbackMaster = classificationService.getClassification("L11010", "");
    Classification rollbackA = classificationService.getClassification("L11010", "DOMAIN_A");
    assertTrue(classificationInUse);
    assertEquals(rollbackMaster.getKey(), rollbackA.getKey());
    assertNotEquals(rollbackMaster.getDomain(), rollbackA.getDomain());
}
Also used : ClassificationInUseException(pro.taskana.exceptions.ClassificationInUseException) Classification(pro.taskana.Classification) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Example 2 with ClassificationInUseException

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

the class ClassificationServiceImpl method deleteClassification.

@Override
public void deleteClassification(String classificationKey, String domain) throws ClassificationInUseException, ClassificationNotFoundException, NotAuthorizedException {
    LOGGER.debug("entry to deleteClassification(key = {}, domain = {})", classificationKey, domain);
    taskanaEngine.checkRoleMembership(TaskanaRole.BUSINESS_ADMIN, TaskanaRole.ADMIN);
    try {
        taskanaEngine.openConnection();
        Classification classification = this.classificationMapper.findByKeyAndDomain(classificationKey, domain);
        if (classification == null) {
            throw new ClassificationNotFoundException(classificationKey, domain, "The classification " + classificationKey + "wasn't found in the domain " + domain);
        }
        List<AttachmentSummaryImpl> attachments = taskanaEngine.getSqlSession().getMapper(AttachmentMapper.class).findAttachmentSummariesByClassificationId(classification.getId());
        if (!attachments.isEmpty()) {
            throw new ClassificationInUseException("Classification " + classification.getId() + " is used by Attachment " + attachments.get(0).getId());
        }
        if (domain.equals("")) {
            // master mode - delete all associated classifications in every domain.
            List<String> domains = this.classificationMapper.getDomainsForClassification(classificationKey);
            domains.remove("");
            for (String classificationDomain : domains) {
                deleteClassification(classificationKey, classificationDomain);
            }
        }
        TaskServiceImpl taskService = (TaskServiceImpl) taskanaEngine.getTaskService();
        try {
            List<TaskSummary> classificationTasks = taskService.createTaskQuery().classificationKeyIn(classificationKey).list();
            if (classificationTasks.stream().anyMatch(t -> domain.equals(t.getClassificationSummary().getDomain()))) {
                throw new ClassificationInUseException("There are Tasks that belong to this classification or a child classification. Please complete them and try again.");
            }
        } catch (NotAuthorizedToQueryWorkbasketException e) {
            LOGGER.error("ClassificationQuery unexpectedly returned NotauthorizedException. Throwing SystemException ");
            throw new SystemException("ClassificationQuery unexpectedly returned NotauthorizedException.");
        }
        List<String> childKeys = this.classificationMapper.getChildrenOfClassification(classification.getId());
        for (String key : childKeys) {
            this.deleteClassification(key, domain);
        }
        this.classificationMapper.deleteClassificationInDomain(classificationKey, domain);
    } finally {
        taskanaEngine.returnConnection();
        LOGGER.debug("exit from deleteClassification()");
    }
}
Also used : ClassificationInUseException(pro.taskana.exceptions.ClassificationInUseException) SystemException(pro.taskana.exceptions.SystemException) Classification(pro.taskana.Classification) TaskSummary(pro.taskana.TaskSummary) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) NotAuthorizedToQueryWorkbasketException(pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException) AttachmentMapper(pro.taskana.mappings.AttachmentMapper)

Aggregations

Classification (pro.taskana.Classification)2 ClassificationInUseException (pro.taskana.exceptions.ClassificationInUseException)2 AbstractAccTest (acceptance.AbstractAccTest)1 Test (org.junit.Test)1 TaskSummary (pro.taskana.TaskSummary)1 ClassificationNotFoundException (pro.taskana.exceptions.ClassificationNotFoundException)1 NotAuthorizedToQueryWorkbasketException (pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException)1 SystemException (pro.taskana.exceptions.SystemException)1 AttachmentMapper (pro.taskana.mappings.AttachmentMapper)1 WithAccessId (pro.taskana.security.WithAccessId)1