public JSONObject getAllExistingTasksWithGlobalId() { JSONObject existingTasks = new JSONObject(); ArrayList<Task> tasks = tasksManager.getAllTasks(); for (Task task : tasks) { if (task.getGlobalId() != 0) { try { existingTasks.put(Long.toString(task.getGlobalId()), Long.toString(task.getId())); } catch (JSONException e) { Log.e(TAG, "Cannot create JSONObject with existing tasks that have globalId"); e.printStackTrace(); } } } return existingTasks; }
public String getAllChangesInXML(HashMap<Long, Pair<String, String>> tasksChanges) throws IOException, JSONException, ParserConfigurationException, TransformerException { String allChangesInXML; Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement("changes"); doc.appendChild(root); Element existingTasks = doc.createElement("existingTasks"); existingTasks.setTextContent(getAllExistingTasksWithGlobalId().toString()); root.appendChild(existingTasks); Element changedTasksEl = doc.createElement("changedTasks"); root.appendChild(changedTasksEl); Iterator it = tasksChanges.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); long localId = (long) pair.getKey(); Pair<String, String> change = (Pair<String, String>) pair.getValue(); Task task = tasksManager.getTaskByLocalId(localId); Element changedTaskEl; if (task != null) { changedTaskEl = TaskHelper.taskToXML(doc, task, "changedTask"); } else { Long globalId = taskChangesDbHelper.getGlobalIdOfDeletedTask(localId); if (globalId != 0 && change.first.equals("DELETED")) { changedTaskEl = doc.createElement("changedTask"); changedTaskEl.setAttribute("globalId", globalId.toString()); changedTaskEl.setAttribute("id", Long.toString(localId)); } else { // We don't need send info about the deleted task, that is not known for server taskChangesDbHelper.uncheckFromSync(localId); continue; } } Element changeEl = doc.createElement("change"); Element statusEl = doc.createElement("status"); statusEl.setTextContent(change.first); changeEl.appendChild(statusEl); Element datetimeEl = doc.createElement("changeDatetime"); datetimeEl.setTextContent(change.second); changeEl.appendChild(datetimeEl); changedTaskEl.appendChild(changeEl); changedTasksEl.appendChild(changedTaskEl); } // Element foldersDriveIdsEl = doc.createElement("foldersIds"); // foldersDriveIdsEl.setTextContent(GoogleDriveManager.getInstance(BrainasApp.getAppContext()).getFoldersIds().toString()); // root.appendChild(foldersDriveIdsEl); Transformer transformer = TransformerFactory.newInstance().newTransformer(); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(new DOMSource(doc), result); allChangesInXML = writer.toString(); return allChangesInXML; }