📄 simplemethod.java
字号:
protected String delegatorName;
protected String securityName;
protected String dispatcherName;
protected String userLoginName;
public SimpleMethod(Element simpleMethodElement, Map parentSimpleMethodsMap) {
this.parentSimpleMethodsMap = parentSimpleMethodsMap;
this.methodName = simpleMethodElement.getAttribute("method-name");
this.shortDescription = simpleMethodElement.getAttribute("short-description");
defaultErrorCode = simpleMethodElement.getAttribute("default-error-code");
if (defaultErrorCode == null || defaultErrorCode.length() == 0) {
defaultErrorCode = "error";
}
defaultSuccessCode = simpleMethodElement.getAttribute("default-success-code");
if (defaultSuccessCode == null || defaultSuccessCode.length() == 0) {
defaultSuccessCode = "success";
}
parameterMapName = simpleMethodElement.getAttribute("parameter-map-name");
if (parameterMapName == null || parameterMapName.length() == 0) {
parameterMapName = "parameters";
}
eventRequestName = simpleMethodElement.getAttribute("event-request-object-name");
if (eventRequestName == null || eventRequestName.length() == 0) {
eventRequestName = "request";
}
eventResponseName = simpleMethodElement.getAttribute("event-response-object-name");
if (eventResponseName == null || eventResponseName.length() == 0) {
eventResponseName = "response";
}
eventResponseCodeName = simpleMethodElement.getAttribute("event-response-code-name");
if (eventResponseCodeName == null || eventResponseCodeName.length() == 0) {
eventResponseCodeName = "_response_code_";
}
eventErrorMessageName = simpleMethodElement.getAttribute("event-error-message-name");
if (eventErrorMessageName == null || eventErrorMessageName.length() == 0) {
eventErrorMessageName = "_error_message_";
}
eventEventMessageName = simpleMethodElement.getAttribute("event-event-message-name");
if (eventEventMessageName == null || eventEventMessageName.length() == 0) {
eventEventMessageName = "_event_message_";
}
serviceResponseMessageName = simpleMethodElement.getAttribute("service-response-message-name");
if (serviceResponseMessageName == null || serviceResponseMessageName.length() == 0) {
serviceResponseMessageName = "responseMessage";
}
serviceErrorMessageName = simpleMethodElement.getAttribute("service-error-message-name");
if (serviceErrorMessageName == null || serviceErrorMessageName.length() == 0) {
serviceErrorMessageName = "errorMessage";
}
serviceErrorMessageListName = simpleMethodElement.getAttribute("service-error-message-list-name");
if (serviceErrorMessageListName == null || serviceErrorMessageListName.length() == 0) {
serviceErrorMessageListName = "errorMessageList";
}
serviceErrorMessageMapName = simpleMethodElement.getAttribute("service-error-message-map-name");
if (serviceErrorMessageMapName == null || serviceErrorMessageMapName.length() == 0) {
serviceErrorMessageMapName = "errorMessageMap";
}
serviceSuccessMessageName = simpleMethodElement.getAttribute("service-success-message-name");
if (serviceSuccessMessageName == null || serviceSuccessMessageName.length() == 0) {
serviceSuccessMessageName = "successMessage";
}
serviceSuccessMessageListName = simpleMethodElement.getAttribute("service-success-message-list-name");
if (serviceSuccessMessageListName == null || serviceSuccessMessageListName.length() == 0) {
serviceSuccessMessageListName = "successMessageList";
}
loginRequired = !"false".equals(simpleMethodElement.getAttribute("login-required"));
useTransaction = !"false".equals(simpleMethodElement.getAttribute("use-transaction"));
localeName = simpleMethodElement.getAttribute("locale-name");
if (localeName == null || localeName.length() == 0) {
localeName = "locale";
}
delegatorName = simpleMethodElement.getAttribute("delegator-name");
if (delegatorName == null || delegatorName.length() == 0) {
delegatorName = "delegator";
}
securityName = simpleMethodElement.getAttribute("security-name");
if (securityName == null || securityName.length() == 0) {
securityName = "security";
}
dispatcherName = simpleMethodElement.getAttribute("dispatcher-name");
if (dispatcherName == null || dispatcherName.length() == 0) {
dispatcherName = "dispatcher";
}
userLoginName = simpleMethodElement.getAttribute("user-login-name");
if (userLoginName == null || userLoginName.length() == 0) {
userLoginName = "userLogin";
}
readOperations(simpleMethodElement, this.methodOperations, this);
}
public String getMethodName() {
return this.methodName;
}
public SimpleMethod getSimpleMethodInSameFile(String simpleMethodName) {
if (parentSimpleMethodsMap == null) return null;
return (SimpleMethod) parentSimpleMethodsMap.get(simpleMethodName);
}
public String getShortDescription() {
return this.shortDescription;
}
public String getDefaultErrorCode() {
return this.defaultErrorCode;
}
public String getDefaultSuccessCode() {
return this.defaultSuccessCode;
}
public String getParameterMapName() {
return this.parameterMapName;
}
// event fields
public String getEventRequestName() {
return this.eventRequestName;
}
public String getEventResponseCodeName() {
return this.eventResponseCodeName;
}
public String getEventErrorMessageName() {
return this.eventErrorMessageName;
}
public String getEventEventMessageName() {
return this.eventEventMessageName;
}
// service fields
public String getServiceResponseMessageName() {
return this.serviceResponseMessageName;
}
public String getServiceErrorMessageName() {
return this.serviceErrorMessageName;
}
public String getServiceErrorMessageListName() {
return this.serviceErrorMessageListName;
}
public String getServiceSuccessMessageName() {
return this.serviceSuccessMessageName;
}
public String getServiceSuccessMessageListName() {
return this.serviceSuccessMessageListName;
}
public boolean getLoginRequired() {
return this.loginRequired;
}
public boolean getUseTransaction() {
return this.useTransaction;
}
public String getDelegatorEnvName() {
return this.delegatorName;
}
public String getSecurityEnvName() {
return this.securityName;
}
public String getDispatcherEnvName() {
return this.dispatcherName;
}
public String getUserLoginEnvName() {
return this.userLoginName;
}
/** Execute the Simple Method operations */
public String exec(MethodContext methodContext) {
methodContext.putEnv(delegatorName, methodContext.getDelegator());
methodContext.putEnv(securityName, methodContext.getSecurity());
methodContext.putEnv(dispatcherName, methodContext.getDispatcher());
methodContext.putEnv(localeName, methodContext.getLocale());
methodContext.putEnv(parameterMapName, methodContext.getParameters());
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.putEnv(eventRequestName, methodContext.getRequest());
methodContext.putEnv(eventResponseName, methodContext.getResponse());
}
GenericValue userLogin = methodContext.getUserLogin();
if (userLogin != null) {
methodContext.putEnv(userLoginName, userLogin);
}
if (loginRequired) {
if (userLogin == null) {
String errMsg = "You must be logged in to complete the " + shortDescription + " process.";
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg);
return defaultErrorCode;
} else if (methodContext.getMethodType() == MethodContext.SERVICE) {
methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg);
methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
return null;
}
}
}
// if using transaction, try to start here
boolean beganTransaction = false;
if (useTransaction) {
try {
beganTransaction = TransactionUtil.begin();
} catch (GenericTransactionException e) {
String errMsg = "Error trying to begin transaction, could not process method: " + e.getMessage();
Debug.logWarning(errMsg, module);
Debug.logWarning(e, module);
if (methodContext.getMethodType() == MethodContext.EVENT) {
methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg);
return defaultErrorCode;
} else if (methodContext.getMethodType() == MethodContext.SERVICE) {
methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg);
methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
return null;
}
}
}
// declare errorMsg here just in case transaction ops fail
String errorMsg = "";
boolean finished = false;
try {
finished = runSubOps(methodOperations, methodContext);
} catch (Throwable t) {
// make SURE nothing gets thrown through
String errMsg = "Error running the simple-method: " + t.toString();
Debug.log(t, errMsg, module);
finished = false;
errorMsg += errMsg + "<br>";
}
String returnValue = null;
String response = null;
if (methodContext.getMethodType() == MethodContext.EVENT) {
boolean forceError = false;
String tempErrorMsg = (String) methodContext.getEnv(eventErrorMessageName);
if (errorMsg.length() > 0 || (tempErrorMsg != null && tempErrorMsg.length() > 0)) {
errorMsg += tempErrorMsg;
methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errorMsg);
forceError = true;
}
String eventMsg = (String) methodContext.getEnv(eventEventMessageName);
if (eventMsg != null && eventMsg.length() > 0) {
methodContext.getRequest().setAttribute("_EVENT_MESSAGE_", eventMsg);
}
response = (String) methodContext.getEnv(eventResponseCodeName);
if (response == null || response.length() == 0) {
if (forceError) {
//override response code, always use error code
Debug.logInfo("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module);
response = defaultErrorCode;
} else {
Debug.logInfo("No response code string or errors found, assuming success; returning code [" + defaultSuccessCode + "]", module);
response = defaultSuccessCode;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -