package cn.itcast.oa.view.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role> {
private static final long serialVersionUID = 5242842204530086588L;

@Resource
private RoleService roleService;
private Role model = new Role();

public String list() throws Exception {
List<Role> roleList = roleService.findAll();
ActionContext.getContext().put("roleList", roleList);
return "list";
}

public String addUI() throws Exception {
return "addUI";
}

public String add() throws Exception {
roleService.add(model);
return "toList";
}

public String delete() throws Exception {
roleService.delete(model.getId());
return "toList";
}

public String editUI() throws Exception {
//回显数据
Role role = roleService.getById(model.getId());
ActionContext.getContext().getValueStack().push(role);
return "editUI";
}

public String edit() throws Exception {
//1.获得实体
Role role = roleService.getById(model.getId());
//2.设值
role.setName(model.getName());
role.setDescription(model.getDescription());
//3.更新
roleService.update(role);
return "toList";
}

@Override
public Role getModel() {
return model;
}

public void setRole(Role role) {
this.model = role;
}

}