package com.mason.server.dao.jdo;

import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.mason.server.dao.jdo.PMF;
import com.mason.server.dao.generics.IProjectAccessDAO;
import com.mason.server.model.ProjectAccess;

public class ProjectAccessDAO implements IProjectAccessDAO {

	@Override
	public ProjectAccess add(ProjectAccess object) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			object = pm.makePersistent(object);
			object = pm.detachCopy(object);
		} finally {
			pm.close();
		}
		return object;
	}

	@Override
	public ProjectAccess get(long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class, "id == :id");
		q.setUnique(true);
		ProjectAccess result = null;
		try {
			result = (ProjectAccess) q.execute(id);
			result = pm.detachCopy(result);
		} finally {
			pm.close();
		}
		return result;
	}

	@Override
	public ProjectAccess getAccessUserToProject(long projectId, long userId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class,
				"userId == :userId && projectId == :projectId");
		q.setUnique(true);
		ProjectAccess result = null;
		try {
			result = (ProjectAccess) q.execute(userId, projectId);
			result = pm.detachCopy(result);
		} finally {
			pm.close();
		}
		return result;
	}

	@Override
	public ProjectAccess update(ProjectAccess object) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			object = pm.makePersistent(object);
		} finally {
			pm.close();
		}
		return object;
	}

	@Override
	public boolean delete(ProjectAccess object) {
		return deleteById(object.getId());
	}

	@Override
	public boolean deleteById(long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.deletePersistent(get(id));
		} catch (Exception e) {
			System.out.println(e);
			return false;
		} finally {
			pm.close();
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectAccess> getAll() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class);
		q.setOrdering("projectId asc");
		Collection<ProjectAccess> results = null;
		try {
			results = pm.detachCopyAll((Collection<ProjectAccess>) q.execute());
		} finally {
			q.closeAll();
			pm.close();
		}
		return (List<ProjectAccess>) results;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ProjectAccess> getAllByProjectId(long projectId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class, "projectId == :projectId");
		Collection<ProjectAccess> results = null;
		try {
			results = pm.detachCopyAll((Collection<ProjectAccess>) q
					.execute(projectId));
		} finally {
			q.closeAll();
			pm.close();
		}
		return (List<ProjectAccess>) results;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ProjectAccess> getAllByUserId(long userId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class, "userId == :userId");
		Collection<ProjectAccess> results = null;
		try {
			results = pm.detachCopyAll((Collection<ProjectAccess>) q
					.execute(userId));
		} finally {
			q.closeAll();
			pm.close();
		}
		return (List<ProjectAccess>) results;
	}

	@Override
	public boolean deleteAllByProjectId(long projectId) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class);
		q.setFilter("projectId == :projectId");
		boolean result = false;
		try {
			q.deletePersistentAll(projectId);
			result = true;
		} finally {
			q.closeAll();
			pm.close();
		}
		return result;
	}

	@Override
	public boolean deleteAllByUserId(long userId) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(ProjectAccess.class);
		q.setFilter("userId == :userId");
		boolean result = false;
		try {
			q.deletePersistentAll(userId);
			result = true;
		} finally {
			q.closeAll();
			pm.close();
		}
		return result;
	}

}
