package controllers;

import models.CommentThread;
import models.enumeration.Operation;
import models.resource.Resource;
import play.db.ebean.Transactional;
import play.mvc.Controller;
import play.mvc.Result;
import utils.AccessControl;

import static models.enumeration.ResourceType.getValue;

public class CommentThreadApp extends Controller {

    @Transactional
    public static Result updateState(Long id, CommentThread.ThreadState state) {
        CommentThread thread = CommentThread.find.byId(id);

        if (thread == null) {
            return notFound();
        }

        if (!AccessControl.isAllowed(UserApp.currentUser(), thread.asResource(), Operation.REOPEN)) {
            return forbidden();
        }

        thread.state = state;
        thread.update();

        return ok();
    }

    public static Result open(Long id) {
        return updateState(id, CommentThread.ThreadState.OPEN);
    }

    public static Result close(Long id) {
        return updateState(id, CommentThread.ThreadState.CLOSED);
    }
}

@@ -85,41 +85,8 @@
 on;%0A
-import models.resource.Resource;%0A
 impo
@@ -223,41 +223,93 @@
 els.
-enumeration.ResourceType.getValue
+CommentThread.ThreadState.CLOSED;%0Aimport static models.CommentThread.ThreadState.OPEN
 ;%0A%0Ap
@@ -584,32 +584,362 @@
 d();%0A        %7D%0A%0A
+        Operation operation;%0A%0A        switch(state) %7B%0A            case OPEN:%0A                operation = Operation.REOPEN;%0A                break;%0A            case CLOSED:%0A                operation = Operation.CLOSE;%0A                break;%0A            default:%0A                throw new UnsupportedOperationException();%0A        %7D%0A%0A
         if (!Acc
@@ -1003,25 +1003,25 @@
 urce(), 
-O
+o
 peration
 .REOPEN)
@@ -1012,23 +1012,16 @@
 peration
-.REOPEN
 )) %7B%0A   
@@ -1220,34 +1220,8 @@
 id, 
-CommentThread.ThreadState.
 OPEN
@@ -1307,34 +1307,8 @@
 id, 
-CommentThread.ThreadState.
 CLOS

Fix a possible access control bug