Viewing a task

The view task screen will open the task in view mode, meaning it will show the details of the task. It also allows you to add a comment and shows the list of comments added to the selected task. The controller method for viewing the task will populate the task and comment data, and redirects the user to view the task screen. The method looks as follows:

@GetMapping("/viewTask")
fun viewTask(@RequestParam(name = "taskId",required = true)
taskId:Int,model:Model):String{
val selectedTask:Task? = taskRepository?.findById(taskId)?.get()
val user:User? = userRepository?.
findById(selectedTask?.getAssignedTo() ?: 0)?.get()

val taskDto= TaskDTO(selectedTask?.getId(),selectedTask?.getTitle(),
selectedTask?.getDetail(),selectedTask?.getAssignedTo(),
(user?.getFirstname() + " "+ user?.getLastname()),
selectedTask?.getStatus(),selectedTask?.getComments())
val commentLst: List<Array<Any>>? = commentRepository?.findByTaskId(taskId)
val commentDtoLst:MutableList<CommentDTO> = ArrayList()
var commentDto:CommentDTO?
for(row in commentLst.orEmpty()){
commentDto = CommentDTO(row[0] as Int,row[1] as String,row[2] as String)
commentDtoLst.add(commentDto)
}
model.addAttribute("task",taskDto)
model.addAttribute("taskComments",commentDtoLst)
model.addAttribute("screenTitle","Add Task Comment")
return "task-view"
}

The taskId parameter will be sent from a task list screen. First, we fetch the task detail from a given taskId. We also fetch the user data from the user ID associated with the Task object. For comments, we do the same thing: fetch comments with a given taskId from commentRepository

We fetch comments from commentRepository , but we could have fetched it from the Task object, since they have one-to-many relationships. The reason we do not fetch them that way is, we want to show the username to whom the task is assigned. If we fetch comments from Task, it will return the collection of the object of the Comments type, which has a user ID but not first and last name. So we might have to make an additional round of database calls to fetch the user first and last name for each comments record. This may result in a poor performance. 

As an alternative, we use a JPA query language mechanism, by associating the join SQL query with the repository method as follows:

interface CommentRepository: JpaRepository<Comments, Int> {

@Query("SELECT c.id, c.comment, concat(u.firstname,' ',u.lastname)
FROM comments c inner join users u on c.user_id=u.id inner join task t
on t. id = c.task_id and t.id =:taskId"
,nativeQuery = true)
fun findByTaskId(taskId: Int):List<Array<Any>>
}

It is returned as a List<Array<Any>> type because the data is fetched from multiple tables (task and comments). We are iterating it and populating the list of CommentDTO , which is defined as a data class as follows:

data class CommentDTO(var id:Int,var comment:String,var userName:String)

The  TaskDTO object is used to show task details while the list of CommentDTO is used to show comments in tabular format in the view task screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.125.171