/**
*文件功能:
*/
package com.${table.pack}.action;
import java.net.URLEncoder;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.common.Config;
import com.common.action.BaseAction;
import com.common.hibernate.IdGenerator;
import com.common.util.PaginationSupport;
import com.common.dao.DaoHelper;
import com.${table.pack}.model.${table.entityname?cap_first};
import com.${table.pack}.service.${table.entityname?cap_first}Service;
/**
* @作者 徐建协
* @日期 ${table.date}
*/
public class ${table.entityname?cap_first}Action extends BaseAction {
private Log log = LogFactory.getLog(${table.entityname?cap_first}Action.class);
private ${table.entityname?cap_first} ${table.entityname};
private List list;
private PaginationSupport page;
private ${table.entityname?cap_first}Service ${table.entityname}Service;
private DaoHelper daoHelper;
public void setDaoHelper(DaoHelper daoHelper) {
this.daoHelper = daoHelper;
}
public ${table.entityname?cap_first} get${table.entityname?cap_first}() {
return ${table.entityname};
}
public void set${table.entityname?cap_first}(${table.entityname?cap_first} ${table.entityname}) {
this.${table.entityname} = ${table.entityname};
}
public List getList() {
return list;
}
public PaginationSupport getPage() {
return page;
}
public void set${table.entityname?cap_first}Service(${table.entityname?cap_first}Service ${table.entityname}Service) {
this.${table.entityname}Service = ${table.entityname}Service;
}
public String add() {
this.setActionName(Config.ADD);
try {
if(${table.entityname}==null){
${table.entityname} = new ${table.entityname?cap_first}();
}
page = new PaginationSupport(getMap());
page.setContextPath(this.getContextPath());
this.setUrlParams(page.getParams());
return INPUT;
} catch (Exception ex) {
log.error("add "+ex.toString());
if(ex.getMessage()==null){
setErrMessage(ex.toString());
}else{
setErrMessage(ex.getMessage());
}
}
return LIST;
}
public String modify() {
this.setActionName(Config.MODIFY);
try {
if (${table.entityname} == null || ${table.entityname}.get<#list table.primaryList as c>${c.propertyName?cap_first}</#list>() == null) {
setErrMessage("修改对象的ID为空!");
return ERROR;
}
set${table.entityname?cap_first}(${table.entityname}Service.getEntity(get${table.entityname?cap_first}().get<#list table.primaryList as c>${c.propertyName?cap_first}</#list>()));
page = new PaginationSupport(getMap());
page.setContextPath(this.getContextPath());
this.setUrlParams(page.getParams());
return INPUT;
} catch (Exception ex) {
log.error("modify "+ex.toString());
if(ex.getMessage()==null){
setErrMessage(ex.toString());
}else{
setErrMessage(ex.getMessage());
}
return LIST;
}
}
public String remove() {
try {
${table.entityname}Service.removeEntity(${table.entityname});
setActionMessage(Config.DELETE_SUCCESS_MESSAGE);
page = new PaginationSupport(getMap());
page.setContextPath(this.getContextPath());
this.setUrlParams(page.getParams()+"&actionMessage="+URLEncoder.encode(Config.DELETE_SUCCESS_MESSAGE,"GBK"));
return LIST;
} catch (Exception ex) {
log.error("remove "+ex.toString());
if(ex.getMessage()==null){
setErrMessage(ex.toString());
}else{
setErrMessage(ex.getMessage());
}
return LIST;
}
}
public String save() throws Exception {
try {
if (get${table.entityname?cap_first}().get<#list table.primaryList as c>${c.propertyName?cap_first}</#list>() != null) {
this.setAction(this.ACTION_UPDATE);
this.setActionName(Config.MODIFY);
${table.entityname}Service.updateEntity(${table.entityname});
setActionMessage(Config.MODIFY_SUCCESS_MESSAGE);
page = new PaginationSupport(getMap());
page.setContextPath(this.getContextPath());
this.setUrlParams(page.getParams()+"&actionMessage="+URLEncoder.encode(Config.MODIFY_SUCCESS_MESSAGE,"GBK"));
return LIST;
} else {
this.setAction(this.ACTION_ADD);
this.setActionName(Config.ADD);
${table.entityname}.set<#list table.primaryList as c>${c.propertyName?cap_first}</#list>(new Long(IdGenerator.getId()));
${table.entityname}Service.saveEntity(${table.entityname});
setActionMessage(Config.ADD_SUCCESS_MESSAGE);
page = new PaginationSupport(getMap());
page.setContextPath(this.getContextPath());
this.setUrlParams(page.getParams()+"&actionMessage="+URLEncoder.encode(Config.ADD_SUCCESS_MESSAGE,"GBK"));
return LIST;
}
} catch (Exception ex) {
log.error("save "+ex.toString());
if(ex.getMessage()==null){
setErrMessage(ex.toString());
}else{
setErrMessage(ex.getMessage());
}
if(this.getAction().equals(this.ACTION_ADD)){//当前的操作异常时添加异常
${table.entityname}.set<#list table.primaryList as c>${c.propertyName?cap_first}</#list>(null);
}
return INPUT;
}
}
public String detail() throws Exception {
try {
set${table.entityname?cap_first}(${table.entityname}Service.getEntity(get${table.entityname?cap_first}().get<#list table.primaryList as c>${c.propertyName?cap_first}</#list>()));
try {
get${table.entityname?cap_first}().get<#list table.primaryList as c>${c.propertyName?cap_first}</#list>();
} catch (Exception ex) {
throw new Exception("此信息已经不存在,请刷新数据");
}
return INPUT;
} catch (Exception ex) {
log.error("detail "+ex.toString());
if(ex.getMessage()==null){
setErrMessage(ex.toString());
}else{
setErrMessage(ex.getMessage());
}
return LIST;
}
}
public String list() {
try {
page = new PaginationSupport(getMap());
page.setUrl(this.getUrl());
page.setContextPath(this.getContextPath());
page.setParam("pageSize","15");
this.setUrlParams(page.getParams());
String countSQL="select count(t.<#list table.primaryList as c>${c.propertyName}</#list>) count from ${table.name} t where 1=1";
String countAlias="count";
String sql="select {t.*} from ${table.name} t where 1=1 order by t.<#list table.primaryList as c>${c.propertyName}</#list> desc";
String sqlAlias="t";
page=daoHelper.findBySQL(page, countSQL, countAlias, sql, sqlAlias,null, ${table.entityname?cap_first}.class, false);
list=page.getItems();
this.setRecordCount(page.getTotalCount());
} catch (Exception ex) {
log.error("list "+ ex.toString());
if(ex.getMessage()==null){
setErrMessage(ex.toString());
}else{
setErrMessage(ex.getMessage());
}
}
return this.SUCCESS;
}
}