Search in sources :

Example 11 with EnvironmentForumArea

use of fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea in project muikku by otavanopisto.

the class ForumRESTService method updateThread.

@PUT
@Path("/areas/{AREAID}/threads/{THREADID}")
@RESTPermit(handling = Handling.INLINE)
public Response updateThread(@PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, ForumThreadRESTModel updThread) {
    ForumThread forumThread = forumController.getForumThread(threadId);
    if (forumThread == null) {
        return Response.status(Status.NOT_FOUND).entity("Forum thread not found").build();
    }
    ForumArea forumArea = forumController.getForumArea(areaId);
    if (forumArea == null) {
        return Response.status(Status.NOT_FOUND).entity("Forum area not found").build();
    }
    if (!(forumArea instanceof EnvironmentForumArea)) {
        logger.severe(String.format("Trying to update non environment forum thread (%d) from environment endpoint", forumThread.getId()));
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (!forumArea.getId().equals(forumThread.getForumArea().getId())) {
        return Response.status(Status.NOT_FOUND).entity("Forum thread not found from the specified area").build();
    }
    if (!forumThread.getId().equals(threadId)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (sessionController.hasPermission(MuikkuPermissions.OWNER, forumThread) || sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_EDIT_ENVIRONMENT_MESSAGES)) {
        // User needs permission to change the value of these parameters
        if (!forumThread.getSticky().equals(updThread.getSticky()) || !forumThread.getLocked().equals(updThread.getLocked())) {
            if (!sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_LOCK_OR_STICKIFY_MESSAGES))
                return Response.status(Status.BAD_REQUEST).build();
        }
        forumController.updateForumThread(forumThread, updThread.getTitle(), updThread.getMessage(), updThread.getSticky(), updThread.getLocked());
        long numReplies = forumController.getThreadReplyCount(forumThread);
        ForumThreadRESTModel result = new ForumThreadRESTModel(forumThread.getId(), forumThread.getTitle(), forumThread.getMessage(), forumThread.getCreator(), forumThread.getCreated(), forumThread.getForumArea().getId(), forumThread.getSticky(), forumThread.getLocked(), forumThread.getUpdated(), numReplies, forumThread.getLastModified());
        return Response.ok(result).build();
    } else {
        return Response.status(Status.FORBIDDEN).build();
    }
}
Also used : ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) PUT(javax.ws.rs.PUT)

Example 12 with EnvironmentForumArea

use of fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea in project muikku by otavanopisto.

the class ForumRESTService method listReplies.

@GET
@Path("/areas/{AREAID}/threads/{THREADID}/replies")
@RESTPermit(handling = Handling.INLINE)
public Response listReplies(@PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
    try {
        ForumArea forumArea = forumController.getForumArea(areaId);
        if (forumArea == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum area not found").build();
        }
        ForumThread forumThread = forumController.getForumThread(threadId);
        if (forumThread == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread not found").build();
        }
        if (!(forumArea instanceof EnvironmentForumArea)) {
            logger.severe(String.format("Trying to create new thread to non environment area (%d) from environment endpoint", forumArea.getId()));
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_READ_ENVIRONMENT_MESSAGES)) {
            if (!forumArea.getId().equals(forumThread.getForumArea().getId())) {
                return Response.status(Status.NOT_FOUND).entity("Forum thread not found from the specified area").build();
            }
            List<ForumThreadReply> replies = forumController.listForumThreadReplies(forumThread, firstResult, maxResults);
            return Response.ok(createRestModel(replies.toArray(new ForumThreadReply[0]))).build();
        } else
            return Response.status(Status.FORBIDDEN).build();
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Listing forum thread replies failed", e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
}
Also used : ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) ForumThreadReply(fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 13 with EnvironmentForumArea

use of fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea in project muikku by otavanopisto.

the class ForumRESTService method createForumArea.

@POST
@Path("/areas")
@RESTPermit(ForumResourcePermissionCollection.FORUM_CREATEENVIRONMENTFORUM)
public Response createForumArea(ForumAreaRESTModel newForum) {
    EnvironmentForumArea forumArea = forumController.createEnvironmentForumArea(newForum.getName(), newForum.getDescription(), newForum.getGroupId());
    ForumAreaRESTModel result = new ForumAreaRESTModel(forumArea.getId(), forumArea.getName(), forumArea.getDescription(), forumArea.getGroup() != null ? forumArea.getGroup().getId() : null, 0l);
    return Response.ok(result).build();
}
Also used : EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 14 with EnvironmentForumArea

use of fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea in project muikku by otavanopisto.

the class ForumRESTService method findReply.

@GET
@Path("/areas/{AREAID}/threads/{THREADID}/replies/{REPLYID}")
@RESTPermit(handling = Handling.INLINE)
public Response findReply(@PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, @PathParam("REPLYID") Long replyId) {
    try {
        ForumArea forumArea = forumController.getForumArea(areaId);
        if (forumArea == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum area not found").build();
        }
        ForumThread forumThread = forumController.getForumThread(threadId);
        if (forumThread == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread not found").build();
        }
        if (!forumArea.getId().equals(forumThread.getForumArea().getId())) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread not found from the specified area").build();
        }
        ForumThreadReply threadReply = forumController.getForumThreadReply(replyId);
        if (threadReply == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread reply not found").build();
        }
        if (!threadReply.getThread().getId().equals(forumThread.getId())) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread reply not found from the specified thread").build();
        }
        if (!(forumArea instanceof EnvironmentForumArea)) {
            logger.severe(String.format("Trying to find thread reply for to non environment area (%d) from environment endpoint", forumArea.getId()));
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_READ_ENVIRONMENT_MESSAGES)) {
            return Response.ok(createRestModel(threadReply)).build();
        } else {
            return Response.status(Status.FORBIDDEN).build();
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Finding forum thread reply failed", e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
}
Also used : ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) ForumThreadReply(fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 15 with EnvironmentForumArea

use of fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea in project muikku by otavanopisto.

the class ForumRESTService method updateReply.

@PUT
@Path("/areas/{AREAID}/threads/{THREADID}/replies/{REPLYID}")
@RESTPermit(handling = Handling.INLINE)
public Response updateReply(@PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, @PathParam("REPLYID") Long replyId, ForumThreadReplyRESTModel reply) {
    try {
        ForumArea forumArea = forumController.getForumArea(areaId);
        if (forumArea == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum area not found").build();
        }
        ForumThread forumThread = forumController.getForumThread(threadId);
        if (forumThread == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread not found").build();
        }
        if (!forumArea.getId().equals(forumThread.getForumArea().getId())) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread not found from the specified area").build();
        }
        ForumThreadReply threadReply = forumController.getForumThreadReply(replyId);
        if (threadReply == null) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread reply not found").build();
        }
        if (!threadReply.getThread().getId().equals(forumThread.getId())) {
            return Response.status(Status.NOT_FOUND).entity("Forum thread reply not found from the specified thread").build();
        }
        if (!reply.getId().equals(replyId)) {
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (!(forumArea instanceof EnvironmentForumArea)) {
            logger.severe(String.format("Trying to edit thread reply for non environment area (%d) from environment endpoint", forumArea.getId()));
            return Response.status(Status.BAD_REQUEST).build();
        }
        if (sessionController.hasPermission(MuikkuPermissions.OWNER, threadReply) || sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_EDIT_ENVIRONMENT_MESSAGES)) {
            forumController.updateForumThreadReply(threadReply, reply.getMessage());
            return Response.ok(createRestModel(threadReply)).build();
        } else {
            return Response.status(Status.FORBIDDEN).build();
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Finding forum thread reply failed", e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
}
Also used : ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) ForumThreadReply(fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) PUT(javax.ws.rs.PUT)

Aggregations

EnvironmentForumArea (fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)17 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)12 Path (javax.ws.rs.Path)12 ForumArea (fi.otavanopisto.muikku.plugins.forum.model.ForumArea)10 ForumThread (fi.otavanopisto.muikku.plugins.forum.model.ForumThread)9 GET (javax.ws.rs.GET)6 ForumThreadReply (fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply)4 ArrayList (java.util.ArrayList)3 POST (javax.ws.rs.POST)3 PUT (javax.ws.rs.PUT)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ResourceRights (fi.otavanopisto.muikku.model.security.ResourceRights)1 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 ForumAreaGroup (fi.otavanopisto.muikku.plugins.forum.model.ForumAreaGroup)1 WorkspaceForumArea (fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea)1 HashMap (java.util.HashMap)1 EntityManager (javax.persistence.EntityManager)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CacheControl (javax.ws.rs.core.CacheControl)1