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