doortts doortts 2018-01-29
issue: Issue sharing feature - Add model
@609e2111414e840159923f24eecfe6244b28bef6
app/models/Issue.java
--- app/models/Issue.java
+++ app/models/Issue.java
@@ -75,6 +75,9 @@
     @OneToMany(cascade = CascadeType.ALL, mappedBy="issue")
     public List<IssueEvent> events;
 
+    @OneToMany(cascade = CascadeType.ALL, mappedBy = "issue")
+    public Set<IssueSharer> sharers = new LinkedHashSet<>();
+
     @ManyToMany(cascade = CascadeType.ALL)
     @JoinTable(
             name = "issue_voter",
@@ -675,4 +678,17 @@
                 .eq("state", State.OPEN)
                 .findRowCount();
     }
+
+    public IssueSharer findSharerByUserId(Long id){
+        for (IssueSharer sharer : sharers) {
+            if (sharer.user.id.equals(id)) {
+                return sharer;
+            }
+        }
+        return null;
+    }
+
+    public List<IssueSharer> getSortedSharer() {
+        return new ArrayList<>(sharers);
+    }
 }
 
app/models/IssueSharer.java (added)
+++ app/models/IssueSharer.java
@@ -0,0 +1,49 @@
+/**
+ * Yona, 21st Century Project Hosting SW
+ * <p>
+ * Copyright Yona & Yobi Authors & NAVER Corp. & NAVER LABS Corp.
+ * https://yona.io
+ **/
+
+package models;
+
+import play.db.ebean.Model;
+
+import javax.persistence.*;
+import java.util.Date;
+
+@Entity
+public class IssueSharer extends Model {
+    private static final long serialVersionUID = 6199025373911652405L;
+
+    @Id
+    public Long id;
+
+    public Date created;
+
+    public String loginId;
+
+    @OneToOne
+    public User user;
+
+    @OneToOne
+    public Issue issue;
+
+    public static final Finder<Long, IssueSharer> find = new Finder<>(Long.class,
+            IssueSharer.class);
+
+    public static IssueSharer createSharer(String loginId, Issue issue) {
+        IssueSharer issueSharer = new IssueSharer();
+        issueSharer.loginId = loginId;
+        issueSharer.created = new Date();
+        issueSharer.issue = issue;
+        issueSharer.user = User.findByLoginId(loginId);
+
+        if (issueSharer.user == null) {
+            String errorMsg  = "Wrong loginId for issue sharing: " + loginId;
+            play.Logger.error(errorMsg);
+            throw new IllegalArgumentException(errorMsg);
+        }
+        return issueSharer;
+    }
+}
 
conf/evolutions/default/19.sql (added)
+++ conf/evolutions/default/19.sql
@@ -0,0 +1,19 @@
+# --- !Ups
+CREATE TABLE issue_sharer (
+  id                        BIGINT AUTO_INCREMENT NOT NULL,
+  created                   DATE,
+  login_id                  VARCHAR(255),
+  user_id                   BIGINT,
+  issue_id                   BIGINT,
+  CONSTRAINT pk_issue_sharer PRIMARY KEY (id),
+  CONSTRAINT fk_issue_sharer_user FOREIGN KEY (user_id) REFERENCES n4user (id) on DELETE CASCADE,
+  CONSTRAINT fk_issue_sharer_issue FOREIGN KEY (issue_id) REFERENCES issue (id) on DELETE CASCADE
+)
+row_format=compressed, key_block_size=8;
+
+CREATE index ix_issue_sharer_login_id ON issue_sharer (login_id);
+CREATE index ix_issue_sharer_user_id ON issue_sharer (user_id);
+CREATE index ix_issue_sharer_issue_id ON issue_sharer (issue_id);
+
+# --- !Downs
+DROP TABLE issue_sharer;
Add a comment
List