1package de.gultsch.chat.entities;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import android.content.ContentValues;
7import android.database.Cursor;
8import android.net.Uri;
9
10public class Conversation extends AbstractEntity {
11
12 private static final long serialVersionUID = -6727528868973996739L;
13
14 public static final String TABLENAME = "conversations";
15
16 public static final int STATUS_AVAILABLE = 0;
17 public static final int STATUS_ARCHIVED = 1;
18 public static final int STATUS_DELETED = 2;
19
20 public static final String NAME = "name";
21 public static final String PHOTO_URI = "profilePhotoUri";
22 public static final String ACCOUNT = "accountUuid";
23 public static final String CONTACT = "contactJid";
24 public static final String STATUS = "status";
25 public static final String CREATED = "created";
26
27 private String name;
28 private String profilePhotoUri;
29 private String accountUuid;
30 private String contactJid;
31 private int status;
32 private long created;
33
34 private transient List<Message> messages = null;
35
36 public Conversation(String name, Uri profilePhoto, Account account,
37 String contactJid) {
38 this(java.util.UUID.randomUUID().toString(), name, profilePhoto
39 .toString(), account.getUuid(), contactJid, System
40 .currentTimeMillis(), STATUS_AVAILABLE);
41 }
42
43 public Conversation(String uuid, String name, String profilePhoto,
44 String accountUuid, String contactJid, long created, int status) {
45 this.uuid = uuid;
46 this.name = name;
47 this.profilePhotoUri = profilePhoto;
48 this.accountUuid = accountUuid;
49 this.contactJid = contactJid;
50 this.created = created;
51 this.status = status;
52 }
53
54 public List<Message> getMessages() {
55 if (messages == null) this.messages = new ArrayList<Message>(); //prevent null pointer
56
57 //populate with Conversation (this)
58
59 for(Message msg : messages) {
60 msg.setConversation(this);
61 }
62
63 return messages;
64 }
65
66 public String getLatestMessage() {
67 if ((this.messages == null)||(this.messages.size()==0)) {
68 return null;
69 } else {
70 return this.messages.get(this.messages.size() - 1).getBody();
71 }
72 }
73
74 public long getLatestMessageDate() {
75 if ((this.messages == null)||(this.messages.size()==0)) {
76 return this.getCreated();
77 } else {
78 return this.messages.get(this.messages.size() - 1).getTimeSent();
79 }
80 }
81
82 public void setMessages(List<Message> msgs) {
83 this.messages = msgs;
84 }
85
86 public String getName() {
87 return this.name;
88 }
89
90 public String getProfilePhotoString() {
91 return this.profilePhotoUri;
92 }
93
94 public String getAccountUuid() {
95 return this.accountUuid;
96 }
97
98 public String getContactJid() {
99 return this.contactJid;
100 }
101
102 public Uri getProfilePhotoUri() {
103 if (this.profilePhotoUri != null) {
104 return Uri.parse(profilePhotoUri);
105 }
106 return null;
107 }
108
109 public int getStatus() {
110 return this.status;
111 }
112
113 public long getCreated() {
114 return this.created;
115 }
116
117 public ContentValues getContentValues() {
118 ContentValues values = new ContentValues();
119 values.put(UUID, uuid);
120 values.put(NAME, name);
121 values.put(PHOTO_URI, profilePhotoUri);
122 values.put(ACCOUNT, accountUuid);
123 values.put(CONTACT, contactJid);
124 values.put(CREATED, created);
125 values.put(STATUS, status);
126 return values;
127 }
128
129 public static Conversation fromCursor(Cursor cursor) {
130 return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)),
131 cursor.getString(cursor.getColumnIndex(NAME)),
132 cursor.getString(cursor.getColumnIndex(PHOTO_URI)),
133 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
134 cursor.getString(cursor.getColumnIndex(CONTACT)),
135 cursor.getLong(cursor.getColumnIndex(CREATED)),
136 cursor.getInt(cursor.getColumnIndex(STATUS)));
137 }
138
139 public void setStatus(int status) {
140 this.status = status;
141 }
142}