package info.bukova.isspst.services.requirement;

import info.bukova.isspst.Constants;
import info.bukova.isspst.data.Requirement;
import info.bukova.isspst.data.RequirementState;
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;

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

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()
	{
		hqlFilter.setDataClass(Requirement.class);
		hqlFilter.andFilter("kind = :kind");
		hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES);
		return super.getMy();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_SHOW_CENTRE_REQ')")
	@PostFilter("hasPermission(filterObject, 'PERM_SHOW_CENTRE_REQ')")
	public List<Requirement> getCentreReq()
	{
		hqlFilter.setDataClass(Requirement.class);
		hqlFilter.andFilter("kind = :kind");
		hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES);
		return super.getCentreReq();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_SHOW_WORKGROUP_REQ')")
	@PostFilter("hasPermission(filterObject, 'PERM_SHOW_WORKGROUP_REQ')")
	public List<Requirement> getWorkgroupReq()
	{
		hqlFilter.setDataClass(Requirement.class);
		hqlFilter.andFilter("kind = :kind");
		hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES);
		return super.getWorkgroupReq();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	@PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_REQ')")
	public List<Requirement> getAll()
	{
		hqlFilter.setDataClass(Requirement.class);
		hqlFilter.andFilter("kind = :kind");
		hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES);
		return super.getAll();
	}
}
