]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/db/TemplateHeader.java
fix crash when duplicating templates due to duplicating UUIDs
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / TemplateHeader.java
index 5422767f9e74d01ffbedb63059263692da8f66c7..994c33098454a5ca09be19e0eddc3b2e44b64bce 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2021 Damyan Ivanov.
+ * Copyright © 2022 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
@@ -21,21 +21,27 @@ import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.room.ColumnInfo;
 import androidx.room.Entity;
+import androidx.room.Index;
 import androidx.room.PrimaryKey;
 
 import net.ktnx.mobileledger.utils.Misc;
 
 import org.jetbrains.annotations.NotNull;
 
-@Entity(tableName = "templates")
+import java.util.UUID;
+
+@Entity(tableName = "templates",
+        indices = {@Index(name = "templates_uuid_idx", unique = true, value = "uuid")})
 public class TemplateHeader extends TemplateBase {
     @PrimaryKey(autoGenerate = true)
-    @NonNull
-    private Long id;
+    private long id;
     @ColumnInfo(name = "name")
     @NonNull
     private String name;
     @NonNull
+    @ColumnInfo
+    private String uuid;
+    @NonNull
     @ColumnInfo(name = "regular_expression")
     private String regularExpression;
     @ColumnInfo(name = "test_text")
@@ -60,11 +66,46 @@ public class TemplateHeader extends TemplateBase {
     private Integer dateDay;
     @ColumnInfo(name = "date_day_match_group")
     private Integer dateDayMatchGroup;
+    @ColumnInfo(name = "is_fallback")
+    private boolean isFallback;
     public TemplateHeader(@NotNull Long id, @NonNull String name,
                           @NonNull String regularExpression) {
         this.id = id;
         this.name = name;
         this.regularExpression = regularExpression;
+        this.uuid = UUID.randomUUID()
+                        .toString();
+    }
+    public TemplateHeader(TemplateHeader origin) {
+        id = origin.id;
+        name = origin.name;
+        uuid = origin.uuid;
+        regularExpression = origin.regularExpression;
+        testText = origin.testText;
+        transactionDescription = origin.transactionDescription;
+        transactionDescriptionMatchGroup = origin.transactionDescriptionMatchGroup;
+        transactionComment = origin.transactionComment;
+        transactionCommentMatchGroup = origin.transactionCommentMatchGroup;
+        dateYear = origin.dateYear;
+        dateYearMatchGroup = origin.dateYearMatchGroup;
+        dateMonth = origin.dateMonth;
+        dateMonthMatchGroup = origin.dateMonthMatchGroup;
+        dateDay = origin.dateDay;
+        dateDayMatchGroup = origin.dateDayMatchGroup;
+        isFallback = origin.isFallback;
+    }
+    @NonNull
+    public String getUuid() {
+        return uuid;
+    }
+    public void setUuid(@NonNull String uuid) {
+        this.uuid = uuid;
+    }
+    public boolean isFallback() {
+        return isFallback;
+    }
+    public void setFallback(boolean fallback) {
+        isFallback = fallback;
     }
     public String getTestText() {
         return testText;
@@ -102,11 +143,10 @@ public class TemplateHeader extends TemplateBase {
     public void setDateDay(Integer dateDay) {
         this.dateDay = dateDay;
     }
-    @NonNull
-    public Long getId() {
+    public long getId() {
         return id;
     }
-    public void setId(@NonNull Long id) {
+    public void setId(long id) {
         this.id = id;
     }
     @NonNull
@@ -176,4 +216,12 @@ 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 = 0;
+        dup.uuid = UUID.randomUUID()
+                       .toString();
+
+        return dup;
+    }
 }