package #{action_p};

import static com.jeecms.common.page.SimplePage.cpn;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.core.web.WebErrors;
import #{entity_p}.#{Entity};
import #{manager_p}.#{Entity}Mng;

@Controller
public class #{Entity}Act {
	private static final Logger log = LoggerFactory.getLogger(#{Entity}Act.class);

	@RequestMapping("/#{config_entity}/v_list.do")
	public String list(Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(cpn(pageNo), CookieUtils
				.getPageSize(request));
		model.addAttribute("pagination",pagination);
		return "#{config_entity}/list";
	}

	@RequestMapping("/#{config_entity}/v_add.do")
	public String add(ModelMap model) {
		return "#{config_entity}/add";
	}

	@RequestMapping("/#{config_entity}/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		model.addAttribute("#{entity}", manager.findById(id));
		return "#{config_entity}/edit";
	}

	@RequestMapping("/#{config_entity}/o_save.do")
	public String save(#{Entity} bean, HttpServletRequest request, ModelMap model) {
		bean = manager.save(bean);
		log.info("save #{Entity} id={}", bean.getId());
		return "redirect:v_list.do";
	}

	@RequestMapping("/#{config_entity}/o_update.do")
	public String update(#{Entity} bean, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.update(bean);
		log.info("update #{Entity} id={}.", bean.getId());
		return list(pageNo, request, model);
	}

	@RequestMapping("/#{config_entity}/o_delete.do")
	public String delete(Integer[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		#{Entity}[] beans = manager.deleteByIds(ids);
		for (#{Entity} bean : beans) {
			log.info("delete #{Entity} id={}", bean.getId());
		}
		return list(pageNo, request, model);
	}
	
	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id,errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id,WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		#{Entity} entity = manager.findById(id);
		if(errors.ifNotExist(entity, #{Entity}.class, id)) {
			return true;
		}
		return false;
	}
	
	@Autowired
	private #{Entity}Mng manager;
}