Project

General

Profile

Actions

JavaScript

//get changes for table (complete method)
$.ajax({
    url:  sqlitesync_SyncServerURL + "Sync/1/users", //Url of the Service
    method: 'GET',
    cache : false,
    scope:this,
    timeout: 5 * 60 * 1000,//10min
    success: function (response, status) { //Success Callback
        if(response.trim().length != 0)
        {
            var responseReturn = JSON.parse(response);
            var tableNameSync = '';
            var syncId = null;
            sqlitesync_DB.transaction(function (tx) {

                var queryInsert = null;
                var queryUpdate = null;
                var queryDelete = null;

                if (responseReturn[0].SyncId > 0) {
                    tableNameSync = responseReturn[0].TableName;
                    sqlitesync_AddLog('<p>Preparing changes for table <b>' + responseReturn[0].TableName + '</b></p>');
                    syncId = responseReturn[0].SyncId;

                    if (window.DOMParser) {
                        parser = new DOMParser();
                        xmlDoc = parser.parseFromString(responseReturn[0].Records, "text/xml");
                    }
                    else // Internet Explorer
                    {
                        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                        xmlDoc.async = "false";
                        xmlDoc.loadXML(responseReturn[0].Records);
                    }

                    queryInsert = responseReturn[0].QueryInsert;
                    queryUpdate = responseReturn[0].QueryUpdate;
                    queryDelete = responseReturn[0].QueryDelete;

                    /****************************/
                    tx.executeSql(responseReturn[0].TriggerInsertDrop, null, null,
                        function (transaction, error) {
                        });
                    tx.executeSql(responseReturn[0].TriggerUpdateDrop, null, null,
                        function (transaction, error) {
                        });
                    tx.executeSql(responseReturn[0].TriggerDeleteDrop, null, null,
                        function (transaction, error) {
                        });
                    /****************************/

                    for (var i = 0; i < xmlDoc.childNodes[0].childElementCount; i++) {

                        var rowValues = new Array();
                        var query = null;
                        var colCount = 0;

                        for (var ii = 0; ii < xmlDoc.childNodes[0].childNodes[i].childElementCount; ii++) {
                            rowValues[ii] = xmlDoc.childNodes[0].childNodes[i].childNodes[ii].textContent;
                            colCount++;
                        }

                        var rowId = rowValues[colCount - 1];
                        var identityCol = rowValues[0];

                        switch (xmlDoc.childNodes[0].childNodes[i].getAttribute("a")) {
                            case "1":
                                tx.executeSql(queryInsert,
                                    rowValues,
                                    function (transaction, result) {

                                    },
                                    function (transaction, error) {
                                        console.log(error);
                                    });
                                break;
                            case "2":
                                tx.executeSql(queryUpdate,
                                    rowValues,
                                    function (transaction, result) {

                                    },
                                    function (transaction, error) {
                                        console.log(error);
                                    });
                                break;
                            case "3":
                                tx.executeSql(queryDelete + "'" + rowId + "'",
                                    null,
                                    null,
                                    function (transaction, error) {
                                        console.log(error);
                                    });
                                break;
                        }
                    }

                    /****************************/
                    tx.executeSql(responseReturn[0].TriggerInsert, null, null,
                        function (transaction, error) {
                        });
                    tx.executeSql(responseReturn[0].TriggerUpdate, null, null,
                        function (transaction, error) {
                        });
                    tx.executeSql(responseReturn[0].TriggerDelete, null, null,
                        function (transaction, error) {
                        });
                    /****************************/

                }

            }, function(error){//error
                sqlitesync_AddLog('<p>Error while syncing with the server ' + error + '</p>');
                sqlitesync_SyncTableCurrentIndex++
                if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
                    sqlitesync_SyncTables();
                else
                    sqlitesync_SyncEnd();

            }, function(){//success
                if(syncId > 0){
                    $.ajax({
                        url: sqlitesync_SyncServerURL + "CommitSync/"+syncId,
                        method: 'GET',
                        scope:this,
                        cache : false,
                        timeout: 5 * 60 * 1000,//10min
                        success: function(){
                            sqlitesync_AddLog('<p style=\"font-weight:bold; color:blue;\">Received the table '+tableNameSync+'.</p>');
                            sqlitesync_SyncTableCurrentIndex++;

                            if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
                                sqlitesync_SyncTables();
                            else
                                sqlitesync_SyncEnd();
                        },
                        failure: function (result, request) {
                            var statusCode = result.status;
                            var responseText = result.responseText;
                            sqlitesync_AddLog('<p>Error while syncing with the server ' + error + '</p>');
                            sqlitesync_SyncTableCurrentIndex++

                            if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
                                sqlitesync_SyncTables();
                            else
                                sqlitesync_SyncEnd();
                        }
                    });
                } else{
                    sqlitesync_SyncTableCurrentIndex++;

                    if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
                        sqlitesync_SyncTables();
                    else
                        sqlitesync_SyncEnd();
                }
            });
        } else {
            sqlitesync_SyncTableCurrentIndex++
            if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
                sqlitesync_SyncTables();
            else
                sqlitesync_SyncEnd();
        }
    },
    failure: function (result, request) {
        var statusCode = result.status;
        var responseText = result.responseText;
        sqlitesync_AddLog('<p>Error while syncing with the server ' + responseText + '</p>');
    }
});
}

Updated by Tomek Dziemidowicz 5 months ago · 1 revisions