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.IUmlElementContainerDAO;
import com.mason.server.model.UmlElementContainer;

public class UmlElementContainerDAO implements IUmlElementContainerDAO {

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

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

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

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

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

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

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

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

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

}
