Actions
SYNC - JAVA¶
/**
* Get changes for table from remote server for specific subscriber
* @param subscriberId id of subscriber
* @param tableName table name
* @throws Exception
*/
private void getRemoteChangesForTable(String subscriberId, String tableName) throws Exception {
HttpURLConnection connection = null;
InputStream resultStream = null;
String resultString = null;
String requestUrl = String.format("%s/Sync/%s/%s", _serverURL, subscriberId, tableName);
try {
connection = (HttpURLConnection) new URL(requestUrl).openConnection();
int status = connection.getResponseCode();
switch (status){
case HttpURLConnection.HTTP_OK:
resultStream = connection.getInputStream();
resultString = IOUtils.toString(resultStream, "UTF-8");
break;
default:
resultStream = connection.getErrorStream();
resultString = IOUtils.toString(resultStream, "UTF-8");
throw new Exception(resultString);
}
}
finally {
if (resultStream != null) {
try {
resultStream.close();
} catch (IOException e) {
}
}
if (connection != null) {
connection.disconnect();
}
}
SQLiteSyncData[] syncDatas = new Gson().fromJson(resultString, SQLiteSyncData[].class);
for(SQLiteSyncData syncData : syncDatas){
if(syncData.SyncId > 0) {
SQLiteDatabase db = null;
try{
db = openOrCreateDatabase(_dbFileName, null);
db.beginTransaction();
if(syncData.TriggerInsertDrop.length() > 0){
db.execSQL(syncData.TriggerInsertDrop);
}
if(syncData.TriggerUpdateDrop.length() > 0){
db.execSQL(syncData.TriggerUpdateDrop);
}
if(syncData.TriggerDeleteDrop.length() > 0){
db.execSQL(syncData.TriggerDeleteDrop);
}
SQLiteSyncDataRecord[] records = syncData.getSQLiteSyncDataRecords();
for(SQLiteSyncDataRecord record : records){
switch (record.Action){
case 1:
db.execSQL(syncData.QueryInsert, record.Columns);
break;
case 2:
db.execSQL(syncData.QueryUpdate, record.Columns);
break;
case 3:
db.execSQL(syncData.QueryDelete + "?", record.Columns);
break;
}
}
if(syncData.TriggerInsert.length() > 0){
db.execSQL(syncData.TriggerInsert);
}
if(syncData.TriggerUpdate.length() > 0){
db.execSQL(syncData.TriggerUpdate);
}
if(syncData.TriggerDelete.length() > 0){
db.execSQL(syncData.TriggerDelete);
}
db.setTransactionSuccessful();
}
finally {
if(db != null && db.isOpen()){
if(db.inTransaction()){
db.endTransaction();
}
db.close();
}
}
commitSynchronization(syncData.SyncId);
}
}
}
Updated by Tomek Dziemidowicz over 5 years ago · 1 revisions
Go to top