blob: 60c4ad5edbd7ad74acdd380fcef77d63a75ac4e7 (
plain)
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
|
package it.niedermann.owncloud.notes.shared.model;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import it.niedermann.owncloud.notes.persistence.NotesClient;
import it.niedermann.owncloud.notes.persistence.entity.Note;
/**
* Provides entity classes for handling server responses with a single note ({@link NoteResponse}) or a list of notes ({@link NotesResponse}).
*/
public class ServerResponse {
public static class NoteResponse extends ServerResponse {
public NoteResponse(NotesClient.ResponseData response) {
super(response);
}
public Note getNote() throws JSONException {
return getNoteFromJSON(new JSONObject(getContent()));
}
}
public static class NotesResponse extends ServerResponse {
public NotesResponse(NotesClient.ResponseData response) {
super(response);
}
public List<Note> getNotes() throws JSONException {
List<Note> notesList = new ArrayList<>();
JSONArray notes = new JSONArray(getContent());
for (int i = 0; i < notes.length(); i++) {
JSONObject json = notes.getJSONObject(i);
notesList.add(getNoteFromJSON(json));
}
return notesList;
}
}
private final NotesClient.ResponseData response;
ServerResponse(NotesClient.ResponseData response) {
this.response = response;
}
protected String getContent() {
return response == null ? null : response.getContent();
}
public String getETag() {
return response.getETag();
}
public long getLastModified() {
return response.getLastModified();
}
@Nullable
public String getSupportedApiVersions() {
return response.getSupportedApiVersions();
}
Note getNoteFromJSON(JSONObject json) throws JSONException {
long remoteId = 0;
String title = "";
String content = "";
Calendar modified = null;
boolean favorite = false;
String category = "";
String etag = null;
if (!json.isNull(NotesClient.JSON_ID)) {
remoteId = json.getLong(NotesClient.JSON_ID);
}
if (!json.isNull(NotesClient.JSON_TITLE)) {
title = json.getString(NotesClient.JSON_TITLE);
}
if (!json.isNull(NotesClient.JSON_CONTENT)) {
content = json.getString(NotesClient.JSON_CONTENT);
}
if (!json.isNull(NotesClient.JSON_MODIFIED)) {
modified = Calendar.getInstance();
modified.setTimeInMillis(json.getLong(NotesClient.JSON_MODIFIED) * 1_000);
}
if (!json.isNull(NotesClient.JSON_FAVORITE)) {
favorite = json.getBoolean(NotesClient.JSON_FAVORITE);
}
if (!json.isNull(NotesClient.JSON_CATEGORY)) {
category = json.getString(NotesClient.JSON_CATEGORY);
}
if (!json.isNull(NotesClient.JSON_ETAG)) {
etag = json.getString(NotesClient.JSON_ETAG);
}
return new Note(remoteId, modified, title, content, category, favorite, etag);
}
}
|