Date: Fri, 29 Mar 2024 03:25:17 -0600 (MDT) Message-ID: <275585961.2805.1711704317016@confluence-external-wiki> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2804_312631920.1711704317016" ------=_Part_2804_312631920.1711704317016 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The functions described here are typically used when working with code w= idgets, which further extend the fu= nctionality and flexibility of Yellowfin. These can be added to the dashboa= rd by importing them through the plugin manager. They will then appear in t= he list of code widgets for selection during the dashboard building process= . Take a look at the guide to creating a code widget for further information and reso= urces.
At certain times during code widget development, you may need to p= ass data to the Yellowfin server. This can be achieved by using the = WidgetAPI.serverRequest function. This makes a = call to the Java class associated with the code widget.
myWidget.js
let widge= tAPI =3D options.api.widget; widgetAPI.serverRequest('hello').then(result =3D> { alert(result.hello); });
= MyWidgetImplementation.java
= public void doRequest(CodeWidgetActionRequest request, CodeWidgetActionResp= onse response) { =20 if("hello".equals(request.getAction())) { response.addData("hello", "Hello JavaScript!"); } }
When a request is made from the front-end component of a code widg= et, a CodeWidgetActionRequest object is created. This object contains the d= ata which was added to that request.
This method will match the string that was passed to the WidgetAPI= .serverRequest function. It should be used to determine which actions your = code widget should be undertaking.
For example, when a request from the front end is made like this:<= /span>
widgetAPI= .serverRequest(=E2=80=9Chello=E2=80=9D);
getAction() will return the string =E2=80=9C= hello=E2=80=9D.
This method returns a string version of a parameter included in th= e JavaScript actionData object. If the p= assed parameterName does not match any parameters in the request, null will= be returned.
widgetAPI= .serverRequest(=E2=80=9Chello=E2=80=9D, { name: =E2=80=9CCode Widget=E2=80= =9D });
When request.getParameter(=E2=80=9Cname=E2=80=9D)= is called, it will return =E2=80=9CCode Widget=E2=80=9D.=
This function returns the value of the passed par= ameterName as the passed Class type. For example:<= /p>
Integer c= ounter =3D getParameterAsObject(=E2=80=9Ccounter=E2=80=9D, Integer.class);<= /pre>
Calling the above code returns an integer. If the value cannot be = parsed, null will be returned.
Or, for example,
WidgetAPI= .serverRequest(=E2=80=98example=E2=80=99, { counter: 1, myClass: { =E2=80=98one=E2=80=99: =E2= =80=98one=E2=80=99, =E2=80=98two=E2=80=99: =E2= =80=98two=E2=80=99 } });
When the request above is made from the front end, these can be re= trieved directly as objects by doing the following in Java:
class M= yClass { private String one =3D null= ; private String two =3D null= ; public MyClass(String one, = String two) { &nb= sp; this.one =3D one; &nb= sp; this.two =3D two; } public String getOne() { ret= urn one; } public String getTwo() { ret= urn two; } } Integer counter =3D request.getParameterAsObject(=E2=80=98counter=E2=80=99,= Integer.class); MyClass myClass =3D request.getParameterAsObject(=E2=80=98myClass=E2=80=99,= MyClass.class); System.out.println(myClass.getTwo()); //Prints =E2=80=9Ctwo=E2=80=9D
In this example, the variable counter= will be parsed as an integer and MyClass will have the values mapped from the JSON to its corresponding priv= ate member variables.
Using this method has a limitation when generics are used, as type= safety cannot be guaranteed. If you are attempting to parse an object usin= g generics, we recommend you use getParameterAsObject(Stri= ng, Type) instead.
This function attempts to parse the passed parameter key using the= passed Type parameter.
This function works similarly to <T> getPar= ameterAsObject(String parameterName, Class<T> clazz), but it lets you define a Type object, which is more suitable when using = generics because you can use classes that support generics.
List<= ;String> myList =3D getParameterAsObject(=E2=80=9CmyList=E2=80=9D, new T= ypeToken<List<String>>() {}.getType());
Using this function introduces a d= ependency on gson, which is shipped with Yellowfin, but is not documented o= n this wiki.
If you're not familiar with gson a= nd wish to avoid using it, an alternative option is to get the String versi= on and then parse it.
This object represents the response that will be sent back to the = JavaScript component of the code widget.
Any data that you wish to return to the JavaScript can be added to= this, and the JavaScript will receive this response as a simple JSON objec= t.
This function adds the passed parameter and value to an Object to = be returned to the JavaScript. For example:
respons= e.addData(=E2=80=98greeting=E2=80=99, =E2=80=98Hello World=E2=80=99);
If the Java text above is used, the parameter and value can be acc= essed by using the JavaScript:
widget.se= rverRequest(=E2=80=98greeting=E2=80=99).then(result =3D> { alert(result.greeting); })
The objects that are passed in can be much more complex than simpl=
e strings. Any Java class can be added to this. If you add an implementatio=
n of a Java class to any publicly-accessible getters, like getProperty or isProperty, they will be added to the JSON data. When this happens, the first let=
ter after =E2=80=9Cget=E2=80=9D will be changed to lowercase in the generat=
ed JSON. For example:
getProperty becomes property= span>
isAProperty becomes aProperty
The following class:
public = Class MyExampleClass { public String getSayHello() { Return "Hello" } public String helloWorld() { return "Hello World"; } =20 public boolean isCodeWidget() { return true; } }
...when added to the response:
respons= e.addData("exampleClass", new MyExampleClass());
...would create create the follow= ing object:
{ sayHello: "Hello", codeWidget: true }
The widget API provides the functionality to allow the JavaScript = component of a code widget to send messages to the back-end component.
Promise - A promise that is resolved when the code widget request = completes.
The promise will be passed an Object containing a JSON representat= ion of the CodeW= idgetResponse that is created by the backend componen= t.
action - String - The action you wish the server to perform=
actionData - Object - An object containing any data you wish to pa= ss along with the request.
myWidget.js
define(fu= nction() { function MyWidget(options) { let widgetAPI =3D options.apis.widgetAPI;=20 //Trigger a "hello" request to the backend of t= he widget widgetAPI.serverRequest('hello').then(result = =3D> { alert(result.hello); });=20 }=20 return MyWidget; }
MyWidgetImplementation.java<= /span>
= public void doRequest(CodeWidgetActionRequest request, CodeWidgetAct= ionResponse response) { if("hello".equals(request.getAction())) { response.addData("hello", "Hello JavaScript!"); } }
In this example, when MyWidget.js is initialized, it will immediately trigger a =E2=80=9Chello= =E2=80=9D request to the code widget=E2=80=99s Java component. It responds = by adding the key-value pair =E2=80=9Chello=E2=80=9D: =E2=80=9CHello JavaSc= ript=E2=80=9D to the response. Which is then alerted by the JavaScript.&nbs= p;