/* * 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.Coursegroup; import ee.tlu.htk.dippler.entities.Learner; import ee.tlu.htk.dippler.entities.User; import ee.tlu.htk.dippler.managers.UserManagerLocal; import ee.tlu.htk.dippler.utils.permissionChecker; import java.io.StringReader; import java.util.Collection; import java.util.LinkedList; 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.NoResultException; import javax.persistence.NonUniqueResultException; import javax.persistence.PersistenceContext; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; /** * * @author metz */ enum GroupActions { ADD, EDIT, DELETE, LOAD } @Stateless public class GroupManager implements GroupManagerLocal { @PersistenceContext private EntityManager em; @EJB private UserManagerLocal userManager; @EJB private LearnerManagerLocal learnerManager; @EJB private ActivityManagerLocal activityManager; public static final Integer GROUP_VIEW = 10; public static final Integer GROUP_EDIT = 10; private Unmarshaller unmarshaller = null; public GroupManager() { try { final JAXBContext context = JAXBContext.newInstance(Coursegroup.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(GroupManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageGroup(String action, String data, User user) { Coursegroup group = unMarshal(data); if ( group != null ) { switch (GroupActions.valueOf(action)) { case EDIT: if (userManager.hasPermission(user, GROUP_EDIT)) { return this.editGroup(group, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for edit group"); } case DELETE: if (userManager.hasPermission(user, GROUP_EDIT)) { return this.deleteGroup(group, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for delete group"); } case LOAD: if (userManager.hasPermission(user, GROUP_VIEW)) { return this.loadGroup(group); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load group"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } public String loadGroup(Coursegroup g) { Coursegroup group = findById(g.getId()); if ( group != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(group)); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not loaded"); } public String editGroup(Coursegroup g, User user) { Coursegroup group = findById(g.getId()); if ( group != null ) { if (!permissionChecker.isFacilitator(user, group.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } group.setTitle(g.getTitle()); group.setDescription(g.getDescription()); group.setWorkspace(g.getWorkspace()); // Collection members = group.getMembers(); // TODO Needs to become a standalone method if add/remove members is allowed not only to owner or admin group.setMembers(editMembers(g)); activityManager.addActivity("EDIT", group.getCourse(), user, group.getId(), group.getTitle(), "Coursegroup"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(group)); } return StatusCodes.respond(StatusCodes.BLOG_ERROR, "Edit failed"); } public String deleteGroup(Coursegroup g, User user) { Coursegroup group = findById(g.getId()); if ( group != null ) { if (!permissionChecker.isFacilitator(user, group.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner or admin"); } activityManager.addActivity("DELETE", group.getCourse(), user, group.getId(), group.getTitle(), "Coursegroup"); Course course = group.getCourse(); course.remove(group); em.remove(group); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Delete failed"); } @Override public Collection editMembers(Coursegroup g) { Collection members = new LinkedList(); Collection ids = g.memberIds; for(Long id : ids ) { members.add(learnerManager.findById(id)); } return members; } public static String getMemberIds(Coursegroup group) { StringBuilder xml = new StringBuilder(); Collection members = group.getMembers(); for(Learner member : members ){ xml.append("").append(member.getId()).append(""); } return xml.toString(); } @Override public Coursegroup unMarshal(String data) { if ( unmarshaller != null ) { try { final Coursegroup coursegroupUNM = (Coursegroup) unmarshaller.unmarshal(new StringReader(data)); return coursegroupUNM; } catch(JAXBException e) { //something went wrong } } return null; } @Override public String marshal(Coursegroup group) { StringBuilder xml = new StringBuilder(""); xml.append("").append(group.getId()).append(""); xml.append("<![CDATA[").append(group.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append("").append(group.getCreated()).append(""); xml.append("").append(group.getCreator().getId().toString()).append(""); xml.append("").append(group.getCourse().getId().toString()).append(""); xml.append("").append(getMemberIds(group)).append(""); xml.append(""); return xml.toString(); } @Override public Coursegroup findById(Long id) throws NoResultException, NonUniqueResultException { if ( id > 0 ) { return em.find(Coursegroup.class, id); } return null; } }