From: Damyan Ivanov Date: Sat, 13 Feb 2021 21:02:54 +0000 (+0200) Subject: methods for duplication of a template with its data X-Git-Tag: v0.17.0~126 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=3aef8f6c15117aa4ea9abd687ad2c69e0f1f25bb;p=mobile-ledger.git methods for duplication of a template with its data --- diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java index 4ffd08b4..2d3d7ee4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java @@ -89,7 +89,11 @@ public abstract class TemplateHeaderDAO { @Transaction @Query("SELECT * FROM templates WHERE id = :id") - public abstract LiveData getTemplateWithAccounts(Long id); + public abstract LiveData getTemplateWithAccounts(@NonNull Long id); + + @Transaction + @Query("SELECT * FROM templates WHERE id = :id") + public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id); @Transaction public void insertSync(TemplateWithAccounts templateWithAccounts) { @@ -124,5 +128,22 @@ public abstract class TemplateHeaderDAO { } }); } + public void duplicateTemplateWitAccounts(@NonNull Long id, @Nullable + AsyncResultCallback callback) { + AsyncTask.execute(() -> { + TemplateWithAccounts src = getTemplateWithAccountsSync(id); + TemplateWithAccounts dup = src.createDuplicate(); + dup.header.setName(dup.header.getName()); + dup.header.setId(insertSync(dup.header)); + TemplateAccountDAO accDao = DB.get() + .getTemplateAccountDAO(); + for (TemplateAccount dupAcc : dup.accounts) { + dupAcc.setTemplateId(dup.header.getId()); + dupAcc.setId(accDao.insertSync(dupAcc)); + } + if (callback != null) + new Handler(Looper.getMainLooper()).post(() -> callback.onResult(dup)); + }); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/db/TemplateAccount.java b/app/src/main/java/net/ktnx/mobileledger/db/TemplateAccount.java index f697b4dd..b1725091 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/TemplateAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/TemplateAccount.java @@ -160,4 +160,11 @@ public class TemplateAccount extends TemplateBase { public void setAccountCommentMatchGroup(Integer accountCommentMatchGroup) { this.accountCommentMatchGroup = accountCommentMatchGroup; } + public TemplateAccount createDuplicate() { + TemplateAccount dup = new TemplateAccount(this); + dup.id = null; + dup.templateId = null; + + return dup; + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java b/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java index 68a11232..6f8e25df 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java @@ -192,4 +192,10 @@ public class TemplateHeader extends TemplateBase { Misc.equalIntegers(dateYear, o.dateYear) && Misc.equalIntegers(dateYearMatchGroup, o.dateYearMatchGroup); } + public TemplateHeader createDuplicate() { + TemplateHeader dup = new TemplateHeader(this); + dup.id = null; + + return dup; + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/db/TemplateWithAccounts.java b/app/src/main/java/net/ktnx/mobileledger/db/TemplateWithAccounts.java index 60d56a5f..8572167d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/db/TemplateWithAccounts.java +++ b/app/src/main/java/net/ktnx/mobileledger/db/TemplateWithAccounts.java @@ -42,4 +42,14 @@ public class TemplateWithAccounts { public Long getId() { return header.getId(); } + public TemplateWithAccounts createDuplicate() { + TemplateWithAccounts result = new TemplateWithAccounts(); + result.header = header.createDuplicate(); + result.accounts = new ArrayList<>(); + for (TemplateAccount acc : accounts) { + result.accounts.add(acc.createDuplicate()); + } + + return result; + } }