2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.table
|
|
|
|
|
|
|
|
import android.content.ContentValues
|
|
|
|
import android.database.Cursor
|
|
|
|
import android.database.sqlite.SQLiteDatabase
|
|
|
|
import android.provider.BaseColumns
|
|
|
|
import jp.juggler.subwaytooter.App1
|
2020-01-07 09:03:32 +01:00
|
|
|
import jp.juggler.util.JsonObject
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.LogCategory
|
|
|
|
import jp.juggler.util.digestSHA256Hex
|
2020-01-08 03:57:10 +01:00
|
|
|
import jp.juggler.util.decodeJsonObject
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class PostDraft {
|
|
|
|
|
|
|
|
var id : Long = 0
|
|
|
|
var time_save : Long = 0
|
2020-01-07 09:03:32 +01:00
|
|
|
var json : JsonObject? = null
|
2018-01-04 19:52:25 +01:00
|
|
|
var hash : String? = null
|
|
|
|
|
|
|
|
class ColIdx(cursor : Cursor) {
|
|
|
|
internal val idx_id : Int
|
|
|
|
internal val idx_time_save : Int
|
|
|
|
internal val idx_json : Int
|
|
|
|
internal val idx_hash : Int
|
|
|
|
|
|
|
|
init {
|
|
|
|
idx_id = cursor.getColumnIndex(COL_ID)
|
|
|
|
idx_time_save = cursor.getColumnIndex(COL_TIME_SAVE)
|
|
|
|
idx_json = cursor.getColumnIndex(COL_JSON)
|
|
|
|
idx_hash = cursor.getColumnIndex(COL_HASH)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fun delete() {
|
|
|
|
try {
|
2018-12-01 00:02:18 +01:00
|
|
|
App1.database.delete(table, "$COL_ID=?", arrayOf(id.toString()))
|
2018-01-04 19:52:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "delete failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-12 15:43:11 +02:00
|
|
|
companion object : TableCompanion {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
private val log = LogCategory("PostDraft")
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
private const val table = "post_draft"
|
|
|
|
private const val COL_ID = BaseColumns._ID
|
|
|
|
private const val COL_TIME_SAVE = "time_save"
|
|
|
|
private const val COL_JSON = "json"
|
|
|
|
private const val COL_HASH = "hash"
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
override fun onDBCreate(db : SQLiteDatabase) {
|
2018-01-04 19:52:25 +01:00
|
|
|
log.d("onDBCreate!")
|
|
|
|
db.execSQL(
|
|
|
|
"create table if not exists " + table
|
|
|
|
+ "(" + COL_ID + " INTEGER PRIMARY KEY"
|
|
|
|
+ "," + COL_TIME_SAVE + " integer not null"
|
|
|
|
+ "," + COL_JSON + " text not null"
|
|
|
|
+ "," + COL_HASH + " text not null"
|
|
|
|
+ ")"
|
|
|
|
)
|
|
|
|
db.execSQL(
|
|
|
|
"create unique index if not exists " + table + "_hash on " + table + "(" + COL_HASH + ")"
|
|
|
|
)
|
|
|
|
db.execSQL(
|
|
|
|
"create index if not exists " + table + "_time on " + table + "(" + COL_TIME_SAVE + ")"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:13:34 +01:00
|
|
|
override fun onDBUpgrade(db : SQLiteDatabase, oldVersion : Int, newVersion : Int) {
|
2018-01-04 19:52:25 +01:00
|
|
|
if(oldVersion < 12 && newVersion >= 12) {
|
|
|
|
onDBCreate(db)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteOld(now : Long) {
|
|
|
|
try {
|
|
|
|
// 古いデータを掃除する
|
|
|
|
val expire = now - 86400000L * 30
|
2018-12-01 00:02:18 +01:00
|
|
|
App1.database.delete(table, "$COL_TIME_SAVE<?", arrayOf(expire.toString()))
|
2018-01-04 19:52:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "deleteOld failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-07 09:03:32 +01:00
|
|
|
fun save(now : Long, json : JsonObject) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
deleteOld(now)
|
|
|
|
|
|
|
|
try {
|
|
|
|
// make hash
|
|
|
|
val sb = StringBuilder()
|
2020-01-07 09:03:32 +01:00
|
|
|
json.keys.toMutableList().apply { sort() }.forEach { k ->
|
|
|
|
val v = json[k]?.toString() ?: "(null)"
|
2018-01-04 19:52:25 +01:00
|
|
|
sb.append("&")
|
|
|
|
sb.append(k)
|
|
|
|
sb.append("=")
|
|
|
|
sb.append(v)
|
|
|
|
}
|
2018-05-16 21:09:07 +02:00
|
|
|
val hash = sb.toString().digestSHA256Hex()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
// save to db
|
2020-01-07 09:03:32 +01:00
|
|
|
App1.database.replace(table, null, ContentValues().apply {
|
|
|
|
put(COL_TIME_SAVE, now)
|
|
|
|
put(COL_JSON, json.toString())
|
|
|
|
put(COL_HASH, hash)
|
|
|
|
})
|
2018-01-04 19:52:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.e(ex, "save failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fun hasDraft() : Boolean {
|
|
|
|
try {
|
2018-01-21 13:46:36 +01:00
|
|
|
App1.database.query(table, arrayOf("count(*)"), null, null, null, null, null)
|
|
|
|
.use { cursor ->
|
2018-01-04 19:52:25 +01:00
|
|
|
if(cursor.moveToNext()) {
|
|
|
|
val count = cursor.getInt(0)
|
|
|
|
return count > 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "hasDraft failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fun createCursor() : Cursor? {
|
|
|
|
try {
|
2018-01-21 13:46:36 +01:00
|
|
|
return App1.database.query(
|
|
|
|
table,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2018-12-01 00:02:18 +01:00
|
|
|
"$COL_TIME_SAVE desc"
|
2018-01-21 13:46:36 +01:00
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
log.e(ex, "createCursor failed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun loadFromCursor(cursor : Cursor, colIdxArg : ColIdx?, position : Int) : PostDraft? {
|
|
|
|
val colIdx = colIdxArg ?: ColIdx(cursor)
|
|
|
|
|
|
|
|
if(! cursor.moveToPosition(position)) {
|
|
|
|
log.d("loadFromCursor: move failed. position=%s", position)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
val dst = PostDraft()
|
|
|
|
dst.id = cursor.getLong(colIdx.idx_id)
|
|
|
|
dst.time_save = cursor.getLong(colIdx.idx_time_save)
|
|
|
|
try {
|
2020-01-08 03:57:10 +01:00
|
|
|
dst.json = cursor.getString(colIdx.idx_json).decodeJsonObject()
|
2018-01-04 19:52:25 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
2020-01-07 09:03:32 +01:00
|
|
|
dst.json = JsonObject()
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dst.hash = cursor.getString(colIdx.idx_hash)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|