/* * 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.Facilitator; import ee.tlu.htk.dippler.entities.Organization; import ee.tlu.htk.dippler.entities.User; import ee.tlu.htk.dippler.managers.UserManagerLocal; import ee.tlu.htk.dippler.utils.XMLHandler; import ee.tlu.htk.dippler.utils.permissionChecker; import java.io.StringReader; 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 FacilitatorActions { ADD, DELETE, LOAD, SEARCH } @Stateless public class FacilitatorManager implements FacilitatorManagerLocal { @PersistenceContext private EntityManager em; @EJB private UserManagerLocal userManager; @EJB private CourseManagerLocal courseManager; @EJB private ActivityManagerLocal activityManager; public static final Integer FACILITATOR_ADD = 50; public static final Integer FACILITATOR_DELETE = 50; public static final Integer FACILITATOR_ACTIVATE = 50; public static final Integer FACILITATOR_LOAD = 1; public static final Integer FACILITATOR_ENROLL = 10; public static final Integer FACILITATOR_SEARCH = 50; private Unmarshaller unmarshaller = null; public FacilitatorManager() { try { final JAXBContext context = JAXBContext.newInstance(Facilitator.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(FacilitatorManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageFacilitator(String action, String data, User user, Organization org) { switch (FacilitatorActions.valueOf(action)) { case ADD: if (userManager.hasPermission(user, FACILITATOR_ADD)) { return this.addFacilitator(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for add facilitator"); } case DELETE: if (userManager.hasPermission(user, FACILITATOR_DELETE)) { return this.removeFacilitator(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for delete facilitator"); } case LOAD: if (userManager.hasPermission(user, FACILITATOR_LOAD)) { return this.loadFacilitator(data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load facilitator"); } case SEARCH: if (userManager.hasPermission(user, FACILITATOR_SEARCH)) { return this.searchFacilitators(data, user, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for search facilitator"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } public String addFacilitator(String data, User actor) { Facilitator facilitator = unMarshal(data); if ( facilitator != null ) { Course course = courseManager.findById(facilitator.getCourseId()); if (!permissionChecker.isFacilitator(actor, course)) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, facilitator or admin"); } User user = userManager.findById(facilitator.getUserId()); if ( user != null && course != null) { course.addFacilitator(user); activityManager.addActivity("CREATE", course, actor, user.getId(), user.getFullname(), "Facilitator"); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Add facilitator failed"); } public String editFacilitator(String data, User user) { Facilitator facilitator = findByData(data); if ( facilitator != null ) { Facilitator l = unMarshal(data); if (!permissionChecker.isFacilitator(user, facilitator.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, facilitator or admin"); } facilitator.setStatus(l.getStatus()); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(facilitator)); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Edit facilitator failed"); } public String removeFacilitator(String data, User actor) { Facilitator facilitator = findByData(data); if ( facilitator != null ) { Course course = facilitator.getCourse(); if (!permissionChecker.isFacilitator(actor, course)) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, facilitator or admin"); } User user = facilitator.getUser(); activityManager.addActivity("DELETE", course, actor, user.getId(), user.getFullname(), "Facilitator"); //Finish remove course.remove(facilitator); user.remove(facilitator); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Remove facilitator failed"); } public String loadFacilitator(String data) { Facilitator facilitator = findByData(data); if ( facilitator != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(facilitator)); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not loaded"); } /** * Responds with XML string containing user data. Respects same organization * policy, only possible facilitators from same Organization are returned. * Filters out users that are already facilitators of the same course. * * @param data XML string containing search term and Course identifier. * @param user User making the request. * @param org Organization session Client belongs to. * @return A string with response XML. */ public String searchFacilitators(String data, User user, Organization org) { XMLHandler doc = new XMLHandler(data); if ( doc.parseSuccess() ) { String term = doc.getNodeValue("term"); Course course = courseManager.findById(doc.getNodeValueLong("course")); if ( course != null ) { if (!permissionChecker.isFacilitator(user, course)) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, facilitator or admin"); } Query query = em.createNamedQuery("User.findAvailableFacilitators"); query.setParameter("term", "%"+term+"%"); query.setParameter("course", course); query.setParameter("organization", org); List users = (List) query.getResultList(); StringBuilder xml = new StringBuilder(""); for(User single : users ) { xml.append("") .append("").append(single.getId().toString()).append("") .append("") .append("") .append(""); } xml.append(""); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", xml.toString()); } } return StatusCodes.respond(StatusCodes.COURSE_ERROR, ""); } public Facilitator unMarshal(String data) { if ( unmarshaller != null ) { try { final Facilitator coursegroupUNM = (Facilitator) unmarshaller.unmarshal(new StringReader(data)); return coursegroupUNM; } catch(JAXBException e) { //something went wrong } } return null; } @Override public String marshal(Facilitator facilitator) { User fu = facilitator.getUser(); StringBuilder xml = new StringBuilder(""); xml.append("").append(facilitator.getId()).append(""); xml.append(""); xml.append(""); xml.append(""); xml.append("").append(fu.getId().toString()).append(""); xml.append("").append(facilitator.getStatus().toString()).append(""); xml.append("").append(facilitator.getCourse().getId()).append(""); xml.append(""); return xml.toString(); } @Override public Facilitator findById(Long id) { if ( id > 0 ) { return em.find(Facilitator.class, id); } return null; } public Facilitator findByData(String data) { Facilitator fakeFacilitator = unMarshal(data); if ( fakeFacilitator != null ) { return findById(fakeFacilitator.getId()); } return null; } }