Skip to content

Commit

Permalink
TSK-137: Get all attachments of a Task.
Browse files Browse the repository at this point in the history
  • Loading branch information
MLengl authored and holgerhagen committed Jan 10, 2018
1 parent 98658ac commit ad8aabb
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void init(@Observes @Initialized(ApplicationScoped.class) Object init)
taskanaEjb.getTaskService().claim(task.getId());
System.out.println(
"---------------------------> Task claimed: "
+ taskanaEjb.getTaskService().getTaskById(task.getId()).getOwner());
+ taskanaEjb.getTaskService().getTask(task.getId()).getOwner());
taskanaEjb.getTaskService().completeTask(task.getId());
System.out.println("---------------------------> Task completed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Task createTask(Task taskToCreate)
* @throws TaskNotFoundException
* thrown of the {@link Task} with taskId is not found
*/
Task getTaskById(String taskId) throws TaskNotFoundException;
Task getTask(String taskId) throws TaskNotFoundException;

/**
* Transfer a task to another work basket. The transfer sets the transferred flag and resets the read flag.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pro.taskana.impl;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.Map;

import pro.taskana.Attachment;
Expand All @@ -20,12 +21,16 @@ public class AttachmentImpl implements Attachment {
private Timestamp modified;
private Classification classification;
private ObjectReference objectReference;
private String porCompany;
private String porSystem;
private String porSystemInstance;
private String porType;
private String porValue;
private String channel;
private Timestamp received;
private Map<String, Object> customAttributes;
private Map<String, Object> customAttributes = Collections.emptyMap();;

AttachmentImpl() {

}

@Override
Expand Down Expand Up @@ -114,6 +119,46 @@ public void setCustomAttributes(Map<String, Object> customAttributes) {
this.customAttributes = customAttributes;
}

public String getPorCompany() {
return porCompany;
}

public void setPorCompany(String porCompany) {
this.porCompany = porCompany;
}

public String getPorSystem() {
return porSystem;
}

public void setPorSystem(String porSystem) {
this.porSystem = porSystem;
}

public String getPorSystemInstance() {
return porSystemInstance;
}

public void setPorSystemInstance(String porSystemInstance) {
this.porSystemInstance = porSystemInstance;
}

public String getPorType() {
return porType;
}

public void setPorType(String porType) {
this.porType = porType;
}

public String getPorValue() {
return porValue;
}

public void setPorValue(String porValue) {
this.porValue = porValue;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand All @@ -127,6 +172,16 @@ public String toString() {
builder.append(modified);
builder.append(", classification=");
builder.append(classification);
builder.append(", porCompany=");
builder.append(porCompany);
builder.append(", porSystem=");
builder.append(porSystem);
builder.append(", porSystemInstance=");
builder.append(porSystemInstance);
builder.append(", porType=");
builder.append(porType);
builder.append(", porValue=");
builder.append(porValue);
builder.append(", objectReference=");
builder.append(objectReference);
builder.append(", channel=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Task claim(String taskId, boolean forceClaim)
TaskImpl task = null;
try {
taskanaEngineImpl.openConnection();
task = (TaskImpl) getTaskById(taskId);
task = (TaskImpl) getTask(taskId);
TaskState state = task.getState();
if (state == TaskState.COMPLETED) {
LOGGER.warn("Method claim() found that task {} is already completed. Throwing InvalidStateException",
Expand Down Expand Up @@ -123,7 +123,7 @@ public Task completeTask(String taskId, boolean isForced)
TaskImpl task = null;
try {
taskanaEngineImpl.openConnection();
task = (TaskImpl) this.getTaskById(taskId);
task = (TaskImpl) this.getTask(taskId);
// check pre-conditions for non-forced invocation
if (!isForced) {
if (task.getClaimed() == null || task.getState() != TaskState.CLAIMED) {
Expand Down Expand Up @@ -191,14 +191,17 @@ public Task createTask(Task taskToCreate)
}

@Override
public Task getTaskById(String id) throws TaskNotFoundException {
public Task getTask(String id) throws TaskNotFoundException {
LOGGER.debug("entry to getTaskById(id = {})", id);
Task result = null;
try {
taskanaEngineImpl.openConnection();
result = taskMapper.findById(id);
if (result != null) {
setPrimaryObjRef((TaskImpl) result);
List<Attachment> attachments = setAttachmentObjRef(
attachmentMapper.findAttachmentsByTaskId(result.getId()));
((TaskImpl) result).setAttachments(attachments);
return result;
} else {
LOGGER.warn("Method getTaskById() didn't find task with id {}. Throwing TaskNotFoundException", id);
Expand All @@ -217,7 +220,7 @@ public Task transfer(String taskId, String destinationWorkbasketKey)
Task result = null;
try {
taskanaEngineImpl.openConnection();
TaskImpl task = (TaskImpl) getTaskById(taskId);
TaskImpl task = (TaskImpl) getTask(taskId);

// transfer requires TRANSFER in source and APPEND on destination workbasket
workbasketService.checkAuthorization(destinationWorkbasketKey, WorkbasketAuthorization.APPEND);
Expand All @@ -238,7 +241,7 @@ public Task transfer(String taskId, String destinationWorkbasketKey)
task.setModified(Timestamp.valueOf(LocalDateTime.now()));
taskMapper.update(task);

result = getTaskById(taskId);
result = getTask(taskId);
LOGGER.debug("Method transfer() transferred Task '{}' to destination workbasket {}", taskId,
destinationWorkbasketKey);
return result;
Expand All @@ -254,11 +257,11 @@ public Task setTaskRead(String taskId, boolean isRead) throws TaskNotFoundExcept
Task result = null;
try {
taskanaEngineImpl.openConnection();
TaskImpl task = (TaskImpl) getTaskById(taskId);
TaskImpl task = (TaskImpl) getTask(taskId);
task.setRead(true);
task.setModified(Timestamp.valueOf(LocalDateTime.now()));
taskMapper.update(task);
result = getTaskById(taskId);
result = getTask(taskId);
LOGGER.debug("Method setTaskRead() set read property of Task '{}' to {} ", result, isRead);
return result;
} finally {
Expand Down Expand Up @@ -307,7 +310,7 @@ public Task updateTask(Task task)
TaskImpl oldTaskImpl = null;
try {
taskanaEngineImpl.openConnection();
oldTaskImpl = (TaskImpl) getTaskById(newTaskImpl.getId());
oldTaskImpl = (TaskImpl) getTask(newTaskImpl.getId());
standardUpdateActions(oldTaskImpl, newTaskImpl);

taskMapper.update(newTaskImpl);
Expand Down Expand Up @@ -431,6 +434,23 @@ static void setPrimaryObjRef(TaskImpl task) {
task.setPrimaryObjRef(objRef);
}

private List<Attachment> setAttachmentObjRef(List<AttachmentImpl> attachments) {
List<Attachment> results = new ArrayList<>();
if (attachments != null && !attachments.isEmpty()) {
for (AttachmentImpl attachment : attachments) {
ObjectReference objRef = new ObjectReference();
objRef.setCompany(attachment.getPorCompany());
objRef.setSystem(attachment.getPorSystem());
objRef.setSystemInstance(attachment.getPorSystemInstance());
objRef.setType(attachment.getPorType());
objRef.setValue(attachment.getPorValue());
attachment.setObjectReference(objRef);
results.add(attachment);
}
}
return results;
}

private void validateObjectReference(ObjectReference objRef, String objRefType, String objName)
throws InvalidArgumentException {
// check that all values in the ObjectReference are set correctly
Expand Down Expand Up @@ -465,7 +485,6 @@ private void validateAttachments(TaskImpl task) throws InvalidArgumentException
throw new InvalidArgumentException("Classification of attachment " + attachment + " must not be null");
}
}

}

private void standardUpdateActions(TaskImpl oldTaskImpl, TaskImpl newTaskImpl)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
package pro.taskana.model.mappings;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;

import pro.taskana.Classification;
import pro.taskana.impl.AttachmentImpl;
import pro.taskana.impl.persistence.MapTypeHandler;

/**
* This class is the mybatis mapping of Attachment.
*/
public interface AttachmentMapper {

@Insert("INSERT INTO ATTACHMENT (ID, TASK_ID, CREATED, MODIFIED, CLASSIFICATION_KEY, REF_COMPANY, REF_SYSTEM, REF_INSTANCE, REF_TYPE, REF_VALUE, CHANNEL, RECEIVED, CUSTOM_ATTRIBUTES) "
String CLASSIFICATION_FINDBYID = "pro.taskana.model.mappings.ClassificationMapper.findById";

@Insert("INSERT INTO ATTACHMENT (ID, TASK_ID, CREATED, MODIFIED, CLASSIFICATION_KEY, REF_COMPANY, REF_SYSTEM, REF_INSTANCE, REF_TYPE, REF_VALUE, CHANNEL, RECEIVED, CUSTOM_ATTRIBUTES) "
+ "VALUES (#{att.id}, #{att.taskId}, #{att.created}, #{att.modified}, #{att.classification.key}, #{att.objectReference.company}, #{att.objectReference.system}, #{att.objectReference.systemInstance}, "
+ " #{att.objectReference.type}, #{att.objectReference.value}, #{att.channel}, #{att.received}, #{att.customAttributes,jdbcType=BLOB,javaType=java.util.Map,typeHandler=pro.taskana.impl.persistence.MapTypeHandler} )")
void insert(@Param("att") AttachmentImpl att);

@Select("SELECT ID, TASK_ID, CREATED, MODIFIED, CLASSIFICATION_KEY, REF_COMPANY, REF_SYSTEM, REF_INSTANCE, REF_TYPE, REF_VALUE, CHANNEL, RECEIVED, CUSTOM_ATTRIBUTES "
+ "FROM ATTACHMENT "
+ "WHERE TASK_ID = #{taskId}")
@Results(value = {
@Result(property = "id", column = "ID"),
@Result(property = "taskId", column = "TASK_ID"),
@Result(property = "created", column = "CREATED"),
@Result(property = "modified", column = "MODIFIED"),
@Result(property = "classification", column = "CLASSIFICATION_KEY", javaType = Classification.class,
one = @One(select = CLASSIFICATION_FINDBYID)),
@Result(property = "porCompany", column = "REF_COMPANY"),
@Result(property = "porSystem", column = "REF_SYSTEM"),
@Result(property = "porSystemInstance", column = "REF_INSTANCE"),
@Result(property = "porType", column = "REF_TYPE"),
@Result(property = "porValue", column = "REF_VALUE"),
@Result(property = "channel", column = "CHANNEL"),
@Result(property = "received", column = "RECEIVED"),
@Result(property = "customAttributes", column = "CUSTOM_ATTRIBUTES", jdbcType = JdbcType.BLOB,
javaType = Map.class, typeHandler = MapTypeHandler.class),
})
List<AttachmentImpl> findAttachmentsByTaskId(@Param("taskId") String taskId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public interface QueryMapper {

String OBJECTREFERENCEMAPPER_FINDBYID = "pro.taskana.model.mappings.ObjectReferenceMapper.findById";
String CLASSIFICATION_FINDBYIDANDDOMAIN = "pro.taskana.model.mappings.ClassificationMapper.findByKeyAndDomain";
String CLASSIFICATION_FINDBYKEYANDDOMAIN = "pro.taskana.model.mappings.ClassificationMapper.findByKeyAndDomain";
String CLASSIFICATION_FINDBYID = "pro.taskana.model.mappings.ClassificationMapper.findById";

@Select("<script>SELECT t.ID, t.CREATED, t.CLAIMED, t.COMPLETED, t.MODIFIED, t.PLANNED, t.DUE, t.NAME, t.DESCRIPTION, t.PRIORITY, t.STATE, t.CLASSIFICATION_KEY, t.WORKBASKET_KEY, t.OWNER, t.POR_COMPANY, t.POR_SYSTEM, t.POR_INSTANCE, t.POR_TYPE, t.POR_VALUE, t.IS_READ, t.IS_TRANSFERRED, t.CUSTOM_1, t.CUSTOM_2, t.CUSTOM_3, t.CUSTOM_4, t.CUSTOM_5, t.CUSTOM_6, t.CUSTOM_7, t.CUSTOM_8, t.CUSTOM_9, t.CUSTOM_10 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public interface TaskMapper {
@Result(property = "custom7", column = "CUSTOM_7"),
@Result(property = "custom8", column = "CUSTOM_8"),
@Result(property = "custom9", column = "CUSTOM_9"),
@Result(property = "custom10", column = "CUSTOM_10")})

@Result(property = "custom10", column = "CUSTOM_10")
})
TaskImpl findById(@Param("id") String id);

@Results({@Result(column = "DUE_DATE", property = "due"),
@Results({ @Result(column = "DUE_DATE", property = "due"),
@Result(column = "WORKBASKET_KEY", property = "workbasketKey"),
@Result(column = "counter", property = "taskCounter")})
@Result(column = "counter", property = "taskCounter") })
List<DueWorkbasketCounter> getTaskCountByWorkbasketIdAndDaysInPastAndState(@Param("fromDate") Date fromDate,
@Param("status") List<TaskState> states);

Expand Down Expand Up @@ -131,8 +131,7 @@ List<DueWorkbasketCounter> getTaskCountByWorkbasketIdAndDaysInPastAndState(@Para
@Result(property = "custom7", column = "CUSTOM_7"),
@Result(property = "custom8", column = "CUSTOM_8"),
@Result(property = "custom9", column = "CUSTOM_9"),
@Result(property = "custom10", column = "CUSTOM_10")})

@Result(property = "custom10", column = "CUSTOM_10") })
List<TaskImpl> findTasksByWorkbasketIdAndState(@Param("workbasketKey") String workbasketKey,
@Param("taskState") TaskState taskState);

Expand All @@ -151,5 +150,4 @@ List<TaskImpl> findTasksByWorkbasketIdAndState(@Param("workbasketKey") String wo
@Result(property = "classificationName", column = "classificationName")
})
List<TaskSummary> findTaskSummariesByWorkbasketKey(@Param("workbasketKey") String workbasketKey);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pro.taskana.exceptions.InvalidWorkbasketException;
import pro.taskana.exceptions.NotAuthorizedException;
import pro.taskana.exceptions.TaskAlreadyExistException;
import pro.taskana.exceptions.TaskNotFoundException;
import pro.taskana.exceptions.WorkbasketNotFoundException;
import pro.taskana.model.TaskState;
import pro.taskana.security.JAASRunner;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void testCreateSimpleManualTask()
@Test
public void testCreateExternalTaskWithAttachment()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException {
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException {

TaskService taskService = taskanaEngine.getTaskService();
Task newTask = taskService.newTask();
Expand All @@ -87,15 +88,16 @@ public void testCreateExternalTaskWithAttachment()

assertNotNull(createdTask.getId());

// rename getTaskById to getTask
// Task readTask = taskService.getTask(createdTask.getId());
Task readTask = taskService.getTask(createdTask.getId());

// assertNotNull(readTask);
// assertNotNull(readTask.getAttachments());
// assertEquals(1, readTask.getAttachments().size());
// assertNotNull(readTask.getAttachments().get(0).getCreated());
// assertNotNull(readTask.getAttachments().get(0).getModified());
// assertEquals(readTask.getAttachments().get(0).getCreated(), readTask.getAttachments().get(0).getModified());
assertNotNull(readTask);
assertNotNull(readTask.getAttachments());
assertEquals(1, readTask.getAttachments().size());
assertNotNull(readTask.getAttachments().get(0).getCreated());
assertNotNull(readTask.getAttachments().get(0).getModified());
assertEquals(readTask.getAttachments().get(0).getCreated(), readTask.getAttachments().get(0).getModified());
// assertNotNull(readTask.getAttachments().get(0).getClassification());
assertNotNull(readTask.getAttachments().get(0).getObjectReference());
}

@WithAccessId(
Expand All @@ -104,7 +106,7 @@ public void testCreateExternalTaskWithAttachment()
@Test
public void testCreateExternalTaskWithMultipleAttachments()
throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException,
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException {
WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException {

TaskService taskService = taskanaEngine.getTaskService();
Task newTask = taskService.newTask();
Expand All @@ -123,15 +125,16 @@ public void testCreateExternalTaskWithMultipleAttachments()

assertNotNull(createdTask.getId());

// rename getTaskById to getTask
// Task readTask = taskService.getTask(createdTask.getId());
Task readTask = taskService.getTask(createdTask.getId());

// assertNotNull(readTask);
// assertNotNull(readTask.getAttachments());
// assertEquals(2, readTask.getAttachments().size());
// assertNotNull(readTask.getAttachments().get(1).getCreated());
// assertNotNull(readTask.getAttachments().get(1).getModified());
// assertEquals(readTask.getAttachments().get(0).getCreated(), readTask.getAttachments().get(1).getModified());
assertNotNull(readTask);
assertNotNull(readTask.getAttachments());
assertEquals(2, readTask.getAttachments().size());
assertNotNull(readTask.getAttachments().get(1).getCreated());
assertNotNull(readTask.getAttachments().get(1).getModified());
assertEquals(readTask.getAttachments().get(0).getCreated(), readTask.getAttachments().get(1).getModified());
// assertNotNull(readTask.getAttachments().get(0).getClassification());
assertNotNull(readTask.getAttachments().get(0).getObjectReference());
}

@WithAccessId(
Expand Down
Loading

0 comments on commit ad8aabb

Please sign in to comment.