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