/* * 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.Grade; import ee.tlu.htk.dippler.entities.Learner; import ee.tlu.htk.dippler.entities.User; import ee.tlu.htk.dippler.utils.permissionChecker; import java.io.StringReader; import java.util.Date; 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 pjotr */ @Stateless public class GradeManager implements GradeManagerLocal { @PersistenceContext private EntityManager em; @EJB private CourseManagerLocal courseManager; @EJB private LearnerManagerLocal learnerManager; private Unmarshaller unmarshaller = null; public GradeManager() { try { final JAXBContext context = JAXBContext.newInstance(Grade.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(GradeManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String saveGrade(String data, User user) { Grade new_grade = unMarshal(data); if (new_grade != null) { Learner learner = learnerManager.findById(new_grade.getId()); if (learner != null) { Course course = courseManager.findById(learner.getCourse().getId()); if (!permissionChecker.isFacilitator(user, course)) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not facilitator or admin"); } Grade grade = findById(new_grade.getId()); Date date = new Date(); if (grade != null) { grade.setModified(date); grade.setGrade(new_grade.getGrade()); grade.setFeedback(new_grade.getFeedback()); } else { new_grade.setCreated(date); new_grade.setModified(date); new_grade.setLearner(learner); learner.setGrade(new_grade); } return StatusCodes.respond(StatusCodes.SUCCESS, ""); } } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Save grade failed"); } @Override public String loadGrade(String data, User user) { Grade grade = findByData(data); if ( grade != null ) { Course course = courseManager.findById(grade.getLearner().getCourse().getId()); if (!(permissionChecker.isFacilitator(user, course) || permissionChecker.isOwner(user, grade.getLearner().getUser()))) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not facilitator or admin"); } return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(grade)); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not loaded"); } public Grade unMarshal(String data) { if ( unmarshaller != null ) { try { final Grade gradeUNM = (Grade) unmarshaller.unmarshal(new StringReader(data)); return gradeUNM; } catch(JAXBException e) { //something went wrong } } return null; } @Override public String marshal(Grade grade) { StringBuilder xml = new StringBuilder(""); xml.append("").append(grade.getId().toString()).append(""); xml.append("").append(grade.getLearner().getId().toString()).append(""); xml.append("").append(grade.getLearner().getUser().getId().toString()).append(""); xml.append("").append(grade.getLearner().getCourse().getId()).append(""); xml.append(""); xml.append(""); xml.append("").append(grade.getCreated()).append(""); xml.append("").append(grade.getModified()).append(""); xml.append(""); return xml.toString(); } public Grade findByLearnerId(Long id) { if ( id != null && id > 0 ) { return em.find(Grade.class, id); } return null; } public Grade findById(Long id) { return findByLearnerId(id); } public Grade findByUserAndCourseIds(Long user_id, Long course_id) { if ( user_id > 0 && course_id > 0) { Query find = em.createNamedQuery("Grde.findByUserAndCourseIds"); find.setParameter("user_id", user_id); find.setParameter("course_id", course_id); try { Grade grade = (Grade) find.getSingleResult(); return grade; } catch(Exception e) { //Could not find profile } } return null; } public Grade findByData(String data) { Grade fake_grade = unMarshal(data); if (fake_grade.getId() != null && fake_grade.getId() > 0) { return findById(fake_grade.getId()); } else if (fake_grade.user_id != null && fake_grade.user_id > 0 && fake_grade.course_id != null && fake_grade.course_id > 0) { return findByUserAndCourseIds(fake_grade.user_id, fake_grade.course_id); } return null; } }