package info.bukova.isspst.services.requirement;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import info.bukova.isspst.data.RequirementType;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.services.AbstractOwnedService;

public class RequirementTypeServiceImpl  extends AbstractOwnedService<RequirementType> implements RequirementTypeService {

	@Override
	@Transactional
	public RequirementType getTypeById(String id) {
		return selectSingle("from RequirementType where type = '" + id + "'");
	}

	@Override
	public List<Workgroup> filterCentres(RequirementType reqType,
			List<Workgroup> source) {
		
		if (reqType.getLimitCentres() == null || !reqType.getLimitCentres()) {
			return source;
		}
		
		List<Workgroup> filtered = new ArrayList<Workgroup>();
		
		for (Workgroup wg : source) {
			if (reqType.getOfferedCentres().contains(wg)) {
				filtered.add(wg);
			}
		}
		
		return filtered;
	}

}
