Access to add and change pages is restricted. See: https://cwiki.apache.org/confluence/display/OFBIZ/Wiki+access

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

In progress of being transferred to the Wiki

Table of Contents

Introduction

he Mini-Language concept in Open For Business is similar to the Gang of Four Interpreter pattern, or the Mark Grand Little Language pattern. This is also the central theme of the Building Parsers with Java book by Steven John Metsker which the OFBiz Rule Engine is based on. The idea is to create simple languages that simplify complex or frequently performed tasks.

In business software there are things that are done hundreds or thousands of times in a single application that a Mini-Language can simplify to the point of cutting implementation and maintenance times not by just 50%, but often as much as 70-90%. Yes, certain tasks will take only 10% of the effort and knowledge to perform. This makes it easier people not familiar with the software to manipulate existing or build new functionality.

Mini-Languages tend to have instructions that are more like method calls in a general purpose language. They are meant to solve a specific problem in a specific context, and are generally worthless or need to be modified for other contexts or problems.

Often this idea is implemented using a free form (BNF) or english-like syntax. In Open For Business the Mini-Languages are expressed as XML files to simplify the learning and manipulation of the syntax, in addition to making the Mini-Languages easier to write and extend.

The DTD for the simple-map-processor and simple-method XML files is in the distribution in ofbiz/core/docs/xmldefs/ofbiz/simple-methods.dtd or on the website at ofbiz.apache.org/dtds/simple-methods.dtd. These are combined into a single DTD to make it easy to use inlined simple-map-processors inside a simple-method.

To specify the DOCTYPE for a simple-map-processors XML file use the following:

<!DOCTYPE simple-map-processors PUBLIC "-//OFBiz//DTD Simple Methods//EN" "http://ofbiz.apache.org/dtds/simple-methods.dtd">

To specify the DOCTYPE for a simple-methods XML file use the following:

<!DOCTYPE simple-methods PUBLIC "-//OFBiz//DTD Simple Methods//EN" "http://ofbiz.apache.org/dtds/simple-methods.dtd">

The only difference between the two DOCTYPE definitions is the root node to use from the DTD.

The Simple Map Processor Mini-Language

  • Simple Map Processor Overview
  • Make In String Operations
  • Process Field Operations
  • Simple Map Processors Example

Simple Map Processor Overview

The Simple Map Processor Mini-Language performes two primary tasks: validation and conversion. It does this in a context of moving values from one Map to another. The input map will commonly contain Strings, but can contain other object types like Integer, Long, Float, Double, java.sql.Date, Time, and Timestamp.

NOTE: The reference information for the simple-map-processor has been moved to annotations in the simple-methods.xsd file.

Simple Map Processors Example

<!DOCTYPE simple-map-processors PUBLIC "-//OFBiz//DTD Simple Methods//EN" "http://ofbiz.apache.org/dtds/simple-methods.dtd">

<simple-map-processors>
<simple-map-processor name="update">
<make-in-string field="estimatedStartDate">
<in-field field="estimatedStartYear"><constant>-</constant>
<in-field field="estimatedStartMonth"><constant>-</constant>
<in-field field="estimatedStartDay"><constant>T</constant>
<in-field field="estimatedStartHour"><constant>:</constant>
<in-field field="estimatedStartMinute"><constant>:</constant>
<in-field field="estimatedStartSecond">
</make-in-string>
<process field="workEffortId"><copy replace="false"/></process>
<process field="scopeEnumId"><copy/></process>
<process field="currentStatusId"><copy/><not-empty><fail-message message="Status is missing."/></not-empty></process>
<process field="priority"><convert type="Long"><fail-message message="Priority is not a valid whole number."/></convert></process>
<process field="estimatedStartDate">
<compare-field operator="less" field="estimatedCompletionDate" type="Timestamp" format="yyyy-MM-dd'T'HH:mm:ss">
<fail-message message="Estimated Start date/time must be BEFORE End date/time."/></compare-field>
<convert type="Timestamp" format="yyyy-MM-dd'T'HH:mm:ss">
<fail-message message="Estimated Start Date is not a valid Date-Time."/></convert></process>
<process field="estimatedCompletionDate">
<convert type="Timestamp"><fail-message message="Estimated Completion Date is not a valid Date-Time."/></convert></process>
<process field="estimatedMilliSeconds">
<convert type="Double"><fail-message message="Estimated Milli-seconds is not a valid number."/></convert></process>
</simple-map-processor>
<simple-map-processor name="delete">
<process field="workEffortId"><copy/><not-empty><fail-message message="Work Effort ID is missing."/></not-empty></process>
</simple-map-processor>
</simple-map-processors>

The Simple Method Mini-Language

  • Simple Method Overview
  • Special Context Access Syntax
  • Call Operations
  • Java Call Operations
  • Control and Error Handling Operations
  • Event Specific Operations
  • Service Specific Operations
  • Method Environment Operations
  • Entity Engine Misc. Operations
  • Entity Engine Find Operations
  • Entity Engine Value Operations
  • Entity Engine List Operations
  • Entity Engine Transaction Operations
  • Conditional (If) Operations
  • Other Operations
  • Simple Methods Example

Simple Method Overview

The Simple Method Mini-Language is a simple way to implement an event that is invoked by the Control Servlet or a service that is invoked by the Service Engine. A Simple Method can be invoked through the static methods on the SimpleMethod class, or as an event through an entry in the controller configuration XML file like the following:

 <event type="simple" path="org/ofbiz/commonapp/workeffort/workeffort/WorkEffortSimpleEvents.xml" invoke="update"/>

or as a service through an entry in a services.xml file like the following:

 <service name="createPartyRole" engine="simple"
location="org/ofbiz/commonapp/party/party/PartyRoleServices.xml" invoke="createPartyRole" auth="true">
<description>Create a Party Role (add a Role to a Party)</description>
<attribute name="partyId" type="String" mode="IN" optional="true"/>
<attribute name="roleTypeId" type="String" mode="IN" optional="false"/>
</service>

The path or location for a Simple Method is the classpath and filename of the XML file.

In this Mini-Language you can invoke Simple Map Processors, Services and bsh scripts, perform entity related operations, and create messages to return to the caller. Specific operations can be enclosed in if blocks to execute conditionally and values or fields can be copied around in the maps, lists and method environment.

There are a number of tags which can be used to get and set attributes to/from a request or session object when called as an event or to set attributes in the result when called as a service. These operations are only applied when applicable. In other words if you include a env-to-request operation it will only be invoked when the simple-method is called as an event and a env-to-result operation will only be invoked when the simple-method is called as a service. Everything else is the same when called as an event or a service which makes it easy to write flexible logic that can be mounted/applied in various ways.

There are a number of objects that exist in the method environment when a simple-method starts or that are used as it executes to keep track of certain information. Some will exist when called as an event or a service, these are marked in the following list. Each name can be overridden using an attribute on the simple-method tag. The defaults are listed below (you can also find them in the DTD).

Here is a complete list of the attributes of the simple-method tag:

Attribute NameReqd?Default ValueDescription
method-nameYN/AA name (preferably a legal Java identifier) for this method. This name must be unique for the XML file it is in as it will be used to reference this method externally.
short-descriptionYN/AA short description of the method for certain system error messages.
login-requiredN"true"Is a logged in user (UserLogin object, or login.username and login.password Strings) required to run this method? "true" or "false".
use-transactionN"true"Create a transaction if none exists for this thread? "true" or "false".
default-error-codeN"error"The default error return code.
default-success-codeN"success"The default success return code.
parameter-map-nameN"parameters"As event: copy of request parameters
As service: incoming context
event-request-object-nameN"request"(as event only)
event-response-code-nameN"_response_code_"(as event only)
event-error-message-nameN"_error_message_"(as event only)
event-event-message-nameN"_event_message_"(as event only)
service-response-message-nameN"responseMessage"(as service only)
service-error-message-nameN"errorMessage"(as service only)
service-error-message-list-nameN"errorMessageList"(as service only)
service-success-message-nameN"successMessage"(as service only)
service-success-message-list-nameN"successMessageList"(as service only)
delegator-nameN"delegator"A GenericDelegator object to use in the method
security-nameN"security"A Security object to use in the method
dispatcher-nameN"dispatcher"A LocalDispatcher object to use in the method

Special Context Access Syntax

In strings and field names a special syntax is supported to flexibly access Map member, List elements and to insert environment values into string constants.

The ${} (dollar-sign-curly-brace) syntax can be used to insert an environment variable value in pretty much any string constant in a simple-method file. Not only can it be used to reference top-level envrionment variables, the syntax elements described below can be used to access values in sub-structures.

You can use the "." (dot) syntax to access Map members. For example if you specify the attribute field-name="product.productName" it will reference the productName member of the product Map. This would be the same as specifying map-name="product" field-name="productName". Note that this is, of course, more flexible than a field-name/map-name combination because the Map structure can be multiple levels deep. For example if you have use the attribute field-name="products.widget.productName" it will reference the productName in the widget Map which is in the products Map.

The "[]" (square-brace) syntax can be used to access list elements. For example you can specify the attribute field-name="products[0].productName" and it will reference the productName of the first (position zero) element in the products List. To make this more useful you can pull a list index from the environment using something like field-name="products[${currentIndex}].productName".

There are two extensions to the [] syntax that can be used when refering to an environment location that is the target of an operation. If you do not include a number between the square braces the value will be put at the end of the list. If you put a "+" (plus sign) in front of the number between the square braces (ie: [+2]) it will insert the value before that position in the list instead of replacing the value at that location. For example, specifying [+0] would insert the value at the beginning of the list.

In fact, you can use the ${} syntax to substitute any string or other value at any location in a field-name or other string constant. So, you could even reference a Map member named in some other environment variable. For example you could use field-name="products[${currentIndex}].${currentMember}" to achieve this effect.

Okay, enough of the general stuff, here is a catalog and description of the available operations.

Call Operations

call-map-processor

The call-map-processor tag invokes a simple map processor from an existing map, creating a new map or adding to an existing one if the named out-map already exists. Resulting messages are added to the named list, and a new list is created if a list with the given name does not yet exist. Note that all lists and maps exist in the same context and must have unique names.

An inline simple-map-processor can be used by putting a simple-map-processor tag under the call-map-processor tag. If both an external and an inline map-processor are specified, the external one will be called first, allowing the inline one to override its behavior.

Attribute NameRequired?Description
xml-resourceNThe full path and filename on the classpath of the XML file which contains an external map processor to execute. This is only required if an external map processor is desired.
processor-nameNThe name of the external map processor to execute in the specified xml-resource. This is only required if an external map processor is desired.
in-map-nameYThe name of a map in the method environment to use as the input map.
out-map-nameYThe name of a map in the method environment to use as the output map. Will be created if it does not exist already. If already exists will be added to in place.
error-list-nameNThe name of a list in the method environment that the error messages will be added to. Will be created if it does not exist. Defaults to "error_list".
Sub-Element NameHow ManyDescription
simple-map-processor0 or 1Uses the same definition as the simple-map-processor in the context of a simple-map-processors XML file. Allows for an inlined simple-map-processor.

call-service

The call-service tag invokes a service through the Service Engine. If the specified error code is returned from the service, the event is aborted and the transaction in the current thread is rolled back. Otherwise, the remaining operations are invoked.

The result-to-request and result-to-session elements will be IGNORED when called in a service context. So, they are ONLY used when called in an event context.

Attribute NameRequired?Description
service-nameY 
in-map-nameY 
include-user-loginNDefaults to "true".
error-codeNDefaults to "error".
success-codeNDefaults to "success".
Sub-Element NameHow ManyDescription
error-prefix0 or 1 
error-suffix0 or 1 
success-prefix0 or 1 
success-suffix0 or 1 
message-prefix0 or 1 
message-suffix0 or 1 
default-message0 or 1 
results-to-map0 to many 
result-to-field0 to many 
result-to-request0 to many 
result-to-session0 to many 
result-to-result0 to many 

call-service-asynch

Calls a service asynchronously and ignores the result, so no return messages are used; that doesn't mean no errors will result, but they would just be system errors like database failures, etc which all have system error messages.

Attribute NameRequired?Description
service-nameY 
in-map-nameY 
include-user-loginNDefaults to "true".

call-bsh

Runs an external bsh script from the classpath if resource is specified and then runs the inlined bsh script if any is specified.

The bsh context is the current simple-method environment including maps, lists and special objects whose names are defined in the simple-method attributes.

The current env cannot be modified, but if a Map is returned by the bsh block the entries in the map will be put into the current env.

Error messages go on the error list and are handled with the check-errors tag.

Attribute NameRequired?Description
resourceN 
error-list-nameNDefaults to "error_list".

call-simple-method

The call-simple-method tag calls another simple-method in the same context as the current one. In other words the called simple-method will have the same environment as the calling simple-method, including all environment fields, and either the event or service objects that the calling simple-method was called with.

Attribute NameRequired?Description
xml-resourceNThe full path and filename on the classpath of the XML file which contains an external simple-method to execute. This is only required if a simple-method in a different file is desired.
method-nameYThe name of the simple-method to execute in the specified xml-resource, or in the current XML file if no xml-resource is specified.

Java Call Operations

create-object

Creates an object of the given class and if the field-name is specified saves it in that field.

The string and field sub-elements are passed to the constructor method as arguments in the order they are specified. If the sub-elements do not match the constructor method arguments an error will be returned.

Attribute NameRequired?Description
class-nameYThe name of the class to construct an object of.
field-nameNThe name of a field to put the new object in. If not specified the object will be created but ignored after that.
map-nameNThe name of the map the field will go in. If not specified the field will be put in the environment.
Sub-Element NameHow ManyDescription
string0 to manyUsed to specify an inline String argument to the constructor method.
field0 to manyUsed to specify a field to be passed as an argument to the constructor method. The field can be in a map in the environment or if no map-name is specified then the field will come directly from the environment.

call-object-method

Calls a method on an existing object that exists in a field in the environment or in a map in the environment.

The string and field sub-elements are passed to the method as arguments in the order they are specified. If the sub-elements do not match the method arguments an error will be returned.

The return value will be put in the named field if an value is returned and if a field and optionally a map name are specified.

Attribute NameRequired?Description
obj-field-nameYThe name of the field the object is in that has the method to be called.
obj-map-nameNThe name of the map the field of the object is in that has the method to be called. If this is not specified the environment will be used to find the field in.
method-nameYThe name of the method to call on the given object.
ret-field-nameNThe name of the field to put the result in. If not specified any return value will be ignored.
ret-map-nameNThe name of the map the field of the return value is in. If not specified but the field name is then the environment will be used to find the field in.
Sub-Element NameHow ManyDescription
string0 to manyUsed to specify an inline String argument to the method call.
field0 to manyUsed to specify a field to be passed as an argument to the method call. The field can be in a map in the environment or if no map-name is specified then the field will come directly from the environment.

call-class-method

Calls a static method on a class.

The string and field sub-elements are passed to the method as arguments in the order they are specified. If the sub-elements do not match the method arguments an error will be returned.

The return value will be put in the named field if an value is returned and if a field and optionally a map name are specified.

Attribute NameRequired?Description
class-nameYThe name of the class to call the static method on.
method-nameYThe name of the static method to call on the given class.
ret-field-nameNThe name of the field to put the result in. If not specified any return value will be ignored.
ret-map-nameNThe name of the map the field of the return value is in. If not specified but the field name is then the environment will be used to find the field in.
Sub-Element NameHow ManyDescription
string0 to manyUsed to specify an inline String argument to the method call.
field0 to manyUsed to specify a field to be passed as an argument to the method call. The field can be in a map in the environment or if no map-name is specified then the field will come directly from the environment.

Control and Error Handling Operations

check-errors

The message lists from invoking are not checked until the check-errors tag is used. The named list is checked and if it contains any messages they are put in the servlet request object and the specified error code is returned to the control servlet.

Attribute NameRequired?Description
error-codeNDefaults to "error".
error-list-nameNThe name of the list to in the method environment to check for error messages. Defaults to "error_list".
Sub-Element NameHow ManyDescription
error-prefix0 or 1 
error-suffix0 or 1 
message-prefix0 or 1 
message-suffix0 or 1 

add-error

Adds an error message with to the given error list from either an inline message or a message from a properties file

Attribute NameRequired?Description
error-list-nameNThe name of the list to in the method environment to check for error messages. Defaults to "error_list".
Sub-Element NameHow ManyDescription
fail-message0 or 1Used to specify an inline message. Has one attribute called 'message'.
fail-property0 or 1Used to get the message from a properties file. Has two attributes: resource and property.

return

Returns immediately from the simple-method with the given response code string.

Attribute NameRequired?Description
response-codeNThe string to return as a response code. Defaults to "success".

Event Specific Operations

field-to-request

The field-to-request tag copies a field from a map to the specified servlet request attribute.

The tag is only used when the simple-method is called as an event, it is ignored otherwise.

Attribute NameRequired?Description
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to get the field from the method environment.
field-nameYThe name (key) of the map field to use.
request-nameNThe name of the request attribute to use. Defaults to the field-name.

field-to-session

The field-to-session tag copies a field from a map to the specified servlet session attribute.

The tag is only used when the simple-method is called as an event, it is ignored otherwise.

Attribute NameRequired?Description 
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to get the field from the method environment. 
field-nameYThe name (key) of the map field to use. 
session-nameNThe name of the session attribute to use. Defaults to the field-name. 

request-to-field

The request-to-field tag copies an attribute from the servlet request to a field of a map in the method environment.

The tag is only used when the simple-method is called as an event, it is ignored otherwise (except the default value, if specified).

Attribute NameRequired?Description
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to set the field in the method environment.
field-nameYThe name (key) of the map field to use.
defaultNA default value to use if the request attribute is null or is a String and is empty. This will also be used when NOT running as an event.
request-nameNThe name of the request attribute to use. Defaults to the field-name.

request-parameters-to-list

The request-parameters-to-list tag appends a request parameter values from the servlet request to the specified list.

The tag is only used when the simple-method is called as an event, it is ignored otherwise.

Attribute NameRequired?Description
request-nameYThe name of the request parameter values to use.
list-nameNThe name of the list in the method environment that the request parameter values will be appended to. (default: request-name)

session-to-field

The session-to-field tag copies an attribute from the servlet session to a field of a map in the method environment.

The tag is only used when the simple-method is called as an event, it is ignored otherwise (except the default value, if specified)

Attribute NameRequired?Description
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to set the field in the method environment.
field-nameYThe name (key) of the map field to use.
defaultNA default value to use if the session attribute is null or is a String and is empty. This will also be used when NOT running as an event.
session-nameNThe name of the session attribute to use. Defaults to the field-name.

webapp-property-to-field

The webapp-property-to-field tag copies a property value from a properties file in a ServletContext resource to a field.

The tag is only used when the simple-method is called as an event, it is ignored otherwise (except the default value, if specified).

Attribute NameRequired?Description
resourceYThe resource location of the properties file inside the webapp, and relative to the root of the webapp (can be inside a war file). An example of this is "/WEB-INF/myprops.properties".
propertyYThe property whose value will be put in the field.
defaultNA default value to use if the property value is null or empty. This will also be used when NOT running as an event.
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to set the field in the method environment.
field-nameYThe name (key) of the map field to use.

Service Specific Operations

field-to-result

The field-to-result tag copies a field from a map to the specified service result field.

The tag is only used when the simple-method is called as a service, it is ignored otherwise.

Attribute NameRequired?Description
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to get the field from the method environment.
field-nameYThe name (key) of the map field to use.
result-nameNThe name of the request attribute to use. Defaults to the field-name.

Method Environment Operations

map-to-map

The map-to-map tag copies all fields from one map to another map

Attribute NameRequired?Description
map-nameYThe name of the map in the method environment the fields will come from.
to-map-nameNThe name of the map in the method environment the fields will go to. If empty the fields will be copied to the environment.

field-to-field

The field-to-field tag copies a field from a map to a field in another map.

Attribute NameRequired?Description
map-nameNThe name of the map in the method environment the field will come from. If this is null or empty it will default to the method environment itself.
field-nameYThe name (key) of the map field to copy.
to-map-nameNThe name of the map in the method environment the field will go to. If empty will default to the map-name. Note that if no map-name was specified this will default to the method environment. So, you can copy from the environment to the environment or from the environment to a map, but to copy from a map to the environment you must use the field-to-env operation defined below.
to-field-nameNThe name (key) of the map field to put the original field in. If empty will default to the field-name.

field-to-list

The field-to-list tag appends a field to the specified list

Attribute NameRequired?Description
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to get the field from the method environment.
field-nameYThe name (key) of the map field to use.
list-nameYThe name of the list in the method environment that the object will be appended to.

env-to-field

The env-to-field tag copies a field from the method environment to a field in a map.

Attribute NameRequired?Description
env-nameYThe name of the method environment field the object will come from.
map-nameNThe name of the map in the method environment the field will go to. If empty or not specified this will default to the method environment itself.
field-nameNThe name (key) of the map field to put the original field in. If empty will default to the env-name.

field-to-env

The field-to-env tag copies a field from a map to a field in the method environment.

Attribute NameRequired?Description
field-nameYThe name (key) of the map field to get the object from.
env-nameNThe name of the method environment field the object will be put in. If empty will default to the field-name.
map-nameNThe name of the map in the method environment the field will come from. If empty or not specified this will default to the method environment itself

string-to-field

The string-to-field tag puts the inlined string value in the specified field.

Attribute NameRequired?Description
stringYThe string value to be put in the field.
map-nameNThe name of the map in the method environment. If not specified the field-name will be used to get the field from the method environment.
field-nameYThe name (key) of the map field to use.
arg-list-nameNThe name (key) of a list to use for argument values for any place holders in the string. Formatting is done with the Java MessageFormat class. Place holders, for example, are denoted as follows: {0}, {1}, etc and can appear anywhere in the string.

string-to-list

The string-to-list tag appends the inlined string value in the specified List.

  • No labels