Search in sources :

Example 11 with ForumThreadReply

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

the class ForumThreadReplyDAO method findLatestReplyByThread.

public ForumThreadReply findLatestReplyByThread(ForumThread thread) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ForumThreadReply> criteria = criteriaBuilder.createQuery(ForumThreadReply.class);
    Root<ForumThreadReply> root = criteria.from(ForumThreadReply.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(ForumThreadReply_.thread), thread), criteriaBuilder.equal(root.get(ForumThreadReply_.archived), Boolean.FALSE)));
    criteria.orderBy(criteriaBuilder.desc(root.get(ForumThreadReply_.created)));
    TypedQuery<ForumThreadReply> query = entityManager.createQuery(criteria);
    query.setMaxResults(1);
    return getSingleResult(query);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ForumThreadReply(fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply)

Example 12 with ForumThreadReply

use of fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply 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 ForumThreadReply

use of fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply 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 14 with ForumThreadReply

use of fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply 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)

Example 15 with ForumThreadReply

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

the class WorkspaceForumRESTService method findReply.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/forumAreas/{AREAID}/threads/{THREADID}/replies/{REPLYID}")
@RESTPermit(handling = Handling.INLINE)
public Response findReply(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, @PathParam("REPLYID") Long replyId) {
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).entity(String.format("Workspace entity %d not found", workspaceEntityId)).build();
    }
    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 WorkspaceForumArea)) {
            logger.severe(String.format("Trying to access forum %d via incorrect REST endpoint", forumArea.getId()));
            return Response.status(Status.NOT_FOUND).build();
        }
        if (!workspaceEntity.getId().equals(((WorkspaceForumArea) forumArea).getWorkspace())) {
            return Response.status(Status.NOT_FOUND).entity(String.format("WorkspaceForumArea %d does not belong to workspace entity %d", forumArea.getId(), workspaceEntity.getId())).build();
        }
        if (sessionController.hasWorkspacePermission(ForumResourcePermissionCollection.FORUM_READ_WORKSPACE_MESSAGES, workspaceEntity)) {
            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 : WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) ForumThreadReply(fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

ForumThreadReply (fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply)21 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)12 Path (javax.ws.rs.Path)12 ForumArea (fi.otavanopisto.muikku.plugins.forum.model.ForumArea)11 ForumThread (fi.otavanopisto.muikku.plugins.forum.model.ForumThread)11 WorkspaceForumArea (fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea)7 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)6 EnvironmentForumArea (fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)6 EntityManager (javax.persistence.EntityManager)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 DELETE (javax.ws.rs.DELETE)4 GET (javax.ws.rs.GET)4 ForumAreaGroup (fi.otavanopisto.muikku.plugins.forum.model.ForumAreaGroup)2 POST (javax.ws.rs.POST)2 PUT (javax.ws.rs.PUT)2