[Notice] Announcing the End of Demo Server [Read me]
Yi EungJun 2013-04-18
issue,posting: refactoring.
Between controllers for issue and posting,
* Make them be similar to each other.
* Remove duplication
@0d498b808cd85ab3d18f499be6b503b5d83feedf
app/controllers/AbstractPostingApp.java
--- app/controllers/AbstractPostingApp.java
+++ app/controllers/AbstractPostingApp.java
@@ -1,8 +1,13 @@
 package controllers;
 
+import play.db.ebean.Model;
+
+import models.resource.Resource;
+
 import models.*;
 import models.enumeration.Direction;
 import models.enumeration.Operation;
+import models.enumeration.ResourceType;
 
 import play.data.Form;
 import play.mvc.Call;
@@ -33,11 +38,8 @@
         }
     }
 
-    public static Result newComment(Comment comment, Form<? extends Comment> commentForm, Call redirectTo, Callback updateCommentContainer) throws IOException {
-        if (session(UserApp.SESSION_USERID) == null) {
-            flash(Constants.WARNING, "user.login.alert");
-            return redirect(redirectTo);
-        }
+    public static Result newComment(Comment comment, Form<? extends Comment> commentForm, Call redirectTo, Callback containerUpdater) throws IOException {
+        Project project = comment.asResource().getProject();
 
         if (commentForm.hasErrors()) {
             flash(Constants.WARNING, "board.comment.empty");
@@ -45,8 +47,7 @@
         }
 
         comment.setAuthor(UserApp.currentUser());
-        updateCommentContainer.run(); // this updates comment.issue or comment.posting;
-        Project project = comment.asResource().getProject();
+        containerUpdater.run(); // this updates comment.issue or comment.posting;
         comment.save();
 
         // Attach all of the files in the current user's temporary storage.
@@ -55,26 +56,12 @@
         return redirect(redirectTo);
     }
 
-    public static Result deletePosting(AbstractPosting posting, Call redirectTo) {
-        if (!AccessControl.isAllowed(UserApp.currentUser(), posting.asResource(), Operation.DELETE)) {
+    protected static Result delete(Model target, Resource resource, Call redirectTo) {
+        if (!AccessControl.isAllowed(UserApp.currentUser(), resource, Operation.DELETE)) {
             return forbidden();
         }
 
-        posting.delete();
-
-        Attachment.deleteAll(posting.asResource().getType(), posting.id);
-
-        return redirect(redirectTo);
-    }
-
-    public static Result deleteComment(Comment comment, Call redirectTo) {
-        if (!AccessControl.isAllowed(UserApp.currentUser(), comment.asResource(), Operation.DELETE)) {
-            return forbidden();
-        }
-
-        comment.delete();
-
-        Attachment.deleteAll(comment.asResource().getType(), comment.id);
+        target.delete();
 
         return redirect(redirectTo);
     }
app/controllers/BoardApp.java
--- app/controllers/BoardApp.java
+++ app/controllers/BoardApp.java
@@ -14,8 +14,11 @@
 import models.enumeration.Direction;
 import models.enumeration.Matching;
 import views.html.board.editPost;
+import views.html.board.post;
 import views.html.board.newPost;
 import views.html.board.postList;
+import views.html.board.notExistingPage;
+import views.html.project.unauthorized;
 
 import utils.AccessControl;
 import utils.Callback;
@@ -49,14 +52,15 @@
     }
 
     public static Result posts(String userName, String projectName, int pageNum) {
-        Form<SearchCondition> postParamForm = new Form<SearchCondition>(SearchCondition.class);
-        SearchCondition searchCondition = postParamForm.bindFromRequest().get();
-        searchCondition.pageNum = pageNum - 1;
         Project project = ProjectApp.getProject(userName, projectName);
 
         if (!AccessControl.isAllowed(UserApp.currentUser(), project.asResource(), Operation.READ)) {
             return unauthorized(views.html.project.unauthorized.render(project));
         }
+
+        Form<SearchCondition> postParamForm = new Form<SearchCondition>(SearchCondition.class);
+        SearchCondition searchCondition = postParamForm.bindFromRequest().get();
+        searchCondition.pageNum = pageNum - 1;
 
         ExpressionList<Posting> el = searchCondition.asExpressionList(project);
         Page<Posting> posts = el.findPagingList(ITEMS_PER_PAGE).getPage(searchCondition.pageNum);
@@ -81,21 +85,21 @@
     public static Result newPost(String userName, String projectName) {
         Form<Posting> postForm = new Form<Posting>(Posting.class).bindFromRequest();
         Project project = ProjectApp.getProject(userName, projectName);
+
         if (postForm.hasErrors()) {
-            flash(Constants.WARNING, "board.post.empty");
-
-            return redirect(routes.BoardApp.newPost(userName, projectName));
-        } else {
-            Posting post = postForm.get();
-            post.createdDate = JodaDateUtil.now();
-            post.setAuthor(UserApp.currentUser());
-            post.project = project;
-
-            post.save();
-
-            // Attach all of the files in the current user's temporary storage.
-            Attachment.attachFiles(UserApp.currentUser().id, post.asResource());
+            boolean isAllowedToNotice = ProjectUser.isAllowedToNotice(UserApp.currentUser(), project);
+            return badRequest(newPost.render(postForm.errors().toString(), postForm, project, isAllowedToNotice));
         }
+
+        Posting post = postForm.get();
+        post.createdDate = JodaDateUtil.now();
+        post.setAuthor(UserApp.currentUser());
+        post.project = project;
+
+        post.save();
+
+        // Attach all of the files in the current user's temporary storage.
+        Attachment.attachFiles(UserApp.currentUser().id, post.asResource());
 
         return redirect(routes.BoardApp.posts(project.owner, project.name, 1));
     }
@@ -109,51 +113,27 @@
         }
 
         if (post == null) {
-            flash(Constants.WARNING, "board.post.notExist");
-            return redirect(routes.BoardApp.posts(project.owner, project.name, 1));
-        } else {
-            Form<PostingComment> commentForm = new Form<PostingComment>(PostingComment.class);
-            return ok(views.html.board.post.render(post, commentForm, project));
+            return notFound(notExistingPage.render("title.post.notExistingPage", project));
         }
-    }
 
-    public static Result newComment(String userName, String projectName, Long postId) throws IOException {
-        final Posting post = Posting.finder.byId(postId);
-        Project project = post.project;
-        Call redirectTo = routes.BoardApp.post(project.owner, project.name, postId);
-        Form<PostingComment> commentForm = new Form<PostingComment>(PostingComment.class)
-                .bindFromRequest();
+        Form<PostingComment> commentForm = new Form<PostingComment>(PostingComment.class);
 
-        final PostingComment comment = commentForm.get();
-
-        return newComment(comment, commentForm, redirectTo, new Callback() {
-            @Override
-            public void run() {
-                comment.posting = post;
-            }
-        });
-    }
-
-    public static Result deletePost(String userName, String projectName, Long postId) {
-        Posting posting = Posting.finder.byId(postId);
-        Project project = posting.project;
-
-        return deletePosting(posting,
-                routes.BoardApp.posts(project.owner, project.name, 1));
+        return ok(views.html.board.post.render(post, commentForm, project));
     }
 
     public static Result editPostForm(String userName, String projectName, Long postId) {
-        Posting existPost = Posting.finder.byId(postId);
-        Form<Posting> editForm = new Form<Posting>(Posting.class).fill(existPost);
+        Posting posting = Posting.finder.byId(postId);
         Project project = ProjectApp.getProject(userName, projectName);
 
-        if (AccessControl.isAllowed(UserApp.currentUser(), existPost.asResource(), Operation.UPDATE)) {
-            boolean isAllowedToNotice = ProjectUser.isAllowedToNotice(UserApp.currentUser(), project);
-            return ok(editPost.render("board.post.modify", editForm, postId, project, isAllowedToNotice));
-        } else {
+        if (!AccessControl.isAllowed(UserApp.currentUser(), posting.asResource(), Operation.UPDATE)) {
             flash(Constants.WARNING, "board.notAuthor");
             return redirect(routes.BoardApp.post(project.owner, project.name, postId));
         }
+
+        Form<Posting> editForm = new Form<Posting>(Posting.class).fill(posting);
+        boolean isAllowedToNotice = ProjectUser.isAllowedToNotice(UserApp.currentUser(), project);
+
+        return ok(editPost.render("board.post.modify", editForm, postId, project, isAllowedToNotice));
     }
 
     public static Result editPost(String userName, String projectName, Long postId) {
@@ -170,12 +150,43 @@
         return editPosting(original, post, postForm, redirectTo, doNothing);
     }
 
+    public static Result deletePost(String userName, String projectName, Long postingId) {
+        Posting posting = Posting.finder.byId(postingId);
+        Project project = posting.project;
+        Call redirectTo = routes.BoardApp.posts(project.owner, project.name, 1);
+
+        return delete(posting, posting.asResource(), redirectTo);
+    }
+
+
+    public static Result newComment(String userName, String projectName, Long postId) throws IOException {
+        final Posting posting = Posting.finder.byId(postId);
+        Project project = posting.project;
+        Call redirectTo = routes.BoardApp.post(project.owner, project.name, postId);
+        Form<PostingComment> commentForm = new Form<PostingComment>(PostingComment.class)
+                .bindFromRequest();
+
+        if (commentForm.hasErrors()) {
+            return badRequest(commentForm.errors().toString());
+        }
+
+        final PostingComment comment = commentForm.get();
+
+        return newComment(comment, commentForm, redirectTo, new Callback() {
+            @Override
+            public void run() {
+                comment.posting = posting;
+            }
+        });
+    }
+
     public static Result deleteComment(String userName, String projectName, Long postId,
             Long commentId) {
         Comment comment = PostingComment.find.byId(commentId);
         Project project = comment.asResource().getProject();
+        Call redirectTo =
+                routes.BoardApp.post(project.owner, project.name, comment.getParent().id);
 
-        return deleteComment(comment,
-                routes.BoardApp.post(project.owner, project.name, comment.getParent().id));
+        return delete(comment, comment.asResource(), redirectTo);
     }
 }
app/controllers/IssueApp.java
--- app/controllers/IssueApp.java
+++ app/controllers/IssueApp.java
@@ -13,6 +13,7 @@
 import views.html.issue.issueList;
 import views.html.issue.newIssue;
 import views.html.issue.notExistingPage;
+import views.html.project.unauthorized;
 
 import utils.AccessControl;
 import utils.Callback;
@@ -34,6 +35,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import java.util.Map;
 
 import static com.avaje.ebean.Expr.contains;
 import static com.avaje.ebean.Expr.icontains;
@@ -125,26 +127,26 @@
         }
 
         Form<SearchCondition> issueParamForm = new Form<SearchCondition>(SearchCondition.class);
-        SearchCondition issueParam = issueParamForm.bindFromRequest().get();
-        issueParam.state = state;
-        issueParam.pageNum = pageNum - 1;
+        SearchCondition searchCondition = issueParamForm.bindFromRequest().get();
+        searchCondition.pageNum = pageNum - 1;
+        searchCondition.state = state;
 
         String[] labelIds = request().queryString().get("labelIds");
         if (labelIds != null) {
             for (String labelId : labelIds) {
-                issueParam.labelIds.add(Long.valueOf(labelId));
+                searchCondition.labelIds.add(Long.valueOf(labelId));
             }
         }
 
-        ExpressionList<Issue> el = issueParam.asExpressionList(project);
+        ExpressionList<Issue> el = searchCondition.asExpressionList(project);
 
         if (format.equals("xls")) {
             return issuesAsExcel(el, project);
         } else {
             Page<Issue> issues = el
-                .findPagingList(ITEMS_PER_PAGE).getPage(issueParam.pageNum);
+                .findPagingList(ITEMS_PER_PAGE).getPage(searchCondition.pageNum);
 
-            return ok(issueList.render("title.issueList", issues, issueParam, project));
+            return ok(issueList.render("title.issueList", issues, searchCondition, project));
         }
     }
 
@@ -169,16 +171,17 @@
         }
 
         if (issueInfo == null) {
-            return ok(notExistingPage.render("title.post.notExistingPage", project));
-        } else {
-            for (IssueLabel label: issueInfo.labels) {
-              label.refresh();
-            }
-            Form<Comment> commentForm = new Form<Comment>(Comment.class);
-            Issue targetIssue = Issue.finder.byId(issueId);
-            Form<Issue> editForm = new Form<Issue>(Issue.class).fill(targetIssue);
-            return ok(issue.render("title.issueDetail", issueInfo, editForm, commentForm, project));
+            return notFound(notExistingPage.render("title.post.notExistingPage", project));
         }
+
+        for (IssueLabel label: issueInfo.labels) {
+            label.refresh();
+        }
+
+        Form<Comment> commentForm = new Form<Comment>(Comment.class);
+        Form<Issue> editForm = new Form<Issue>(Issue.class).fill(Issue.finder.byId(issueId));
+
+        return ok(issue.render("title.issueDetail", issueInfo, editForm, commentForm, project));
     }
 
     public static Result newIssueForm(String userName, String projectName) {
@@ -195,36 +198,26 @@
     public static Result newIssue(String ownerName, String projectName) throws IOException {
         Form<Issue> issueForm = new Form<Issue>(Issue.class).bindFromRequest();
         Project project = ProjectApp.getProject(ownerName, projectName);
+
         if (issueForm.hasErrors()) {
             return badRequest(newIssue.render(issueForm.errors().toString(), issueForm, project));
-        } else {
-            Issue newIssue = issueForm.get();
-            newIssue.createdDate = JodaDateUtil.now();
-            newIssue.setAuthor(UserApp.currentUser());
-            newIssue.project = project;
-
-            newIssue.state = State.OPEN;
-            addLabels(newIssue.labels, request());
-
-            newIssue.save();
-
-            // Attach all of the files in the current user's temporary storage.
-            Attachment.attachFiles(UserApp.currentUser().id, newIssue.asResource());
         }
+
+        Issue newIssue = issueForm.get();
+        newIssue.createdDate = JodaDateUtil.now();
+        newIssue.setAuthor(UserApp.currentUser());
+        newIssue.project = project;
+
+        newIssue.state = State.OPEN;
+        addLabels(newIssue.labels, request());
+
+        newIssue.save();
+
+        // Attach all of the files in the current user's temporary storage.
+        Attachment.attachFiles(UserApp.currentUser().id, newIssue.asResource());
 
         return redirect(routes.IssueApp.issues(project.owner, project.name,
                 State.OPEN.state(), "html", 1));
-    }
-
-    public static Result editIssueForm(String userName, String projectName, Long id) {
-        Issue targetIssue = Issue.finder.byId(id);
-        Form<Issue> editForm = new Form<Issue>(Issue.class).fill(targetIssue);
-        Project project = ProjectApp.getProject(userName, projectName);
-        if (!AccessControl.isAllowed(UserApp.currentUser(), targetIssue.asResource(), Operation.UPDATE)) {
-            return unauthorized(views.html.project.unauthorized.render(project));
-        }
-
-        return ok(editIssue.render("title.editIssue", editForm, targetIssue, project));
     }
 
     public static void addLabels(Set<IssueLabel> labels, Http.Request request) {
@@ -236,6 +229,19 @@
             }
         }
     };
+
+    public static Result editIssueForm(String userName, String projectName, Long id) {
+        Issue issue = Issue.finder.byId(id);
+        Project project = ProjectApp.getProject(userName, projectName);
+
+        if (!AccessControl.isAllowed(UserApp.currentUser(), issue.asResource(), Operation.UPDATE)) {
+            return unauthorized(views.html.project.unauthorized.render(project));
+        }
+
+        Form<Issue> editForm = new Form<Issue>(Issue.class).fill(issue);
+
+        return ok(editIssue.render("title.editIssue", editForm, issue, project));
+    }
 
     public static Result editIssue(String userName, String projectName, Long id) throws IOException {
         Form<Issue> issueForm = new Form<Issue>(Issue.class).bindFromRequest();
@@ -261,9 +267,10 @@
     public static Result deleteIssue(String userName, String projectName, Long issueId) {
         Issue issue = Issue.finder.byId(issueId);
         Project project = issue.project;
+        Call redirectTo =
+            routes.IssueApp.issues(project.owner, project.name, State.OPEN.state(), "html", 1);
 
-        return deletePosting(issue, routes.IssueApp.issues(project.owner, project.name,
-                    State.OPEN.state(), "html", 1));
+        return delete(issue, issue.asResource(), redirectTo);
     }
 
     public static Result newComment(String userName, String projectName, Long issueId) throws IOException {
@@ -291,8 +298,9 @@
             Long commentId) {
         Comment comment = IssueComment.find.byId(commentId);
         Project project = comment.asResource().getProject();
+        Call redirectTo =
+            routes.IssueApp.issue(project.owner, project.name, comment.getParent().id);
 
-        return deleteComment(comment,
-                routes.IssueApp.issue(project.owner, project.name, comment.getParent().id));
+        return delete(comment, comment.asResource(), redirectTo);
     }
 }
app/models/AbstractPosting.java
--- app/models/AbstractPosting.java
+++ app/models/AbstractPosting.java
@@ -134,4 +134,9 @@
         authorLoginId = user.loginId;
         authorName = user.name;
     }
+
+    public void delete() {
+        Attachment.deleteAll(asResource().getType(), id);
+        super.delete();
+    }
 }
 
app/views/board/notExistingPage.scala.html (added)
+++ app/views/board/notExistingPage.scala.html
@@ -0,0 +1,10 @@
+@(title:String, project:Project)
+
+@import helper.twitterBootstrap._
+
+@main(title, project, utils.MenuType.NONE) {
+<div class="page">
+    <h1>존재하지 않는 게시물 입니다.</h1>
+    <a href="@routes.BoardApp.posts(project.owner, project.name)" class="btn">목록으로 가기</a>
+</div>
+}
Add a comment
List