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, String profilePhoto, Account account,
37 String contactJid) {
38 this(java.util.UUID.randomUUID().toString(), name, profilePhoto, account.getUuid(), contactJid, System
39 .currentTimeMillis(), STATUS_AVAILABLE);
40 }
41
42 public Conversation(String uuid, String name, String profilePhoto,
43 String accountUuid, String contactJid, long created, int status) {
44 this.uuid = uuid;
45 this.name = name;
46 this.profilePhotoUri = profilePhoto;
47 this.accountUuid = accountUuid;
48 this.contactJid = contactJid;
49 this.created = created;
50 this.status = status;
51 }
52
53 public List<Message> getMessages() {
54 if (messages == null) this.messages = new ArrayList<Message>(); //prevent null pointer
55
56 //populate with Conversation (this)
57
58 for(Message msg : messages) {
59 msg.setConversation(this);
60 }
61
62 return messages;
63 }
64
65 public String getLatestMessage() {
66 if ((this.messages == null)||(this.messages.size()==0)) {
67 return null;
68 } else {
69 return this.messages.get(this.messages.size() - 1).getBody();
70 }
71 }
72
73 public long getLatestMessageDate() {
74 if ((this.messages == null)||(this.messages.size()==0)) {
75 return this.getCreated();
76 } else {
77 return this.messages.get(this.messages.size() - 1).getTimeSent();
78 }
79 }
80
81 public void setMessages(List<Message> msgs) {
82 this.messages = msgs;
83 }
84
85 public String getName() {
86 return this.name;
87 }
88
89 public String getProfilePhotoString() {
90 return this.profilePhotoUri;
91 }
92
93 public String getAccountUuid() {
94 return this.accountUuid;
95 }
96
97 public String getContactJid() {
98 return this.contactJid;
99 }
100
101 public Uri getProfilePhotoUri() {
102 if (this.profilePhotoUri != null) {
103 return Uri.parse(profilePhotoUri);
104 }
105 return null;
106 }
107
108 public int getStatus() {
109 return this.status;
110 }
111
112 public long getCreated() {
113 return this.created;
114 }
115
116 public ContentValues getContentValues() {
117 ContentValues values = new ContentValues();
118 values.put(UUID, uuid);
119 values.put(NAME, name);
120 values.put(PHOTO_URI, profilePhotoUri);
121 values.put(ACCOUNT, accountUuid);
122 values.put(CONTACT, contactJid);
123 values.put(CREATED, created);
124 values.put(STATUS, status);
125 return values;
126 }
127
128 public static Conversation fromCursor(Cursor cursor) {
129 return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)),
130 cursor.getString(cursor.getColumnIndex(NAME)),
131 cursor.getString(cursor.getColumnIndex(PHOTO_URI)),
132 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
133 cursor.getString(cursor.getColumnIndex(CONTACT)),
134 cursor.getLong(cursor.getColumnIndex(CREATED)),
135 cursor.getInt(cursor.getColumnIndex(STATUS)));
136 }
137
138 public void setStatus(int status) {
139 this.status = status;
140 }
141}