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 String messageId;
 18	protected final Jid sender;
 19	protected final String thread;
 20	protected final String threadParent;
 21	protected final String info;
 22	protected final String document;
 23	protected final String summary;
 24	protected final String payload;
 25
 26	public WebxdcUpdate(final Conversation conversation, final String messageId, final Jid sender, final Element thread, final String info, final String document, final String summary, final String payload) {
 27		this.serial = null;
 28		this.maxSerial = null;
 29		this.conversationId = conversation.getUuid();
 30		this.messageId = messageId;
 31		this.sender = sender;
 32		this.thread = thread.getContent();
 33		this.threadParent = thread.getAttribute("parent");
 34		this.info = info;
 35		this.document = document;
 36		this.summary = summary;
 37		this.payload = payload;
 38	}
 39
 40	public WebxdcUpdate(final Cursor cursor, long maxSerial) {
 41		this.maxSerial = maxSerial;
 42		this.serial = cursor.getLong(cursor.getColumnIndex("serial"));
 43		this.conversationId = cursor.getString(cursor.getColumnIndex(Message.CONVERSATION));
 44		this.messageId = cursor.getString(cursor.getColumnIndex("message_id"));
 45		this.sender = Jid.of(cursor.getString(cursor.getColumnIndex("sender")));
 46		this.thread = cursor.getString(cursor.getColumnIndex("thread"));
 47		this.threadParent = cursor.getString(cursor.getColumnIndex("threadParent"));
 48		this.info = cursor.getString(cursor.getColumnIndex("threadParent"));
 49		this.document = cursor.getString(cursor.getColumnIndex("document"));
 50		this.summary = cursor.getString(cursor.getColumnIndex("summary"));
 51		this.payload = cursor.getString(cursor.getColumnIndex("payload"));
 52	}
 53
 54	public String getSummary() {
 55		return summary;
 56	}
 57
 58	public String getDocument() {
 59		return document;
 60	}
 61
 62	public ContentValues getContentValues() {
 63		ContentValues cv = new ContentValues();
 64		cv.put(Message.CONVERSATION, conversationId);
 65		cv.put("message_id", messageId);
 66		cv.put("sender", sender.toString());
 67		cv.put("thread", thread);
 68		cv.put("threadParent", threadParent);
 69		if (info != null) cv.put("info", info);
 70		if (document != null) cv.put("document", document);
 71		if (summary != null) cv.put("summary", summary);
 72		if (payload != null) cv.put("payload", payload);
 73		return cv;
 74	}
 75
 76	public String toString() {
 77		StringBuilder body = new StringBuilder("{\"sender\":");
 78		body.append(JSONObject.quote(sender.toString()));
 79		if (serial != null) {
 80			body.append(",\"serial\":");
 81			body.append(serial.toString());
 82		}
 83		if (maxSerial != null) {
 84			body.append(",\"max_serial\":");
 85			body.append(maxSerial.toString());
 86		}
 87		if (info != null) {
 88			body.append(",\"info\":");
 89			body.append(JSONObject.quote(info));
 90		}
 91		if (document != null) {
 92			body.append(",\"document\":");
 93			body.append(JSONObject.quote(document));
 94		}
 95		if (summary != null) {
 96			body.append(",\"summary\":");
 97			body.append(JSONObject.quote(summary));
 98		}
 99		if (payload != null) {
100			body.append(",\"payload\":");
101			body.append(payload);
102		}
103		body.append("}");
104		return body.toString();
105	}
106}