/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.course; import ee.tlu.htk.dippler.backoffice.StatusCodes; import ee.tlu.htk.dippler.entities.Course; import ee.tlu.htk.dippler.entities.CourseblogComment; import ee.tlu.htk.dippler.entities.CourseblogPost; import ee.tlu.htk.dippler.entities.Organization; import ee.tlu.htk.dippler.entities.Tag; import ee.tlu.htk.dippler.entities.TagRelationship; import ee.tlu.htk.dippler.entities.User; import ee.tlu.htk.dippler.managers.CategoryManagerLocal; import ee.tlu.htk.dippler.managers.TagManagerLocal; import ee.tlu.htk.dippler.managers.UserManagerLocal; import ee.tlu.htk.dippler.utils.permissionChecker; import ee.tlu.htk.dippler.utils.Manipulators; import java.io.StringReader; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; /** * * @author metz */ enum BlogActions { CREATE_POST, EDIT_POST, DELETE_POST, LOAD_POST } enum BlogPostActions { CREATE_COMMENT, EDIT_COMMENT, DELETE_COMMENT, LOAD_COMMENT } @Stateless public class BlogManager implements BlogManagerLocal { @PersistenceContext private EntityManager em; @EJB private TagManagerLocal tagManager; @EJB private CategoryManagerLocal categoryManager; @EJB private CourseManagerLocal courseManager; @EJB private UserManagerLocal userManager; @EJB private ActivityManagerLocal activityManager; public static final Integer POST_CREATE = 50; public static final Integer POST_EDIT = 50; public static final Integer POST_DELETE = 50; public static final Integer POST_LOAD = 1; public static final Integer POST_LIST = 1; public static final Integer COMMENT_CREATE = 10; public static final Integer COMMENT_EDIT = 50; public static final Integer COMMENT_DELETE = 50; public static final Integer COMMENT_LOAD = 1; public static final Integer COMMENT_LIST = 1; private static Unmarshaller unmarshaller_comment, unmarshaller_blogpost = null; public BlogManager() { try { JAXBContext context = JAXBContext.newInstance(CourseblogComment.class); unmarshaller_comment = context.createUnmarshaller(); context = JAXBContext.newInstance(CourseblogPost.class); unmarshaller_blogpost = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(BlogManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageBlog(Long course_id, String action, String data, User user, Organization org) { switch (BlogActions.valueOf(action)) { case CREATE_POST: if (userManager.hasPermission(user, POST_CREATE)) { Course course = courseManager.findById(course_id); return this.createPost(user, course, data, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for creating post"); } case EDIT_POST: if (userManager.hasPermission(user, POST_EDIT)) { return this.editPost(data, user, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for edit post"); } case DELETE_POST: if (userManager.hasPermission(user, POST_DELETE)) { return this.deletePost(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for delete post"); } case LOAD_POST: if (userManager.hasPermission(user, POST_LOAD)) { return this.loadPost(data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load post"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } @Override public String manageBlogPost(Long post_id, String action, String data, User user) { switch (BlogPostActions.valueOf(action)) { case CREATE_COMMENT: if (userManager.hasPermission(user, COMMENT_CREATE)) { return this.createComment(user, post_id, data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for creating post"); } case EDIT_COMMENT: if (userManager.hasPermission(user, COMMENT_EDIT)) { return this.editComment(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for edit comment"); } case DELETE_COMMENT: if (userManager.hasPermission(user, COMMENT_DELETE)) { return this.deleteComment(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for delete comment"); } case LOAD_COMMENT: if (userManager.hasPermission(user, COMMENT_LOAD)) { return this.loadComment(data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load comment"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } // POST methods public String createPost(User user, Course course, String data, Organization org) { System.out.println("CREATE POST"); CourseblogPost post = unMarshalPost(data); if ( post != null ) { post.setUser(user); post.setCourse(course); post.setHidden(false); Date dateIn = new Date(); post.setCreated(dateIn); post.setModified(dateIn); //if (post.tagIds != null) { // post.setTags(tagManager.editTags(post.tagIds)); //} if (post.categoryIds != null) { //post.setCategories(categoryManager.editCategories(post.categoryIds)); } em.persist(post); tagManager.manageTags(post.tags, post, org); activityManager.addActivity("CREATE", course, user, post.getId(), post.getTitle(), "CourseBlogPost"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalPost(post)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Create failed"); } public String editPost(String data, User user, Organization org) { CourseblogPost post = findPostByData(data); if ( post != null ) { CourseblogPost cbp = unMarshalPost(data); if (!permissionChecker.isFacilitator(user, post.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } post.setTitle(cbp.getTitle()); post.setBody(cbp.getBody()); post.setModified(new Date()); //if (post.tagIds != null) { // post.setTags(tagManager.editTags(cbp.tagIds)); //} if (post.categoryIds != null) { //post.setCategories(categoryManager.editCategories(post.categoryIds)); } tagManager.manageTags(cbp.tags, post, org); activityManager.addActivity("EDIT", post.getCourse(), user, post.getId(), post.getTitle(), "CourseBlogPost"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalPost(post)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Edit failed"); } public String deletePost(String data, User user) { CourseblogPost post = findPostByData(data); if ( post != null ) { if (!permissionChecker.isFacilitator(user, post.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } activityManager.addActivity("DELETE", post.getCourse(), user, post.getId(), post.getTitle(), "CourseBlogPost"); Course course = post.getCourse(); course.remove(post); em.remove(post); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Delete failed"); } public String loadPost(String data) { System.out.println("LOAD POST"); CourseblogPost post = findPostByData(data); if ( post != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalPost(post)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Not loaded"); } // COMMENT methods public String createComment(User user, Long post_id, String data) { System.out.println("CREATE COMMENT"); CourseblogPost post = findPostById(post_id); if ( post != null ) { if (!(permissionChecker.isOwnerOrAdmin(user, post.getCourse().getOwner()) || courseManager.isLearner(user, post.getCourse()))) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not admin, owner or learner"); } CourseblogComment comment = unMarshalComment(data); comment.setUser(user); comment.setCourse(post.getCourse()); comment.setCourseblogPost(post); comment.setHidden(false); em.persist(comment); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalComment(comment)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Create failed"); } public String editComment(String data, User user) { System.out.println(data); CourseblogComment comment = findCommentByData(data); if ( comment != null ) { CourseblogComment cbc = unMarshalComment(data); if (!permissionChecker.isOwnerOrAdmin(user, comment.getUser())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner or admin"); } comment.setTitle(cbc.getTitle()); comment.setBody(cbc.getBody()); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalComment(comment)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Edit failed"); } public String deleteComment(String data, User user) { CourseblogComment comment = findCommentByData(data); if ( comment != null ) { if (!permissionChecker.isOwnerOrAdmin(user, comment.getUser())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner or admin"); } em.remove(comment); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Delete failed"); } public String loadComment(String data) { System.out.println("LOAD COMMENT"); CourseblogComment comment = findCommentByData(data); if ( comment != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalComment(comment)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Not loaded"); } @Override public String loadLatestInfo(Course course) { StringBuilder xml = new StringBuilder(""); List posts = course.getPosts(0, 3); for(CourseblogPost post : posts ){ xml.append(""); xml.append("").append(post.getId().toString()).append(""); xml.append("<![CDATA[").append(post.getTitle()).append("]]>"); xml.append(""); User u = post.getUser(); xml.append("").append(u.getId().toString()).append(""); xml.append(""); } xml.append(""); return xml.toString(); } public final String getTagsXML(CourseblogPost post) { try { Query trListing = em.createNamedQuery("TagRelationship.findByParent"); trListing.setParameter("parentId", post.getId()); trListing.setParameter("parentType", "CourseblogPost"); List tagrelationships = (List) trListing.getResultList(); StringBuilder xml = new StringBuilder(); if (!tagrelationships.isEmpty()) { for (TagRelationship tr : tagrelationships) { Tag tag = tr.getTag(); xml.append(""); xml.append("").append(tag.getId()).append(""); xml.append(""); xml.append(""); xml.append("").append(tag.getCount()).append(""); xml.append(""); } return xml.toString(); } } catch (Exception e){ } return ""; } // marshaller for post public static CourseblogPost unMarshalPost(String data) { if ( unmarshaller_blogpost != null ) { try { final CourseblogPost courseUNM = (CourseblogPost) unmarshaller_blogpost.unmarshal(new StringReader(data)); return courseUNM; } catch(JAXBException e) { //Something went wrong } } return null; } @Override public String marshalPost(CourseblogPost post) { StringBuilder xml = new StringBuilder(""); xml.append("").append(post.getId()).append(""); xml.append("<![CDATA[").append(post.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append("").append(post.getCreated()).append(""); xml.append("").append(post.getModified()).append(""); xml.append("").append(post.getHidden()).append(""); xml.append("").append(post.getCourse().getId()).append(""); xml.append("").append(getTagsXML(post)).append(""); xml.append(""); return xml.toString(); } // marshaller for comment public static CourseblogComment unMarshalComment(String data) { if ( unmarshaller_comment != null ) { try { final CourseblogComment courseUNM = (CourseblogComment) unmarshaller_comment.unmarshal(new StringReader(data)); return courseUNM; } catch(JAXBException e ) { //Something went wrong } } return null; } public static String marshalComment(CourseblogComment comment) { StringBuilder xml = new StringBuilder(""); xml.append("").append(comment.getId()).append(""); xml.append("<![CDATA[").append(comment.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append("").append(comment.getCreated()).append(""); xml.append("").append(comment.getModified()).append(""); xml.append("").append(comment.getHidden()).append(""); xml.append("").append(comment.getCourse().getId()).append(""); xml.append("").append(comment.getCourseblogPost().getId()).append(""); xml.append(""); return xml.toString(); } public CourseblogPost findPostById(Long id) { if ( id > 0 ) { return em.find(CourseblogPost.class, id); } return null; } public CourseblogPost findPostByData(String data) { CourseblogPost fakeCourseblogPost = unMarshalPost(data); if ( fakeCourseblogPost != null ) { return findPostById(fakeCourseblogPost.getId()); } return null; } public CourseblogComment findCommentById(Long id) { if ( id > 0 ) { return em.find(CourseblogComment.class, id); } return null; } public CourseblogComment findCommentByData(String data) { CourseblogComment fakeCourseblogComment = unMarshalComment(data); if ( fakeCourseblogComment != null ) { return findCommentById(fakeCourseblogComment.getId()); } return null; } }