2022
Now we will discuss the steps for creating a new "Hello World" AppCode. In this example we want to demonstrate how to create one AppCode implementation from scratch. This AppCode class which we are talking about, we will be able to demonstrate the operations which we need to implement as a part of the AppCode implementation.
The HelloWorld implementation class will take the input data as a list of HelloParameter objects and will print it out by prefixing "Hello" to it. The Read operation on it will return an array of pre-defined HelloParameters with their data
1. Open your TestAppCode project which you downloaded as a part of the QuickStart section
2. Define your new HelloWorld AppCode implementation class in the source folder and ensure you are selecting the interface as AppCode while defining the class
You would see a new class created with the default method signatures
Coming to the 3 methods showing up, the following would be the basic functionality
1. Open
This method should open the connection with the 3rd party system and should initiate the session as mentioned earlier. In our case since it is a Hello World and there is no 3rd party system which we are integrating with actually. We will just initialize the session key and print it out to the console and to the log.
@Override public void open() throws RemoteException { // TODO Auto-generated method stub sessionKey = UUID.randomUUID().toString(); logger.log(Level.INFO, "Hello World AppCode Session opened:" + sessionKey); System.out.println("Hello World AppCode Session opened:" + sessionKey); }
2. setContext
This method should initialize the context for the AppCode Implementation and is called before the open method is called. It normally the contains the initialization parameters which are required to initialize the custom implementation.
In our HellWorld we can just log a trace stating setContext is called in the console and in the log
@Override public void setContext(Context arg0) { // TODO Auto-generated method stub logger.log(Level.INFO, "Hello World AppCode Set Context called"); System.out.println("Hello World AppCode Set Context called"); }
3. Close
This method should close and terminate the session and invalidate it. In our case we can invalidate the session-key and log the call
@Override public void close() throws RemoteException { // TODO Auto-generated method stub // TODO Auto-generated method stub logger.log(Level.INFO, "Hello World AppCode close called:" + sessionKey); System.out.println("Hello World AppCode close called:" + sessionKey ); sessionKey = null; }
Now before we start the implementation of the "read" and "write" methods we need to defined the define the data transfer class ( "HelloParameter") for carrying data and out of the AppCode and the Filter class ("HelloFilter")
public static class HelloParameter{ public String id; public String name; } public static class HelloFilter{ public String key; }
Define and initialize the initParameters to be used as HelloParameters for the sample read operation
private List<HelloParameter> initParams ; private void init() { initParams = new ArrayList<HelloWorldAppCode.HelloParameter>(); HelloParameter initparam1 = new HelloParameter(); initparam1.id = "1"; initparam1.name = "Scott"; HelloParameter initparam2 = new HelloParameter(); initparam2.id = "2"; initparam2.name = "Tiger"; HelloParameter initparam3 = new HelloParameter(); initparam3.id = "3"; initparam3.name = "Matt"; initParams.add(initparam1); initParams.add(initparam2); initParams.add(initparam3); }
Now we need to ensure the init is called in the open method so that the parameters are initalized when the AppCode is instantiated and initialized
@Override public void open() throws RemoteException { // TODO Auto-generated method stub init(); sessionKey = UUID.randomUUID().toString(); logger.log(Level.INFO, "Hello World AppCode Session opened:" + sessionKey); System.out.println("Hello World AppCode Session opened:" + sessionKey); }
Now it is time to add the read and write operations
d. ReadHello
In this read method we match the filter passed with that of the initialized parameters in the arraylist and return back the parameter which matches the key and set the value based on the sessionId initialized by prepending it with "Hello" and appending the name: "Hello:"<name>":"<sessionKey>
public List<HelloParameter> readHello(HelloFilter filter){ logger.info("Received Read operation with filter:" + filter.key + ":with session" + sessionKey); System.out.println("Received Read operation with filter:" + filter.key + ":with session" + sessionKey); List<HelloParameter> returnList = new ArrayList<HelloWorldSvc.HelloParameter>(); for(HelloParameter initParam : initParams) { if(filter.key.equalsIgnoreCase(initParam.name)) { //Now set the value to "Hello":name:sessionId initParam.value = "Hello:"+initParam.name+":"+sessionKey; System.out.println("Matched Key:" + initParam.name + ":value:" + initParam.value ); logger.info("Matched Key:" + initParam.name + ":value:" + initParam.value ); returnList.add(initParam); } } return returnList; }
e. WriteHello
In the write method we take the input list and print the value based on the logic explained above after iterating through the list and returns back an array of SaveResults with the newid which contains the sessionKey appended
You can view the code below
public SaveResult[] writeHello(List<HelloParameter> inputParams){ List<SaveResult> saveResults = new ArrayList<SaveResult>(); logger.info("Received write operation with session" + sessionKey); System.out.println("Received write operation with session" + sessionKey); for(HelloParameter inputParam : inputParams) { System.out.println("Received input parameter:id:" + inputParam.id + ":name:" + inputParam.name); logger.info("Received input parameter:id:" + inputParam.id + ":name:" + inputParam.name); inputParam.value = "Hello:"+inputParam.name+":"+sessionKey; System.out.println("Writing value:" + inputParam.value ); logger.info("Writing value:" + inputParam.value); SaveResult result = new SaveResult(); result.id = inputParam.id; result.newid = inputParam.id+":"+sessionKey; result.success = true; saveResults.add(result); } return saveResults.toArray(new SaveResult[] {}); }
Now, we can view the entire class below:
package myappcode; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import com.appmashups.appcode.AppCode; import com.appmashups.appcode.Context; import com.appmashups.appcode.SaveResult; import java.util.List; public class HelloWorldSvc implements AppCode { public static class HelloParameter{ public String id; public String name; public String value; } public static class HelloFilter{ public String key; } private List<HelloParameter> initParams ; private String sessionKey; static Logger logger = Logger.getLogger(HelloWorldSvc.class.getName()); @Override public void close() throws RemoteException { // TODO Auto-generated method stub // TODO Auto-generated method stub logger.log(Level.INFO, "Hello World AppCode close called:" + sessionKey); System.out.println("Hello World AppCode close called:" + sessionKey ); sessionKey = null; } @Override public void open() throws RemoteException { // TODO Auto-generated method stub init(); sessionKey = UUID.randomUUID().toString(); logger.log(Level.INFO, "Hello World AppCode Session opened:" + sessionKey); System.out.println("Hello World AppCode Session opened:" + sessionKey); } @Override public void setContext(Context arg0) { // TODO Auto-generated method stub logger.log(Level.INFO, "Hello World AppCode Set Context called"); System.out.println("Hello World AppCode Set Context called"); } private void init() { initParams = new ArrayList<HelloWorldSvc.HelloParameter>(); HelloParameter initparam1 = new HelloParameter(); initparam1.id = "1"; initparam1.name = "Scott"; HelloParameter initparam2 = new HelloParameter(); initparam2.id = "2"; initparam2.name = "Tiger"; HelloParameter initparam3 = new HelloParameter(); initparam3.id = "3"; initparam3.name = "Matt"; initParams.add(initparam1); initParams.add(initparam2); initParams.add(initparam3); } public List<HelloParameter> readHello(HelloFilter filter){ logger.info("Received Read operation with filter:" + filter.key + ":with session" + sessionKey); System.out.println("Received Read operation with filter:" + filter.key + ":with session" + sessionKey); List<HelloParameter> returnList = new ArrayList<HelloWorldSvc.HelloParameter>(); for(HelloParameter initParam : initParams) { if(filter.key.equalsIgnoreCase(initParam.name)) { //Now set the value to "Hello":name:sessionId initParam.value = "Hello:"+initParam.name+":"+sessionKey; System.out.println("Matched Key:" + initParam.name + ":value:" + initParam.value ); logger.info("Matched Key:" + initParam.name + ":value:" + initParam.value ); returnList.add(initParam); } } return returnList; } public SaveResult[] writeHello(List<HelloParameter> inputParams){ List<SaveResult> saveResults = new ArrayList<SaveResult>(); logger.info("Received write operation with session" + sessionKey); System.out.println("Received write operation with session" + sessionKey); for(HelloParameter inputParam : inputParams) { System.out.println("Received input parameter:id:" + inputParam.id + ":name:" + inputParam.name); logger.info("Received input parameter:id:" + inputParam.id + ":name:" + inputParam.name); inputParam.value = "Hello:"+inputParam.name+":"+sessionKey; System.out.println("Writing value:" + inputParam.value ); logger.info("Writing value:" + inputParam.value); SaveResult result = new SaveResult(); result.id = inputParam.id; result.newid = inputParam.id+":"+sessionKey; result.success = true; saveResults.add(result); } return saveResults.toArray(new SaveResult[] {}); } }
@Test public void testReadHello() { try { HelloWorldSvc svc = new HelloWorldSvc(); svc.setContext(null); svc.open(); HelloFilter filter = new HelloFilter(); filter.key = "Scott"; svc.readHello(filter); svc.close(); } catch (Exception e) { e.printStackTrace(); } }
Now we will test the write operation where we take and Array of HelloParameter objects as input and the write operation prints it out
@Test
public void testWriteHello() {
try
catch (Exception e)
}
The Write operation reads the input parameter array and prints the input parameter by prepending "Hello" to the name and appending the SessionKey to the same
Now that we have developed our HelloWorldSvc AppCode implementation it is time to deploy it to DBSync platform and then running a test workfow.
For your reference the complete code is given below:
package myappcode.test; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import myappcode.HelloWorldSvc; import myappcode.HelloWorldSvc.HelloFilter; import myappcode.HelloWorldSvc.HelloParameter; import org.apache.tools.ant.taskdefs.Concat; import org.junit.Test; import com.appmashups.appcode.Context; public class TestHelloWorldAppCode { //@Test public void testOpen() { try { HelloWorldSvc svc = new HelloWorldSvc(); svc.setContext(null); svc.open(); svc.close(); } catch (Exception e) { e.printStackTrace(); } } //@Test public void testReadHello() { try { HelloWorldSvc svc = new HelloWorldSvc(); svc.setContext(null); svc.open(); HelloFilter filter = new HelloFilter(); filter.key = "Scott"; svc.readHello(filter); svc.close(); } catch (Exception e) { e.printStackTrace(); } } @Test public void testWriteHello() { try { HelloWorldSvc svc = new HelloWorldSvc(); svc.setContext(null); svc.open(); HelloParameter param = new HelloParameter(); param.name = "Test1"; param.id = "id1"; HelloParameter param2 = new HelloParameter(); param2.name = "Test1"; param2.id = "id2"; List<HelloParameter> inputParams = new ArrayList<HelloWorldSvc.HelloParameter>(); inputParams.add(param); inputParams.add(param2); svc.writeHello(inputParams); svc.close(); } catch (Exception e) { e.printStackTrace(); } }