Java Single Connection Example

import lrapi.lr;
import lrapi.vts.Lrvtc;
import lrapi.vts.Constants;
public class Actions {
    private static final String VtsServer = "my_vts_server.my_company.com";
    private static final int nPort = 8890;
    private static int rc = 0;
    public int init() throws Throwable {
        rc = Lrvtc.connect(VtsServer, nPort, Constants.VTOPT_KEEP_ALIVE);
        lr.log_message("rc=" + rc + "\n");
        return 0;
    }// end of init
	
    public int action() throws Throwable {
        String Message = "";
        lr.start_transaction("create_columns");
		
        // ---create columns---
        rc = Lrvtc.create_column("C_ID");
        lr.log_message("create_column rc=" + rc + "\n");
        rc = Lrvtc.create_column("P_ID");
        lr.log_message("create_column rc=" + rc + "\n");
        rc = Lrvtc.create_column("P_name");
        lr.log_message("create_column rc=" + rc + "\n");
        rc = Lrvtc.create_column("P_info");
        lr.log_message("create_column rc=" + rc + "\n");
        rc = Lrvtc.create_column("P_status");
        lr.log_message("create_column rc=" + rc + "\n");
        lr.end_transaction("create_columns", lr.AUTO);
        lr.start_transaction("fill_data");
        for (int i = 1; i <= 10; i++) {
            Message = "product" + i + ";productname" + i + ";productinfo" + i
                    + ";instock";
            rc = Lrvtc.send_row1("P_ID;P_name;P_info;P_status", Message, ";",
                    Constants.VTSEND_SAME_ROW);
            lr.log_message("send_row1 rc=" + rc + "\n");
        }
        lr.end_transaction("fill_data", lr.AUTO);
		
        // -- API column_size
        lr.start_transaction("get_colsize");
        int colsize = 0;
        colsize = Lrvtc.column_size("P_ID");
        lr.log_message("size of column P_ID=" + colsize);
        colsize = Lrvtc.column_size("P_name");
        lr.log_message("size of column P_name=" + colsize);
        colsize = Lrvtc.column_size("P_info");
        lr.log_message("size of column P_info=" + colsize);
        colsize = Lrvtc.column_size("P_status");
        lr.log_message("size of column P_status=" + colsize);
        
        // wrong column name
        colsize = Lrvtc.column_size("P_ID1");
        lr.log_message("size of column P_ID1=" + colsize);
        lr.end_transaction("get_colsize", lr.AUTO);
        // end of API column_size --

        // -- API ensure_index
        lr.start_transaction("create_index");
		
        // empty column
        rc = Lrvtc.ensure_index("C_ID");
        lr.log_message("ensure index of empty column rc=" + rc + "\n");
		
        // non-empty column
        rc = Lrvtc.ensure_index("P_ID");
        lr.log_message("ensure index of non-empty column rc=" + rc + "\n");
        
        // wrong column name
        rc = Lrvtc.ensure_index("P_ID1");
        lr.log_message("ensure index of wrong column rc=" + rc + "\n");
        lr.end_transaction("create_index", lr.AUTO);

        // end API ensure_index --
		
        // -- API drop_index
        lr.start_transaction("drop_index");
		
        // empty column
        rc = Lrvtc.drop_index("C_ID");
        lr.log_message("drop index of empty column rc=" + rc + "\n");
		
        // non-empty column
        rc = Lrvtc.drop_index("P_ID");
        lr.log_message("drop index of non-empty column rc=" + rc + "\n");
        
        // wrong column name
        rc = Lrvtc.drop_index("P_ID1");
        lr.log_message("drop index of wrong column rc=" + rc + "\n");
        lr.end_transaction("drop_index", lr.AUTO);
        // end API drop_index --

        // -- API send_if_unique
        lr.start_transaction("send_if_unique");
        // insert unique
        colsize = Lrvtc.column_size("P_ID");
        rc = Lrvtc.send_if_unique("P_ID", "product-1234");
        lr.log_message("send_if_unique rc=" + rc + "\n");
        if (colsize == Lrvtc.column_size("P_ID") - 1) {
            lr.log_message("send_if_unique (unique) succeed\n");
            // insert duplicated
            colsize = Lrvtc.column_size("P_ID");
            rc = Lrvtc.send_if_unique("P_ID", "product-1234");
            lr.log_message("send_if_unique rc=" + rc + "\n");
            if (colsize == Lrvtc.column_size("P_ID")) {
                lr.log_message("send_if_unique (duplicated) succeed\n");
                // insert into wrong column
                rc = Lrvtc.send_if_unique("P_ID1", "product-1234");
                lr.log_message("send_if_unique (wrong col) rc=" + rc + "\n");
                // --
                lr.end_transaction("send_if_unique", lr.PASS);
            } else {
                lr.error_message("send_if_unique (duplicated) failed\n");
                lr.end_transaction("send_if_unique", lr.FAIL);
            }
        } else {
            lr.error_message("send_if_unique (unique) failed\n");
            lr.end_transaction("send_if_unique", lr.FAIL);
        }
        // end of API send_if_unique --

        // -- API clear_message
        lr.start_transaction("clear_message");
        colsize = Lrvtc.column_size("P_ID");
        
        rc = Lrvtc.clear_message("P_ID", colsize);
        lr.log_message("clear_message rc=" + rc + "\n");
        
        // wrong column name
        rc = Lrvtc.clear_message("P_ID11", 1);
        lr.log_message("clear_message (wrong col name) rc=" + rc + "\n");
        
        // wrong row number
        rc = Lrvtc.clear_message("P_ID", colsize + 1);
        lr.log_message("clear_message rc=" + rc + "\n");
        lr.end_transaction("clear_message", lr.AUTO);
        // end of API clear_message --

        // -- API send_message
        lr.start_transaction("send_message");
        rc = Lrvtc.send_message("C_ID", "customer1");
        lr.log_message("send_message rc=" + rc + "\n");
        
        // wrong column name
        rc = Lrvtc.send_message("C_ID1", "customer1");
        lr.log_message("send_message (wrong col name) rc=" + rc + "\n");
        lr.end_transaction("send_message", lr.AUTO);
        // end of API send_message --
		
        // -- API query_column
        lr.start_transaction("query_column");
        colsize = Lrvtc.column_size("P_ID");
        for (int i = 1; i <= colsize; i++) {
            rc = Lrvtc.query_column("P_ID", i);
            lr.log_message("query_column rc=" + rc + "\n");
            lr.log_message("P_ID[" + i + "]=" + lr.eval_string("{P_ID}\n"));
        }
        
        // wrong column name
        rc = Lrvtc.query_column("P_ID12", 1);
        lr.log_message("query_column (wrong name) rc=" + rc + "\n");
        lr.log_message("P_ID12=" + lr.eval_string("{P_ID12}\n"));
        
        // wrong row number
        rc = Lrvtc.query_column("P_ID", Lrvtc.column_size("P_ID") + 1);
        lr.log_message("query_column (wrong row num) rc=" + rc + "\n");
        lr.log_message("P_ID=" + lr.eval_string("{P_ID}\n"));
        lr.end_transaction("query_column", lr.AUTO);
        // end of API query_column --

        // -- API query_row
        lr.start_transaction("query_row");
        colsize = Lrvtc.column_size("P_ID");
        for (int i = 1; i <= colsize; i++) {
            rc = Lrvtc.query_row(i);
            lr.log_message("query_row rc=" + rc + "\n");
            lr.log_message("Row["
                    + i
                    + "]={"
                    + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status}}\n"));
        }
        
        // wrong row number
        rc = Lrvtc.query_row(Lrvtc.column_size("P_ID") + 1);
        lr.log_message("query_row (wrong row num) rc=" + rc + "\n");
        lr.log_message("Row={" + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status}}\n"));
        
        lr.end_transaction("query_row", lr.AUTO);
        // end of API query_row --

        // -- API retrieve_message
        lr.start_transaction("retrieve_message");
        colsize = Lrvtc.column_size("P_status");
        lr.log_message("size of P_status=" + colsize);
        rc = Lrvtc.retrieve_message("P_status");
        lr.log_message("retrieve_message rc=" + rc + "\n");
        lr.log_message("P_status=" + lr.eval_string("{P_status}\n"));
        colsize = Lrvtc.column_size("P_status");
        lr.log_message("size of P_status=" + colsize);
        
        // wrong column name
        rc = Lrvtc.retrieve_message("P_status1");
        lr.log_message("retrieve_message (wrong col name) rc=" + rc + "\n");
        lr.log_message("P_status1=" + lr.eval_string("{P_status1}\n"));
        lr.end_transaction("retrieve_message", lr.AUTO);
        // end of API retrieve_message --

        // -- API retrieve_messages1
        lr.start_transaction("retrieve_messages1");
        lr.log_message("size of P_ID=" + Lrvtc.column_size("P_ID"));
        lr.log_message("size of P_name=" + Lrvtc.column_size("P_name"));
        lr.log_message("size of P_info=" + Lrvtc.column_size("P_info"));
        lr.log_message("size of P_status=" + Lrvtc.column_size("P_status"));
        rc = Lrvtc.retrieve_messages1("P_ID;P_name;P_info;P_status", ";");
        lr.log_message("retrieve_messages1 rc=" + rc + "\n");
        lr.log_message("P_ID=" + lr.eval_string("{P_ID}\n"));
        lr.log_message("P_name=" + lr.eval_string("{P_name}\n"));
        lr.log_message("P_info=" + lr.eval_string("{P_info}\n"));
        lr.log_message("P_status=" + lr.eval_string("{P_status}\n"));
        lr.log_message("size of P_ID=" + Lrvtc.column_size("P_ID"));
        lr.log_message("size of P_name=" + Lrvtc.column_size("P_name"));
        lr.log_message("size of P_info=" + Lrvtc.column_size("P_info"));
        lr.log_message("size of P_status=" + Lrvtc.column_size("P_status"));
        
        // one wrong column
        lr.log_message("size of P_ID=" + Lrvtc.column_size("P_ID"));
        lr.log_message("size of P_name=" + Lrvtc.column_size("P_name"));
        lr.log_message("size of P_info=" + Lrvtc.column_size("P_info"));
        lr.log_message("size of P_status=" + Lrvtc.column_size("P_status"));
        rc = Lrvtc.retrieve_messages1("P_ID;P_name;P_info;P_status;WR_col", ";");
        lr.log_message("retrieve_messages1 (one wrong col) rc=" + rc + "\n");
        lr.log_message("P_ID=" + lr.eval_string("{P_ID}\n"));
        lr.log_message("P_name=" + lr.eval_string("{P_name}\n"));
        lr.log_message("P_info=" + lr.eval_string("{P_info}\n"));
        lr.log_message("P_status=" + lr.eval_string("{P_status}\n"));
        lr.log_message("WR_col=" + lr.eval_string("{WR_col}\n"));
        
        // all wrong columns
        rc = Lrvtc.retrieve_messages1("P_ID1;P_name1;P_info1;P_status1", ";");
        lr.log_message("retrieve_messages1 (all wrong cols) rc=" + rc + "\n");
        lr.log_message("P_ID1=" + lr.eval_string("{P_ID1}\n"));
        lr.log_message("P_name1=" + lr.eval_string("{P_name1}\n"));
        lr.log_message("P_info1=" + lr.eval_string("{P_info1}\n"));
        lr.log_message("P_status1=" + lr.eval_string("{P_status1}\n"));
        lr.end_transaction("retrieve_messages1", lr.AUTO);
        // end of API retrieve_messages1 --

        // -- API retrieve_row
        lr.start_transaction("retrieve_row");
        lr.log_message("size of C_ID=" + Lrvtc.column_size("C_ID"));
        lr.log_message("size of P_ID=" + Lrvtc.column_size("P_ID"));
        lr.log_message("size of P_name=" + Lrvtc.column_size("P_name"));
        lr.log_message("size of P_info=" + Lrvtc.column_size("P_info"));
        lr.log_message("size of P_status=" + Lrvtc.column_size("P_status"));
        rc = Lrvtc.retrieve_row();
        lr.log_message("retrieve_row rc=" + rc + "\n");
        lr.log_message("Row={"
                + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status}}\n"));
        lr.log_message("size of C_ID=" + Lrvtc.column_size("C_ID"));
        lr.log_message("size of P_ID=" + Lrvtc.column_size("P_ID"));
        lr.log_message("size of P_name=" + Lrvtc.column_size("P_name"));
        lr.log_message("size of P_info=" + Lrvtc.column_size("P_info"));
        lr.log_message("size of P_status=" + Lrvtc.column_size("P_status"));
        lr.end_transaction("retrieve_row", lr.AUTO);
        // end of API retrieve_row --

        // -- API update_message
        lr.start_transaction("update_message");
        rc = Lrvtc.query_column("P_name", 1);
        lr.log_message("query_column rc=" + rc + "\n");
        if (rc != 0) {
            lr.end_transaction("update_message", lr.FAIL);
        } else {
            lr.log_message("P_name old value=" + lr.eval_string("{P_name}\n"));
            rc = Lrvtc.update_message("P_name", 1,
                    lr.eval_string("{P_name}_new"));
            lr.log_message("update_message rc=" + rc + "\n");
            rc = Lrvtc.query_column("P_name", 1);
            lr.log_message("P_name new value=" + lr.eval_string("{P_name}\n"));
        }
        
        // wrong column name
        rc = Lrvtc.update_message("P_name2", 1, "P_name_new");
        lr.log_message("update_message (wrong col) rc=" + rc + "\n");
        
        // wrong row number
        rc = Lrvtc.update_message("P_name", Lrvtc.column_size("P_name") + 10, "P_name_new");
        lr.log_message("update_message (wrong row num) rc=" + rc + "\n");
        lr.end_transaction("update_message", lr.AUTO);
        // end of API update_message --
		
        // -- API update_message_ifequals
        lr.start_transaction("update_message_ifequals");
		
        // update_message_ifequals if match
        lr.start_sub_transaction("update_message_ifequals_match",
                "update_message_ifequals");
        rc = Lrvtc.query_column("P_name", 1);
        lr.log_message("query_column rc=" + rc + "\n");
        if (rc != 0) {
            lr.end_sub_transaction("update_message_ifequals_match", lr.FAIL);
        } else {
            lr.log_message("P_name old value=" + lr.eval_string("{P_name}\n"));
            rc = Lrvtc.update_message_ifequals("P_name", 1,
                    lr.eval_string("{P_name}_newestM"),
                    lr.eval_string("{P_name}"));
            lr.log_message("update_message_ifequals rc=" + rc + "\n");
            rc = Lrvtc.query_column("P_name", 1);
            lr.log_message("P_name new value=" + lr.eval_string("{P_name}\n"));
            lr.end_sub_transaction("update_message_ifequals_match", lr.PASS);
        }
		
        // update_message_ifequals if mismatch
        lr.start_sub_transaction("update_message_ifequals_mismatch",
                "update_message_ifequals");
        rc = Lrvtc.query_column("P_name", 1);
        lr.log_message("query_column rc=" + rc + "\n");
        if (rc != 0) {
            lr.end_sub_transaction("update_message_ifequals_mismatch", lr.FAIL);
        } else {
            lr.log_message("P_name old value=" + lr.eval_string("{P_name}\n"));
            rc = Lrvtc.update_message_ifequals("P_name", 1,
                    lr.eval_string("{P_name}_newestNM"),
                    lr.eval_string("{P_name}_mismatch"));
            lr.log_message("update_message_ifequals rc=" + rc + "\n");
            rc = Lrvtc.query_column("P_name", 1);
            lr.log_message("P_name new value=" + lr.eval_string("{P_name}\n"));
            lr.end_sub_transaction("update_message_ifequals_mismatch", lr.PASS);
        }
		
        // update_message_ifequals if empty
        lr.start_sub_transaction("update_message_ifequals_empty",
                "update_message_ifequals");
        rc = Lrvtc.clear_message("P_name", 1);
        lr.log_message("query_column rc=" + rc + "\n");
        if (rc != 0) {
            lr.end_sub_transaction("update_message_ifequals_empty", lr.FAIL);
        } else {
            lr.log_message("P_name old value=" + lr.eval_string("{P_name}\n"));
            rc = Lrvtc.update_message_ifequals("P_name", 1,
                    lr.eval_string("{P_name}_newestE"), "");
            lr.log_message("update_message_ifequals rc=" + rc + "\n");
            rc = Lrvtc.query_column("P_name", 1);
            lr.log_message("P_name new value=" + lr.eval_string("{P_name}\n"));
            lr.end_sub_transaction("update_message_ifequals_empty", lr.PASS);
        }
        
        // wrong column name
        rc = Lrvtc.update_message_ifequals("P_name3", 1, "P_name_newestE", "");
        lr.log_message("update_message_ifequals (wrong col) rc=" + rc + "\n");
        
        // wrong row number
        rc = Lrvtc.update_message_ifequals("P_name", Lrvtc.column_size("P_name") + 20, "P_name_newestE", "");
        lr.log_message("update_message_ifequals (wrong row num) rc=" + rc + "\n");
        lr.end_transaction("update_message_ifequals", lr.AUTO);
        // end of API update_message_ifequals --

        // -- API update_row1
        lr.start_transaction("update_row1");
        // query current values
        rc = Lrvtc.query_row(1);
        lr.log_message("query_row rc=" + rc + "\n");
        lr.log_message("Row old={"
                + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status}}\n"));

		// update row
        rc = Lrvtc.update_row1(
                        "P_ID;P_name;P_info;P_status", 1,
                        "product1_newest1;productname1_newest1;productinfo1_newest1;instock", ";");
        lr.log_message("update_row1 rc=" + rc + "\n");

        // query new values
        rc = Lrvtc.query_row(1);
        lr.log_message("query_row rc=" + rc + "\n");
        lr.log_message("Row new={"
                + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status}}\n"));
        
        // one wrong column
        rc = Lrvtc.update_row1(
                "P_ID;P_name;P_info;P_status4", 1,
                "product1_newest1;productname1_newest1;productinfo1_newest1;wrong_place", ";");
        lr.log_message("update_row1 (one wrong col) rc=" + rc + "\n");
        rc = Lrvtc.query_row(1);
        lr.log_message("Row new={"
                + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status4}}\n"));
        
        // all wrong columns
        rc = Lrvtc.update_row1(
                "P_ID5;P_name5;P_info5;P_status5", 1,
                "product1_newest1;productname1_newest1;productinfo1_newest1;wrong_place", ";");
        lr.log_message("update_row1 (all wrong cols) rc=" + rc + "\n");
        rc = Lrvtc.query_row(1);
        lr.log_message("Row new={"
                + lr.eval_string("{C_ID5}; {P_ID5}; {P_name5}; {P_info5}; {P_status5}}\n"));
        
        // wrong row number
        rc = Lrvtc.update_row1(
                "P_ID;P_name;P_info;P_status", Lrvtc.column_size("P_ID") + 30,
                "product1_newest1;productname1_newest1;productinfo1_newest1;instock", ";");
        lr.log_message("update_row1 (wrong row num) rc=" + rc + "\n");
        rc = Lrvtc.query_row(Lrvtc.column_size("P_ID"));
        lr.log_message("Row new={"
                + lr.eval_string("{C_ID}; {P_ID}; {P_name}; {P_info}; {P_status}}\n"));
        
        lr.end_transaction("update_row1", lr.AUTO);
        
        // end of API update_row1 --
        
        //-- API increment
        
        lr.start_transaction("increment");
        
        // init counter
        rc = Lrvtc.increment("P_ID", 1, 10);
        lr.log_message("increment init rc=" + rc + "\n");
        
        // increment counter
        rc = Lrvtc.increment("P_ID", 1, 15);
        lr.log_message("increment new rc=" + rc + "\n");
        
        // wrong column name
        rc = Lrvtc.increment("P_ID6", 1, 10);
        lr.log_message("increment (wrong col) rc=" + rc + "\n");
        
        // wrong row number
        rc = Lrvtc.increment("P_ID", Lrvtc.column_size("P_ID") + 30, 10);
        lr.log_message("increment (wrong row num) rc=" + rc + "\n");
        
        lr.end_transaction("increment", lr.AUTO);
        
        // end of API increment --
        
        //-- API clear_row
        
        lr.start_transaction("clear_row");
        
        rc = Lrvtc.clear_row(1);
        lr.log_message("clear_row rc=" + rc + "\n");
        
        // wrong row number
        rc = Lrvtc.clear_row(Lrvtc.column_size("P_ID") + 1000);
        lr.log_message("clear_row (wrong row num) rc=" + rc + "\n");
        
        lr.end_transaction("clear_row", lr.AUTO);
        
        // end of API clear_row --
        
        //-- API clear_column
        
        lr.start_transaction("clear_column");
        
        rc = Lrvtc.clear_column("P_info");
        lr.log_message("clear_column rc=" + rc + "\n");
        
        // wrong column name
        rc = Lrvtc.clear_column("P_info7");
        lr.log_message("clear_column (wrong col) rc=" + rc + "\n");
        
        lr.end_transaction("clear_column", lr.AUTO);
        
        // end of API clear_column --
        
        // remove all columns
        rc = Lrvtc.clear_column("C_ID");        
        rc = Lrvtc.clear_column("P_ID");        
        rc = Lrvtc.clear_column("P_name");        
        rc = Lrvtc.clear_column("P_info");        
        rc = Lrvtc.clear_column("P_status");        
        return 0;
    }// end of action

    public int end() throws Throwable {
        // -- API disconnect
        Lrvtc.disconnect();
        // end of API disconnect --
        return 0;
    }// end of end
}