]> git.ktnx.net Git - mobile-ledger.git/commitdiff
methods for duplication of a template with its data
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 13 Feb 2021 21:02:54 +0000 (23:02 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:19:43 +0000 (07:19 +0000)
app/src/main/java/net/ktnx/mobileledger/dao/TemplateHeaderDAO.java
app/src/main/java/net/ktnx/mobileledger/db/TemplateAccount.java
app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java
app/src/main/java/net/ktnx/mobileledger/db/TemplateWithAccounts.java

index 4ffd08b44f556b2e0376a4ba9cfd944f02c99f70..2d3d7ee48884eb3012218dcb70f1322d3fb8d793 100644 (file)
@@ -89,7 +89,11 @@ public abstract class TemplateHeaderDAO {
 
     @Transaction
     @Query("SELECT * FROM templates WHERE id = :id")
-    public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(Long id);
+    public abstract LiveData<TemplateWithAccounts> 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<TemplateWithAccounts> 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));
+        });
+    }
 
 }
index f697b4dd54466431da96c383107f3e68235acf50..b17250917cac753d482d758f4b8c5abc13b9b336 100644 (file)
@@ -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;
+    }
 }
index 68a11232af671cda89a9dba6439e0897142f3557..6f8e25df384725c28569a9a56a6c54215763c1f4 100644 (file)
@@ -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;
+    }
 }
index 60d56a5fbaa12aa5c7668ac7c75b953b05555544..8572167ddc63677bd416080ac2741875ac7d434c 100644 (file)
@@ -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;
+    }
 }