From ac02090e6042d34afc00e2dde45e70f49e2dbedc Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 29 Jan 2021 05:18:45 +0000 Subject: [PATCH] Room database class plus a couple of DAO classes --- .../ktnx/mobileledger/dao/CurrencyDAO.java | 52 +++++++ .../mobileledger/dao/PatternAccountDAO.java | 52 +++++++ .../mobileledger/dao/PatternHeaderDAO.java | 53 +++++++ .../net/ktnx/mobileledger/db/Currency.java | 52 +++++++ .../java/net/ktnx/mobileledger/db/DB.java | 32 ++++ .../ktnx/mobileledger/db/PatternAccount.java | 118 +++++++++++++++ .../net/ktnx/mobileledger/db/PatternBase.java | 20 +++ .../ktnx/mobileledger/db/PatternHeader.java | 141 ++++++++++++++++++ 8 files changed, 520 insertions(+) create mode 100644 app/src/main/java/net/ktnx/mobileledger/dao/CurrencyDAO.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/dao/PatternAccountDAO.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/dao/PatternHeaderDAO.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/db/Currency.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/db/DB.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/db/PatternAccount.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/db/PatternBase.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/db/PatternHeader.java diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/CurrencyDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/CurrencyDAO.java new file mode 100644 index 00000000..f650ff2d --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/dao/CurrencyDAO.java @@ -0,0 +1,52 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.Query; +import androidx.room.Update; + +import net.ktnx.mobileledger.db.Currency; + +import java.util.List; + +@Dao +public interface CurrencyDAO { + @Insert + void insert(Currency... items); + + @Update + void update(Currency... items); + + @Delete + void delete(Currency item); + + @Query("SELECT * FROM currencies") + LiveData> getCurrencies(); + + @Query("SELECT * FROM currencies WHERE id = :id") + LiveData getCurrencyById(Long id); + +// not useful for now +// @Transaction +// @Query("SELECT * FROM patterns") +// List getPatternsWithAccounts(); +} diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/PatternAccountDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/PatternAccountDAO.java new file mode 100644 index 00000000..f33bdde7 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/dao/PatternAccountDAO.java @@ -0,0 +1,52 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.Query; +import androidx.room.Update; + +import net.ktnx.mobileledger.db.PatternAccount; + +import java.util.List; + +@Dao +public interface PatternAccountDAO { + @Insert + Long insert(PatternAccount item); + + @Update + void update(PatternAccount... items); + + @Delete + void delete(PatternAccount item); + + @Query("SELECT * FROM pattern_accounts WHERE pattern_id=:pattern_id") + LiveData> getPatternAccounts(Long pattern_id); + + @Query("SELECT * FROM pattern_accounts WHERE id = :id") + LiveData getPatternAccountById(Long id); + +// not useful for now +// @Transaction +// @Query("SELECT * FROM patterns") +// List getPatternsWithAccounts(); +} diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/PatternHeaderDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/PatternHeaderDAO.java new file mode 100644 index 00000000..40659b5b --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/dao/PatternHeaderDAO.java @@ -0,0 +1,53 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.dao; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; +import androidx.room.Update; + +import net.ktnx.mobileledger.db.PatternHeader; + +import java.util.List; + +@Dao +public interface PatternHeaderDAO { + @Insert(onConflict = OnConflictStrategy.REPLACE) + long insert(PatternHeader item); + + @Update + void update(PatternHeader... items); + + @Delete + void delete(PatternHeader item); + + @Query("SELECT * FROM patterns") + LiveData> getPatterns(); + + @Query("SELECT * FROM patterns WHERE id = :id") + LiveData getPattern(Long id); + +// not useful for now +// @Transaction +// @Query("SELECT * FROM patterns") +// List getPatternsWithAccounts(); +} diff --git a/app/src/main/java/net/ktnx/mobileledger/db/Currency.java b/app/src/main/java/net/ktnx/mobileledger/db/Currency.java new file mode 100644 index 00000000..80c72cf5 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/Currency.java @@ -0,0 +1,52 @@ +package net.ktnx.mobileledger.db; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +@Entity(tableName = "currencies") +public class Currency { + @PrimaryKey(autoGenerate = true) + @NonNull + private final Long id; + @NonNull + private String name; + @NonNull + private String position; + @NonNull + @ColumnInfo(name = "has_gap") + private Boolean hasGap; + public Currency(@NonNull Long id, @NonNull String name, @NonNull String position, + @NonNull Boolean hasGap) { + this.id = id; + this.name = name; + this.position = position; + this.hasGap = hasGap; + } + @NonNull + public Long getId() { + return id; + } + @NonNull + public String getName() { + return name; + } + public void setName(@NonNull String name) { + this.name = name; + } + @NonNull + public String getPosition() { + return position; + } + public void setPosition(@NonNull String position) { + this.position = position; + } + @NonNull + public Boolean getHasGap() { + return hasGap; + } + public void setHasGap(@NonNull Boolean hasGap) { + this.hasGap = hasGap; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/db/DB.java b/app/src/main/java/net/ktnx/mobileledger/db/DB.java new file mode 100644 index 00000000..9c2bc113 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/DB.java @@ -0,0 +1,32 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.db; + +import androidx.room.Database; +import androidx.room.RoomDatabase; + +import net.ktnx.mobileledger.dao.CurrencyDAO; +import net.ktnx.mobileledger.dao.PatternAccountDAO; +import net.ktnx.mobileledger.dao.PatternHeaderDAO; + +@Database(version = 49, entities = {PatternHeader.class, PatternAccount.class, Currency.class}) +abstract public class DB extends RoomDatabase { + public abstract PatternHeaderDAO getPatternDAO(); + public abstract PatternAccountDAO getPatternAccountDAO(); + public abstract CurrencyDAO getCurrencyDAO(); +} diff --git a/app/src/main/java/net/ktnx/mobileledger/db/PatternAccount.java b/app/src/main/java/net/ktnx/mobileledger/db/PatternAccount.java new file mode 100644 index 00000000..fe8769bf --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/PatternAccount.java @@ -0,0 +1,118 @@ +package net.ktnx.mobileledger.db; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.ForeignKey; +import androidx.room.Index; +import androidx.room.PrimaryKey; + +import org.jetbrains.annotations.NotNull; + +@Entity(tableName = "pattern_accounts", + indices = {@Index(name = "un_pattern_accounts", unique = true, value = "id")}, + foreignKeys = {@ForeignKey(childColumns = "pattern_id", parentColumns = "id", + entity = PatternHeader.class), + @ForeignKey(childColumns = "currency", parentColumns = "id", + entity = Currency.class) + }) +public class PatternAccount extends PatternBase { + @NonNull + @ColumnInfo(name = "pattern_id") + private Long patternId; + @PrimaryKey(autoGenerate = true) + @NotNull + private Long id; + @ColumnInfo(name = "acc") + private String accountName; + @ColumnInfo(name = "position") + @NonNull + private Long position; + @ColumnInfo(name = "acc_match_group") + private Integer accountNameMatchGroup; + @ColumnInfo(name = "currency") + private Integer currency; + @ColumnInfo(name = "currency_match_group") + private Integer currencyMatchGroup; + @ColumnInfo(name = "amount") + private Float amount; + @ColumnInfo(name = "amount_match_group") + private Integer amountMatchGroup; + @ColumnInfo(name = "comment") + private String accountComment; + @ColumnInfo(name = "comment_match_group") + private Integer accountCommentMatchGroup; + public PatternAccount(@NotNull Long id, @NonNull Long patternId, @NonNull Long position) { + this.id = id; + this.patternId = patternId; + this.position = position; + } + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public @NotNull Long getPatternId() { + return patternId; + } + public void setPatternId(@NonNull Long patternId) { + this.patternId = patternId; + } + @NonNull + public String getAccountName() { + return accountName; + } + public void setAccountName(@NonNull String accountName) { + this.accountName = accountName; + } + @NonNull + public Long getPosition() { + return position; + } + public void setPosition(@NonNull Long position) { + this.position = position; + } + public Integer getAccountNameMatchGroup() { + return accountNameMatchGroup; + } + public void setAccountNameMatchGroup(Integer accountNameMatchGroup) { + this.accountNameMatchGroup = accountNameMatchGroup; + } + public Integer getCurrency() { + return currency; + } + public void setCurrency(Integer currency) { + this.currency = currency; + } + public Integer getCurrencyMatchGroup() { + return currencyMatchGroup; + } + public void setCurrencyMatchGroup(Integer currencyMatchGroup) { + this.currencyMatchGroup = currencyMatchGroup; + } + public Float getAmount() { + return amount; + } + public void setAmount(Float amount) { + this.amount = amount; + } + public Integer getAmountMatchGroup() { + return amountMatchGroup; + } + public void setAmountMatchGroup(Integer amountMatchGroup) { + this.amountMatchGroup = amountMatchGroup; + } + public String getAccountComment() { + return accountComment; + } + public void setAccountComment(String accountComment) { + this.accountComment = accountComment; + } + public Integer getAccountCommentMatchGroup() { + return accountCommentMatchGroup; + } + public void setAccountCommentMatchGroup(Integer accountCommentMatchGroup) { + this.accountCommentMatchGroup = accountCommentMatchGroup; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/db/PatternBase.java b/app/src/main/java/net/ktnx/mobileledger/db/PatternBase.java new file mode 100644 index 00000000..5d048e84 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/PatternBase.java @@ -0,0 +1,20 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.db; + +public class PatternBase {} diff --git a/app/src/main/java/net/ktnx/mobileledger/db/PatternHeader.java b/app/src/main/java/net/ktnx/mobileledger/db/PatternHeader.java new file mode 100644 index 00000000..e518bb09 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/db/PatternHeader.java @@ -0,0 +1,141 @@ +package net.ktnx.mobileledger.db; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.Index; +import androidx.room.PrimaryKey; + +import org.jetbrains.annotations.NotNull; + +@Entity(tableName = "patterns", + indices = {@Index(name = "un_patterns_id", value = "id", unique = true)}) +public class PatternHeader extends PatternBase { + @PrimaryKey(autoGenerate = true) + @NonNull + private Long id; + @ColumnInfo(name = "name") + @NonNull + private String name; + @ColumnInfo(name = "position") + @NonNull + private Long position; + @NonNull + @ColumnInfo(name = "regular_expression") + private String regularExpression; + @ColumnInfo(name = "transaction_description") + private String transactionDescription; + @ColumnInfo(name = "transaction_description_match_group") + private Integer transactionDescriptionMatchGroup; + @ColumnInfo(name = "transaction_comment") + private String transactionComment; + @ColumnInfo(name = "transaction_comment_match_group") + private Integer transactionCommentMatchGroup; + @ColumnInfo(name = "date_year") + private Integer dateYear; + @ColumnInfo(name = "date_year_match_group") + private Integer dateYearMatchGroup; + @ColumnInfo(name = "date_month") + private Integer dateMonth; + @ColumnInfo(name = "date_month_match_group") + private Integer dateMonthMatchGroup; + @ColumnInfo(name = "date_day") + private Integer dateDay; + @ColumnInfo(name = "date_day_match_group") + private Integer dateDayMatchGroup; + public PatternHeader(@NotNull Long id, @NonNull String name, @NotNull Long position, + @NonNull String regularExpression) { + this.id = id; + this.name = name; + this.position = position; + this.regularExpression = regularExpression; + } + public Integer getTransactionDescriptionMatchGroup() { + return transactionDescriptionMatchGroup; + } + public void setTransactionDescriptionMatchGroup(Integer transactionDescriptionMatchGroup) { + this.transactionDescriptionMatchGroup = transactionDescriptionMatchGroup; + } + public Integer getTransactionCommentMatchGroup() { + return transactionCommentMatchGroup; + } + public void setTransactionCommentMatchGroup(Integer transactionCommentMatchGroup) { + this.transactionCommentMatchGroup = transactionCommentMatchGroup; + } + public Integer getDateYear() { + return dateYear; + } + public void setDateYear(Integer dateYear) { + this.dateYear = dateYear; + } + public Integer getDateMonth() { + return dateMonth; + } + public void setDateMonth(Integer dateMonth) { + this.dateMonth = dateMonth; + } + public Integer getDateDay() { + return dateDay; + } + public void setDateDay(Integer dateDay) { + this.dateDay = dateDay; + } + @NonNull + public Long getId() { + return id; + } + public void setId(@NonNull Long id) { + this.id = id; + } + @NonNull + public String getName() { + return name; + } + public void setName(@NonNull String name) { + this.name = name; + } + @NonNull + public Long getPosition() { + return position; + } + public void setPosition(@NonNull Long position) { + this.position = position; + } + @NonNull + public String getRegularExpression() { + return regularExpression; + } + public void setRegularExpression(@NonNull String regularExpression) { + this.regularExpression = regularExpression; + } + public String getTransactionDescription() { + return transactionDescription; + } + public void setTransactionDescription(String transactionDescription) { + this.transactionDescription = transactionDescription; + } + public String getTransactionComment() { + return transactionComment; + } + public void setTransactionComment(String transactionComment) { + this.transactionComment = transactionComment; + } + public Integer getDateYearMatchGroup() { + return dateYearMatchGroup; + } + public void setDateYearMatchGroup(Integer dateYearMatchGroup) { + this.dateYearMatchGroup = dateYearMatchGroup; + } + public Integer getDateMonthMatchGroup() { + return dateMonthMatchGroup; + } + public void setDateMonthMatchGroup(Integer dateMonthMatchGroup) { + this.dateMonthMatchGroup = dateMonthMatchGroup; + } + public Integer getDateDayMatchGroup() { + return dateDayMatchGroup; + } + public void setDateDayMatchGroup(Integer dateDayMatchGroup) { + this.dateDayMatchGroup = dateDayMatchGroup; + } +} -- 2.39.2