package org.springframework.webflow.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ClassUtils;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
public abstract class AbstractAction implements Action, InitializingBean {
protected final Log logger = LogFactory.getLog(getClass());
public EventFactorySupport getEventFactorySupport() {
return new EventFactorySupport();
}
public void afterPropertiesSet() throws Exception {
try {
initAction();
} catch (Exception ex) {
throw new BeanInitializationException("Initialization of this Action failed: " + ex.getMessage(), ex);
}
}
protected void initAction() throws Exception {
}
protected Event success() {
return getEventFactorySupport().success(this);
}
protected Event success(Object result) {
return getEventFactorySupport().success(this, result);
}
protected Event error() {
return getEventFactorySupport().error(this);
}
protected Event error(Exception e) {
return getEventFactorySupport().error(this, e);
}
protected Event yes() {
return getEventFactorySupport().yes(this);
}
protected Event no() {
return getEventFactorySupport().no(this);
}
protected Event result(boolean booleanResult) {
return getEventFactorySupport().event(this, booleanResult);
}
protected Event result(String eventId) {
return getEventFactorySupport().event(this, eventId);
}
protected Event result(String eventId, AttributeMap resultAttributes) {
return getEventFactorySupport().event(this, eventId, resultAttributes);
}
protected Event result(String eventId, String resultAttributeName, Object resultAttributeValue) {
return getEventFactorySupport().event(this, eventId, resultAttributeName, resultAttributeValue);
}
public final Event execute(RequestContext context) throws Exception {
Event result = doPreExecute(context);
if (result == null) {
result = doExecute(context);
doPostExecute(context);
} else {
if (logger.isInfoEnabled()) {
logger.info("Action execution disallowed; pre-execution result is '" + result.getId() + "'");
}
}
return result;
}
protected String getActionNameForLogging() {
return ClassUtils.getShortName(getClass());
}
protected Event doPreExecute(RequestContext context) throws Exception {
return null;
}
//抽象方法
protected abstract Event doExecute(RequestContext context) throws Exception;
protected void doPostExecute(RequestContext context) throws Exception {
}
}