package com.example.beer.dao;

import javax.ejb.Singleton;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.example.beer.model.Comment;
import com.example.beer.model.Project;
import com.example.beer.model.Task;
import com.example.beer.model.Task.TaskStatus;
import com.example.beer.model.User;

@Singleton
public class TaskDAO {
	@PersistenceContext
	private EntityManager em;
	
	@Inject
	private ProjectDAO projectDAO;

	@Inject
	private UserDAO userDAO;
	
	public boolean addTask(String name, String description, User user) {
		Task foundTask = findTaskByName(name);
		
		if (foundTask == null) {
			em.persist(foundTask);
			return true;
		}
		
		return false;
	}
	
	public Task findTaskByName(String name) {
		String idQueryText = "SELECT t FROM Task t WHERE t.name = :name";
		TypedQuery<Task> taskQuery = em.createQuery(idQueryText, Task.class);
		taskQuery.setParameter("name", name);
		
		Task task = null;
		try {
            task = taskQuery.getSingleResult();
		} catch (Exception e) {
			return null;
		}
		
		return task;
	}

	public Task getTaskById(int id) {
		String idQueryText = "SELECT t FROM Task t WHERE t.id = :id";
		TypedQuery<Task> taskQuery = em.createQuery(idQueryText, Task.class);
		taskQuery.setParameter("id", id);

		Task task = null;
		try {
            task = taskQuery.getSingleResult();
		} catch (Exception e) {
			return null;
		}
		
		return task;
	}
	
	public boolean addToProject(Project project, Task task) {
		if (projectDAO.findProjectById(project.getId()) != null &&
			getTaskById(task.getId()) != null) {
			
			project.addTask(task);
			task.setProject(project);
			em.persist(project);
			em.persist(task);
			
			return true;
		} else {
			return false;
		}
	}

	public boolean assignUser(Task task, User user) {
		if (findTaskByName(task.getName()) == null) {
			return false;
		}
		
		if (userDAO.getUserByName(user.getName()) == null) {
			return false;
		}
		
		task.assignUser(user);
		user.getTasks().add(task);
		em.persist(task);
		em.persist(user);
		return true;
	}

	public boolean changeStatus(Task task, TaskStatus status) {
		if (findTaskByName(task.getName()) == null) {
			return false;
		}
		
		task.setTaskStatus(status);
		em.persist(task);
		return true;
	}

	public boolean addComment(Task task, Comment comment) {
		if (findTaskByName(task.getName()) == null) {
			return false;
		}
		
		task.addComment(comment);
		em.persist(task);
		return true;
	}
}

@@ -24,16 +24,40 @@
 r.dao;%0A%0A
+import java.util.Date;%0A%0A
 import j
@@ -76,16 +76,16 @@
 gleton;%0A
-
 import j
@@ -507,18 +507,17 @@
 ger em;%0A
-%09
 %0A
+
 %09@Inject
@@ -585,17 +585,16 @@
 serDAO;%0A
-%09
 %0A%09public
@@ -642,16 +642,72 @@
 ription,
+ Date finalDate,%0A%09%09%09int estimatedHours, Project project,
  User us
@@ -753,19 +753,17 @@
 (name);%0A
-%09%09
 %0A
+
 %09%09if (fo
@@ -795,25 +795,90 @@
 persist(
-foundTask
+new Task(name, description, finalDate, estimatedHours,%0A%09%09%09%09%09project, user)
 );%0A%09%09%09re
@@ -888,27 +888,25 @@
 n true;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09return fal
@@ -908,26 +908,25 @@
 n false;%0A%09%7D%0A
-%09
 %0A
+
 %09public Task
@@ -1136,19 +1136,17 @@
  name);%0A
-%09%09
 %0A
+
 %09%09Task t
@@ -1161,36 +1161,27 @@
 ll;%0A%09%09try %7B%0A
-            
+%09%09%09
 task = taskQ
@@ -1242,35 +1242,33 @@
 eturn null;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09return task;%0A%09
@@ -1510,20 +1510,11 @@
 y %7B%0A
-            
+%09%09%09
 task
@@ -1591,19 +1591,17 @@
 ll;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09return
@@ -1610,18 +1610,17 @@
 ask;%0A%09%7D%0A
-%09
 %0A
+
 %09public 
@@ -1731,15 +1731,16 @@
 null
- &&
 %0A%09%09%09
+%09&& 
 getT
@@ -1772,19 +1772,16 @@
 null) %7B%0A
-%09%09%09
 %0A%09%09%09proj
@@ -1877,12 +1877,9 @@
 k);%0A
-%09%09%09
 %0A
+
 %09%09%09r
@@ -2046,19 +2046,17 @@
 se;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09if (us
@@ -2115,35 +2115,33 @@
 turn false;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09task.assignUse
@@ -2363,27 +2363,25 @@
  false;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09task.setTa
@@ -2563,18 +2563,16 @@
 se;%0A%09%09%7D%0A
-%09%09
 %0A%09%09task.

Add all needed arguments to TaskDAO.addTask