1 24 package org.ofbiz.minilang; 25 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import javolution.util.FastList; 36 import javolution.util.FastMap; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 40 import org.ofbiz.base.location.FlexibleLocation; 41 import org.ofbiz.base.util.Debug; 42 import org.ofbiz.base.util.UtilMisc; 43 import org.ofbiz.base.util.UtilProperties; 44 import org.ofbiz.base.util.UtilValidate; 45 import org.ofbiz.base.util.UtilXml; 46 import org.ofbiz.base.util.cache.UtilCache; 47 import org.ofbiz.entity.GenericEntity; 48 import org.ofbiz.entity.GenericValue; 49 import org.ofbiz.entity.transaction.GenericTransactionException; 50 import org.ofbiz.entity.transaction.TransactionUtil; 51 import org.ofbiz.minilang.method.MethodContext; 52 import org.ofbiz.minilang.method.MethodOperation; 53 import org.ofbiz.service.DispatchContext; 54 import org.ofbiz.service.ModelService; 55 56 64 public class SimpleMethod { 65 66 public static final String module = SimpleMethod.class.getName(); 67 public static final String err_resource = "MiniLangErrorUiLabels"; 68 69 protected static UtilCache simpleMethodsDirectCache = new UtilCache("minilang.SimpleMethodsDirect", 0, 0); 70 protected static UtilCache simpleMethodsResourceCache = new UtilCache("minilang.SimpleMethodsResource", 0, 0); 71 protected static UtilCache simpleMethodsURLCache = new UtilCache("minilang.SimpleMethodsURL", 0, 0); 72 73 75 public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response) throws MiniLangException { 76 return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, null)); 77 } 78 79 public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response, ClassLoader loader) throws MiniLangException { 80 return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, loader)); 81 } 82 83 public static String runSimpleEvent(URL xmlURL, String methodName, HttpServletRequest request, HttpServletResponse response, ClassLoader loader) throws MiniLangException { 84 return runSimpleMethod(xmlURL, methodName, new MethodContext(request, response, loader)); 85 } 86 87 89 public static Map runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map context) throws MiniLangException { 90 MethodContext methodContext = new MethodContext(ctx, context, null); 91 runSimpleMethod(xmlResource, methodName, methodContext); 92 return methodContext.getResults(); 93 } 94 95 public static Map runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map context, ClassLoader loader) throws MiniLangException { 96 MethodContext methodContext = new MethodContext(ctx, context, loader); 97 runSimpleMethod(xmlResource, methodName, methodContext); 98 return methodContext.getResults(); 99 } 100 101 public static Map runSimpleService(URL xmlURL, String methodName, DispatchContext ctx, Map context, ClassLoader loader) throws MiniLangException { 102 MethodContext methodContext = new MethodContext(ctx, context, loader); 103 runSimpleMethod(xmlURL, methodName, methodContext); 104 return methodContext.getResults(); 105 } 106 107 109 public static String runSimpleMethod(String xmlResource, String methodName, MethodContext methodContext) throws MiniLangException { 110 Map simpleMethods = getSimpleMethods(xmlResource, methodName, methodContext.getLoader()); 111 SimpleMethod simpleMethod = (SimpleMethod) simpleMethods.get(methodName); 112 if (simpleMethod == null) { 113 throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource); 114 } 115 return simpleMethod.exec(methodContext); 116 } 117 118 public static String runSimpleMethod(URL xmlURL, String methodName, MethodContext methodContext) throws MiniLangException { 119 Map simpleMethods = getSimpleMethods(xmlURL, methodName); 120 SimpleMethod simpleMethod = (SimpleMethod) simpleMethods.get(methodName); 121 if (simpleMethod == null) { 122 throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document from URL: " + xmlURL.toString()); 123 } 124 return simpleMethod.exec(methodContext); 125 } 126 127 public static Map getSimpleMethods(String xmlResource, String methodName, ClassLoader loader) throws MiniLangException { 128 Map simpleMethods = (Map ) simpleMethodsResourceCache.get(xmlResource); 129 if (simpleMethods == null) { 130 synchronized (SimpleMethod.class) { 131 simpleMethods = (Map ) simpleMethodsResourceCache.get(xmlResource); 132 if (simpleMethods == null) { 133 URL xmlURL = null; 135 try { 136 xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader); 137 } catch (MalformedURLException e) { 138 throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource + "; error was: " + e.toString(), e); 139 } 140 141 if (xmlURL == null) { 142 throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource); 143 } 144 simpleMethods = getAllSimpleMethods(xmlURL); 145 146 simpleMethodsResourceCache.put(xmlResource, simpleMethods); 148 } 149 } 150 } 151 152 return simpleMethods; 153 } 154 155 public static Map getSimpleMethods(URL xmlURL, String methodName) throws MiniLangException { 156 Map simpleMethods = (Map ) simpleMethodsURLCache.get(xmlURL); 157 158 if (simpleMethods == null) { 159 synchronized (SimpleMethod.class) { 160 simpleMethods = (Map ) simpleMethodsURLCache.get(xmlURL); 161 if (simpleMethods == null) { 162 simpleMethods = getAllSimpleMethods(xmlURL); 163 164 simpleMethodsURLCache.put(xmlURL, simpleMethods); 166 } 167 } 168 } 169 170 return simpleMethods; 171 } 172 173 protected static Map getAllSimpleMethods(URL xmlURL) throws MiniLangException { 174 Map simpleMethods = FastMap.newInstance(); 175 176 Document document = null; 178 try { 179 document = UtilXml.readXmlDocument(xmlURL, true); 180 } catch (java.io.IOException e) { 181 throw new MiniLangException("Could not read XML file", e); 182 } catch (org.xml.sax.SAXException e) { 183 throw new MiniLangException("Could not parse XML file", e); 184 } catch (javax.xml.parsers.ParserConfigurationException e) { 185 throw new MiniLangException("XML parser not setup correctly", e); 186 } 187 188 if (document == null) { 189 throw new MiniLangException("Could not find SimpleMethod XML document: " + xmlURL.toString()); 190 } 191 192 Element rootElement = document.getDocumentElement(); 193 List simpleMethodElements = UtilXml.childElementList(rootElement, "simple-method"); 194 195 Iterator simpleMethodIter = simpleMethodElements.iterator(); 196 197 while (simpleMethodIter.hasNext()) { 198 Element simpleMethodElement = (Element ) simpleMethodIter.next(); 199 SimpleMethod simpleMethod = new SimpleMethod(simpleMethodElement, simpleMethods, xmlURL.toString()); 200 simpleMethods.put(simpleMethod.getMethodName(), simpleMethod); 201 } 202 203 return simpleMethods; 204 } 205 206 public static Map getDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException { 207 Map simpleMethods = (Map ) simpleMethodsDirectCache.get(name); 208 209 if (simpleMethods == null) { 210 synchronized (SimpleMethod.class) { 211 simpleMethods = (Map ) simpleMethodsDirectCache.get(name); 212 if (simpleMethods == null) { 213 simpleMethods = getAllDirectSimpleMethods(name, content, fromLocation); 214 215 simpleMethodsDirectCache.put(name, simpleMethods); 217 } 218 } 219 } 220 221 return simpleMethods; 222 } 223 224 protected static Map getAllDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException { 225 if (UtilValidate.isEmpty(fromLocation)) { 226 fromLocation = "<location not known>"; 227 } 228 229 Map simpleMethods = FastMap.newInstance(); 230 231 Document document = null; 233 234 try { 235 if (content != null) { 236 document = UtilXml.readXmlDocument(content, true); 237 } 238 } catch (java.io.IOException e) { 239 throw new MiniLangException("Could not read XML content", e); 240 } catch (org.xml.sax.SAXException e) { 241 throw new MiniLangException("Could not parse direct XML content", e); 242 } catch (javax.xml.parsers.ParserConfigurationException e) { 243 throw new MiniLangException("XML parser not setup correctly", e); 244 } 245 246 if (document == null) { 247 throw new MiniLangException("Could not load SimpleMethod XML document: " + name); 248 } 249 250 Element rootElement = document.getDocumentElement(); 251 List simpleMethodElements = UtilXml.childElementList(rootElement, "simple-method"); 252 253 Iterator simpleMethodIter = simpleMethodElements.iterator(); 254 255 while (simpleMethodIter.hasNext()) { 256 Element simpleMethodElement = (Element ) simpleMethodIter.next(); 257 SimpleMethod simpleMethod = new SimpleMethod(simpleMethodElement, simpleMethods, fromLocation); 258 simpleMethods.put(simpleMethod.getMethodName(), simpleMethod); 259 } 260 261 return simpleMethods; 262 } 263 264 protected List methodOperations = FastList.newInstance(); 266 protected Map parentSimpleMethodsMap; 267 protected String fromLocation; 268 protected String methodName; 269 protected String shortDescription; 270 protected String defaultErrorCode; 271 protected String defaultSuccessCode; 272 273 protected String parameterMapName; 274 275 protected String eventRequestName; 277 protected String eventResponseName; 278 protected String eventResponseCodeName; 279 protected String eventErrorMessageName; 280 protected String eventErrorMessageListName; 281 protected String eventEventMessageName; 282 protected String eventEventMessageListName; 283 284 protected String serviceResponseMessageName; 286 protected String serviceErrorMessageName; 287 protected String serviceErrorMessageListName; 288 protected String serviceErrorMessageMapName; 289 protected String serviceSuccessMessageName; 290 protected String serviceSuccessMessageListName; 291 292 protected boolean loginRequired = true; 293 protected boolean useTransaction = true; 294 295 protected String localeName; 296 protected String delegatorName; 297 protected String securityName; 298 protected String dispatcherName; 299 protected String userLoginName; 300 301 public SimpleMethod(Element simpleMethodElement, Map parentSimpleMethodsMap, String fromLocation) { 302 this.parentSimpleMethodsMap = parentSimpleMethodsMap; 303 this.fromLocation = fromLocation; 304 this.methodName = simpleMethodElement.getAttribute("method-name"); 305 this.shortDescription = simpleMethodElement.getAttribute("short-description"); 306 307 defaultErrorCode = simpleMethodElement.getAttribute("default-error-code"); 308 if (defaultErrorCode == null || defaultErrorCode.length() == 0) { 309 defaultErrorCode = "error"; 310 } 311 defaultSuccessCode = simpleMethodElement.getAttribute("default-success-code"); 312 if (defaultSuccessCode == null || defaultSuccessCode.length() == 0) { 313 defaultSuccessCode = "success"; 314 } 315 316 parameterMapName = simpleMethodElement.getAttribute("parameter-map-name"); 317 if (parameterMapName == null || parameterMapName.length() == 0) { 318 parameterMapName = "parameters"; 319 } 320 321 eventRequestName = simpleMethodElement.getAttribute("event-request-object-name"); 322 if (eventRequestName == null || eventRequestName.length() == 0) { 323 eventRequestName = "request"; 324 } 325 eventResponseName = simpleMethodElement.getAttribute("event-response-object-name"); 326 if (eventResponseName == null || eventResponseName.length() == 0) { 327 eventResponseName = "response"; 328 } 329 eventResponseCodeName = simpleMethodElement.getAttribute("event-response-code-name"); 330 if (eventResponseCodeName == null || eventResponseCodeName.length() == 0) { 331 eventResponseCodeName = "_response_code_"; 332 } 333 eventErrorMessageName = simpleMethodElement.getAttribute("event-error-message-name"); 334 if (eventErrorMessageName == null || eventErrorMessageName.length() == 0) { 335 eventErrorMessageName = "_error_message_"; 336 } 337 eventErrorMessageListName = simpleMethodElement.getAttribute("event-error-message-list-name"); 338 if (eventErrorMessageListName == null || eventErrorMessageListName.length() == 0) { 339 eventErrorMessageListName = "_error_message_list_"; 340 } 341 eventEventMessageName = simpleMethodElement.getAttribute("event-event-message-name"); 342 if (eventEventMessageName == null || eventEventMessageName.length() == 0) { 343 eventEventMessageName = "_event_message_"; 344 } 345 eventEventMessageListName = simpleMethodElement.getAttribute("event-event-message-list-name"); 346 if (eventEventMessageListName == null || eventEventMessageListName.length() == 0) { 347 eventEventMessageListName = "_event_message_list_"; 348 } 349 350 serviceResponseMessageName = simpleMethodElement.getAttribute("service-response-message-name"); 351 if (serviceResponseMessageName == null || serviceResponseMessageName.length() == 0) { 352 serviceResponseMessageName = "responseMessage"; 353 } 354 serviceErrorMessageName = simpleMethodElement.getAttribute("service-error-message-name"); 355 if (serviceErrorMessageName == null || serviceErrorMessageName.length() == 0) { 356 serviceErrorMessageName = "errorMessage"; 357 } 358 serviceErrorMessageListName = simpleMethodElement.getAttribute("service-error-message-list-name"); 359 if (serviceErrorMessageListName == null || serviceErrorMessageListName.length() == 0) { 360 serviceErrorMessageListName = "errorMessageList"; 361 } 362 serviceErrorMessageMapName = simpleMethodElement.getAttribute("service-error-message-map-name"); 363 if (serviceErrorMessageMapName == null || serviceErrorMessageMapName.length() == 0) { 364 serviceErrorMessageMapName = "errorMessageMap"; 365 } 366 367 serviceSuccessMessageName = simpleMethodElement.getAttribute("service-success-message-name"); 368 if (serviceSuccessMessageName == null || serviceSuccessMessageName.length() == 0) { 369 serviceSuccessMessageName = "successMessage"; 370 } 371 serviceSuccessMessageListName = simpleMethodElement.getAttribute("service-success-message-list-name"); 372 if (serviceSuccessMessageListName == null || serviceSuccessMessageListName.length() == 0) { 373 serviceSuccessMessageListName = "successMessageList"; 374 } 375 376 loginRequired = !"false".equals(simpleMethodElement.getAttribute("login-required")); 377 useTransaction = !"false".equals(simpleMethodElement.getAttribute("use-transaction")); 378 379 localeName = simpleMethodElement.getAttribute("locale-name"); 380 if (localeName == null || localeName.length() == 0) { 381 localeName = "locale"; 382 } 383 delegatorName = simpleMethodElement.getAttribute("delegator-name"); 384 if (delegatorName == null || delegatorName.length() == 0) { 385 delegatorName = "delegator"; 386 } 387 securityName = simpleMethodElement.getAttribute("security-name"); 388 if (securityName == null || securityName.length() == 0) { 389 securityName = "security"; 390 } 391 dispatcherName = simpleMethodElement.getAttribute("dispatcher-name"); 392 if (dispatcherName == null || dispatcherName.length() == 0) { 393 dispatcherName = "dispatcher"; 394 } 395 userLoginName = simpleMethodElement.getAttribute("user-login-name"); 396 if (userLoginName == null || userLoginName.length() == 0) { 397 userLoginName = "userLogin"; 398 } 399 400 readOperations(simpleMethodElement, this.methodOperations, this); 401 } 402 403 public String getFromLocation() { 404 return this.fromLocation; 405 } 406 public String getMethodName() { 407 return this.methodName; 408 } 409 410 public SimpleMethod getSimpleMethodInSameFile(String simpleMethodName) { 411 if (parentSimpleMethodsMap == null) return null; 412 return (SimpleMethod) parentSimpleMethodsMap.get(simpleMethodName); 413 } 414 415 public String getShortDescription() { 416 return this.shortDescription + " [" + this.fromLocation + "#" + this.methodName + "]"; 417 } 418 419 public String getDefaultErrorCode() { 420 return this.defaultErrorCode; 421 } 422 423 public String getDefaultSuccessCode() { 424 return this.defaultSuccessCode; 425 } 426 427 public String getParameterMapName() { 428 return this.parameterMapName; 429 } 430 431 public String getEventRequestName() { 433 return this.eventRequestName; 434 } 435 436 public String getEventResponseCodeName() { 437 return this.eventResponseCodeName; 438 } 439 440 public String getEventErrorMessageName() { 441 return this.eventErrorMessageName; 442 } 443 public String getEventErrorMessageListName() { 444 return this.eventErrorMessageListName; 445 } 446 447 public String getEventEventMessageName() { 448 return this.eventEventMessageName; 449 } 450 public String getEventEventMessageListName() { 451 return this.eventEventMessageListName; 452 } 453 454 public String getServiceResponseMessageName() { 456 return this.serviceResponseMessageName; 457 } 458 459 public String getServiceErrorMessageName() { 460 return this.serviceErrorMessageName; 461 } 462 463 public String getServiceErrorMessageListName() { 464 return this.serviceErrorMessageListName; 465 } 466 467 public String getServiceSuccessMessageName() { 468 return this.serviceSuccessMessageName; 469 } 470 471 public String getServiceSuccessMessageListName() { 472 return this.serviceSuccessMessageListName; 473 } 474 475 public boolean getLoginRequired() { 476 return this.loginRequired; 477 } 478 479 public boolean getUseTransaction() { 480 return this.useTransaction; 481 } 482 483 public String getDelegatorEnvName() { 484 return this.delegatorName; 485 } 486 487 public String getSecurityEnvName() { 488 return this.securityName; 489 } 490 491 public String getDispatcherEnvName() { 492 return this.dispatcherName; 493 } 494 495 public String getUserLoginEnvName() { 496 return this.userLoginName; 497 } 498 499 500 public String exec(MethodContext methodContext) { 501 methodContext.putEnv("null", GenericEntity.NULL_FIELD); 503 methodContext.putEnv("nullField", GenericEntity.NULL_FIELD); 504 505 methodContext.putEnv(delegatorName, methodContext.getDelegator()); 506 methodContext.putEnv(securityName, methodContext.getSecurity()); 507 methodContext.putEnv(dispatcherName, methodContext.getDispatcher()); 508 methodContext.putEnv(localeName, methodContext.getLocale()); 509 methodContext.putEnv(parameterMapName, methodContext.getParameters()); 510 511 if (methodContext.getMethodType() == MethodContext.EVENT) { 512 methodContext.putEnv(eventRequestName, methodContext.getRequest()); 513 methodContext.putEnv(eventResponseName, methodContext.getResponse()); 514 } 515 516 methodContext.putEnv("methodName", this.getMethodName()); 517 methodContext.putEnv("methodShortDescription", this.getShortDescription()); 518 519 520 GenericValue userLogin = methodContext.getUserLogin(); 521 Locale locale = methodContext.getLocale(); 522 523 if (userLogin != null) { 524 methodContext.putEnv(userLoginName, userLogin); 525 } 526 if (loginRequired) { 527 if (userLogin == null) { 528 Map messageMap = UtilMisc.toMap("shortDescription", shortDescription); 529 String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.must_logged_process", messageMap, locale) + "."; 530 531 if (methodContext.getMethodType() == MethodContext.EVENT) { 532 methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg); 533 return defaultErrorCode; 534 } else if (methodContext.getMethodType() == MethodContext.SERVICE) { 535 methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg); 536 methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 537 return null; 538 } 539 } 540 } 541 542 boolean beganTransaction = false; 544 545 if (useTransaction) { 546 try { 547 beganTransaction = TransactionUtil.begin(); 548 } catch (GenericTransactionException e) { 549 String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_begin_transaction", locale) + ": " + e.getMessage(); 550 Debug.logWarning(errMsg, module); 551 Debug.logWarning(e, module); 552 if (methodContext.getMethodType() == MethodContext.EVENT) { 553 methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg); 554 return defaultErrorCode; 555 } else if (methodContext.getMethodType() == MethodContext.SERVICE) { 556 methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg); 557 methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 558 return null; 559 } 560 } 561 } 562 563 String errorMsg = ""; 565 566 boolean finished = false; 567 try { 568 finished = runSubOps(methodOperations, methodContext); 569 } catch (Throwable t) { 570 String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_running", locale) + ": " + t.getMessage(); 572 Debug.logError(errMsg, module); 573 finished = false; 574 errorMsg += errMsg + "<br/>"; 575 } 576 577 String returnValue = null; 578 String response = null; 579 StringBuffer summaryErrorStringBuffer = new StringBuffer (); 580 if (methodContext.getMethodType() == MethodContext.EVENT) { 581 boolean forceError = false; 582 583 String tempErrorMsg = (String ) methodContext.getEnv(eventErrorMessageName); 584 if (errorMsg.length() > 0 || (tempErrorMsg != null && tempErrorMsg.length() > 0)) { 585 errorMsg += tempErrorMsg; 586 methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errorMsg); 587 forceError = true; 588 589 summaryErrorStringBuffer.append(errorMsg); 590 } 591 List tempErrorMsgList = (List ) methodContext.getEnv(eventErrorMessageListName); 592 if (tempErrorMsgList != null && tempErrorMsgList.size() > 0) { 593 methodContext.getRequest().setAttribute("_ERROR_MESSAGE_LIST_", tempErrorMsgList); 594 forceError = true; 595 596 summaryErrorStringBuffer.append("; "); 597 summaryErrorStringBuffer.append(tempErrorMsgList.toString()); 598 } 599 600 String eventMsg = (String ) methodContext.getEnv(eventEventMessageName); 601 if (eventMsg != null && eventMsg.length() > 0) { 602 methodContext.getRequest().setAttribute("_EVENT_MESSAGE_", eventMsg); 603 } 604 List eventMsgList = (List ) methodContext.getEnv(eventEventMessageListName); 605 if (eventMsgList != null && eventMsgList.size() > 0) { 606 methodContext.getRequest().setAttribute("_EVENT_MESSAGE_LIST_", eventMsgList); 607 } 608 609 response = (String ) methodContext.getEnv(eventResponseCodeName); 610 if (response == null || response.length() == 0) { 611 if (forceError) { 612 Debug.logInfo("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module); 614 response = defaultErrorCode; 615 } else { 616 Debug.logInfo("No response code string or errors found, assuming success; returning code [" + defaultSuccessCode + "]", module); 617 response = defaultSuccessCode; 618 } 619 } 620 returnValue = response; 621 } else if (methodContext.getMethodType() == MethodContext.SERVICE) { 622 boolean forceError = false; 623 624 String tempErrorMsg = (String ) methodContext.getEnv(serviceErrorMessageName); 625 if (errorMsg.length() > 0 || (tempErrorMsg != null && tempErrorMsg.length() > 0)) { 626 errorMsg += tempErrorMsg; 627 methodContext.putResult(ModelService.ERROR_MESSAGE, errorMsg); 628 forceError = true; 629 630 summaryErrorStringBuffer.append(errorMsg); 631 } 632 633 List errorMsgList = (List ) methodContext.getEnv(serviceErrorMessageListName); 634 if (errorMsgList != null && errorMsgList.size() > 0) { 635 methodContext.putResult(ModelService.ERROR_MESSAGE_LIST, errorMsgList); 636 forceError = true; 637 638 summaryErrorStringBuffer.append("; "); 639 summaryErrorStringBuffer.append(errorMsgList.toString()); 640 } 641 642 Map errorMsgMap = (Map ) methodContext.getEnv(serviceErrorMessageMapName); 643 if (errorMsgMap != null && errorMsgMap.size() > 0) { 644 methodContext.putResult(ModelService.ERROR_MESSAGE_MAP, errorMsgMap); 645 forceError = true; 646 647 summaryErrorStringBuffer.append("; "); 648 summaryErrorStringBuffer.append(errorMsgMap.toString()); 649 } 650 651 String successMsg = (String ) methodContext.getEnv(serviceSuccessMessageName); 652 if (successMsg != null && successMsg.length() > 0) { 653 methodContext.putResult(ModelService.SUCCESS_MESSAGE, successMsg); 654 } 655 656 List successMsgList = (List ) methodContext.getEnv(serviceSuccessMessageListName); 657 if (successMsgList != null && successMsgList.size() > 0) { 658 methodContext.putResult(ModelService.SUCCESS_MESSAGE_LIST, successMsgList); 659 } 660 661 response = (String ) methodContext.getEnv(serviceResponseMessageName); 662 if (response == null || response.length() == 0) { 663 if (forceError) { 664 Debug.logVerbose("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module); 666 response = defaultErrorCode; 667 } else { 668 Debug.logVerbose("No response code string or errors found, assuming success; returning code [" + defaultSuccessCode + "]", module); 669 response = defaultSuccessCode; 670 } 671 } 672 methodContext.putResult(ModelService.RESPONSE_MESSAGE, response); 673 returnValue = null; 674 } else { 675 response = defaultSuccessCode; 676 returnValue = defaultSuccessCode; 677 } 678 679 boolean doCommit = true; 681 if (!finished && defaultErrorCode.equals(response)) { 682 doCommit = false; 683 } 684 685 if (doCommit) { 686 try { 688 TransactionUtil.commit(beganTransaction); 689 } catch (GenericTransactionException e) { 690 String errMsg = "Error trying to commit transaction, could not process method: " + e.getMessage(); 691 Debug.logWarning(e, errMsg, module); 692 errorMsg += errMsg + "<br/>"; 693 } 694 } else { 695 try { 697 TransactionUtil.rollback(beganTransaction, "Error in simple-method [" + this.getShortDescription() + "]: " + summaryErrorStringBuffer, null); 698 } catch (GenericTransactionException e) { 699 String errMsg = "Error trying to rollback transaction, could not process method: " + e.getMessage(); 700 Debug.logWarning(e, errMsg, module); 701 errorMsg += errMsg + "<br/>"; 702 } 703 } 704 705 return returnValue; 706 } 707 708 public static void readOperations(Element simpleMethodElement, List methodOperations, SimpleMethod simpleMethod) { 709 List operationElements = UtilXml.childElementList(simpleMethodElement); 710 711 if (operationElements != null && operationElements.size() > 0) { 712 Iterator operElemIter = operationElements.iterator(); 713 714 while (operElemIter.hasNext()) { 715 Element curOperElem = (Element ) operElemIter.next(); 716 String nodeName = curOperElem.getNodeName(); 717 718 if ("call-map-processor".equals(nodeName)) { 719 methodOperations.add(new org.ofbiz.minilang.method.callops.CallSimpleMapProcessor(curOperElem, simpleMethod)); 720 } else if ("check-errors".equals(nodeName)) { 721 methodOperations.add(new org.ofbiz.minilang.method.callops.CheckErrors(curOperElem, simpleMethod)); 722 } else if ("add-error".equals(nodeName)) { 723 methodOperations.add(new org.ofbiz.minilang.method.callops.AddError(curOperElem, simpleMethod)); 724 } else if ("return".equals(nodeName)) { 725 methodOperations.add(new org.ofbiz.minilang.method.callops.Return(curOperElem, simpleMethod)); 726 } else if ("set-service-fields".equals(nodeName)) { 727 methodOperations.add(new org.ofbiz.minilang.method.callops.SetServiceFields(curOperElem, simpleMethod)); 728 } else if ("call-service".equals(nodeName)) { 729 methodOperations.add(new org.ofbiz.minilang.method.callops.CallService(curOperElem, simpleMethod)); 730 } else if ("call-service-asynch".equals(nodeName)) { 731 methodOperations.add(new org.ofbiz.minilang.method.callops.CallServiceAsynch(curOperElem, simpleMethod)); 732 } else if ("call-bsh".equals(nodeName)) { 733 methodOperations.add(new org.ofbiz.minilang.method.callops.CallBsh(curOperElem, simpleMethod)); 734 } else if ("call-simple-method".equals(nodeName)) { 735 methodOperations.add(new org.ofbiz.minilang.method.callops.CallSimpleMethod(curOperElem, simpleMethod)); 736 737 } else if ("call-object-method".equals(nodeName)) { 738 methodOperations.add(new org.ofbiz.minilang.method.callops.CallObjectMethod(curOperElem, simpleMethod)); 739 } else if ("call-class-method".equals(nodeName)) { 740 methodOperations.add(new org.ofbiz.minilang.method.callops.CallClassMethod(curOperElem, simpleMethod)); 741 } else if ("create-object".equals(nodeName)) { 742 methodOperations.add(new org.ofbiz.minilang.method.callops.CreateObject(curOperElem, simpleMethod)); 743 744 } else if ("field-to-request".equals(nodeName)) { 745 methodOperations.add(new org.ofbiz.minilang.method.eventops.FieldToRequest(curOperElem, simpleMethod)); 746 } else if ("field-to-session".equals(nodeName)) { 747 methodOperations.add(new org.ofbiz.minilang.method.eventops.FieldToSession(curOperElem, simpleMethod)); 748 } else if ("request-to-field".equals(nodeName)) { 749 methodOperations.add(new org.ofbiz.minilang.method.eventops.RequestToField(curOperElem, simpleMethod)); 750 } else if ("request-parameters-to-list".equals(nodeName)) { 751 methodOperations.add(new org.ofbiz.minilang.method.eventops.RequestParametersToList(curOperElem, simpleMethod)); 752 } else if ("session-to-field".equals(nodeName)) { 753 methodOperations.add(new org.ofbiz.minilang.method.eventops.SessionToField(curOperElem, simpleMethod)); 754 } else if ("webapp-property-to-field".equals(nodeName)) { 755 methodOperations.add(new org.ofbiz.minilang.method.eventops.WebappPropertyToField(curOperElem, simpleMethod)); 756 757 } else if ("field-to-result".equals(nodeName)) { 758 methodOperations.add(new org.ofbiz.minilang.method.serviceops.FieldToResult(curOperElem, simpleMethod)); 759 760 } else if ("map-to-map".equals(nodeName)) { 761 methodOperations.add(new org.ofbiz.minilang.method.envops.MapToMap(curOperElem, simpleMethod)); 762 } else if ("field-to-list".equals(nodeName)) { 763 methodOperations.add(new org.ofbiz.minilang.method.envops.FieldToList(curOperElem, simpleMethod)); 764 } else if ("list-to-list".equals(nodeName)) { 765 methodOperations.add(new org.ofbiz.minilang.method.envops.ListToList(curOperElem, simpleMethod)); 766 } else if ("order-map-list".equals(nodeName)) { 767 methodOperations.add(new org.ofbiz.minilang.method.envops.OrderMapList(curOperElem, simpleMethod)); 768 769 } else if ("set".equals(nodeName)) { 770 methodOperations.add(new org.ofbiz.minilang.method.envops.SetOperation(curOperElem, simpleMethod)); 771 } else if ("env-to-env".equals(nodeName)) { 772 MethodOperation mop = new org.ofbiz.minilang.method.envops.EnvToEnv(curOperElem, simpleMethod); 773 methodOperations.add(mop); 774 Debug.logInfo("The env-to-env operation has been deprecated in favor of the set operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + mop.rawString(), module); 775 } else if ("env-to-field".equals(nodeName)) { 776 MethodOperation mop = new org.ofbiz.minilang.method.envops.EnvToField(curOperElem, simpleMethod); 777 methodOperations.add(mop); 778 Debug.logInfo("The env-to-field operation has been deprecated in favor of the set operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + mop.rawString(), module); 779 } else if ("field-to-env".equals(nodeName)) { 780 MethodOperation mop = new org.ofbiz.minilang.method.envops.FieldToEnv(curOperElem, simpleMethod); 781 methodOperations.add(mop); 782 Debug.logInfo("The field-to-env operation has been deprecated in favor of the set operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + mop.rawString(), module); 783 } else if ("field-to-field".equals(nodeName)) { 784 MethodOperation mop = new org.ofbiz.minilang.method.envops.FieldToField(curOperElem, simpleMethod); 785 methodOperations.add(mop); 786 Debug.logInfo("The field-to-field operation has been deprecated in favor of the set operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + mop.rawString(), module); 787 } else if ("string-to-field".equals(nodeName)) { 788 MethodOperation mop = new org.ofbiz.minilang.method.envops.StringToField(curOperElem, simpleMethod); 789 methodOperations.add(mop); 790 Debug.logInfo("The string-to-field operation has been deprecated in favor of the set operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + mop.rawString(), module); 791 792 } else if ("string-append".equals(nodeName)) { 793 methodOperations.add(new org.ofbiz.minilang.method.envops.StringAppend(curOperElem, simpleMethod)); 794 } else if ("string-to-list".equals(nodeName)) { 795 methodOperations.add(new org.ofbiz.minilang.method.envops.StringToList(curOperElem, simpleMethod)); 796 } else if ("to-string".equals(nodeName)) { 797 methodOperations.add(new org.ofbiz.minilang.method.envops.ToString(curOperElem, simpleMethod)); 798 } else if ("clear-field".equals(nodeName)) { 799 methodOperations.add(new org.ofbiz.minilang.method.envops.ClearField(curOperElem, simpleMethod)); 800 } else if ("iterate".equals(nodeName)) { 801 methodOperations.add(new org.ofbiz.minilang.method.envops.Iterate(curOperElem, simpleMethod)); 802 } else if ("iterate-map".equals(nodeName)) { 803 methodOperations.add(new org.ofbiz.minilang.method.envops.IterateMap(curOperElem, simpleMethod)); 804 } else if ("loop".equals(nodeName)) { 805 methodOperations.add(new org.ofbiz.minilang.method.envops.Loop(curOperElem, simpleMethod)); 806 } else if ("first-from-list".equals(nodeName)) { 807 methodOperations.add(new org.ofbiz.minilang.method.envops.FirstFromList(curOperElem, simpleMethod)); 808 809 } else if ("transaction-begin".equals(nodeName)) { 810 methodOperations.add(new org.ofbiz.minilang.method.entityops.TransactionBegin(curOperElem, simpleMethod)); 811 } else if ("transaction-commit".equals(nodeName)) { 812 methodOperations.add(new org.ofbiz.minilang.method.entityops.TransactionCommit(curOperElem, simpleMethod)); 813 } else if ("transaction-rollback".equals(nodeName)) { 814 methodOperations.add(new org.ofbiz.minilang.method.entityops.TransactionRollback(curOperElem, simpleMethod)); 815 816 } else if ("now-timestamp-to-env".equals(nodeName)) { 817 methodOperations.add(new org.ofbiz.minilang.method.entityops.NowTimestampToEnv(curOperElem, simpleMethod)); 818 } else if ("now-date-to-env".equals(nodeName)) { 819 methodOperations.add(new org.ofbiz.minilang.method.entityops.NowDateToEnv(curOperElem, simpleMethod)); 820 } else if ("sequenced-id-to-env".equals(nodeName)) { 821 methodOperations.add(new org.ofbiz.minilang.method.entityops.SequencedIdToEnv(curOperElem, simpleMethod)); 822 } else if ("make-next-seq-id".equals(nodeName)) { 823 methodOperations.add(new org.ofbiz.minilang.method.entityops.MakeNextSeqId(curOperElem, simpleMethod)); 824 } else if ("set-current-user-login".equals(nodeName)) { 825 methodOperations.add(new org.ofbiz.minilang.method.entityops.SetCurrentUserLogin(curOperElem, simpleMethod)); 826 827 } else if ("find-by-primary-key".equals(nodeName)) { 828 methodOperations.add(new org.ofbiz.minilang.method.entityops.FindByPrimaryKey(curOperElem, simpleMethod)); 829 } else if ("find-by-and".equals(nodeName)) { 830 methodOperations.add(new org.ofbiz.minilang.method.entityops.FindByAnd(curOperElem, simpleMethod)); 831 } else if ("entity-one".equals(nodeName)) { 832 methodOperations.add(new org.ofbiz.minilang.method.entityops.EntityOne(curOperElem, simpleMethod)); 833 } else if ("entity-and".equals(nodeName)) { 834 methodOperations.add(new org.ofbiz.minilang.method.entityops.EntityAnd(curOperElem, simpleMethod)); 835 } else if ("entity-condition".equals(nodeName)) { 836 methodOperations.add(new org.ofbiz.minilang.method.entityops.EntityCondition(curOperElem, simpleMethod)); 837 } else if ("entity-count".equals(nodeName)) { 838 methodOperations.add(new org.ofbiz.minilang.method.entityops.EntityCount(curOperElem, simpleMethod)); 839 } else if ("get-related-one".equals(nodeName)) { 840 methodOperations.add(new org.ofbiz.minilang.method.entityops.GetRelatedOne(curOperElem, simpleMethod)); 841 } else if ("get-related".equals(nodeName)) { 842 methodOperations.add(new org.ofbiz.minilang.method.entityops.GetRelated(curOperElem, simpleMethod)); 843 } else if ("filter-list-by-and".equals(nodeName)) { 844 methodOperations.add(new org.ofbiz.minilang.method.entityops.FilterListByAnd(curOperElem, simpleMethod)); 845 } else if ("filter-list-by-date".equals(nodeName)) { 846 methodOperations.add(new org.ofbiz.minilang.method.entityops.FilterListByDate(curOperElem, simpleMethod)); 847 } else if ("order-value-list".equals(nodeName)) { 848 methodOperations.add(new org.ofbiz.minilang.method.entityops.OrderValueList(curOperElem, simpleMethod)); 849 850 } else if ("make-value".equals(nodeName)) { 851 methodOperations.add(new org.ofbiz.minilang.method.entityops.MakeValue(curOperElem, simpleMethod)); 852 } else if ("clone-value".equals(nodeName)) { 853 methodOperations.add(new org.ofbiz.minilang.method.entityops.CloneValue(curOperElem, simpleMethod)); 854 } else if ("create-value".equals(nodeName)) { 855 methodOperations.add(new org.ofbiz.minilang.method.entityops.CreateValue(curOperElem, simpleMethod)); 856 } else if ("store-value".equals(nodeName)) { 857 methodOperations.add(new org.ofbiz.minilang.method.entityops.StoreValue(curOperElem, simpleMethod)); 858 } else if ("refresh-value".equals(nodeName)) { 859 methodOperations.add(new org.ofbiz.minilang.method.entityops.RefreshValue(curOperElem, simpleMethod)); 860 } else if ("remove-value".equals(nodeName)) { 861 methodOperations.add(new org.ofbiz.minilang.method.entityops.RemoveValue(curOperElem, simpleMethod)); 862 } else if ("remove-related".equals(nodeName)) { 863 methodOperations.add(new org.ofbiz.minilang.method.entityops.RemoveRelated(curOperElem, simpleMethod)); 864 } else if ("remove-by-and".equals(nodeName)) { 865 methodOperations.add(new org.ofbiz.minilang.method.entityops.RemoveByAnd(curOperElem, simpleMethod)); 866 } else if ("clear-cache-line".equals(nodeName)) { 867 methodOperations.add(new org.ofbiz.minilang.method.entityops.ClearCacheLine(curOperElem, simpleMethod)); 868 } else if ("clear-entity-caches".equals(nodeName)) { 869 methodOperations.add(new org.ofbiz.minilang.method.entityops.ClearEntityCaches(curOperElem, simpleMethod)); 870 } else if ("set-pk-fields".equals(nodeName)) { 871 methodOperations.add(new org.ofbiz.minilang.method.entityops.SetPkFields(curOperElem, simpleMethod)); 872 } else if ("set-nonpk-fields".equals(nodeName)) { 873 methodOperations.add(new org.ofbiz.minilang.method.entityops.SetNonpkFields(curOperElem, simpleMethod)); 874 875 } else if ("store-list".equals(nodeName)) { 876 methodOperations.add(new org.ofbiz.minilang.method.entityops.StoreList(curOperElem, simpleMethod)); 877 } else if ("remove-list".equals(nodeName)) { 878 methodOperations.add(new org.ofbiz.minilang.method.entityops.RemoveList(curOperElem, simpleMethod)); 879 880 } else if ("assert".equals(nodeName)) { 881 methodOperations.add(new org.ofbiz.minilang.method.conditional.Assert(curOperElem, simpleMethod)); 882 } else if ("if".equals(nodeName)) { 883 methodOperations.add(new org.ofbiz.minilang.method.conditional.MasterIf(curOperElem, simpleMethod)); 884 } else if ("if-validate-method".equals(nodeName)) { 885 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfValidateMethod(curOperElem, simpleMethod)); 886 } else if ("if-instance-of".equals(nodeName)) { 887 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfInstanceOf(curOperElem, simpleMethod)); 888 } else if ("if-compare".equals(nodeName)) { 889 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfCompare(curOperElem, simpleMethod)); 890 } else if ("if-compare-field".equals(nodeName)) { 891 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfCompareField(curOperElem, simpleMethod)); 892 } else if ("if-regexp".equals(nodeName)) { 893 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfRegexp(curOperElem, simpleMethod)); 894 } else if ("if-empty".equals(nodeName)) { 895 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfEmpty(curOperElem, simpleMethod)); 896 } else if ("if-not-empty".equals(nodeName)) { 897 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfNotEmpty(curOperElem, simpleMethod)); 898 } else if ("if-has-permission".equals(nodeName)) { 899 methodOperations.add(new org.ofbiz.minilang.method.ifops.IfHasPermission(curOperElem, simpleMethod)); 900 } else if ("check-permission".equals(nodeName)) { 901 methodOperations.add(new org.ofbiz.minilang.method.ifops.CheckPermission(curOperElem, simpleMethod)); 902 } else if ("check-id".equals(nodeName)) { 903 methodOperations.add(new org.ofbiz.minilang.method.ifops.CheckId(curOperElem, simpleMethod)); 904 } else if ("else".equals(nodeName)) { 905 } else if ("property-to-field".equals(nodeName)) { 907 methodOperations.add(new org.ofbiz.minilang.method.otherops.PropertyToField(curOperElem, simpleMethod)); 908 } else if ("calculate".equals(nodeName)) { 909 methodOperations.add(new org.ofbiz.minilang.method.otherops.Calculate(curOperElem, simpleMethod)); 910 } else if ("log".equals(nodeName)) { 911 methodOperations.add(new org.ofbiz.minilang.method.otherops.Log(curOperElem, simpleMethod)); 912 913 } else { 914 Debug.logWarning("Operation element \"" + nodeName + "\" no recognized", module); 915 } 916 } 917 } 918 } 919 920 923 public static boolean runSubOps(List methodOperations, MethodContext methodContext) { 924 Iterator methodOpsIter = methodOperations.iterator(); 925 while (methodOpsIter.hasNext()) { 926 MethodOperation methodOperation = (MethodOperation) methodOpsIter.next(); 927 try { 928 if (!methodOperation.exec(methodContext)) { 929 return false; 930 } 931 } catch (Throwable t) { 932 String errMsg = "Error in simple-method operation [" + methodOperation.rawString() + "]: " + t.toString(); 933 Debug.logError(t, errMsg, module); 934 throw new RuntimeException (errMsg); 935 } 936 } 937 return true; 938 } 939 } 940 | Popular Tags |