The include directive in the Script Unit
From WebRatio WebML Wiki
|
The #include directive feature enables the inclusion of "library" scripts in a Groovy script.
The term "inclusion" means that all the code of the included script is copied exactly in the place in which there is the directive. Consequently, if the included script declares variables, multiple inclusion of the same script causes error, because the same variable is declared different times. To include a script, the file must be stored in the "WebContent/WEB-INF/groovy" folder. There are different syntaxes:
//inclusion of the Function.groovy file stored in the "helpers" folder #include helpers.Functions.groovy #include helpers/Functions.groovy #include helpers.Functions // <-- Preferred (allows the use of code completion for included script variables) #include helpers/Functions
When the content assist is invoked in a point of the script following one of #include directive, all variables and functions imported from the included script are available among the proposals. It's possible that a included script includes itself another script.
Let's see an example. Suppose you want to create a script that given two dates, calculates the difference in minutes between them. You can use a simple Web Model like the one in the image on the right to test the script.- In the WebRatio Explorer create the folder "WebContent/WEB-INF/lib"
- Download the "Joda Time" library from this page
- Extract the zip content and copy the jar file in the "WebContent/WEB-INF/lib" folder
- Add a Page names "Script Unit #include TEST"
- Add an Entry Unit to the page and name it "Event"
- Add two date Fields to the Entry Unit ("Start Date" and "End Date")
- Add a MultiMessageUnit to the page and name it "Difference in Minutes"
- Add a Script Unit outside the page and name it "Calculate Dates Interval"
- In the WebRatio Explorer create the folder "WebContent/WEB-INF/groovy"
- Create a new file "loadPatterns.groovy" in the just created folder
- Write the following Groovy code in the file
import com.webratio.rtx.RTXLocalizationService
import java.util.Locale
import org.apache.struts.Globals
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpSession
import com.webratio.rtx.RTXConstants
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
//retrieve the current Locale using the HttpRequest
HttpServletRequest request = localContext.get(RTXConstants.HTTP_SERVLET_REQUEST_KEY)
HttpSession session = request.getSession()
Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY)
//retrieve the current localization pattern for date, time and timestamp
RTXLocalizationService localizationService = rtx.getLocalizationService();
def datePattern = localizationService.getPattern("date",locale);
def timePattern = localizationService.getPattern("time",locale);
def timestampPattern = localizationService.getPattern("timestamp",locale);
//create the formatter using the found patterns
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(datePattern)
DateTimeFormatter timeFormatter = DateTimeFormat.forPattern(timePattern)
DateTimeFormatter timestampFormatter = DateTimeFormat.forPattern(timestampPattern)
//create the neutral formatter for all data types
DateTimeFormatter neutralDateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd")
DateTimeFormatter neutralTimeFormatter = DateTimeFormat.forPattern("HH:mm:ss")
DateTimeFormatter neutralTimestampFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
- Create a new file "datesDifference.groovy" in the WebContent/WEB-INF/groovy
- Write this code in the file
#input String startDate, String endDate import org.joda.time.DateTime import org.joda.time.Period import org.joda.time.PeriodType //include of the code that loads all patterns #include loadPatterns //create date objects starting from string DateTime start = neutralDateFormatter.parseDateTime(startDate) DateTime end = neutralDateFormatter.parseDateTime(endDate) //calculate difference between dates def duration = new Period(start, end, PeriodType.minutes()) return duration.getMinutes()
N.B. Starting from WebRatio 5.1.1 version it's possible to declare Typed Inputs and Outputs. See this article for further details. According to this, the first line becomes #inputs String startDate, String endDate
- Select the Script Unit and associate the "datesDifference.groovy" to it
- Connect the Entry Unit and the Script Unit with a normal link named "Calculate Difference". Open the Coupling Dialog, remove the flag on the "Enable Default Coupling" option and press the Guess Coupling button. Then click on the OK button
- Connect the Script Unit and the MultiMessageUnit with an OK Link. Open the Coupling Dialog, remove the flag on the "Enable Default Coupling" option and couple the "Result" output parameter of the Script Unit with the "Shown Messages" input parameter of the MultiMessageUnit.
In the generated Web application you can see that, after submitting the form with two different dates, in the MultiMessageUnit there's the difference between them in minutes.

