584 lines
21 KiB
Java
584 lines
21 KiB
Java
|
|
package Database;
|
||
|
|
|
||
|
|
import android.content.ContentValues;
|
||
|
|
import android.content.Context;
|
||
|
|
import android.database.Cursor;
|
||
|
|
import android.database.sqlite.SQLiteDatabase;
|
||
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
||
|
|
import android.util.Log;
|
||
|
|
|
||
|
|
import java.text.DateFormat;
|
||
|
|
import java.text.ParseException;
|
||
|
|
import java.text.SimpleDateFormat;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.Date;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Locale;
|
||
|
|
|
||
|
|
import beyondsoft.bewomitarbeiterapp.ContactListItem;
|
||
|
|
import entities.ChatMessage;
|
||
|
|
import soapConnection.SoapConnectionManager;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Created by bib on 07.06.2016.
|
||
|
|
*/
|
||
|
|
public class DatabaseHandler extends SQLiteOpenHelper {
|
||
|
|
|
||
|
|
//DataBase version
|
||
|
|
private static final int DATABASE_VERSION = 1;
|
||
|
|
|
||
|
|
//Database name
|
||
|
|
private static final String DATABASE_NAME = "Contacts_Manager";
|
||
|
|
|
||
|
|
//Table Name
|
||
|
|
private static final String TABLE_CONTACTS = "Contactlist";
|
||
|
|
private static final String TABLE_CHATMESSAGE = "ChatMessage";
|
||
|
|
private static final String TABLE_DATABASEOWNER = "DatabaseOwner";
|
||
|
|
|
||
|
|
//Table Columns name
|
||
|
|
private static final String KEY_ID_OWNER = "id";
|
||
|
|
private static final String KEY_OWNER_PERSON_OID = "ownerpersonoid";
|
||
|
|
|
||
|
|
|
||
|
|
// Contacts-Tabelle
|
||
|
|
private static final String KEY_ID ="id";
|
||
|
|
private static final String KEY_KONTAKT_NAME = "kontaktname";
|
||
|
|
private static final String KEY_PERSONOID = "personoid";
|
||
|
|
private static final String KEY_PICTURE_BLOB = "bild";
|
||
|
|
private static final String KEY_IS_TEAM = "isteam";
|
||
|
|
private static final String KEY_TEAM_MEMBER_OIDS = "teammemberoids";
|
||
|
|
private static final String KEY_TEAM_MEMBER_NAMES = "teammembernames";
|
||
|
|
|
||
|
|
|
||
|
|
// ChatMessage-Tabelle
|
||
|
|
private static final String KEY_ID_CHATMESSAGE = "id";
|
||
|
|
private static final String KEY_SENDER_PERSON_OID_CHATMESSAGE = "senderpersonoid";
|
||
|
|
private static final String KEY_RECIPIENT_PERSON_OID_CHATMESSAGE = "recipientpersonoid";
|
||
|
|
private static final String KEY_INSTS_CHATMESSAGE = "insts";
|
||
|
|
private static final String KEY_CHAT_TEXT_CHATMESSAGE = "chattext";
|
||
|
|
private static final String KEY_IS_DELIVERED_CHATMESSAGE = "isdelivered";
|
||
|
|
private static final String KEY_SERVERSEITIGE_OID_CHATMESSAGE = "serverseitigeoid";
|
||
|
|
private static final String KEY_IS_TEAM_CHATMESSAGE = "isteam";
|
||
|
|
|
||
|
|
private static DatabaseHandler mInstance = null;
|
||
|
|
|
||
|
|
private Context mContext;
|
||
|
|
|
||
|
|
public static DatabaseHandler getInstance(Context context) {
|
||
|
|
if(mInstance == null) {
|
||
|
|
mInstance = new DatabaseHandler(context.getApplicationContext());
|
||
|
|
}
|
||
|
|
|
||
|
|
return mInstance;
|
||
|
|
}
|
||
|
|
|
||
|
|
private DatabaseHandler(Context context){
|
||
|
|
super(context,DATABASE_NAME, null, DATABASE_VERSION);
|
||
|
|
this.mContext = context;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void clearDatabase() {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
|
||
|
|
String deleteQuery = "DELETE FROM " + TABLE_DATABASEOWNER;
|
||
|
|
db.execSQL(deleteQuery);
|
||
|
|
|
||
|
|
deleteQuery = "DELETE FROM " + TABLE_CHATMESSAGE;
|
||
|
|
db.execSQL(deleteQuery);
|
||
|
|
|
||
|
|
deleteQuery = "DELETE FROM " + TABLE_CONTACTS;
|
||
|
|
db.execSQL(deleteQuery);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void onCreate(SQLiteDatabase db) {
|
||
|
|
String CREATE_CONTACTS_TABLE =
|
||
|
|
"CREATE TABLE " + TABLE_CONTACTS + "(" +
|
||
|
|
KEY_ID + " INTEGER PRIMARY KEY," +
|
||
|
|
KEY_KONTAKT_NAME + " TEXT,"+
|
||
|
|
KEY_PERSONOID + " INTEGER," +
|
||
|
|
KEY_PICTURE_BLOB + " BLOB, " +
|
||
|
|
KEY_IS_TEAM + " INTEGER, " +
|
||
|
|
KEY_TEAM_MEMBER_NAMES + " TEXT, " +
|
||
|
|
KEY_TEAM_MEMBER_OIDS + " TEXT)";
|
||
|
|
|
||
|
|
db.execSQL(CREATE_CONTACTS_TABLE);
|
||
|
|
|
||
|
|
String createChatMessageTable =
|
||
|
|
"CREATE TABLE " + TABLE_CHATMESSAGE + "(" +
|
||
|
|
KEY_ID_CHATMESSAGE + " INTEGER PRIMARY KEY," +
|
||
|
|
KEY_SENDER_PERSON_OID_CHATMESSAGE + " INTEGER," +
|
||
|
|
KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + " INTEGER," +
|
||
|
|
KEY_INSTS_CHATMESSAGE + " DATETIME DEFAULT CURRENT_TIMESTAMP," +
|
||
|
|
KEY_CHAT_TEXT_CHATMESSAGE + " TEXT," +
|
||
|
|
KEY_IS_DELIVERED_CHATMESSAGE + " INTEGER," +
|
||
|
|
KEY_SERVERSEITIGE_OID_CHATMESSAGE + " INTEGER DEFAULT NULL," +
|
||
|
|
KEY_IS_TEAM_CHATMESSAGE + " INTEGER DEFAULT 0)";
|
||
|
|
|
||
|
|
db.execSQL(createChatMessageTable);
|
||
|
|
|
||
|
|
String createOwnerTable = "CREATE TABLE " + TABLE_DATABASEOWNER + " (" +
|
||
|
|
KEY_ID_OWNER + " INTEGER PRIMARY KEY," +
|
||
|
|
KEY_OWNER_PERSON_OID + " INTEGER" +
|
||
|
|
")";
|
||
|
|
|
||
|
|
db.execSQL(createOwnerTable);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void insertOwnerOid(int ownerOid) {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
|
||
|
|
ContentValues values = new ContentValues();
|
||
|
|
values.put(KEY_ID_OWNER, ownerOid);
|
||
|
|
|
||
|
|
db.insert(TABLE_DATABASEOWNER, null, values);
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getOwnerOid() {
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
String selectQuery = "SELECT * FROM " + TABLE_DATABASEOWNER;
|
||
|
|
|
||
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
||
|
|
|
||
|
|
int ownerOid = 0;
|
||
|
|
|
||
|
|
if(cursor.moveToFirst()) {
|
||
|
|
ownerOid = cursor.getInt(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return ownerOid;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void addChatMessages(ArrayList<ChatMessage> messages) {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
|
||
|
|
ContentValues values = new ContentValues();
|
||
|
|
|
||
|
|
try {
|
||
|
|
db.beginTransaction();
|
||
|
|
|
||
|
|
for(ChatMessage message : messages) {
|
||
|
|
values.put(KEY_SENDER_PERSON_OID_CHATMESSAGE, message.SenderPersonOid);
|
||
|
|
values.put(KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, message.RecipientPersonOid);
|
||
|
|
values.put(KEY_INSTS_CHATMESSAGE, getDateTimeString(message.InsTs));
|
||
|
|
values.put(KEY_CHAT_TEXT_CHATMESSAGE, message.ChatText);
|
||
|
|
values.put(KEY_IS_DELIVERED_CHATMESSAGE, (message.IsDelivered ? 1 : 0));
|
||
|
|
values.put(KEY_SERVERSEITIGE_OID_CHATMESSAGE, message.ServerseitigeOid);
|
||
|
|
values.put(KEY_IS_TEAM_CHATMESSAGE, message.IsTeam);
|
||
|
|
|
||
|
|
db.insertOrThrow(TABLE_CHATMESSAGE, null, values);
|
||
|
|
|
||
|
|
values.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
db.setTransactionSuccessful();
|
||
|
|
} finally {
|
||
|
|
if(db.inTransaction()) {
|
||
|
|
db.endTransaction();
|
||
|
|
}
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public long addChatMessage(ChatMessage message) {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
|
||
|
|
long rowId = db.insert(TABLE_CHATMESSAGE, null, fillValuesForChatMessage(message));
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return rowId;
|
||
|
|
}
|
||
|
|
|
||
|
|
private ContentValues fillValuesForChatMessage(ChatMessage message) {
|
||
|
|
ContentValues values = new ContentValues();
|
||
|
|
|
||
|
|
values.put(KEY_SENDER_PERSON_OID_CHATMESSAGE, message.SenderPersonOid);
|
||
|
|
values.put(KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, message.RecipientPersonOid);
|
||
|
|
values.put(KEY_INSTS_CHATMESSAGE, getDateTimeString(message.InsTs));
|
||
|
|
values.put(KEY_CHAT_TEXT_CHATMESSAGE, message.ChatText);
|
||
|
|
values.put(KEY_IS_DELIVERED_CHATMESSAGE, (message.IsDelivered ? 1 : 0));
|
||
|
|
values.put(KEY_SERVERSEITIGE_OID_CHATMESSAGE, message.ServerseitigeOid);
|
||
|
|
values.put(KEY_IS_TEAM_CHATMESSAGE, message.IsTeam);
|
||
|
|
|
||
|
|
return values;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int updateChatMessage(ChatMessage message) {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
ContentValues values = new ContentValues();
|
||
|
|
|
||
|
|
values.put(KEY_SENDER_PERSON_OID_CHATMESSAGE, message.SenderPersonOid);
|
||
|
|
values.put(KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, message.RecipientPersonOid);
|
||
|
|
values.put(KEY_INSTS_CHATMESSAGE, getDateTimeString(message.InsTs));
|
||
|
|
values.put(KEY_CHAT_TEXT_CHATMESSAGE, message.ChatText);
|
||
|
|
values.put(KEY_IS_DELIVERED_CHATMESSAGE, (message.IsDelivered ? 1 : 0));
|
||
|
|
values.put(KEY_SERVERSEITIGE_OID_CHATMESSAGE, message.ServerseitigeOid);
|
||
|
|
values.put(KEY_IS_TEAM_CHATMESSAGE, message.IsTeam);
|
||
|
|
|
||
|
|
int result = db.update(TABLE_CHATMESSAGE, values, KEY_ID_CHATMESSAGE + " = ?", new String[] { String.valueOf(message.Id)});
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ArrayList<Integer> getExistingChatMessageIds() {
|
||
|
|
ArrayList<Integer> result = new ArrayList<Integer>();
|
||
|
|
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
String selectQuery = "SELECT "+ KEY_SERVERSEITIGE_OID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE " + KEY_SERVERSEITIGE_OID_CHATMESSAGE + " <> 0";
|
||
|
|
|
||
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
||
|
|
|
||
|
|
if(cursor.moveToFirst()) {
|
||
|
|
do {
|
||
|
|
result.add(cursor.getInt(0));
|
||
|
|
} while(cursor.moveToNext());
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ArrayList<Integer> getExistingTeamChatMessageIds() {
|
||
|
|
ArrayList<Integer> result = new ArrayList<Integer>();
|
||
|
|
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
String selectQuery = "SELECT " + KEY_SERVERSEITIGE_OID_CHATMESSAGE + " FROM " + TABLE_CHATMESSAGE + " WHERE " + KEY_SERVERSEITIGE_OID_CHATMESSAGE + " <> 0 AND " + KEY_IS_TEAM_CHATMESSAGE + " = 1";
|
||
|
|
|
||
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
||
|
|
|
||
|
|
if(cursor.moveToFirst()) {
|
||
|
|
do {
|
||
|
|
result.add(cursor.getInt(0));
|
||
|
|
} while(cursor.moveToNext());
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
private String getDateTimeString(Date date) {
|
||
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN);
|
||
|
|
return dateFormat.format(date);
|
||
|
|
}
|
||
|
|
|
||
|
|
private Date getDateFromString(String dateString) {
|
||
|
|
Date result = new Date();
|
||
|
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN);
|
||
|
|
|
||
|
|
try {
|
||
|
|
result = df.parse(dateString);
|
||
|
|
} catch (ParseException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
|
||
|
|
|
||
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
|
||
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHATMESSAGE);
|
||
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATABASEOWNER);
|
||
|
|
|
||
|
|
onCreate(db);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void addContact(ContactListItem contact) {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
ContentValues values = new ContentValues();
|
||
|
|
|
||
|
|
values.put(KEY_KONTAKT_NAME, contact.getChatName());
|
||
|
|
values.put(KEY_PERSONOID, contact.getPersonOid());
|
||
|
|
values.put(KEY_PICTURE_BLOB, contact.getPicture());
|
||
|
|
values.put(KEY_IS_TEAM, contact.isTeam);
|
||
|
|
values.put(KEY_TEAM_MEMBER_NAMES, contact.teamMemberNames);
|
||
|
|
values.put(KEY_TEAM_MEMBER_OIDS, contact.teamMemberOids);
|
||
|
|
|
||
|
|
db.insert(TABLE_CONTACTS, null, values);
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public ChatMessage getChatMessageById(int id) {
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
Cursor cursor = db.query(
|
||
|
|
TABLE_CHATMESSAGE,
|
||
|
|
new String[] {KEY_ID_CHATMESSAGE, KEY_SENDER_PERSON_OID_CHATMESSAGE, KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, KEY_INSTS_CHATMESSAGE, KEY_CHAT_TEXT_CHATMESSAGE, KEY_IS_DELIVERED_CHATMESSAGE, KEY_SERVERSEITIGE_OID_CHATMESSAGE, KEY_IS_TEAM_CHATMESSAGE},
|
||
|
|
KEY_ID_CHATMESSAGE + "=?",
|
||
|
|
new String[]{String.valueOf(id)},
|
||
|
|
null, null, null, null);
|
||
|
|
|
||
|
|
if(cursor != null) {
|
||
|
|
cursor.moveToFirst();
|
||
|
|
|
||
|
|
int messageId = cursor.getInt(0);
|
||
|
|
int senderPersonOid = cursor.getInt(1);
|
||
|
|
int recipientPersonOid = cursor.getInt(2);
|
||
|
|
Date insTs = getDateFromString(cursor.getString(3));
|
||
|
|
String chatText = cursor.getString(4);
|
||
|
|
boolean isDelivered = cursor.getInt(5) != 0;
|
||
|
|
int serverseitigeOid = cursor.getInt(6);
|
||
|
|
boolean isTeam = cursor.getInt(7) != 0;
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return new ChatMessage(messageId, senderPersonOid, recipientPersonOid, insTs, chatText, isDelivered, serverseitigeOid, isTeam);
|
||
|
|
}
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ArrayList<ChatMessage> getMessagesForRecipient(int recipientOid) {
|
||
|
|
ArrayList<ChatMessage> messages = new ArrayList<>();
|
||
|
|
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
Cursor cursor = db.query(
|
||
|
|
TABLE_CHATMESSAGE,
|
||
|
|
new String[] {KEY_ID_CHATMESSAGE, KEY_SENDER_PERSON_OID_CHATMESSAGE, KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, KEY_INSTS_CHATMESSAGE, KEY_CHAT_TEXT_CHATMESSAGE, KEY_IS_DELIVERED_CHATMESSAGE, KEY_SERVERSEITIGE_OID_CHATMESSAGE, KEY_IS_TEAM_CHATMESSAGE},
|
||
|
|
KEY_SENDER_PERSON_OID_CHATMESSAGE + "=? AND " + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + "=? OR " + KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + "=? AND " + KEY_SENDER_PERSON_OID_CHATMESSAGE + "=?",
|
||
|
|
new String[] {String.valueOf(SoapConnectionManager.User.getPersonOid()), String.valueOf(recipientOid), String.valueOf(SoapConnectionManager.User.getPersonOid()), String.valueOf(recipientOid)},
|
||
|
|
null, null, null, null);
|
||
|
|
|
||
|
|
if(cursor.moveToFirst()) {
|
||
|
|
do{
|
||
|
|
ChatMessage message = new ChatMessage(
|
||
|
|
cursor.getInt(0),
|
||
|
|
cursor.getInt(1),
|
||
|
|
cursor.getInt(2),
|
||
|
|
getDateFromString(cursor.getString(3)),
|
||
|
|
cursor.getString(4),
|
||
|
|
cursor.getInt(5) != 0,
|
||
|
|
cursor.getInt(6),
|
||
|
|
cursor.getInt(7) != 0
|
||
|
|
);
|
||
|
|
|
||
|
|
messages.add(message);
|
||
|
|
} while(cursor.moveToNext());
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return messages;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ArrayList<ChatMessage> getMessagesForTeam(int teamOid) {
|
||
|
|
ArrayList<ChatMessage> messages = new ArrayList<>();
|
||
|
|
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
Cursor cursor = db.query(
|
||
|
|
TABLE_CHATMESSAGE,
|
||
|
|
new String[] {KEY_ID_CHATMESSAGE, KEY_SENDER_PERSON_OID_CHATMESSAGE, KEY_RECIPIENT_PERSON_OID_CHATMESSAGE, KEY_INSTS_CHATMESSAGE, KEY_CHAT_TEXT_CHATMESSAGE, KEY_IS_DELIVERED_CHATMESSAGE, KEY_SERVERSEITIGE_OID_CHATMESSAGE, KEY_IS_TEAM_CHATMESSAGE},
|
||
|
|
KEY_RECIPIENT_PERSON_OID_CHATMESSAGE + "=? AND " + KEY_IS_TEAM_CHATMESSAGE + "= 1",
|
||
|
|
new String[] {String.valueOf(teamOid)},
|
||
|
|
null, null, null, null);
|
||
|
|
|
||
|
|
if(cursor.moveToFirst()) {
|
||
|
|
do{
|
||
|
|
ChatMessage message = new ChatMessage(
|
||
|
|
cursor.getInt(0),
|
||
|
|
cursor.getInt(1),
|
||
|
|
cursor.getInt(2),
|
||
|
|
getDateFromString(cursor.getString(3)),
|
||
|
|
cursor.getString(4),
|
||
|
|
cursor.getInt(5) != 0,
|
||
|
|
cursor.getInt(6),
|
||
|
|
cursor.getInt(7) != 0
|
||
|
|
);
|
||
|
|
|
||
|
|
messages.add(message);
|
||
|
|
} while(cursor.moveToNext());
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return messages;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ContactListItem getContact(int id) {
|
||
|
|
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
Cursor cursor = db.query(TABLE_CONTACTS,
|
||
|
|
new String[]{
|
||
|
|
KEY_ID,
|
||
|
|
KEY_KONTAKT_NAME,
|
||
|
|
KEY_PERSONOID,
|
||
|
|
KEY_PICTURE_BLOB,
|
||
|
|
KEY_IS_TEAM,
|
||
|
|
KEY_TEAM_MEMBER_NAMES,
|
||
|
|
KEY_TEAM_MEMBER_OIDS},
|
||
|
|
KEY_ID + "=?",
|
||
|
|
new String[]{String.valueOf(id)},
|
||
|
|
null,null,null,null);
|
||
|
|
|
||
|
|
if(cursor != null) {
|
||
|
|
cursor.moveToFirst();
|
||
|
|
|
||
|
|
int contactId = cursor.getInt(0);
|
||
|
|
String contactName = cursor.getString(1);
|
||
|
|
int contactPersonOid = cursor.getInt(2);
|
||
|
|
byte[] contactPicture = cursor.getBlob(3);
|
||
|
|
boolean contactIsTeam = cursor.getInt(4) == 1;
|
||
|
|
String teamMemberOids = cursor.getString(5);
|
||
|
|
String teamMemberNames = cursor.getString(6);
|
||
|
|
|
||
|
|
ContactListItem contact = new ContactListItem(contactId, contactName, contactPersonOid, contactPicture, contactIsTeam, teamMemberOids, teamMemberNames);
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return contact;
|
||
|
|
}
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<ContactListItem> getAllContacts(){
|
||
|
|
List<ContactListItem> contactList = new ArrayList<ContactListItem>();
|
||
|
|
|
||
|
|
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
|
||
|
|
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
||
|
|
|
||
|
|
if (cursor.moveToFirst()) {
|
||
|
|
do {
|
||
|
|
ContactListItem contact = new ContactListItem();
|
||
|
|
contact.setId(cursor.getInt(0));
|
||
|
|
contact.setChatName(cursor.getString(1));
|
||
|
|
contact.setPersonOid(cursor.getInt(2));
|
||
|
|
contact.setPicture(cursor.getBlob(3));
|
||
|
|
contact.isTeam = cursor.getInt(4) == 1;
|
||
|
|
contact.teamMemberNames = cursor.getString(5);
|
||
|
|
contact.teamMemberOids = cursor.getString(6);
|
||
|
|
|
||
|
|
contactList.add(contact);
|
||
|
|
} while (cursor.moveToNext());
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return contactList;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int updateContact(ContactListItem contact) {
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
|
||
|
|
ContentValues values = new ContentValues();
|
||
|
|
values.put(KEY_KONTAKT_NAME, contact.getChatName());
|
||
|
|
values.put(KEY_PERSONOID, contact.getPersonOid());
|
||
|
|
values.put(KEY_PICTURE_BLOB, contact.getPicture());
|
||
|
|
values.put(KEY_IS_TEAM, (contact.isTeam ? 1 : 0));
|
||
|
|
values.put(KEY_TEAM_MEMBER_NAMES, contact.teamMemberNames);
|
||
|
|
values.put(KEY_TEAM_MEMBER_OIDS, contact.teamMemberOids);
|
||
|
|
|
||
|
|
int result = db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] { String.valueOf(contact.getId()) });
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void deleteContacts(ContactListItem contact){
|
||
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
||
|
|
db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { String.valueOf(contact.getId()) });
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getContactsCount(){
|
||
|
|
String countQuery = "SELECT COUNT(*) FROM " + TABLE_CONTACTS;
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
Cursor cursor = db.rawQuery(countQuery, null);
|
||
|
|
|
||
|
|
int result = 0;
|
||
|
|
|
||
|
|
if(cursor.moveToFirst()) {
|
||
|
|
result = cursor.getInt(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public byte[] getUserImage(Integer personOid) {
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
Cursor cursor = db.query(TABLE_CONTACTS, new String[] {KEY_PERSONOID}, KEY_PERSONOID + " = " + personOid, null, null, null, null);
|
||
|
|
|
||
|
|
byte[] picture = cursor.getBlob(0);
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return picture;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ContactListItem getContactByPersonOid(int personOid) {
|
||
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
||
|
|
|
||
|
|
Cursor cursor = db.query(TABLE_CONTACTS,
|
||
|
|
new String[]{
|
||
|
|
KEY_ID,
|
||
|
|
KEY_KONTAKT_NAME,
|
||
|
|
KEY_PERSONOID,
|
||
|
|
KEY_PICTURE_BLOB,
|
||
|
|
KEY_IS_TEAM,
|
||
|
|
KEY_TEAM_MEMBER_NAMES,
|
||
|
|
KEY_TEAM_MEMBER_OIDS},
|
||
|
|
KEY_PERSONOID + "=?",
|
||
|
|
new String[]{String.valueOf(personOid)},
|
||
|
|
null,null,null,null);
|
||
|
|
|
||
|
|
if(cursor != null && cursor.moveToFirst()) {
|
||
|
|
|
||
|
|
int contactId = cursor.getInt(0);
|
||
|
|
String contactName = cursor.getString(1);
|
||
|
|
int contactPersonOid = cursor.getInt(2);
|
||
|
|
byte[] contactPicture = cursor.getBlob(3);
|
||
|
|
boolean contactIsTeam = cursor.getInt(4) == 1;
|
||
|
|
String teamMemberOids = cursor.getString(5);
|
||
|
|
String teamMemberNames = cursor.getString(6);
|
||
|
|
|
||
|
|
ContactListItem contact = new ContactListItem(contactId, contactName, contactPersonOid, contactPicture, contactIsTeam, teamMemberOids, teamMemberNames);
|
||
|
|
|
||
|
|
cursor.close();
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return contact;
|
||
|
|
} else {
|
||
|
|
Log.i("DATABASE_HANDLER", "Keinen Kontakt mit PersonOid " + personOid + " gefunden.");
|
||
|
|
}
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|