Screen Layout Action Scripts
Allow you to extend the Javascript you right in a Screen Layout to call into server side Java code. This can be necessary if you need to securely access a third party system and don't want access tokens exposed to Javascript. It also allows to take advantage of Java's more powerful programming constructs (not to mention the better error handling and compile errors rather than runtime errors).
To create and user a Screen Layout Action scripts requires two components:
- Javascript in your Screen Layout to call the Action script (and possibly get a response).
- A Java Action Script to handle the request.
Example Javascript:
/** * Create a callback function that is called when the remote action script completes */ $.njDialerPlugin.callback = function (jsonResponse) { // parse the response into a local object. var response = JSON.parse(jsonResponse); if (reponse.code == "OK") alert("Selected Customer:" + response.customer); else alert("An Error Occurred:" + response.error); } /** * Call the server side Action Script. $.njDialerPlugin.getCustomer = function() { $.njDialerPlugin.callActionScript('getCustomer',"Noojee Pty Ltd", $.njDialerPlugin.callback); }; /* * Need to wire up a button to make the call to the action script. * You should also wire the button during the 'onConnect' method. */ $.njDialerPlugin.onPreview = function() { // Wire the client search button. Start by making certain we are doing this twice. $("#btnFetch").unbind('click'); $('#btnFetch').click(function() { // add your action here. $.njDialerPlugin.getCustomer(); }); };
The required Action Script:
package script; import au.com.noojee.vaadin.views.scripts.BaseScreenLayoutAction; import au.com.noojee.vaadin.views.scripts.JSCallback; import elemental.json.JsonObject; import elemental.json.JsonArray; import au.com.noojee.vaadin.views.scripts.ScreenLayoutAction; import com.vaadin.ui.Notification; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.text.ParseException; import java.util.List; import java.util.Map; import java.util.LinkedList; import java.lang.reflect.Type; import com.google.gson.reflect.TypeToken; import com.google.gson.Gson; import java.text.SimpleDateFormat; import java.util.Date; public class getCustomer extends BaseScreenLayoutAction { @Override public void exec(String argument,Map<String,String> fieldValues, JSCallback jsCallback) throws Exception { String searchFilter = argument; // Init api SomeExternalApi api = SomeExternalApi.init(); Customer customer = api.getCustomer(searchFilter); Response response = new Response(); if (customer == null) { response.code = "Error"; response.error = "Customer Not found"; } else { response.code = "OK"; response.customer = customer; } // Send the data back to the javascript callback. jsCallback.call(response); } static class Response { String code; String error; Customer customer; } }
Noojee Also provides a method for the server base Action script to present a list to the user for them to select from:
package script; import au.com.noojee.vaadin.views.scripts.BaseScreenLayoutAction; import au.com.noojee.vaadin.views.scripts.JSCallback; import elemental.json.JsonObject; import elemental.json.JsonArray; import au.com.noojee.vaadin.views.scripts.ScreenLayoutAction; import com.vaadin.ui.Notification; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.text.ParseException; import java.util.List; import java.util.Map; import java.util.LinkedList; import com.google.gson.reflect.TypeToken; import com.google.gson.Gson; import java.text.SimpleDateFormat; import java.util.Date; public class SearchForPractitionerAction extends BaseScreenLayoutAction { @Override public void exec(String argument,Map<String,String> fieldValues, JSCallback jsCallback) throws Exception { // Init api SomeExternalAPI api = SomeExternalAPI.getApi(); List<ClientGroup> clientGroup = api.getClientGroups(); presentSelectionList("Select One",ClientGroup.class,clientGroup,jsCallback); } }
presentSelectionList("Select One",ClientGroup.class,clientGroup,jsCallback);