package com.jasonchen.microlang.database;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jasonchen.microlang.beans.MessageBean;
import com.jasonchen.microlang.beans.MessageListBean;
import com.jasonchen.microlang.database.table.HomeOtherGroupTable;
import com.jasonchen.microlang.debug.AppLogger;
import com.jasonchen.microlang.utils.AppConfig;
import java.util.ArrayList;
import java.util.List;
/**
* jasonchen
* 2015/04/21
*/
public class HomeOtherGroupTimeLineDBTask {
private HomeOtherGroupTimeLineDBTask() {
}
private static SQLiteDatabase getWsd() {
DatabaseHelper databaseHelper = DatabaseHelper.getInstance();
return databaseHelper.getWritableDatabase();
}
private static SQLiteDatabase getRsd() {
DatabaseHelper databaseHelper = DatabaseHelper.getInstance();
return databaseHelper.getReadableDatabase();
}
private static void addHomeLineMsg(MessageListBean list, String accountId, String groupId) {
if (list == null || list.getSize() == 0) {
return;
}
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.JSONDATA);
final int groupidColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.GROUPID);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
ih.bind(groupidColumn, groupId);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
ih.bind(groupidColumn, groupId);
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceHomeOtherGroupTable(accountId, groupId);
}
private static void reduceHomeOtherGroupTable(String accountId, String groupId) {
String searchCount = "select count(" + HomeOtherGroupTable.HomeOtherGroupDataTable.ID + ") as total" + " from "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME + " where "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.ACCOUNTID + " = "
+ accountId + " and " + HomeOtherGroupTable.HomeOtherGroupDataTable.GROUPID + " = " + groupId;
int total = 0;
Cursor c = getWsd().rawQuery(searchCount, null);
if (c.moveToNext()) {
total = c.getInt(c.getColumnIndex("total"));
}
c.close();
AppLogger.e("total=" + total);
int needDeletedNumber = total - AppConfig.DEFAULT_HOME_DB_CACHE_COUNT;
if (needDeletedNumber > 0) {
AppLogger.e("" + needDeletedNumber);
String sql = " delete from " + HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME + " where "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.ID
+ " in " + "( select " + HomeOtherGroupTable.HomeOtherGroupDataTable.ID + " from "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME
+ " where " + HomeOtherGroupTable.HomeOtherGroupDataTable.ACCOUNTID + " in " + "(" + accountId + ") "
+ " and "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.GROUPID + " = " + groupId + " order by "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.ID
+ " desc limit " + needDeletedNumber + " ) ";
getWsd().execSQL(sql);
}
}
public static void replace(MessageListBean list, String accountId, String groupId) {
deleteGroupTimeLine(accountId, groupId);
addHomeLineMsg(list, accountId, groupId);
}
static void deleteGroupTimeLine(String accountId, String groupId) {
String sql = "delete from " + HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME + " where "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.ACCOUNTID + " in " + "(" + accountId + ")" + " and "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.GROUPID + " = " + groupId;
getWsd().execSQL(sql);
}
public static MessageListBean get(String accountId, String groupId, int limitCount) {
Gson gson = new Gson();
MessageListBean result = new MessageListBean();
int limit = limitCount > AppConfig.DEFAULT_MSG_COUNT_50 ? limitCount : AppConfig.DEFAULT_MSG_COUNT_50;
List<MessageBean> msgList = new ArrayList<MessageBean>();
String sql = "select * from " + HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME + " where "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.ACCOUNTID + " = " + accountId + " and "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.GROUPID
+ " = " + groupId + " order by " + HomeOtherGroupTable.HomeOtherGroupDataTable.ID + " asc limit " + limit;
Cursor c = getRsd().rawQuery(sql, null);
while (c.moveToNext()) {
String json = c.getString(c.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.JSONDATA));
if (!TextUtils.isEmpty(json)) {
try {
MessageBean value = gson.fromJson(json, MessageBean.class);
value.getListViewSpannableString();
msgList.add(value);
} catch (JsonSyntaxException e) {
AppLogger.e(e.getMessage());
}
} else {
msgList.add(null);
}
}
// delete the null flag at the head positon and the end position
for (int i = msgList.size() - 1; i >= 0; i--) {
if (msgList.get(i) == null) {
msgList.remove(i);
} else {
break;
}
}
for (int i = 0; i < msgList.size(); i++) {
if (msgList.get(i) == null) {
msgList.remove(i);
} else {
break;
}
}
result.setStatuses(msgList);
c.close();
return result;
}
static void updateCount(String msgId, int commentCount, int repostCount) {
String sql = "select * from " + HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME + " where "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.MBLOGID + " = " + msgId + " order by "
+ HomeOtherGroupTable.HomeOtherGroupDataTable.ID
+ " asc limit 50";
Cursor c = getRsd().rawQuery(sql, null);
Gson gson = new Gson();
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.ID));
String json = c.getString(c.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.JSONDATA));
if (!TextUtils.isEmpty(json)) {
try {
MessageBean value = gson.fromJson(json, MessageBean.class);
value.setComments_count(commentCount);
value.setReposts_count(repostCount);
String[] args = {
id
};
ContentValues cv = new ContentValues();
cv.put(HomeOtherGroupTable.HomeOtherGroupDataTable.JSONDATA, gson.toJson(value));
getWsd().update(HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME, cv,
HomeOtherGroupTable.HomeOtherGroupDataTable.ID + "=?", args);
} catch (JsonSyntaxException e) {
}
}
}
}
}