package info.bukova.isspst.services.requirement;

import info.bukova.isspst.Constants;
import info.bukova.isspst.data.Requirement;
import info.bukova.isspst.data.RequirementState;
import info.bukova.isspst.data.Workgroup;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;

public class ReqServicesServiceImpl extends RequirementServiceImpl implements RequirementService, RequirementBaseService<Requirement>
{
	@Autowired
	private RequirementTypeService reqTypeService;

	@Override
	protected Requirement createEntity()
	{
		Requirement entity = new Requirement();

		entity.setReqDate(new Date());
		entity.setType(reqTypeService.getTypeById(Constants.REQTYPE_ORDER));
		entity.setState(RequirementState.NEW);
		entity.setKind(Constants.REQ_TYPE_SERVICES);

		return entity;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_READ')")
	public List<Requirement> getMy()
	{
		Query q = dao.getQuery("from " + dao.getEntityName() + " where ownedBy = :owner and kind = :kind");
		q.setParameter("owner", getLoggedInUser());
		q.setParameter("kind", Constants.REQ_TYPE_SERVICES);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_SHOW_CENTRE_REQ')")
	@PostFilter("hasPermission(filterObject, 'PERM_SHOW_CENTRE_REQ')")
	public List<Requirement> getCentreReq()
	{
		List<Workgroup> wgList = workgroupService.getUserCentres(getLoggedInUser());
		Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.centre c where c in (:wgList) and kind = :kind order by tr.numser");
		q.setParameterList("wgList", wgList);
		q.setParameter("kind", Constants.REQ_TYPE_SERVICES);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_SHOW_WORKGROUP_REQ')")
	@PostFilter("hasPermission(filterObject, 'PERM_SHOW_WORKGROUP_REQ')")
	public List<Requirement> getWorkgroupReq()
	{
		List<Workgroup> wgList = workgroupService.getUserWorkgroups(getLoggedInUser());
		Query q = dao.getQuery("select tr from "
				+ dao.getEntityName()
				+ " tr join fetch tr.ownedBy join tr.workgroup w where w in (:wgList) and kind = :kind order by tr.numser");
		q.setParameterList("wgList", wgList);
		q.setParameter("kind", Constants.REQ_TYPE_SERVICES);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_REQ')")
	public List<Requirement> getAll()
	{
		Query q = dao.getQuery("from " + dao.getEntityName() + " as tr join fetch tr.ownedBy where kind = :kind order by tr.numser");
		q.setParameter("kind", Constants.REQ_TYPE_SERVICES);
		return q.list();
	}
}
