WebxdcUpdate.java

 1package com.cheogram.android;
 2
 3import android.content.ContentValues;
 4import android.database.Cursor;
 5
 6import org.json.JSONObject;
 7
 8import eu.siacs.conversations.entities.Conversation;
 9import eu.siacs.conversations.entities.Message;
10import eu.siacs.conversations.xml.Element;
11import eu.siacs.conversations.xmpp.Jid;
12
13public class WebxdcUpdate {
14	protected final Long serial;
15	protected final Long maxSerial;
16	protected final String conversationId;
17	protected final Jid sender;
18	protected final String thread;
19	protected final String threadParent;
20	protected final String info;
21	protected final String document;
22	protected final String summary;
23	protected final String payload;
24
25	public WebxdcUpdate(final Conversation conversation, final Jid sender, final Element thread, final String info, final String document, final String summary, final String payload) {
26		this.serial = null;
27		this.maxSerial = null;
28		this.conversationId = conversation.getUuid();
29		this.sender = sender;
30		this.thread = thread.getContent();
31		this.threadParent = thread.getAttribute("parent");
32		this.info = info;
33		this.document = document;
34		this.summary = summary;
35		this.payload = payload;
36	}
37
38	public WebxdcUpdate(final Cursor cursor, long maxSerial) {
39		this.maxSerial = maxSerial;
40		this.serial = cursor.getLong(cursor.getColumnIndex("serial"));
41		this.conversationId = cursor.getString(cursor.getColumnIndex(Message.CONVERSATION));
42		this.sender = Jid.of(cursor.getString(cursor.getColumnIndex("sender")));
43		this.thread = cursor.getString(cursor.getColumnIndex("thread"));
44		this.threadParent = cursor.getString(cursor.getColumnIndex("threadParent"));
45		this.info = cursor.getString(cursor.getColumnIndex("threadParent"));
46		this.document = cursor.getString(cursor.getColumnIndex("document"));
47		this.summary = cursor.getString(cursor.getColumnIndex("summary"));
48		this.payload = cursor.getString(cursor.getColumnIndex("payload"));
49	}
50
51	public String getSummary() {
52		return summary;
53	}
54
55	public ContentValues getContentValues() {
56		ContentValues cv = new ContentValues();
57		cv.put(Message.CONVERSATION, conversationId);
58		cv.put("sender", sender.toEscapedString());
59		cv.put("thread", thread);
60		cv.put("threadParent", threadParent);
61		if (info != null) cv.put("info", info);
62		if (document != null) cv.put("document", document);
63		if (summary != null) cv.put("summary", summary);
64		if (payload != null) cv.put("payload", payload);
65		return cv;
66	}
67
68	public String toString() {
69		StringBuilder body = new StringBuilder("{\"sender\":");
70		body.append(JSONObject.quote(sender.toEscapedString()));
71		if (serial != null) {
72			body.append(",\"serial\":");
73			body.append(serial.toString());
74		}
75		if (maxSerial != null) {
76			body.append(",\"max_serial\":");
77			body.append(maxSerial.toString());
78		}
79		if (info != null) {
80			body.append(",\"info\":");
81			body.append(JSONObject.quote(info));
82		}
83		if (document != null) {
84			body.append(",\"document\":");
85			body.append(JSONObject.quote(document));
86		}
87		if (summary != null) {
88			body.append(",\"summary\":");
89			body.append(JSONObject.quote(summary));
90		}
91		if (payload != null) {
92			body.append(",\"payload\":");
93			body.append(payload);
94		}
95		body.append("}");
96		return body.toString();
97	}
98}