Search in sources :

Example 6 with EnvironmentForumArea

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

the class ForumRESTService method createReply.

@POST
@Path("/areas/{AREAID}/threads/{THREADID}/replies")
@RESTPermit(handling = Handling.INLINE)
public Response createReply(@PathParam("AREAID") Long areaId, @PathParam("THREADID") Long threadId, ForumThreadReplyRESTModel newReply) {
    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 EnvironmentForumArea)) {
            logger.severe(String.format("Trying to post 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_WRITE_ENVIRONMENT_MESSAGES)) {
            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 : 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) POST(javax.ws.rs.POST)

Example 7 with EnvironmentForumArea

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

the class ForumRESTService method listThreads.

@GET
@Path("/areas/{AREAID}/threads")
@RESTPermit(handling = Handling.INLINE)
public Response listThreads(@PathParam("AREAID") Long areaId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
    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 list non environment forum area (%d) threads from environment endpoint", forumArea.getId()));
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_READ_ENVIRONMENT_MESSAGES)) {
        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 : ArrayList(java.util.ArrayList) 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) GET(javax.ws.rs.GET)

Example 8 with EnvironmentForumArea

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

the class DiscussionBackingBean method init.

@RequestAction
public String init() {
    if (!sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_ACCESSENVIRONMENTFORUM) || !sessionController.isActiveUser()) {
        return NavigationRules.ACCESS_DENIED;
    }
    List<EnvironmentForumArea> forumAreas = forumController.listEnvironmentForums();
    lockStickyPermission = sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_LOCK_OR_STICKIFY_MESSAGES);
    showFullNamePermission = sessionController.hasEnvironmentPermission(ForumResourcePermissionCollection.FORUM_SHOW_FULL_NAMES);
    Map<Long, AreaPermission> areaPermissions = new HashMap<>();
    for (EnvironmentForumArea forumArea : forumAreas) {
        AreaPermission areaPermission = new AreaPermission(sessionController.hasPermission(ForumResourcePermissionCollection.FORUM_EDIT_ENVIRONMENT_MESSAGES, forumArea), sessionController.hasPermission(ForumResourcePermissionCollection.FORUM_DELETE_ENVIRONMENT_MESSAGES, forumArea));
        areaPermissions.put(forumArea.getId(), areaPermission);
    }
    try {
        this.areaPermissions = new ObjectMapper().writeValueAsString(areaPermissions);
    } catch (JsonProcessingException e) {
        return NavigationRules.INTERNAL_ERROR;
    }
    return null;
}
Also used : HashMap(java.util.HashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestAction(org.ocpsoft.rewrite.annotation.RequestAction)

Example 9 with EnvironmentForumArea

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

the class EnvironmentForumAreaDAO method create.

public EnvironmentForumArea create(String name, String description, ForumAreaGroup group, Boolean archived, UserEntity owner, ResourceRights rights) {
    EnvironmentForumArea environmentForumArea = new EnvironmentForumArea();
    environmentForumArea.setName(name);
    environmentForumArea.setDescription(description);
    environmentForumArea.setGroup(group);
    environmentForumArea.setArchived(archived);
    environmentForumArea.setOwner(owner.getId());
    environmentForumArea.setRights(rights.getId());
    getEntityManager().persist(environmentForumArea);
    return environmentForumArea;
}
Also used : EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)

Example 10 with EnvironmentForumArea

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

the class EnvironmentForumAreaDAO method listAllNonArchived.

public List<EnvironmentForumArea> listAllNonArchived() {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EnvironmentForumArea> criteria = criteriaBuilder.createQuery(EnvironmentForumArea.class);
    Root<EnvironmentForumArea> root = criteria.from(EnvironmentForumArea.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(EnvironmentForumArea_.archived), Boolean.FALSE));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) EnvironmentForumArea(fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)

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