Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ServerResponse.java « util « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb7c3870ad91f60b235e470e73aa0d17155f20c6 (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
package it.niedermann.owncloud.notes.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import it.niedermann.owncloud.notes.model.CloudNote;
import it.niedermann.owncloud.notes.persistence.NotesClient;

/**
 * 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 CloudNote getNote() throws JSONException {
            return getNoteFromJSON(new JSONObject(getContent()));
        }
    }

    public static class NotesResponse extends ServerResponse {
        public NotesResponse(NotesClient.ResponseData response) {
            super(response);
        }

        public List<CloudNote> getNotes() throws JSONException {
            List<CloudNote> 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;

    public ServerResponse(NotesClient.ResponseData response) {
        this.response = response;
    }

    protected String getContent() {
        return response.getContent();
    }

    public String getETag() {
        return response.getETag();
    }

    public long getLastModified() {
        return response.getLastModified();
    }

    protected CloudNote getNoteFromJSON(JSONObject json) throws JSONException {
        long id = 0;
        String title = "";
        String content = "";
        Calendar modified = null;
        boolean favorite = false;
        String category = null;
        String etag = null;
        if (!json.isNull(NotesClient.JSON_ID)) {
            id = 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 = GregorianCalendar.getInstance();
            modified.setTimeInMillis(json.getLong(NotesClient.JSON_MODIFIED) * 1000);
        }
        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 CloudNote(id, modified, title, content, favorite, category, etag);
    }
}