Custom Java Updates with Connector + DataProcessor

I am having trouble creating custom insert, update, and delete behaviors using the Java version of Connector + DataProcessor.

On the client side I am using drag-and-drop between two DHTMLXTree components. On one of the tree components I have DataProcessor attached and configured. When I drag nodes onto the tree they show up in bold.

In my TreeConnector servlet I have added the following class:

[code] //Fully custom update
class customBehavior extends ConnectorBehavior{
private TreeConnector conn;
public customBehavior(TreeConnector conn){
this.conn = conn;
System.out.println(“Custom behavior constructor”);
}

        @Override
        public void beforeInsert(DataAction action) {
            System.out.println("Insert Triggered");
            action.success();
        }
      
        @Override
        public void beforeUpdate(DataAction action) {
            System.out.println("Update Triggered");   
            action.success();
        }
        @Override
        public void beforeDelete(DataAction action) {
            System.out.println("Delete Triggered");
            action.success();
        }
    }
    treeConnector.event.attach(new customBehavior(treeConnector));[/code]

When I drag the first node onto the tree I can see that the custom behavior constructor is being triggered. The beforeInsert method is never called. The constructor is only called on the first node drop. Using dhtmlxdataprocessor_debug.js I can see that the tree is registering additional drops though.

I’ve tried to follow the example found at:
docs.dhtmlx.com/doku.php?id=dhtm … ex_updates
(I think there might be an error in this example. The example show the use of the variable data in the custom functions, but data is not defined. I think it should use the variable action instead.)

I would appreciate any help getting custom behaviors working.

Thanks
Bill

Sample updated, it must be “action”, not the “data”

As for your original problem can you provide the full code of connector init ? The snippet which you have provide looks correct.

Working sample of behavior usage is attached
behavior_sample.zip (992 Bytes)

Sure. Here is a sample of what I’m trying to do. Two tree components. One is the source tree I drag nodes from. The other is the target tree I drop nodes to. I want to enable custom insert, delete, update behavior on the target tree using DataProcessor.

Here is the web page:

[code]

    <style type="text/css">
    /*these styles allows dhtmlxLayout to work in the Full Screen mode in different browsers correctly*/
    html, body {
       width: 100%;
       height: 100%;
       margin: 0px;
       overflow: hidden;
       background-color:white;
    }
<script type="text/javascript">
    window.onload = function(){	
        dhtmlx.image_path='./codebase/imgs/';
                    
        var main_layout = new dhtmlXLayoutObject(document.body, '2U');

        // Click-and-drag source tree.
        var source_cell = main_layout.cells('a');
        var source_tree = source_cell.attachTree();
        source_tree.setIconsPath('./codebase/imgs/');
        source_tree.enableSmartXMLParsing(true);
        source_tree.enableDistributedParsing(true,100);
        source_tree.enableDragAndDrop('1', true);
        
        source_tree.setXMLAutoLoading("TheTreeConnector?prog_code=0001&is_flag=N");
        source_tree.loadXML("TheTreeConnector?prog_code=0001&is_flag=N");
        
        // Click-and-drag target tree.
    var target_cell = main_layout.cells('b');
        var target_tree = target_cell.attachTree();
        target_tree.setIconsPath('./codebase/imgs/');
        target_tree.enableSmartXMLParsing(true);
        target_tree.enableDistributedParsing(true,100);
        target_tree.enableDragAndDrop('1', true);
        
        target_tree.setXMLAutoLoading("TheTreeConnector?prog_code=0005&is_flag=Y");
        target_tree.loadXML("TheTreeConnector?prog_code=0005&is_flag=Y");
        
        target_tree_DP = new dataProcessor("TheTreeConnector?prog_code=0005&is_flag=Y");
        target_tree_DP.init(target_tree); 
    }
</script>
[/code]

The Java servlet containing the Connector code looks like this:
(I populate my tree with a Stored Procedure)

[code]package StoredProcedureServlets;

import java.sql.Connection;
import java.sql.DriverManager;

import com.dhtmlx.connector.*;
import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TheTreeConnector extends ThreadSafeConnectorServlet {

@Override
protected void configure(HttpServletRequest req, HttpServletResponse res) {
    // Get database connection.
    Connection conn = null;

    String connectionURL = "";
    String db_username = "";
    String db_password = "";

    try {
        // Get application's component environment naming context.
        javax.naming.Context ctx = new javax.naming.InitialContext();
        javax.naming.Context env = (Context)ctx.lookup("java:comp/env");

        // Get Enviroment variables from web.xml
        connectionURL = (String)env.lookup("DBConnectionString");
        db_username = (String)env.lookup("DBUserName");
        db_password = (String)env.lookup("DBPassword");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection(connectionURL, db_username, db_password);

    } catch (Throwable e) {
        e.printStackTrace();
    }

    String prog_code;
    String is_flag = "N";

    // Attempt to read values from Query String parameters.
    try {
           prog_code = req.getParameter("prog_code");

        if (req.getParameter("is_flag").equalsIgnoreCase("Y"))
            is_flag = "Y";
    } catch (Exception e) {
        System.out.println("Missing Query String Parameter.");
    }


    String sql_query;
    sql_query =
            "SELECT * FROM TABLE(STORED_PROCEDURE_PKG.CONNECTOR_TREE_TABLE('" +
            prog_code + "','" + is_flag + "')) ";

    //Initialize Connector
    TreeConnector treeConnector = new TreeConnector(conn, DBType.Oracle);
    treeConnector.dynamic_loading(true);

    // Attach Custom Tree Behaviors
    treeConnector.event.attach(new TheTreeConnectorTreeItemBehavior());
    treeConnector.event.attach(new TheTreeConnectorSQLBehavior(treeConnector));
    
    // Initialization statement required for ThreadSafe Connector.
    treeConnector.servlet(req, res);   
    treeConnector.render_sql(sql_query, "ID", "NAME", "", "PARENT_ID");

    try {
        conn.close();
    } catch (Exception e) {
        System.out.println("Closing DB exception: " + e.toString());
    }

}

}[/code]

Lastly, the class that has my custom SQL behavior looks like:

[code]package StoredProcedureServlets;

import com.dhtmlx.connector.*;

public class TheTreeConnectorSQLBehavior extends ConnectorBehavior {
private TreeConnector conn;

public TheTreeConnectorSQLBehavior(TreeConnector conn) {
    this.conn = conn;
    System.out.println("Custom behavior constructor");
}


@Override
public void beforeInsert(DataAction action) {
    System.out.println("Insert Triggered");
    action.success();
}

@Override
public void beforeUpdate(DataAction action) {
    System.out.println("Update Triggered");   
    action.success();
}
@Override
public void beforeDelete(DataAction action) {
    System.out.println("Delete Triggered");
    action.success();
}

}[/code]

I can see that the custom SQL behavior constructor is called, but the the beforeInsert method is not called when I drop a node on the target tree.

Just checked the similar code and behavior works correctly locally
Be sure to use latest connector lib - support.dhtmlx.com/x-files/beta_ … .1.dvl.zip

I included the updated dhtmlxconnector.jar file but it did not solve the problem.

The behavior I’m seeing is that my custom behavior class constructor is called on the first node I drop on the target tree (but not after that). The data processor debug windows updates after each node I drop on the tree. My custom beforeInsert method is never called. The new nodes in the target tree are bold and remain bold as I add new nodes.

Let me check my assumptions. I’m assuming that the beforeInsert method should automatically be called every time I drop a node onto the target tree. Is that correct? Is there anything I need to do to trigger the DataProcessor to send the data to Connector?

Will the new nodes unbold after they are sucessfully synced with Connector?

To me it seems like DataProcessor isn’t communicating the tree changes to Connector.

Your further help is appreciated.
Bill

Let me check my assumptions. I'm assuming that the beforeInsert method should automatically be called every time I drop a node onto the target tree. Is that correct? 

You are correct - beforeInsert will be called at server side for each insert operation on client side.

Is there anything I need to do to trigger the DataProcessor to send the data to Connector?

You need to configure dataprocessor in auto-update mode ( default behavior ) and server side must provide correct response for insert|update operations ( in case of connector - must work as well, if you are using auto increment or identity for the ID column )

Well I finially figured out the piece I was missing. The custom methods aren’t triggered unless I configure my data processor as follows:

target_tree_DP.setTransactionMode(“POST”,true);

If I leave the true off then it doesn’t work. I don’t fully understand why this fixes it and just stumbled across it via trial and error.

Bill