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);
}
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;
}
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);
}
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));
}
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));
}
Aggregations