Project

General

Profile

JAVA » History » Version 1

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

1 1 Tomek Dziemidowicz
h1. SYNC - JAVA
2
3
<pre><code class="java">
4
/**
5
 * Get changes for table from remote server for specific subscriber
6
 * @param subscriberId id of subscriber
7
 * @param tableName table name
8
 * @throws Exception
9
 */
10
private void getRemoteChangesForTable(String subscriberId, String tableName) throws Exception {
11
    HttpURLConnection connection = null;
12
    InputStream resultStream = null;
13
    String resultString = null;
14
15
    String requestUrl = String.format("%s/Sync/%s/%s", _serverURL, subscriberId, tableName);
16
17
    try {
18
        connection = (HttpURLConnection) new URL(requestUrl).openConnection();
19
20
        int status = connection.getResponseCode();
21
22
        switch (status){
23
            case HttpURLConnection.HTTP_OK:
24
                resultStream = connection.getInputStream();
25
                resultString = IOUtils.toString(resultStream, "UTF-8");
26
                break;
27
            default:
28
                resultStream = connection.getErrorStream();
29
                resultString = IOUtils.toString(resultStream, "UTF-8");
30
                throw new Exception(resultString);
31
        }
32
    }
33
    finally {
34
        if (resultStream != null) {
35
            try {
36
                resultStream.close();
37
            } catch (IOException e) {
38
            }
39
        }
40
        if (connection != null) {
41
            connection.disconnect();
42
        }
43
    }
44
45
    SQLiteSyncData[] syncDatas = new Gson().fromJson(resultString, SQLiteSyncData[].class);
46
47
    for(SQLiteSyncData syncData : syncDatas){
48
        if(syncData.SyncId > 0) {
49
            SQLiteDatabase db = null;
50
51
            try{
52
                db = openOrCreateDatabase(_dbFileName, null);
53
                db.beginTransaction();
54
55
                if(syncData.TriggerInsertDrop.length() > 0){
56
                    db.execSQL(syncData.TriggerInsertDrop);
57
                }
58
                if(syncData.TriggerUpdateDrop.length() > 0){
59
                    db.execSQL(syncData.TriggerUpdateDrop);
60
                }
61
                if(syncData.TriggerDeleteDrop.length() > 0){
62
                    db.execSQL(syncData.TriggerDeleteDrop);
63
                }
64
65
                SQLiteSyncDataRecord[] records = syncData.getSQLiteSyncDataRecords();
66
67
                for(SQLiteSyncDataRecord record : records){
68
                    switch (record.Action){
69
                        case 1:
70
                            db.execSQL(syncData.QueryInsert, record.Columns);
71
                            break;
72
                        case 2:
73
                            db.execSQL(syncData.QueryUpdate, record.Columns);
74
                            break;
75
                        case 3:
76
                            db.execSQL(syncData.QueryDelete + "?", record.Columns);
77
                            break;
78
                    }
79
                }
80
81
                if(syncData.TriggerInsert.length() > 0){
82
                    db.execSQL(syncData.TriggerInsert);
83
                }
84
                if(syncData.TriggerUpdate.length() > 0){
85
                    db.execSQL(syncData.TriggerUpdate);
86
                }
87
                if(syncData.TriggerDelete.length() > 0){
88
                    db.execSQL(syncData.TriggerDelete);
89
                }
90
91
                db.setTransactionSuccessful();
92
            }
93
            finally {
94
                if(db != null && db.isOpen()){
95
                    if(db.inTransaction()){
96
                        db.endTransaction();
97
                    }
98
                    db.close();
99
                }
100
            }
101
102
            commitSynchronization(syncData.SyncId);
103
        }
104
    }
105
}
106
</code></pre>
Go to top