/* * 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.Answer; import ee.tlu.htk.dippler.entities.Assignment; 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.LearningOutcome; 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.TagManagerLocal; import ee.tlu.htk.dippler.managers.UserManagerLocal; import ee.tlu.htk.dippler.utils.Manipulators; import ee.tlu.htk.dippler.utils.permissionChecker; import java.io.StringReader; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; 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 AssignmentActions { DELETE, EDIT, LOAD, LOAD_ANSWERS } @Stateless public class AssignmentManager implements AssignmentManagerLocal { @PersistenceContext private EntityManager em; @EJB private UserManagerLocal userManager; @EJB private GroupManagerLocal groupManager; @EJB private TagManagerLocal tagManager; @EJB private OutcomeManagerLocal outcomeManager; @EJB private ActivityManagerLocal activityManager; public static final Integer LOAD = 1; public static final Integer DELETE = 50; public static final Integer EDIT = 50; public static final Integer LOAD_ANSWERS = 50; private Unmarshaller unmarshaller = null; public AssignmentManager() { try { final JAXBContext context = JAXBContext.newInstance(Assignment.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(AssignmentManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageAssignment(String action, String data, User user, Organization org) { Assignment assignment = unMarshal(data); if ( assignment != null ) { switch (AssignmentActions.valueOf(action)) { case EDIT: if (userManager.hasPermission(user, EDIT)) { return this.editAssignment(assignment, user, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for edit assignemnt"); } case DELETE: if (userManager.hasPermission(user, DELETE)) { return this.deleteAssignment(assignment, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for delete assignemnt"); } case LOAD: if (userManager.hasPermission(user, LOAD)) { return this.loadAssignment(assignment, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load assignemnt"); } case LOAD_ANSWERS: if (userManager.hasPermission(user, LOAD_ANSWERS)) { // madis - fixed spelling of assignment in method loadAssignmentWithAnswers() return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", loadAssignmentWithAnswers(assignment)); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load assignemnt"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } public String loadAssignment(Assignment a, User u) { try { Assignment assignment = findById(a.getId()); // XXX Thest this one, make it work - REALLY // XXX CHECK IF I WORK /*if (!permissionChecker.isFacilitator(u, assignment.getCourse()) || (!(permissionChecker.isLearner(u, assignment.getCourse()) && assignment.getStart() >= (System.currentTimeMillis() / 1000L))) ) { return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not loaded"); }*/ if (u.inClientSession() && assignment.getCreator().equals(u)) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(assignment)); } return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(assignment, u)); } catch (Throwable t) { t.printStackTrace(System.out); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not loaded"); } public String editAssignment(Assignment a, User user, Organization org) { Assignment assignment = findById(a.getId()); if ( assignment != null ) { if (!permissionChecker.isFacilitator(user, assignment.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } assignment.setTitle(a.getTitle()); assignment.setDescription(a.getDescription()); assignment.setStructure(a.getStructure()); assignment.setStart(a.getStart()); assignment.setEnd(a.getEnd()); assignment.setTargetGroup(editTargetGroup(a)); assignment.setMaxpoints(a.getMaxpoints()); assignment.setLate_submission(a.isLate_submission()); assignment.setSubmission_visibility(a.isSubmission_visibility()); assignment.setLearning_outcomes(editOutcomes(a)); tagManager.manageTags(a.tags, assignment, org); activityManager.addActivity("EDIT", assignment.getCourse(), user, assignment.getId(), assignment.getTitle(), "Assignment"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(assignment)); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Edit failed"); } public String deleteAssignment(Assignment a, User user) { Assignment assignment = findById(a.getId()); if ( assignment != null ) { if (!permissionChecker.isFacilitator(user, assignment.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } activityManager.addActivity("DELETE", assignment.getCourse(), user, assignment.getId(), assignment.getTitle(), "Assignment"); em.remove(assignment); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Delete failed"); } @Override public Collection editTargetGroup(Assignment a) { Collection target_group = new LinkedList(); Collection ids = a.groupIds; for(Long id : ids ) { target_group.add(groupManager.findById(id)); } return target_group; } public Set editOutcomes(Assignment a) { Set los = (Set) new HashSet(); Set ids = a.outcomes; for(Long id : ids ) { los.add(outcomeManager.findById(id)); } return los; } public static String getGroupIds(Assignment a) { StringBuilder xml = new StringBuilder(); Collection targets = a.getTargetGroup(); for(Coursegroup target : targets ){ xml.append("").append(target.getId()).append(""); } return xml.toString(); } public String getUserAnswerIds(Assignment a, User u) { StringBuilder xml = new StringBuilder(); Query listing = em.createNativeQuery("SELECT * FROM Answer a WHERE a.assignment=?1 AND a.creator=?2", Answer.class); listing.setParameter(1, a.getId()); listing.setParameter(2, u.getId()); List answers = (List) listing.getResultList(); for(Answer answer : answers) { xml.append("").append(answer.getId().toString()).append(""); } return xml.toString(); } public static String getAnswerIds(Assignment a) { StringBuilder xml = new StringBuilder(); Collection answers = a.getAnswers(); System.out.println("assignment: "+a.getId().toString()); for(Answer answer : answers) { xml.append("").append(answer.getId().toString()).append(""); System.out.println("answer. "+answer.getId().toString()); } return xml.toString(); } @Override public String loadLatestInfo(Course course) { StringBuilder xml = new StringBuilder(""); try { List assignments = course.getAssignments(0, 3); Integer learner_nr = course.getLearners().size(); for(Assignment assignment : assignments ){ Integer answer_nr = assignment.getAnswers().size(); xml.append(""); xml.append("").append(assignment.getId().toString()).append(""); xml.append("<![CDATA[").append(assignment.getTitle()).append("]]>"); xml.append(""); xml.append("").append(answer_nr).append("").append(learner_nr).append(""); xml.append(""); } } catch (Throwable t) { t.printStackTrace(System.out); } xml.append(""); return xml.toString(); } // madis - fixed assignment spelling in method name public String loadAssignmentWithAnswers(Assignment a) { Assignment assignment = findById(a.getId()); Course course = assignment.getCourse(); List learners = (List) course.getLearners(); return answerXML(course, assignment, learners); } @Override public String answerXML(Course course, Assignment assignment, List learners) { StringBuilder xml = new StringBuilder(""); xml.append("").append(assignment.getId()).append(""); xml.append("<![CDATA[").append(assignment.getTitle()).append("]]>"); xml.append(""); xml.append(""); for(Learner learner : learners ){ User u = learner.getUser(); xml.append(""); xml.append("").append(u.getId().toString()).append(""); xml.append(""); try { Query listing = em.createNativeQuery("SELECT * FROM Answer a WHERE a.assignment=?1 AND a.creator=?2", Answer.class); listing.setParameter(1, assignment.getId().toString()); listing.setParameter(2, u.getId().toString()); Answer answer = (Answer) listing.getSingleResult(); xml.append(""); xml.append("").append(answer.getId().toString()).append(""); xml.append("<![CDATA[").append(answer.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append("").append(answer.getCreated()).append(""); xml.append("").append(answer.getModified()).append(""); xml.append("").append(answer.getGraded()).append(""); xml.append("").append(answer.getCreator().getId()).append(""); xml.append("").append(answer.getAssignment().getId().toString()).append(""); xml.append(""); xml.append(""); xml.append(""); } catch (Throwable t) { if (!(t instanceof javax.persistence.NoResultException)) { t.printStackTrace(System.err); } } xml.append(""); } xml.append(""); return xml.toString(); } public final String getTagsXML(Assignment assignment) { try { Query trListing = em.createNamedQuery("TagRelationship.findByParent"); trListing.setParameter("parentId", assignment.getId()); trListing.setParameter("parentType", "Assignment"); 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 ""; } public final String getLearningOutcomesXML(Assignment assignment) { try { Query trListing = em.createNamedQuery("TagRelationship.findByParent"); trListing.setParameter("parentId", assignment.getId()); trListing.setParameter("parentType", "Assignment"); List tagrelationships = (List) trListing.getResultList(); StringBuilder xml = new StringBuilder(); if (!tagrelationships.isEmpty()) { for (LearningOutcome lo : assignment.getLearning_outcomes()) { try { xml.append(outcomeManager.marshal(lo)); } catch (Throwable ex) { Logger.getLogger(AssignmentManager.class.getName()).log(Level.SEVERE, null, ex); } } return xml.toString(); } } catch (Exception e){ } return ""; } @Override public Assignment unMarshal(String data) { if ( unmarshaller != null ) { try { final Assignment assignmentUNM = (Assignment) unmarshaller.unmarshal(new StringReader(data)); return assignmentUNM; } catch(JAXBException e) { //Something went wrong } } return null; } @Override public String marshal(Assignment assignment) { StringBuilder xml = new StringBuilder(); xml.append(""); xml.append("").append(assignment.getId()).append(""); xml.append("<![CDATA[").append(assignment.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append(""); xml.append("").append(assignment.getMaxpoints()).append(""); xml.append("").append(assignment.getStart()).append(""); xml.append("").append(assignment.getEnd()).append(""); xml.append("").append(assignment.isLate_submission()).append(""); xml.append("").append(assignment.isSubmission_visibility()).append(""); xml.append("").append(assignment.getCreated()).append(""); xml.append("").append(assignment.getCreator().getId()).append(""); xml.append("").append(assignment.getCourse().getId().toString()).append(""); xml.append("").append(getAnswerIds(assignment)).append(""); xml.append("").append(getGroupIds(assignment)).append(""); xml.append("").append(getTagsXML(assignment)).append(""); xml.append("").append(getLearningOutcomesXML(assignment)).append(""); xml.append(""); return xml.toString(); } @Override public String marshal(Assignment assignment, User user) { StringBuilder xml = new StringBuilder(); xml.append(""); xml.append("").append(assignment.getId()).append(""); xml.append("<![CDATA[").append(assignment.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append(""); xml.append("").append(assignment.getMaxpoints()).append(""); xml.append("").append(assignment.getStart()).append(""); xml.append("").append(assignment.getEnd()).append(""); xml.append("").append(assignment.isLate_submission()).append(""); xml.append("").append(assignment.isSubmission_visibility()).append(""); xml.append("").append(assignment.getCreated()).append(""); xml.append("").append(assignment.getCreator().getId()).append(""); xml.append("").append(assignment.getCourse().getId().toString()).append(""); xml.append("").append(getUserAnswerIds(assignment, user)).append(""); xml.append("").append(getGroupIds(assignment)).append(""); xml.append("").append(getTagsXML(assignment)).append(""); xml.append("").append(getLearningOutcomesXML(assignment)).append(""); xml.append(""); return xml.toString(); } @Override public Assignment findById(Long id) { if ( id > 0 ) { return em.find(Assignment.class, id); } return null; } }