Project

General

Profile

JavaScript » History » Version 1

Tomek Dziemidowicz, 2019-07-04 08:52 PM

1 1 Tomek Dziemidowicz
h1. JavaScript
2
3
<pre><code class="javascript">
4
//get changes for table (complete method)
5
$.ajax({
6
    url:  sqlitesync_SyncServerURL + "Sync/1/users", //Url of the Service
7
    method: 'GET',
8
    cache : false,
9
    scope:this,
10
    timeout: 5 * 60 * 1000,//10min
11
    success: function (response, status) { //Success Callback
12
        if(response.trim().length != 0)
13
        {
14
            var responseReturn = JSON.parse(response);
15
            var tableNameSync = '';
16
            var syncId = null;
17
            sqlitesync_DB.transaction(function (tx) {
18
19
                var queryInsert = null;
20
                var queryUpdate = null;
21
                var queryDelete = null;
22
23
                if (responseReturn[0].SyncId > 0) {
24
                    tableNameSync = responseReturn[0].TableName;
25
                    sqlitesync_AddLog('<p>Preparing changes for table <b>' + responseReturn[0].TableName + '</b></p>');
26
                    syncId = responseReturn[0].SyncId;
27
28
                    if (window.DOMParser) {
29
                        parser = new DOMParser();
30
                        xmlDoc = parser.parseFromString(responseReturn[0].Records, "text/xml");
31
                    }
32
                    else // Internet Explorer
33
                    {
34
                        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
35
                        xmlDoc.async = "false";
36
                        xmlDoc.loadXML(responseReturn[0].Records);
37
                    }
38
39
                    queryInsert = responseReturn[0].QueryInsert;
40
                    queryUpdate = responseReturn[0].QueryUpdate;
41
                    queryDelete = responseReturn[0].QueryDelete;
42
43
                    /****************************/
44
                    tx.executeSql(responseReturn[0].TriggerInsertDrop, null, null,
45
                        function (transaction, error) {
46
                        });
47
                    tx.executeSql(responseReturn[0].TriggerUpdateDrop, null, null,
48
                        function (transaction, error) {
49
                        });
50
                    tx.executeSql(responseReturn[0].TriggerDeleteDrop, null, null,
51
                        function (transaction, error) {
52
                        });
53
                    /****************************/
54
55
                    for (var i = 0; i < xmlDoc.childNodes[0].childElementCount; i++) {
56
57
                        var rowValues = new Array();
58
                        var query = null;
59
                        var colCount = 0;
60
61
                        for (var ii = 0; ii < xmlDoc.childNodes[0].childNodes[i].childElementCount; ii++) {
62
                            rowValues[ii] = xmlDoc.childNodes[0].childNodes[i].childNodes[ii].textContent;
63
                            colCount++;
64
                        }
65
66
                        var rowId = rowValues[colCount - 1];
67
                        var identityCol = rowValues[0];
68
69
                        switch (xmlDoc.childNodes[0].childNodes[i].getAttribute("a")) {
70
                            case "1":
71
                                tx.executeSql(queryInsert,
72
                                    rowValues,
73
                                    function (transaction, result) {
74
75
                                    },
76
                                    function (transaction, error) {
77
                                        console.log(error);
78
                                    });
79
                                break;
80
                            case "2":
81
                                tx.executeSql(queryUpdate,
82
                                    rowValues,
83
                                    function (transaction, result) {
84
85
                                    },
86
                                    function (transaction, error) {
87
                                        console.log(error);
88
                                    });
89
                                break;
90
                            case "3":
91
                                tx.executeSql(queryDelete + "'" + rowId + "'",
92
                                    null,
93
                                    null,
94
                                    function (transaction, error) {
95
                                        console.log(error);
96
                                    });
97
                                break;
98
                        }
99
                    }
100
101
                    /****************************/
102
                    tx.executeSql(responseReturn[0].TriggerInsert, null, null,
103
                        function (transaction, error) {
104
                        });
105
                    tx.executeSql(responseReturn[0].TriggerUpdate, null, null,
106
                        function (transaction, error) {
107
                        });
108
                    tx.executeSql(responseReturn[0].TriggerDelete, null, null,
109
                        function (transaction, error) {
110
                        });
111
                    /****************************/
112
113
                }
114
115
            }, function(error){//error
116
                sqlitesync_AddLog('<p>Error while syncing with the server ' + error + '</p>');
117
                sqlitesync_SyncTableCurrentIndex++
118
                if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
119
                    sqlitesync_SyncTables();
120
                else
121
                    sqlitesync_SyncEnd();
122
123
            }, function(){//success
124
                if(syncId > 0){
125
                    $.ajax({
126
                        url: sqlitesync_SyncServerURL + "CommitSync/"+syncId,
127
                        method: 'GET',
128
                        scope:this,
129
                        cache : false,
130
                        timeout: 5 * 60 * 1000,//10min
131
                        success: function(){
132
                            sqlitesync_AddLog('<p style=\"font-weight:bold; color:blue;\">Received the table '+tableNameSync+'.</p>');
133
                            sqlitesync_SyncTableCurrentIndex++;
134
135
                            if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
136
                                sqlitesync_SyncTables();
137
                            else
138
                                sqlitesync_SyncEnd();
139
                        },
140
                        failure: function (result, request) {
141
                            var statusCode = result.status;
142
                            var responseText = result.responseText;
143
                            sqlitesync_AddLog('<p>Error while syncing with the server ' + error + '</p>');
144
                            sqlitesync_SyncTableCurrentIndex++
145
146
                            if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
147
                                sqlitesync_SyncTables();
148
                            else
149
                                sqlitesync_SyncEnd();
150
                        }
151
                    });
152
                } else{
153
                    sqlitesync_SyncTableCurrentIndex++;
154
155
                    if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
156
                        sqlitesync_SyncTables();
157
                    else
158
                        sqlitesync_SyncEnd();
159
                }
160
            });
161
        } else {
162
            sqlitesync_SyncTableCurrentIndex++
163
            if(sqlitesync_SyncTableCurrentIndex < sqlitesync_SyncTableList.length)
164
                sqlitesync_SyncTables();
165
            else
166
                sqlitesync_SyncEnd();
167
        }
168
    },
169
    failure: function (result, request) {
170
        var statusCode = result.status;
171
        var responseText = result.responseText;
172
        sqlitesync_AddLog('<p>Error while syncing with the server ' + responseText + '</p>');
173
    }
174
});
175
}
176
</code></pre>
Go to top