Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dk.medware.model
- import android.content.Context
- import android.util.Log
- import android.view.View
- import androidsync.BackendModel
- import dk.medware.mitforloeb.modules.account.change_password.ChangePasswordAsync
- import dk.medware.mitforloeb.modules.account.login.LoginAsync
- import dk.medware.mitforloeb.persistence.DistributedBackendModel
- import dk.medware.mitforloeb.util.DateUtil
- import dk.medware.sugarcipherlib.SugarRecord
- import dk.medware.sugarcipherlib.dsl.Ignore
- import dk.medware.sugarcipherlib.util.ReflectionUtil
- import net.danlew.android.joda.DateUtils
- import net.danlew.android.joda.JodaTimeAndroid
- import java.util.*
- /**
- * Created by Mads on 12/05/2016.
- */
- class User : SugarRecord() {
- var cpr = ""
- var currentCpr = ""
- var sessionKey: String? = null
- var rememberMe = false
- var mustChangePassword = false
- var gcmToken: String? = null
- var isLoggedIn = false
- var currentPatientGroupCode = ""
- var currentPatientGroupTitle = ""
- var hasCourse = false
- @Ignore
- private var currentPatientGroup: PatientGroup? = null
- @Ignore
- private var currentPatient: Patient? = null
- fun getPatient(): Patient? {
- currentPatient = try {
- if (SugarRecord.count<Patient>(Patient::class.java) == 0L) {
- Patient()
- } else {
- SugarRecord.find(Patient::class.java, "cpr = ?", this.currentCpr).first()
- }
- } catch (e: Exception) {
- Patient()
- }
- return currentPatient
- }
- fun logIn(view: View, context: Context, cpr: String, password: String, rememberMe: Boolean) {
- LoginAsync(view, context, cpr, password, rememberMe).execute()
- }
- fun changePassword(context: Context, newPassword: String, confirmNewPassword: String) {
- ChangePasswordAsync(context, newPassword, confirmNewPassword).execute()
- }
- fun logOut() {
- clearData()
- }
- fun hasActivePatientRelation(): Boolean = getActivePatientRelations().isNotEmpty()
- fun getActivePatientRelations(): List<PatientRelation> {
- Log.d("RELATIONS COUNT", SugarRecord.listAll(PatientRelation::class.java).count().toString())
- val relations = SugarRecord.find(PatientRelation::class.java, "ended_at IS NULL OR ended_at > ?", Date().time.toString()).toList()
- relations.forEach { Log.d("PGC", it.patientGroupId) }
- return relations
- }
- fun setCurrentPatientGroup(patientGroup: PatientGroup) {
- this.currentPatientGroup = patientGroup
- }
- fun getCurrentPatientGroup(): PatientGroup? {
- if (currentPatientGroup == null) {
- try {
- currentPatientGroup = SugarRecord.find(PatientGroup::class.java, "code = ?", currentPatientGroupCode)[0]
- } catch (e: IndexOutOfBoundsException) {
- e.printStackTrace()
- }
- }
- return currentPatientGroup
- }
- private fun clearData() {
- this.isLoggedIn = false
- this.sessionKey = null
- this.rememberMe = false
- this.mustChangePassword = false
- this.save()
- }
- fun clearStoredData(context: Context, deletePatient: Boolean = true) {
- try {
- val tablesToDelete = ReflectionUtil.getDomainClasses(context)
- tablesToDelete
- .filter { BackendModel::class.java.isAssignableFrom(it) }
- .forEach {
- try {
- if (it.toString() == "class dk.medware.model.Patient" && !deletePatient) {
- } else if (!isCourseObject(it)) {
- } else {
- SugarRecord.deleteAll(it)
- }
- SugarRecord.deleteAll(Attachment::class.java)
- } catch (ex: Exception) {
- ex.printStackTrace()
- }
- }
- } catch (e: NoClassDefFoundError) {
- e.printStackTrace()
- }
- }
- private fun isCourseObject(it: Any): Boolean = it is EducationCourse || it is EducationCourseSession || it is EducationCourseTemplate || it is EducationFamily
- companion object Factory {
- private var currentUser: User? = null
- fun getCurrent(): User? {
- if (currentUser == null) {
- if (count<User>(User::class.java) == 0L) {
- Log.d("Current User", "Creating new")
- currentUser = User()
- currentUser?.save()
- } else {
- currentUser = first(User::class.java)
- }
- }
- return currentUser
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement