-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncServerDataTask.java
208 lines (182 loc) · 10 KB
/
SyncServerDataTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.mathur.android.listit;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
/**
* Created by neerajpooja on 5/31/2016.
*/
public class SyncServerDataTask extends AsyncTask {
MySQLiteHelper mDb;
private Context mContext;
SyncServerDataTask(Context context) {
this.mContext = context;
mDb = new MySQLiteHelper(mContext);
}
@Override
protected Object doInBackground(Object[] params) {
URL url = null;
String result;
try {
User user = mDb.getUser();
if (user.getName() != null) {
ToDoLists toDoLists = GetToDoListsToSyncFromServer(user);
SyncServerToDosToClient(toDoLists);
ToDoLists clientToDoListsToSyncOnServer = GetClientToDoListsToSync();
SyncClientToDoListsToServer(clientToDoListsToSyncOnServer, user);
ToDoItemsLists toDoItemsLists = GetToDoListItemsToSyncFromServer(user);
SyncServerToDoItemsToClient(toDoItemsLists);
ToDoItemsLists ClienttoDoItemsLists = GetClientToDoListItemsToSync();
SyncClientToDoListItemsToServer(ClienttoDoItemsLists, user);
user.lastUpdated = new Date();
mDb.updateUser(user);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private ToDoLists GetToDoListsToSyncFromServer(User user) throws MalformedURLException, UnsupportedEncodingException, JSONException {
URL url;
String result;
Common common = new Common();
if (user.getName() != null) {
String lastUpdatedDate = common.GetUTCDate(user.lastUpdated);//.replace(' ','%');
url = new URL("http://52.38.93.60/todolists/sync/" + user.getId() + "?lastSyncDate=" + URLEncoder.encode(lastUpdatedDate, "UTF-8"));
String token = ((Global) mContext.getApplicationContext()).get_Token();
RestClient restClient = new RestClient();
result = restClient.getClient(url, user.getName(), user.getPassword());
ToDoLists toDoListsToSyncOnClient = common.ParseJsonTodoListFromHttpResponse(result);
return toDoListsToSyncOnClient;
}
return null;
}
private ToDoItemsLists GetToDoListItemsToSyncFromServer(User user) throws MalformedURLException, UnsupportedEncodingException, JSONException {
URL url;
String result;
Common common = new Common();
String lastUpdatedDate = common.GetUTCDate(user.lastUpdated);//.replace(' ','%');
url = new URL("http://52.38.93.60/todolistItems/sync/" + user.getId() + "?lastSyncDate=" + URLEncoder.encode(lastUpdatedDate, "UTF-8"));
String token = ((Global) mContext.getApplicationContext()).get_Token();
RestClient restClient = new RestClient();
result = restClient.getClient(url, user.getName(), user.getPassword());
ToDoItemsLists toDoListItemsToSyncOnClient = common.ParseJsonTodoListItemsFromHttpResponse(result);
return toDoListItemsToSyncOnClient;
}
private void SyncServerToDosToClient(ToDoLists toDoLists) {
if (toDoLists != null && toDoLists.ListToDoList != null && toDoLists.ListToDoList.size() > 0) {
for (ToDoList toDoList : toDoLists.ListToDoList) {
toDoList.SetSyncStatus(1);
if (toDoList.GetOperationType().equals(Constants.KEY_Delete)) {
mDb.deleteToDoLists(toDoList);
}
if (toDoList.GetOperationType().equals(Constants.KEY_Update)) {
mDb.updateToDoList(toDoList, Constants.KEY_Update);
} else {
mDb.addToDoLists(toDoList);
}
}
}
}
private void SyncServerToDoItemsToClient(ToDoItemsLists toDoListItems) {
if (toDoListItems != null && toDoListItems.ListToDoList != null && toDoListItems.ListToDoList.size() > 0) {
for (ToDoListItems toDoListItem : toDoListItems.ListToDoList) {
toDoListItem.SetSyncStatus(1);
ToDoList toDoList = mDb.getToDoListByServerKey(toDoListItem.getListId());
toDoListItem.setListId(toDoList.getId());
if (toDoListItem.GetOperationType().equals(Constants.KEY_Delete)) {
mDb.deleteToDoListItems(toDoListItem);
}
if (toDoListItem.GetOperationType().equals(Constants.KEY_Update)) {
mDb.updateToDoListItem(toDoListItem, Constants.KEY_Update);
}
mDb.addToDoListItems(toDoListItem);
}
}
}
private ToDoLists GetClientToDoListsToSync() throws ParseException {
ToDoLists toDoLists = new ToDoLists();
toDoLists.ListToDoList = mDb.getAllUnSyncedToDoLists();
return toDoLists;
}
private ToDoItemsLists GetClientToDoListItemsToSync() throws ParseException {
ToDoItemsLists toDoItemsLists = new ToDoItemsLists();
toDoItemsLists.ListToDoList = mDb.getAllUnSyncedToDoListItems();
return toDoItemsLists;
}
private void SyncClientToDoListsToServer(ToDoLists todoLists, User user) throws IOException, JSONException, ParseException {
if (todoLists != null) {
URL url;
String result;
String token = ((Global) mContext.getApplicationContext()).get_Token();
RestClient restClient = new RestClient();
for (ToDoList toDoList : todoLists.ListToDoList) {
toDoList.SetSyncStatus(1);
if (toDoList.GetOperationType().equals(Constants.KEY_Delete)) {
url = new URL("http://52.38.93.60/todolists/delete/" + toDoList.GetServerKey());
restClient.deleteClient(url, user.getName(), user.getPassword());
mDb.deleteToDoLists(toDoList);
}
if (toDoList.GetOperationType().equals(Constants.KEY_Insert)) {
Common common = new Common();
String lastUpdated = common.GetUTCDate(toDoList.getLastUpdated());
url = new URL("http://52.38.93.60/todolists/sync/" + user.getId() + "?lastSyncDate=" + URLEncoder.encode(lastUpdated, "UTF-8")
+ "&name=" + URLEncoder.encode(toDoList.getName(), "UTF-8") + "&operationType=" + URLEncoder.encode(Constants.KEY_Insert, "UTF-8")
+ "&status=" + URLEncoder.encode(toDoList.getStatus(), "UTF-8") + "&public=" + URLEncoder.encode("1", "UTF-8"));
result = restClient.postRequest(url, user.getName(), user.getPassword(), toDoList.getLastUpdated());
String id = common.ParseJsonFromHttpResponse(result, "id", "List");
Log.d("server key", String.valueOf(id));
toDoList.SetServerKey(Integer.valueOf(id));
toDoList.SetSyncStatus(1);
mDb.updateToDoList(toDoList, Constants.KEY_Insert);
}
}
}
}
private void SyncClientToDoListItemsToServer(ToDoItemsLists todoLists, User user) throws IOException, JSONException {
if (todoLists != null) {
URL url;
String result;
Common common = new Common();
String token = ((Global) mContext.getApplicationContext()).get_Token();
RestClient restClient = new RestClient();
for (ToDoListItems toDoListItems : todoLists.ListToDoList) {
toDoListItems.SetSyncStatus(1);
if (toDoListItems.GetOperationType().equals(Constants.KEY_Delete)) {
mDb.deleteToDoListItems(toDoListItems);
//TO DO Add server call to delete
url = new URL("http://52.38.93.60/todolistItems/delete/" + toDoListItems.GetServerKey());
restClient.deleteClient(url, user.getName(), user.getPassword());
}
if (toDoListItems.GetOperationType().equals(Constants.KEY_Insert)) {
String lastUpdated = common.GetUTCDate(toDoListItems.getLastUpdated());
ToDoList toDoList = mDb.getToDoList(toDoListItems.getListId());
int serverKey = toDoList.GetServerKey();
String serverListKey = String.valueOf(serverKey);
Log.d("to do server key", serverListKey);
url = new URL("http://52.38.93.60/todolistItems/sync/" + user.getId() + "?lastSyncDate=" + URLEncoder.encode(lastUpdated.toString(), "UTF-8")
+ "&description=" + URLEncoder.encode(toDoListItems.getDescription(), "UTF-8") + "&operationType=" + URLEncoder.encode(Constants.KEY_Insert, "UTF-8")
+ "&status=" + URLEncoder.encode(toDoListItems.getStatus(), "UTF-8") + "&listId=" + URLEncoder.encode(serverListKey, "UTF-8") + "&public=" + URLEncoder.encode("1", "UTF-8"));
result = restClient.postRequest(url, user.getName(), user.getPassword(), toDoListItems.getLastUpdated());
String id = common.ParseJsonFromHttpResponse(result, "id", "List");
//toDoListItems.setListId(toDoList.GetServerKey());
toDoListItems.SetServerKey(Integer.valueOf(id));
mDb.updateToDoListItem(toDoListItems, Constants.KEY_Insert);
}
}
}
}
}