Implement import from db

pull/2020/head
Dharanish 1 year ago
parent 34a7f782e8
commit 7a2d486fed

@ -22,15 +22,19 @@ import org.isoron.uhabits.core.AppScope
import org.isoron.uhabits.core.DATABASE_VERSION
import org.isoron.uhabits.core.commands.CommandRunner
import org.isoron.uhabits.core.commands.CreateHabitCommand
import org.isoron.uhabits.core.commands.CreateHabitGroupCommand
import org.isoron.uhabits.core.commands.EditHabitCommand
import org.isoron.uhabits.core.commands.EditHabitGroupCommand
import org.isoron.uhabits.core.database.DatabaseOpener
import org.isoron.uhabits.core.database.MigrationHelper
import org.isoron.uhabits.core.database.Repository
import org.isoron.uhabits.core.models.Entry
import org.isoron.uhabits.core.models.HabitGroupList
import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.ModelFactory
import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.models.sqlite.records.EntryRecord
import org.isoron.uhabits.core.models.sqlite.records.HabitGroupRecord
import org.isoron.uhabits.core.models.sqlite.records.HabitRecord
import org.isoron.uhabits.core.utils.isSQLite3File
import java.io.File
@ -42,6 +46,7 @@ import javax.inject.Inject
class LoopDBImporter
@Inject constructor(
@AppScope val habitList: HabitList,
@AppScope val habitGroupList: HabitGroupList,
@AppScope val modelFactory: ModelFactory,
@AppScope val opener: DatabaseOpener,
@AppScope val runner: CommandRunner,
@ -74,26 +79,66 @@ class LoopDBImporter
helper.migrateTo(DATABASE_VERSION)
val habitsRepository = Repository(HabitRecord::class.java, db)
val habitGroupsRepository = Repository(HabitGroupRecord::class.java, db)
val entryRepository = Repository(EntryRecord::class.java, db)
for (groupRecord in habitGroupsRepository.findAll("order by position")) {
var hgr = habitGroupList.getByUUID(groupRecord.uuid)
if (hgr == null) {
hgr = modelFactory.buildHabitGroup()
groupRecord.id = null
groupRecord.copyTo(hgr)
CreateHabitGroupCommand(modelFactory, habitGroupList, hgr).run()
} else {
val modified = modelFactory.buildHabitGroup()
groupRecord.id = hgr.id
groupRecord.copyTo(modified)
EditHabitGroupCommand(habitGroupList, hgr.id!!, modified).run()
}
}
for (habitRecord in habitsRepository.findAll("order by position")) {
var habit = habitList.getByUUID(habitRecord.uuid)
var habit = habitList.getByUUID(habitRecord.uuid) ?: habitGroupList.getHabitByUUID(habitRecord.uuid)
val entryRecords = entryRepository.findAll("where habit = ?", habitRecord.id.toString())
if (habit == null) {
habit = modelFactory.buildHabit()
habitRecord.id = null
habitRecord.copyTo(habit)
CreateHabitCommand(modelFactory, habitList, habit).run()
val list = if (habit.groupId != null) {
val hgr = habitGroupList.getByUUID(habit.groupUUID)
if (hgr != null) {
habit.group = hgr
habit.groupId = hgr.id
hgr.habitList
} else {
habit.group = null
habit.groupId = null
habit.groupUUID = null
habitList
}
} else {
habitList
}
CreateHabitCommand(modelFactory, list, habit).run()
} else {
val modified = modelFactory.buildHabit()
habitRecord.id = habit.id
habitRecord.copyTo(modified)
EditHabitCommand(habitList, habit.id!!, modified).run()
val list = if (habit.group != null) {
modified.group = habit.group
modified.groupId = habit.groupId
modified.groupUUID = habit.groupUUID
habit.group!!.habitList
} else {
habitList
}
EditHabitCommand(list, habit.id!!, modified).run()
}
// Reload saved version of the habit
habit = habitList.getByUUID(habitRecord.uuid)!!
habit = habitList.getByUUID(habitRecord.uuid) ?: habitGroupList.getHabitByUUID(habitRecord.uuid)!!
val entries = habit.originalEntries
// Import entries
@ -106,6 +151,9 @@ class LoopDBImporter
}
habit.recompute()
}
habitGroupList.forEach { it.recompute() }
habitGroupList.resort()
habitList.resort()
db.close()
}

@ -31,6 +31,7 @@ import org.isoron.uhabits.core.models.Timestamp
import org.isoron.uhabits.core.models.memory.MemoryModelFactory
import org.isoron.uhabits.core.tasks.SingleThreadTaskRunner
import org.isoron.uhabits.core.test.HabitFixtures
import org.isoron.uhabits.core.test.HabitGroupFixtures
import org.isoron.uhabits.core.utils.DateUtils.Companion.getStartOfTodayCalendar
import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime
import org.isoron.uhabits.core.utils.DateUtils.Companion.setStartDayOffset
@ -55,6 +56,7 @@ open class BaseUnitTest {
protected open lateinit var habitList: HabitList
protected open lateinit var habitGroupList: HabitGroupList
protected lateinit var fixtures: HabitFixtures
protected lateinit var groupFixtures: HabitGroupFixtures
protected lateinit var modelFactory: ModelFactory
protected lateinit var taskRunner: SingleThreadTaskRunner
protected open lateinit var commandRunner: CommandRunner
@ -84,6 +86,7 @@ open class BaseUnitTest {
habitList = spy(memoryModelFactory.buildHabitList())
habitGroupList = spy(memoryModelFactory.buildHabitGroupList())
fixtures = HabitFixtures(memoryModelFactory, habitList)
groupFixtures = HabitGroupFixtures(memoryModelFactory, habitList, habitGroupList)
modelFactory = memoryModelFactory
taskRunner = SingleThreadTaskRunner()
commandRunner = CommandRunner(taskRunner)

@ -190,6 +190,7 @@ class ImportTest : BaseUnitTest() {
val importer = GenericImporter(
LoopDBImporter(
habitList,
habitGroupList,
modelFactory,
databaseOpener,
commandRunner,

Loading…
Cancel
Save