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

import it.mice.voila.runtime.entity.Entity;
import it.mice.voila.runtime.springmvc.form.JfFormBean;

import java.util.Collection;
import java.util.List;

public class ManageController<F extends JfFormBean> extends DefaultController<F> {

	protected int getSearchResultFirstResult(JfFormBean form, String displayTagTableName) {
		int pageSize = getSearchResultPageSize(form, displayTagTableName);
		return getFirstResult(pageSize, displayTagTableName);
	}

	protected int getSearchResultPageSize(JfFormBean form, String displayTagTableName) {
		if (isExportListRequested()) {
			return 0;
		}
		return form.getSearchResultPageSize();
	}

	protected String viewOrEdit(F form) {
		if (form.getPageStatus().isReadOnly()) {
			return "view";
		} else {
			return "edit";
		}
	}

	@SuppressWarnings("unchecked")
	protected void setEntityToChecked(Entity entity, Collection<? extends Entity> entityList) {
		List<Entity> existingEntityAsList = (List<Entity>)entityList;
		Entity existingEntity = (Entity)existingEntityAsList.get(existingEntityAsList.indexOf(entity));
		existingEntity.setChecked(true);
	}
}
