Search in sources :

Example 16 with Attachment

use of pro.taskana.Attachment in project taskana by Taskana.

the class TaskServiceImpl method handleAttachments.

private PrioDurationHolder handleAttachments(TaskImpl task) throws InvalidArgumentException {
    List<Attachment> attachments = task.getAttachments();
    if (attachments == null || attachments.isEmpty()) {
        return new PrioDurationHolder(null, Integer.MIN_VALUE);
    }
    Duration minDuration = MAX_DURATION;
    int maxPrio = Integer.MIN_VALUE;
    Iterator<Attachment> i = attachments.iterator();
    while (i.hasNext()) {
        Attachment attachment = i.next();
        if (attachment == null) {
            i.remove();
        } else {
            ObjectReference objRef = attachment.getObjectReference();
            validateObjectReference(objRef, "ObjectReference", "Attachment");
            if (attachment.getClassificationSummary() == null) {
                throw new InvalidArgumentException("Classification of attachment " + attachment + " must not be null");
            } else {
                ClassificationSummary classificationSummary = attachment.getClassificationSummary();
                if (classificationSummary != null) {
                    PrioDurationHolder newPrioDuraton = getNewPrioDuration(maxPrio, minDuration, classificationSummary.getPriority(), classificationSummary.getServiceLevel());
                    maxPrio = newPrioDuraton.getPrio();
                    minDuration = newPrioDuraton.getDuration();
                }
            }
        }
    }
    if (minDuration != null && MAX_DURATION.equals(minDuration)) {
        minDuration = null;
    }
    return new PrioDurationHolder(minDuration, maxPrio);
}
Also used : InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) ClassificationSummary(pro.taskana.ClassificationSummary) Attachment(pro.taskana.Attachment) Duration(java.time.Duration)

Example 17 with Attachment

use of pro.taskana.Attachment in project taskana by Taskana.

the class TaskServiceImpl method augmentAttachmentsByClassification.

private List<Attachment> augmentAttachmentsByClassification(List<AttachmentImpl> attachmentImpls, BulkOperationResults<String, Exception> bulkLog) {
    List<Attachment> result = new ArrayList<>();
    if (attachmentImpls == null || attachmentImpls.isEmpty()) {
        return result;
    }
    Set<String> classificationIds = attachmentImpls.stream().map(t -> t.getClassificationSummary().getId()).collect(Collectors.toSet());
    List<ClassificationSummary> classifications = classificationService.createClassificationQuery().idIn(classificationIds.toArray(new String[0])).list();
    for (AttachmentImpl att : attachmentImpls) {
        ClassificationSummary classificationSummary = classifications.stream().filter(cl -> cl.getId().equals(att.getClassificationSummary().getId())).findFirst().orElse(null);
        if (classificationSummary == null) {
            String id = att.getClassificationSummary().getId();
            bulkLog.addError(att.getClassificationSummary().getId(), new ClassificationNotFoundException(id, "When processing task updates due to change of classification, the classification with id " + id + " was not found."));
        } else {
            att.setClassificationSummary(classificationSummary);
            result.add(att);
        }
    }
    return result;
}
Also used : Arrays(java.util.Arrays) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) IdGenerator(pro.taskana.impl.util.IdGenerator) LoggerFactory(org.slf4j.LoggerFactory) WorkbasketService(pro.taskana.WorkbasketService) ArrayList(java.util.ArrayList) CurrentUserContext(pro.taskana.security.CurrentUserContext) HashSet(java.util.HashSet) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) SystemException(pro.taskana.exceptions.SystemException) CustomPropertySelector(pro.taskana.mappings.CustomPropertySelector) Task(pro.taskana.Task) InvalidStateException(pro.taskana.exceptions.InvalidStateException) Duration(java.time.Duration) Map(java.util.Map) TaskState(pro.taskana.TaskState) WorkbasketPermission(pro.taskana.WorkbasketPermission) WorkbasketSummary(pro.taskana.WorkbasketSummary) TimeIntervalColumnHeader(pro.taskana.impl.report.impl.TimeIntervalColumnHeader) ClassificationSummary(pro.taskana.ClassificationSummary) TaskNotFoundException(pro.taskana.exceptions.TaskNotFoundException) TaskanaEngine(pro.taskana.TaskanaEngine) Attachment(pro.taskana.Attachment) TaskAlreadyExistException(pro.taskana.exceptions.TaskAlreadyExistException) TaskSummary(pro.taskana.TaskSummary) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) ConcurrencyException(pro.taskana.exceptions.ConcurrencyException) Logger(org.slf4j.Logger) InvalidOwnerException(pro.taskana.exceptions.InvalidOwnerException) Iterator(java.util.Iterator) Set(java.util.Set) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) AttachmentMapper(pro.taskana.mappings.AttachmentMapper) Classification(pro.taskana.Classification) LoggerUtils(pro.taskana.impl.util.LoggerUtils) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) AttachmentPersistenceException(pro.taskana.exceptions.AttachmentPersistenceException) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskService(pro.taskana.TaskService) TaskMapper(pro.taskana.mappings.TaskMapper) List(java.util.List) Workbasket(pro.taskana.Workbasket) TaskanaRole(pro.taskana.TaskanaRole) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) TaskanaException(pro.taskana.exceptions.TaskanaException) TaskQuery(pro.taskana.TaskQuery) Collections(java.util.Collections) ClassificationSummary(pro.taskana.ClassificationSummary) ArrayList(java.util.ArrayList) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) Attachment(pro.taskana.Attachment)

Example 18 with Attachment

use of pro.taskana.Attachment in project taskana by Taskana.

the class TaskServiceImpl method handleAttachmentsOnTaskUpdate.

private PrioDurationHolder handleAttachmentsOnTaskUpdate(TaskImpl oldTaskImpl, TaskImpl newTaskImpl) throws AttachmentPersistenceException {
    Duration minDuration = MAX_DURATION;
    int maxPrio = Integer.MIN_VALUE;
    // Iterator for removing invalid current values directly. OldAttachments can be ignored.
    Iterator<Attachment> i = newTaskImpl.getAttachments().iterator();
    while (i.hasNext()) {
        Attachment attachment = i.next();
        if (attachment != null) {
            boolean wasAlreadyPresent = false;
            if (attachment.getId() != null) {
                for (Attachment oldAttachment : oldTaskImpl.getAttachments()) {
                    if (oldAttachment != null && attachment.getId().equals(oldAttachment.getId())) {
                        wasAlreadyPresent = true;
                        if (!attachment.equals(oldAttachment)) {
                            AttachmentImpl temp = (AttachmentImpl) attachment;
                            ClassificationSummary classification = attachment.getClassificationSummary();
                            if (classification != null) {
                                PrioDurationHolder newPrioDuration = getNewPrioDuration(maxPrio, minDuration, classification.getPriority(), classification.getServiceLevel());
                                maxPrio = newPrioDuration.getPrio();
                                minDuration = newPrioDuration.getDuration();
                            }
                            temp.setModified(Instant.now());
                            attachmentMapper.update(temp);
                            LOGGER.debug("TaskService.updateTask() for TaskId={} UPDATED an Attachment={}.", newTaskImpl.getId(), attachment);
                            break;
                        }
                    }
                }
            }
            // ADD, when ID not set or not found in elements
            if (!wasAlreadyPresent) {
                AttachmentImpl attachmentImpl = (AttachmentImpl) attachment;
                initAttachment(attachmentImpl, newTaskImpl);
                ClassificationSummary classification = attachment.getClassificationSummary();
                if (classification != null) {
                    PrioDurationHolder newPrioDuration = getNewPrioDuration(maxPrio, minDuration, classification.getPriority(), classification.getServiceLevel());
                    maxPrio = newPrioDuration.getPrio();
                    minDuration = newPrioDuration.getDuration();
                }
                try {
                    attachmentMapper.insert(attachmentImpl);
                    LOGGER.debug("TaskService.updateTask() for TaskId={} INSERTED an Attachment={}.", newTaskImpl.getId(), attachmentImpl);
                } catch (PersistenceException e) {
                    LOGGER.error("TaskService.updateTask() for TaskId={} can NOT INSERT the current Attachment, because it was added fored multiple times and wasn´t persisted before. ID={}", newTaskImpl.getId(), attachmentImpl.getId());
                    throw new AttachmentPersistenceException(attachmentImpl.getId());
                }
            }
        } else {
            i.remove();
        }
    }
    // DELETE, when an Attachment was only represented before
    for (Attachment oldAttachment : oldTaskImpl.getAttachments()) {
        if (oldAttachment != null) {
            boolean isRepresented = false;
            for (Attachment newAttachment : newTaskImpl.getAttachments()) {
                if (newAttachment != null && oldAttachment.getId().equals(newAttachment.getId())) {
                    isRepresented = true;
                    break;
                }
            }
            if (!isRepresented) {
                attachmentMapper.deleteAttachment(oldAttachment.getId());
                LOGGER.debug("TaskService.updateTask() for TaskId={} DELETED an Attachment={}.", newTaskImpl.getId(), oldAttachment);
            }
        }
    }
    if (minDuration != null && MAX_DURATION.equals(minDuration)) {
        minDuration = null;
    }
    return new PrioDurationHolder(minDuration, maxPrio);
}
Also used : ClassificationSummary(pro.taskana.ClassificationSummary) PersistenceException(org.apache.ibatis.exceptions.PersistenceException) AttachmentPersistenceException(pro.taskana.exceptions.AttachmentPersistenceException) Duration(java.time.Duration) Attachment(pro.taskana.Attachment) AttachmentPersistenceException(pro.taskana.exceptions.AttachmentPersistenceException)

Example 19 with Attachment

use of pro.taskana.Attachment in project taskana by Taskana.

the class TaskAttachmentTest method testAddSameTwice.

@Test
public void testAddSameTwice() {
    // Same values, not same REF. Important.
    Attachment attachment1 = createAttachment("ID1", "taskId1");
    Attachment attachment2 = createAttachment("ID1", "taskId1");
    cut.addAttachment(attachment1);
    cut.addAttachment(attachment2);
    assertThat(cut.getAttachments().size(), equalTo(1));
    // Check with not same vlaues (same ID)
    String newChannel = "I will overwrite the other!";
    attachment1.setChannel(newChannel);
    cut.addAttachment(attachment1);
    assertThat(cut.getAttachments().size(), equalTo(1));
    assertThat(cut.getAttachments().get(0).getChannel(), equalTo(newChannel));
}
Also used : Attachment(pro.taskana.Attachment) Test(org.junit.Test)

Example 20 with Attachment

use of pro.taskana.Attachment in project taskana by Taskana.

the class TaskAttachmentTest method testAddAttachmentWithValidValue.

@Test
public void testAddAttachmentWithValidValue() {
    Attachment attachment1 = createAttachment("ID1", "taskId1");
    Attachment attachment2 = createAttachment("ID2", "taskId1");
    Attachment attachment3 = createAttachment("ID3", "taskId1");
    cut.addAttachment(attachment1);
    cut.addAttachment(attachment2);
    cut.addAttachment(attachment3);
    assertThat(cut.getAttachments().size(), equalTo(3));
}
Also used : Attachment(pro.taskana.Attachment) Test(org.junit.Test)

Aggregations

Attachment (pro.taskana.Attachment)28 Test (org.junit.Test)17 Classification (pro.taskana.Classification)9 AbstractAccTest (acceptance.AbstractAccTest)7 ClassificationSummary (pro.taskana.ClassificationSummary)7 WithAccessId (pro.taskana.security.WithAccessId)7 Task (pro.taskana.Task)6 Workbasket (pro.taskana.Workbasket)6 Duration (java.time.Duration)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 AttachmentPersistenceException (pro.taskana.exceptions.AttachmentPersistenceException)4 SystemException (pro.taskana.exceptions.SystemException)4 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 PersistenceException (org.apache.ibatis.exceptions.PersistenceException)3 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)3 AttachmentSummary (pro.taskana.AttachmentSummary)2 TaskService (pro.taskana.TaskService)2 TaskSummary (pro.taskana.TaskSummary)2 WorkbasketSummary (pro.taskana.WorkbasketSummary)2