Example 2: Methods
If you can't play the video, make sure that you are logged in at Wex Solutions.
-
What will we do? - Learn how to implement methods with WEX
-
What do you need? - Site access to a non-production Windchill
-
How long will it take - 8 minutes
This example illustrates the basics regarding method-calling across different Windchill Extensions through the WexInvoker.invoke method.
You can find the code for this example in our Git-Hub repository.
Implementation
The methods are defined as Features in the ExampleMethods.java file.
As seen in the Development section of the Developer's Guide, a Method Feature is declared with the use of the @WexComponent
and @WexMethod
annotations. This will make the WEX Kernel expose the method to Windchill, which will make it callable by other entities in the system.
Execution
To execute the test, deploy the extension to a Windchill server from the Extension Manager. Then go to the extension's User Guide and click the link:
Clicking the link will run the JSP located at wex\jsp\test.jsp
which calls the abovementioned methods through the WexInvoker.invoke command. Said JSP file contains the following code:
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="com.wincomplm.wex.kernel.api.invoke.WexInvoker"%>
<html>
<body style="font-family: Arial, Helvetica, sans-serif;">
<h1>Sample method examples</h1>
<table>
<tr><td><b>Simple call</b></td><td><%= (String) WexInvoker.invoke("com.wincomplm.wex-example-method","methods.example") %></td></tr>
<tr><td><b>Call with argument</b></td><td><%= (String) WexInvoker.invoke("com.wincomplm.wex-example-method","methods.argName","Bob") %></td></tr>
<tr><td><b>Windchill user name</b></td><td><%= (String) WexInvoker.invoke("com.wincomplm.wex-example-method","methods.userName") %></td></tr>
</table>
</body>
</html>
The following pop-up window will appear after clicking on the link:
What is displayed in the window is the result of the three calls made in the JSP file. They are, as you can see by reviewing the code:
- Simple call with no arguments.
- A call where
Bob
is a parameter. - A call that makes a Windchill API call to get the current user.
These are the Features that were defined with the use of the annotations, which can be seen from the Extension Manager:
Command Line
Any command line methods must be implemented as a Feature, due to the code within the WEX being isolated and not available to the Windchill system. If you'd like to learn more about the system's architecture, there's an explanation on it on the Developer Guide.
The following Command Line example, even though basic, covers the general aspects of the implementation process.
First, let's take a look at the code:
- As in any regular .java file, we start declaring the package name.
- The following six imports are necessary for the implementation of the Command Line feature, since they bring in the dependencies and annotations that make the declaration of the Feature and the Command Line parameters possible.
- The class
CmdLineParser
only needs to be brought in when we need to pass arguments to the Command Line, as in this example we did with the variablemsg
. - Notice how there are two implementations of the
execute()
method. In thepublic void execute()
we define the actions to be executed by the command; the naming for this one is optional and can be done to taste.public int execute(String[] args)
, on the other hand, is mandatory, since it needs to@Override
the method with the same name that's declared in theIWexCommandLine
Interface. The purpose of this second method will be to parse the input arguments, when needed, and call the other one.