Search in sources :

Example 21 with WorkspaceForumArea

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

the class WorkspaceForumRESTService method createReply.

@POST
@Path("/workspaces/{WORKSPACEENTITYID}/forumAreas/{AREAID}/threads/{THREADID}/replies")
@RESTPermit(handling = Handling.INLINE)
public Response createReply(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, ForumThreadReplyRESTModel newReply) {
    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();
        }
        if (forumThread.getLocked()) {
            return Response.status(Status.BAD_REQUEST).entity("Forum thread is locked").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_WRITE_WORKSPACE_MESSAGES, workspaceEntity)) {
            ForumThreadReply parentReply = null;
            if (newReply.getParentReplyId() != null) {
                parentReply = forumController.getForumThreadReply(newReply.getParentReplyId());
                if (parentReply == null) {
                    return Response.status(Status.BAD_REQUEST).entity("Invalid parent reply id").build();
                }
                if (!Objects.equals(parentReply.getThread().getId(), threadId)) {
                    return Response.status(Status.BAD_REQUEST).entity("Parent reply is in wrong thread").build();
                }
            }
            return Response.ok(createRestModel(forumController.createForumThreadReply(forumThread, newReply.getMessage(), parentReply))).build();
        } else {
            return Response.status(Status.FORBIDDEN).build();
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to create new forum thread reply", 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) POST(javax.ws.rs.POST)

Example 22 with WorkspaceForumArea

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

the class WorkspaceForumRESTService method listThreads.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/forumAreas/{AREAID}/threads")
@RESTPermit(handling = Handling.INLINE)
public Response listThreads(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("AREAID") Long areaId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).entity(String.format("Workspace entity %d not found", workspaceEntityId)).build();
    }
    ForumArea forumArea = forumController.getForumArea(areaId);
    if (forumArea == null) {
        return Response.status(Status.NOT_FOUND).entity("Forum area not found").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)) {
        List<ForumThread> threads = forumController.listForumThreads(forumArea, firstResult, maxResults);
        List<ForumThreadRESTModel> result = new ArrayList<ForumThreadRESTModel>();
        for (ForumThread thread : threads) {
            long numReplies = forumController.getThreadReplyCount(thread);
            result.add(new ForumThreadRESTModel(thread.getId(), thread.getTitle(), thread.getMessage(), thread.getCreator(), thread.getCreated(), thread.getForumArea().getId(), thread.getSticky(), thread.getLocked(), thread.getUpdated(), numReplies, thread.getLastModified()));
        }
        return Response.ok(result).build();
    } else {
        return Response.status(Status.FORBIDDEN).build();
    }
}
Also used : WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ArrayList(java.util.ArrayList) ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 23 with WorkspaceForumArea

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

the class ForumController method listLatestForumThreads.

public List<ForumThread> listLatestForumThreads(int firstResult, int maxResults) {
    List<EnvironmentForumArea> environmentForums = listEnvironmentForums();
    // List<WorkspaceForumArea> workspaceForums = listCourseForums();
    List<ForumArea> forumAreas = new ArrayList<ForumArea>();
    for (EnvironmentForumArea ef : environmentForums) {
        forumAreas.add(ef);
    }
    // for (WorkspaceForumArea wf : workspaceForums) {
    // forumAreas.add(wf);
    // }
    List<ForumThread> threads;
    if (!forumAreas.isEmpty())
        threads = forumThreadDAO.listLatestOrdered(forumAreas, firstResult, maxResults);
    else
        threads = new ArrayList<ForumThread>();
    return threads;
}
Also used : ArrayList(java.util.ArrayList) ForumThread(fi.otavanopisto.muikku.plugins.forum.model.ForumThread) WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) ForumArea(fi.otavanopisto.muikku.plugins.forum.model.ForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)

Example 24 with WorkspaceForumArea

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

the class ForumMessageDAO method listByWorkspaceEntityAndCreatorOrderByCreated.

public List<ForumMessage> listByWorkspaceEntityAndCreatorOrderByCreated(Long workspaceEntityId, Long creatorId, int firstResult, int maxResults) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ForumMessage> criteria = criteriaBuilder.createQuery(ForumMessage.class);
    Root<ForumMessage> root = criteria.from(ForumMessage.class);
    Root<WorkspaceForumArea> workspaceAreaJoin = criteria.from(WorkspaceForumArea.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(workspaceAreaJoin.get(WorkspaceForumArea_.workspace), workspaceEntityId), criteriaBuilder.equal(workspaceAreaJoin.get(WorkspaceForumArea_.id), root.get(ForumMessage_.forumArea)), criteriaBuilder.equal(root.get(ForumMessage_.creator), creatorId), criteriaBuilder.equal(root.get(ForumMessage_.archived), Boolean.FALSE)));
    criteria.orderBy(criteriaBuilder.desc(root.get(ForumMessage_.created)));
    TypedQuery<ForumMessage> query = entityManager.createQuery(criteria);
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) EntityManager(javax.persistence.EntityManager) ForumMessage(fi.otavanopisto.muikku.plugins.forum.model.ForumMessage)

Example 25 with WorkspaceForumArea

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

the class ForumMessageDAO method listByWorkspace.

public List<ForumMessage> listByWorkspace(WorkspaceEntity workspace) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ForumMessage> criteria = criteriaBuilder.createQuery(ForumMessage.class);
    Root<ForumMessage> root = criteria.from(ForumMessage.class);
    Root<WorkspaceForumArea> root2 = criteria.from(WorkspaceForumArea.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root2.get(WorkspaceForumArea_.workspace), workspace.getId()), criteriaBuilder.equal(root2.get(WorkspaceForumArea_.id), root.get(ForumMessage_.forumArea)), criteriaBuilder.equal(root.get(ForumMessage_.archived), Boolean.FALSE)));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceForumArea(fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea) EntityManager(javax.persistence.EntityManager) ForumMessage(fi.otavanopisto.muikku.plugins.forum.model.ForumMessage)

Aggregations

WorkspaceForumArea (fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea)26 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)17 ForumArea (fi.otavanopisto.muikku.plugins.forum.model.ForumArea)16 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)15 Path (javax.ws.rs.Path)15 ForumThread (fi.otavanopisto.muikku.plugins.forum.model.ForumThread)11 GET (javax.ws.rs.GET)6 ForumThreadReply (fi.otavanopisto.muikku.plugins.forum.model.ForumThreadReply)5 EntityManager (javax.persistence.EntityManager)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 ArrayList (java.util.ArrayList)4 ForumMessage (fi.otavanopisto.muikku.plugins.forum.model.ForumMessage)3 DELETE (javax.ws.rs.DELETE)3 POST (javax.ws.rs.POST)3 PUT (javax.ws.rs.PUT)3 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)2 EnvironmentForumArea (fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Permission (fi.otavanopisto.muikku.model.security.Permission)1