// Path: sample-app/src/main/java/virtua/training/polymer/mvc/todo/model/Task.java
// public class Task {
// 
//     private String userId;
// 
//     private String id;
// 
//     private String name;
// 
//     private boolean completed;
// 
// 
//     public Task() {
//     }
// 
//     public Task(String id, String userId, String name, boolean completed) {
//         this.id = id;
//         this.userId = userId;
//         this.name = name;
//         this.completed = completed;
//     }
// 
//     public String getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(String userId) {
//         this.userId = userId;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public boolean isCompleted() {
//         return completed;
//     }
// 
//     public void setCompleted(boolean completed) {
//         this.completed = completed;
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// }


import virtua.training.polymer.mvc.todo.model.Task;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedDeque;
    public void init() {
        add(new Task(null, userService.getSystemUser().getUserId(), "Read Java EE MVC 1.0 Spec", true));
        add(new Task(null, userService.getSystemUser().getUserId(), "Read Polymer docs", false));
    }

    public Task[] getTasks() {
        return queue.toArray(new Task[]{});
    }

    public void add(Task task) {
        if (task.getId() == null) {
            task.setId(UUID.randomUUID().toString());
        }
        queue.offerFirst(task);
    }

    public void update(Task task) {
        Optional<Task> result = get(task.getId());
        if (result.isPresent()) {
            Task currentTask = result.get();
            currentTask.setName(task.getName());
            currentTask.setCompleted(task.isCompleted());
            currentTask.setUserId(task.getUserId());
        } else {
            throw new NoSuchElementException("There is no task with the id " + task.getId());
        }
    }

    public Optional<Task> get(String id) {
        // Inefficient, but this is just a sample app....
// Path: sample-app/src/main/java/virtua/training/polymer/mvc/todo/model/Task.java
// public class Task {
// 
//     private String userId;
// 
//     private String id;
// 
//     private String name;
// 
//     private boolean completed;
// 
// 
//     public Task() {
//     }
// 
//     public Task(String id, String userId, String name, boolean completed) {
//         this.id = id;
//         this.userId = userId;
//         this.name = name;
//         this.completed = completed;
//     }
// 
//     public String getUserId() {
//         return userId;
//     }
// 
//     public void setUserId(String userId) {
//         this.userId = userId;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public boolean isCompleted() {
//         return completed;
//     }
// 
//     public void setCompleted(boolean completed) {
//         this.completed = completed;
//     }
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// }


// Path: sample-app/src/main/java/virtua/training/polymer/mvc/todo/service/TaskService.java
import virtua.training.polymer.mvc.todo.model.Task;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedDeque;

    public void init() {
        add(new Task(null, userService.getSystemUser().getUserId(), "Read Java EE MVC 1.0 Spec", true));
        add(new Task(null, userService.getSystemUser().getUserId(), "Read Polymer docs", false));
    }

    public Task[] getTasks() {
        return queue.toArray(new Task[]{});
    }

    public void add(Task task) {
        if (task.getId() == null) {
            task.setId(UUID.randomUUID().toString());
        }
        queue.offerFirst(task);
    }

    public void update(Task task) {
        Optional<Task> result = get(task.getId());
        if (result.isPresent()) {
            Task currentTask = result.get();
            currentTask.setName(task.getName());
            currentTask.setCompleted(task.isCompleted());
            currentTask.setUserId(task.getUserId());
        } else {
            throw new NoSuchElementException("There is no task with the id " + task.getId());
        }
    }

    public Optional<Task> get(String id) {
        // Inefficient, but this is just a sample app....
        return queue.stream().filter((task) -> task.getId().equals(id)).findFirst();