/* * 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.Category; import ee.tlu.htk.dippler.entities.CategoryRelationship; import ee.tlu.htk.dippler.entities.Course; import ee.tlu.htk.dippler.entities.LearningOutcome; import ee.tlu.htk.dippler.entities.Organization; import ee.tlu.htk.dippler.entities.User; import ee.tlu.htk.dippler.managers.CategoryManagerLocal; import ee.tlu.htk.dippler.managers.UserManagerLocal; import ee.tlu.htk.dippler.utils.permissionChecker; import java.io.StringReader; import java.util.ArrayList; 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 OutcomeActions { CREATE, EDIT, LOAD, DELETE, CREATE_CATEGORY } @Stateless public class OutcomeManager implements OutcomeManagerLocal { @PersistenceContext private EntityManager em; @EJB private CourseManagerLocal courseManager; @EJB private UserManagerLocal userManager; @EJB private CategoryManagerLocal categoryManager; @EJB private ActivityManagerLocal activityManager; /* * Access level for load learning outcome */ public static final Integer LOAD = 1; /* * Access level for create learning outcome */ public static final Integer CREATE = 50; /* * Access level for edit learning outcome */ public static final Integer EDIT = 50; private Unmarshaller unmarshaller = null; public OutcomeManager() { try { final JAXBContext context = JAXBContext.newInstance(LearningOutcome.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(OutcomeManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageOutcome(String action, String data, User user, Organization org) { System.out.println("manageOutcome manager"); switch (OutcomeActions.valueOf(action)) { case CREATE: if (userManager.hasPermission(user, CREATE)) { return this.createOutcome(user, data, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for creating resource"); } case EDIT: if (userManager.hasPermission(user, EDIT)) { return this.editOutcome(user, data, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for editing resource"); } case DELETE: if (userManager.hasPermission(user, EDIT)) { return this.deleteOutcome(user, data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for deleting resource"); } case LOAD: if (userManager.hasPermission(user, LOAD)) { return this.loadOutcome(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load resource"); } case CREATE_CATEGORY: if (userManager.hasPermission(user, CREATE)) { return categoryManager.createCategory(data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for creating resource"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Unknown operation"); } } public String createOutcome(User user, String data, Organization org) { System.out.println("CREATE LEARNING OUTCOME"); LearningOutcome outcome = unMarshal(data); if ( outcome != null ) { outcome.setCreator(user); outcome.setType("LearningOutcome"); Course course = courseManager.findById(outcome.getCourseId()); if ( course != null ) { outcome.setCourse(course); outcome.setCategories(categoryManager.editCategories(outcome.categoryIds)); Date created = new Date(); outcome.setCreated(created); outcome.setModified(created); em.persist(outcome); activityManager.addActivity("CREATE", course, user, outcome.getId(), outcome.getTitle(), "LearningOutcome"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(outcome)); } } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Create failed"); } public String editOutcome(User user, String data, Organization org) { System.out.println("EDIT LEARNING OUTCOME"); System.out.println(data); LearningOutcome outcome = findByData(data); if ( outcome != null ) { LearningOutcome od = unMarshal(data); if ( od != null ) { if (!permissionChecker.isFacilitator(user, outcome.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not facilitator or admin"); } outcome.setTitle(od.getTitle()); Date modified = new Date(); outcome.setModified(modified); List catrelationships = new ArrayList(); try { Query crListing = em.createNamedQuery("CategoryRelationship.findByParent"); crListing.setParameter("parentId", outcome.getId()); crListing.setParameter("parentType", "LearningOutcome"); catrelationships = (List) crListing.getResultList(); } catch (Exception e) {} //TODO check this later for (CategoryRelationship catr: catrelationships) { Category cat = catr.getCategory(); em.remove(catr); em.flush(); em.refresh(cat); } em.refresh(outcome); outcome.setCategories(categoryManager.editCategories(od.categoryIds)); activityManager.addActivity("EDIT", outcome.getCourse(), user, outcome.getId(), outcome.getTitle(), "LearningOutcome"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(outcome)); } } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Edit failed"); } public String deleteOutcome(User user, String data) { System.out.println("DELETE LEARNING OUTCOME"); LearningOutcome outcome = findByData(data); if ( outcome != null ) { if (!permissionChecker.isFacilitator(user, outcome.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not facilitator or admin"); } activityManager.addActivity("DELETE", outcome.getCourse(), user, outcome.getId(), outcome.getTitle(), "LearningOutcome"); Course course = outcome.getCourse(); course.remove(outcome); em.remove(outcome); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Delete failed"); } public String loadOutcome(String data, User user) { System.out.println("LOAD OUTCOME"); LearningOutcome outcome = findByData(data); if ( outcome != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(outcome)); } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Not loaded"); } /* public final String getCategories(LearningOutcome outcome) { try { String xml = ""; for (Category cat : outcome.getCategories()) { String parent = ""; try { parent = cat.getParentCategory().getId().toString(); } catch (Exception e) {} xml += ""+ ""+cat.getId()+""+ ""+ ""+ ""+cat.getCount()+""+ ""+parent+""+ ""+cat.getCourse().getId().toString()+""+ ""; } return xml; } catch (Exception e){ } return ""; } * */ public final String getCategories(LearningOutcome outcome) { try { Query trListing = em.createNamedQuery("CategoryRelationship.findByParent"); trListing.setParameter("parentId", outcome.getId()); trListing.setParameter("parentType", "LearningOutcome"); List catrelationships = (List) trListing.getResultList(); StringBuilder xml = new StringBuilder(); if (!catrelationships.isEmpty()) { for (CategoryRelationship tr : catrelationships) { Category cat = tr.getCategory(); String parent = ""; try { parent = cat.getParentCategory().getId().toString(); } catch (Exception e) {} xml.append(""); xml.append("").append(cat.getId()).append(""); xml.append(""); xml.append(""); xml.append("").append(cat.getCount()).append(""); xml.append("").append(parent).append(""); xml.append("").append(cat.getCourse().getId().toString()).append(""); xml.append(""); } return xml.toString(); } } catch (Exception e){ } return ""; } /* public String getCategories(LearningOutcome outcome) { String xml = ""; try { Query trListing = em.createNamedQuery("Relationship.findByParent"); trListing.setParameter("parentId", post.getId()); trListing.setParameter("parentType", "CourseblogPost"); List tagrelationships = (List) trListing.getResultList(); } catch (Exception e) {} for (Category cat : outcome.getCategories()) { try { xml += categoryManager.marshal(cat); } catch (Throwable ex) { Logger.getLogger(OutcomeManager.class.getName()).log(Level.SEVERE, null, ex); } } return xml; }*/ @Override public LearningOutcome unMarshal(String data) { if ( unmarshaller != null ) { try { final LearningOutcome learningoutcomeUNM = (LearningOutcome) unmarshaller.unmarshal(new StringReader(data)); return learningoutcomeUNM; } catch (JAXBException e) { //Something went wrong } } return null; } @Override public String marshal(LearningOutcome outcome) { StringBuilder xml = new StringBuilder(""); xml.append("").append(outcome.getId().toString()).append(""); xml.append("<![CDATA[").append(outcome.getTitle()).append("]]>"); xml.append("").append(getCategories(outcome)).append(""); xml.append(""); xml.append("").append(outcome.getCourse().getId().toString()).append(""); xml.append(""); return xml.toString(); } public LearningOutcome findByData(String data) { LearningOutcome fakeLearningOutcome = unMarshal(data); if ( fakeLearningOutcome != null ) { return findById(fakeLearningOutcome.getId()); } return null; } @Override public LearningOutcome findById(Long id) { if ( id > 0 ) { return em.find(LearningOutcome.class, id); } return null; } }