package com.example.beer.dao;

import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.example.beer.model.Project;

@Singleton
public class ProjectDAO {
	
	@PersistenceContext
	private EntityManager em;
	
	public void addProject(Project project) {
		Project foundProject = findProjectByName(project.getName());
		
		if (foundProject != null) {
			em.persist(project);
		} else {
			// TODO: Maybe update project title or users, or tasks?
		}
	}

	public Project findProjectByName(String name) {
		TypedQuery<Project> query = em
				.createNamedQuery("findProjectByName", Project.class)
				.setParameter("name", name);
		
		try {
			return query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
}

@@ -865,10 +865,363 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09%0A%09public Project findProjectById(int projectId) %7B%0A%09%09String idQueryText = %22SELECT p FROM Project p WHERE p.id= :projectId%22;%0A%09%09TypedQuery%3CProject%3E idQuery = em.createQuery(idQueryText, Project.class);%0A%09%09idQuery.setParameter(%22projectId%22, projectId);%0A%09%09%0A%09%09try %7B%0A%09%09%09return idQuery.getSingleResult();%0A%09%09%7D catch (NoResultException e) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Add searching for a project by id functionality