package it.mice.voila.runtime.springmvc.controller;

import java.util.Collection;

import it.mice.voila.runtime.entity.Entity;
import it.mice.voila.runtime.service.EntityService;
import it.mice.voila.runtime.springmvc.util.SpringMvcUtil;

/**
 * Abstract list handler to get all results useful for populating combo boxes.
 * @author zzy9v4
 */
public abstract class AbstractListHandler {
	protected <T extends Entity> Collection<T> getList(EntityService<T> service) {
		return getList(service, false);
	}

	@SuppressWarnings("unchecked")
	protected <T extends Entity> Collection<T> getList(EntityService<T> service, boolean cacheable) {
		return getList(service, cacheable, false);
	}

	@SuppressWarnings("unchecked")
	protected <T extends Entity> Collection<T> getList(EntityService<T> service, boolean cacheable, boolean useAcl) {
		String attrId = service.getClass().getName() + ".findAll";
		Collection<T> cachedResult = (Collection<T>)SpringMvcUtil.getRequest().getAttribute(attrId);
		if (cachedResult == null) {
			cachedResult = service.listAll(cacheable, useAcl);
			SpringMvcUtil.getRequest().setAttribute(attrId, cachedResult);
		}
		return cachedResult;
	}
}
