/* * To change this template, choose Tools | Templates * and open the template in the editor. */ // TODO fix static method problem in marshalResourceFolder() 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.LearningResource; import ee.tlu.htk.dippler.entities.ResourceFolder; import ee.tlu.htk.dippler.entities.User; import ee.tlu.htk.dippler.entities.Course; 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.managers.CategoryManagerLocal; import ee.tlu.htk.dippler.managers.TagManagerLocal; 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.Collection; 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 madis */ enum ResourceActions { CREATE_RESOURCE, EDIT_RESOURCE, DELETE_RESOURCE, LOAD_RESOURCE, CREATE_FOLDER, EDIT_FOLDER, DELETE_FOLDER, LOAD_FOLDER, REORGANISE_RESOURCES, REORGANISE_FOLDERS, LOAD_ROOT_FOLDER //, LOAD_FOLDER_WITH_CONTENT } @Stateless public class ResourceManager implements ResourceManagerLocal { @PersistenceContext private EntityManager em; @EJB private CourseManagerLocal courseManager; @EJB private UserManagerLocal userManager; @EJB private TagManagerLocal tagManager; @EJB private CategoryManagerLocal categoryManager; @EJB private ActivityManagerLocal activityManager; /* * Access level for load learning resource */ public static final Integer LOAD_RESOURCE = 1; /* * Access level for create learning resource */ public static final Integer CREATE_RESOURCE = 50; /* * Access level for edit learning resource */ public static final Integer EDIT_RESOURCE = 50; /* * Access level for delete learning resource */ public static final Integer DELETE_RESOURCE = 50; /* * Access level for load resource folder */ public static final Integer LOAD_FOLDER = 1; /* * Access level for create resource folder */ public static final Integer CREATE_FOLDER = 50; /* * Access level for edit resource folder */ public static final Integer EDIT_FOLDER = 50; /* * Access level for delete resource folder */ public static final Integer DELETE_FOLDER = 50; /* * Access level for reorganise learnign resources */ public static final Integer REORGANISE_RESOURCES = 50; /* * Access level for reorganize resource folders */ public static final Integer REORGANISE_FOLDERS = 50; // public static final Integer LOAD_FOLDER_WITH_CONTENT = 1; private static Unmarshaller unmarshaller_resource = null; private static Unmarshaller unmarshaller_folder = null; public ResourceManager() { try { JAXBContext context = JAXBContext.newInstance(LearningResource.class); unmarshaller_resource = context.createUnmarshaller(); //resource folder unmarshaller context = JAXBContext.newInstance(ResourceFolder.class); unmarshaller_folder = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex); } } @Override public String manageResource(String action, String data, User user, Organization org) { System.out.println("manageResource manager"); switch (ResourceActions.valueOf(action)) { case CREATE_RESOURCE: if (userManager.hasPermission(user, CREATE_RESOURCE)) { return this.createResource(user, data, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for creating resource"); } case EDIT_RESOURCE: if (userManager.hasPermission(user, EDIT_RESOURCE)) { return this.editResource(user, data, org); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for editing resource"); } case DELETE_RESOURCE: if (userManager.hasPermission(user, DELETE_RESOURCE)) { return this.deleteResource(user, data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for deleting resource"); } case LOAD_RESOURCE: if (userManager.hasPermission(user, LOAD_RESOURCE)) { return this.loadResource(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load resource"); } case CREATE_FOLDER: if (userManager.hasPermission(user, CREATE_FOLDER)) { return this.createFolder(user, data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for creating folder"); } case EDIT_FOLDER: if (userManager.hasPermission(user, EDIT_FOLDER)) { return this.editFolder(user, data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for editing folder"); } case DELETE_FOLDER: if (userManager.hasPermission(user, DELETE_FOLDER)) { return this.deleteFolder(user, data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for deleting folder"); } case LOAD_FOLDER: if (userManager.hasPermission(user, LOAD_FOLDER)) { return this.loadFolder(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load folder"); } case LOAD_ROOT_FOLDER: if (userManager.hasPermission(user, LOAD_FOLDER)) { return this.loadRootFolder(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load folder"); } case REORGANISE_RESOURCES: if (userManager.hasPermission(user, REORGANISE_RESOURCES)) { return this.reorganiseResources(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for reorganising resources"); } case REORGANISE_FOLDERS: if (userManager.hasPermission(user, REORGANISE_FOLDERS)) { return this.reorganiseFolders(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for reorganising folders"); } /* case LOAD_FOLDER_WITH_CONTENT: if (userManager.hasPermission(user, LOAD_FOLDER_WITH_CONTENT)) { return this.loadFolderWithContent(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for loading resource/folder tree"); } */ default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Unknown operation"); } } private boolean areNewFolderIdAndCurrentFolderDifferent(Long new_folder_id, ResourceFolder current_folder) { boolean folders_are_different; // Needed to get null comparisons right while keeping them simple if (new_folder_id == 0) { new_folder_id = null; } if (new_folder_id == null && current_folder == null) { folders_are_different = false; } else if ((new_folder_id == null && current_folder != null) || (new_folder_id != null && current_folder == null)) { folders_are_different = true; } else { // Both old and new resource folder are not null if (current_folder.getId().equals(new_folder_id)) { folders_are_different = false; } else { folders_are_different = true; } } return folders_are_different; } private int getNewResourcePosition(Long new_folder_id, LearningResource resource) { int position = 1; // When created, course might not be set yet, but transient courseId should be set in that case Long course_id; if (resource.getCourse() == null || resource.getCourse().getId() == null) { course_id = resource.getCourseId(); } else { course_id = resource.getCourse().getId(); } if (new_folder_id != null && new_folder_id != 0) { try { Query last_resource = em.createNativeQuery("SELECT * FROM learning_resource WHERE course=?1 AND folder=?2 ORDER BY position DESC", LearningResource.class); last_resource.setParameter(1, course_id); last_resource.setParameter(2, new_folder_id); last_resource.setFirstResult(0); last_resource.setMaxResults(1); LearningResource lr = (LearningResource) last_resource.getSingleResult(); position = lr.getPosition()+1; } catch (Exception e) { position = 1; } } else { // Setting position try { Query last_resource = em.createNativeQuery("SELECT * FROM learning_resource WHERE course=?1 AND folder IS NULL ORDER BY position DESC", LearningResource.class); last_resource.setParameter(1, course_id); last_resource.setFirstResult(0); last_resource.setMaxResults(1); LearningResource lr = (LearningResource) last_resource.getSingleResult(); position = lr.getPosition()+1; } catch (Exception e) { position = 1; } } return position; } private int getNewFolderPosition(Long new_parent_folder_id, ResourceFolder folder) { int position = 1; // When created, course might not be set yet, but transient courseId should be set in that case Long course_id; if (folder.getCourse() == null || folder.getCourse().getId() == null) { course_id = folder.getCourseId(); } else { course_id = folder.getCourse().getId(); } if (new_parent_folder_id != null && new_parent_folder_id != 0) { // Setting position try { Query last_folder = em.createNativeQuery("SELECT * FROM resource_folder WHERE course=?1 AND parent=?2 ORDER BY position DESC", ResourceFolder.class); last_folder.setParameter(1, course_id); last_folder.setParameter(2, new_parent_folder_id); last_folder.setFirstResult(0); last_folder.setMaxResults(1); ResourceFolder lf = (ResourceFolder) last_folder.getSingleResult(); position = lf.getPosition()+1; } catch (Exception e) { position = 1; } } else { // Setting position try { Query last_folder = em.createNativeQuery("SELECT * FROM resource_folder WHERE course=?1 AND parent IS NULL ORDER BY position DESC", ResourceFolder.class); last_folder.setParameter(1, course_id); last_folder.setFirstResult(0); last_folder.setMaxResults(1); ResourceFolder lf = (ResourceFolder) last_folder.getSingleResult(); position = lf.getPosition()+1; } catch (Exception e) { position = 1; } } return position; } //TODO fix this, this does not work at all public String createResource(User user, String data, Organization org) { System.out.println("CREATE LEARNING RESOURCE"); LearningResource resource = unMarshalLearningResource(data); if ( resource != null ) { // XXX TODO Need to check that creator has permission to do that within the course context Course course = courseManager.findById(resource.getCourseId()); if ( course != null ) { resource.setCreator(user); resource.setType("LearningResource"); resource.setCreated(new Date()); resource.setModified(new Date()); // Set position resource.setPosition(getNewResourcePosition(resource.getFolderId(), resource)); //Set parent folder; if (resource.getFolderId() != null && resource.getFolderId() != 0) { resource.setResourceFolder(folderFindById(resource.getFolderId())); } resource.setCategories(categoryManager.editCategories(resource.categoryIds)); //Add via course - learningresource relationship course.addLearningResource(resource); em.persist(resource); tagManager.manageTags(resource.tags, resource, org); activityManager.addActivity("CREATE", course, user, resource.getId(), resource.getTitle(), "LearningResource"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalLearningResource(resource)); } } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Create failed"); } public String editResource(User user, String data, Organization org) { System.out.println(data); LearningResource rd = unMarshalLearningResource(data); if ( rd != null ) { LearningResource resource = resourceFindById(rd.getId()); if ( resource != null ) { if (!permissionChecker.isFacilitator(user, resource.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } resource.setTitle(rd.getTitle()); resource.setDescription(rd.getDescription()); //resource.setSubtype(rd.getSubtype()); //resource.setUrl(rd.getUrl()); resource.setAuthor(rd.getAuthor()); resource.setLicence(rd.getLicence()); System.out.println(rd.getPublished()); resource.setPublished(rd.getPublished()); //resource.setCreated(rd.getCreated()); //resource.setModified(rd.getModified()); // TODO Check if setting that to 1 is an option //resource.setPosition(rd.getPosition()); //resource.courseId = rd.getCourseId(); resource.folderId = rd.getFolderId(); // Determine if resource folder was changed boolean resource_folder_changed = areNewFolderIdAndCurrentFolderDifferent(rd.folderId, resource.getResourceFolder()); // Reset position if resource folder is changed if (resource_folder_changed) { resource.setPosition(getNewResourcePosition(rd.getFolderId(), resource)); } // Set folder id if (rd.getFolderId() != null && rd.getFolderId() != 0) { resource.setResourceFolder(folderFindById(rd.getFolderId())); } else { resource.setResourceFolder(null); } tagManager.manageTags(rd.tags, resource, org); List catrelationships = new ArrayList(); try { Query crListing = em.createNamedQuery("CategoryRelationship.findByParent"); crListing.setParameter("parentId", resource.getId()); crListing.setParameter("parentType", "LearningResource"); catrelationships = (List) crListing.getResultList(); } catch (Exception e) {} for (CategoryRelationship catr: catrelationships) { em.remove(catr); } resource.setCategories(categoryManager.editCategories(rd.categoryIds)); activityManager.addActivity("EDIT", resource.getCourse(), user, resource.getId(), resource.getTitle(), "LearningResource"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalLearningResource(resource)); } } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Edit failed"); } public String deleteResource(User user, String data) { LearningResource resource = resourceFindByData(data); if ( resource != null ) { if (!permissionChecker.isFacilitator(user, resource.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } activityManager.addActivity("DELETE", resource.getCourse(), user, resource.getId(), resource.getTitle(), "LearningResource"); em.remove(resource); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Delete failed"); } public String loadResource(String data, User user) { System.out.println("LOAD RESOURCE"); LearningResource resource = resourceFindByData(data); if ( resource != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalLearningResource(resource)); } return StatusCodes.respond(StatusCodes.RESOURCE_ERROR, "Not loaded"); } public String createFolder(User user, String data) { System.out.println("CREATE RESOURCE FOLDER"); ResourceFolder folder = unMarshalResourceFolder(data); if ( folder != null ) { Course course = courseManager.findById(folder.getCourseId()); if ( course != null ) { // XXX TODO Need to check that creator has permission to do that within the course context folder.setCreator(user); //folder.setCourse(courseManager.findById(folder.getCourseId())); // Set position folder.setPosition(getNewFolderPosition(folder.parentFolderId, folder)); // Set parent folder if (folder.parentFolderId != null && folder.parentFolderId != 0) { folder.setParentFolder(folderFindById(folder.getParentFolderId())); } folder.setCreated(new Date()); folder.setModified(new Date()); //Finally add via course - folder relationship course.addResourceFolder(folder); em.persist(folder); activityManager.addActivity("CREATE", course, user, folder.getId(), folder.getTitle(), "ResourceFolder"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalResourceFolder(folder)); } } return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Create failed"); } /** * Edits a ResourceFolder, setting from temporary ResourceFolder object * that is marshalled from data provided. * * @param user User making current request. * @param data XML string holding marshallable data for ResourceFolder. * @return XML string with full response, holding data if successful. */ public String editFolder(User user, String data) { ResourceFolder fd = unMarshalResourceFolder(data); if ( fd != null ) { ResourceFolder folder = folderFindById(fd.getId()); if ( folder != null ) { if (!permissionChecker.isFacilitator(user, folder.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } // Fail if parent id is the same as current id or is in // children, check if parent id is set if ( (fd.parentFolderId != null && fd.parentFolderId != 0) && (folder.getId().equals(fd.parentFolderId) || getAllFolderSubfolderIds(folder).contains(fd.parentFolderId)) ) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Can not be own parent"); } folder.setTitle(fd.getTitle()); // TODO Check if setting that to 1 is an option //folder.setPosition(fd.getPosition()); folder.courseId = fd.getCourseId(); // Determine if parent folder was changed boolean parent_folder_changed = areNewFolderIdAndCurrentFolderDifferent(fd.parentFolderId, folder.getParentFolder()); // Reset position if parent folder changed if (parent_folder_changed) { folder.setPosition(getNewFolderPosition(fd.parentFolderId, folder)); } // Set parent folder if (fd.parentFolderId != null && fd.parentFolderId != 0) { folder.setParentFolder(folderFindById(fd.getParentFolderId())); } else { folder.setParentFolder(null); } activityManager.addActivity("EDIT", folder.getCourse(), user, folder.getId(), folder.getTitle(), "ResourceFolder"); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalResourceFolder(folder)); } } return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Edit failed"); } public String deleteFolder(User user, String data) { ResourceFolder folder = folderFindByData(data); if ( folder != null ) { if (!permissionChecker.isFacilitator(user, folder.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } activityManager.addActivity("DELETE", folder.getCourse(), user, folder.getId(), folder.getTitle(), "ResourceFolder"); em.remove(folder); return StatusCodes.respond(StatusCodes.SUCCESS, ""); } return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Delete failed"); } public String loadFolder(String data, User user) { System.out.println("LOAD FOLDER"); ResourceFolder folder = folderFindByData(data); if ( folder != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalResourceFolder(folder)); } return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Not loaded"); } public String loadRootFolder(String data, User user) { System.out.println("LOAD ROOT FOLDER"); Course course = courseManager.findByData(data); if ( course != null ) { StringBuilder xml = new StringBuilder(); xml.append(""); xml.append("").append(getSubfolders(course)).append(""); xml.append("").append(getFolderResources(course)).append(""); xml.append(""); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", xml.toString()); } return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Not loaded"); } /** * Returns a collection of all ResourceFolder type children and children of * children of ResourceFolder. Walks through children of all sublevels. * @param folder ResourceFolder to get ResourceFolder type children for * @return A collection of all children ids. */ public Collection getAllFolderSubfolderIds(ResourceFolder folder) { Collection all_subfolder_ids = new ArrayList(); Collection subfolders = folder.getSubfolders(); for ( ResourceFolder subfolder : subfolders) { all_subfolder_ids.add(subfolder.getId()); // If subfolder has children, get ids and add to collection if (!subfolder.getSubfolders().isEmpty()) { all_subfolder_ids.addAll(getAllFolderSubfolderIds(subfolder)); } } return all_subfolder_ids; } // static? public static String getSubfolderIds(ResourceFolder f) { StringBuilder xml = new StringBuilder(); Collection subfolders = f.getSubfolders(); for(ResourceFolder subfolder : subfolders){ xml.append("").append(+subfolder.getId()).append(" resources = f.getLearningResources(); for(LearningResource resource : resources){ xml.append("").append(resource.getId()).append(" editSubfolderPositions(ResourceFolder f) { Collection subfolders = new ArrayList(); Collection ids = f.getSubfolderIds(); int position = 0; for (Long id : ids ) { ResourceFolder sf = folderFindById(id); subfolders.add(sf); sf.setPosition(position); position ++; } return subfolders; } public Collection editResourcePositions(ResourceFolder f) { Collection resources = new ArrayList(); Collection ids = f.getResourceIds(); int position = 0; for (Long id : ids ) { LearningResource r = resourceFindById(id); resources.add(r); r.setPosition(position); position ++; } return resources; } public final String getCategories(LearningResource resource) { try { Query trListing = em.createNamedQuery("CategoryRelationship.findByParent"); trListing.setParameter("parentId", resource.getId()); trListing.setParameter("parentType", "LearningResource"); 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 getParentFolder(ResourceFolder folder) { System.out.println("LOAD ROOT PARENTFOLDERS"); ResourceFolder parent = folder.getParentFolder(); StringBuilder xml = new StringBuilder(); if (parent != null) { System.out.println(parent.getTitle()); xml.append(""); xml.append("").append(parent.getId().toString()).append(""); xml.append("").append(parent.getTitle()).append(""); xml.append("").append(parent.getCreator().getId()).append(""); xml.append("").append(parent.getCourse().getId()).append(""); xml.append("").append(parent.getCreated()).append(""); xml.append("").append(parent.getPosition()).append(""); xml.append("").append(getParentFolder(parent)).append(""); xml.append(""); return xml.toString(); } return ""; } /* * returns XML string of subfolders contained by course root * @param course Course * @return XML String */ public String getSubfolders(Course course) { System.out.println("LOAD ROOT SUBFOLDERS"); Query listing = em.createNamedQuery("ResourceFolder.rootSubfolders"); listing.setParameter("course", course); List subfolders = (List) listing.getResultList(); return buildSubFolderXML(subfolders); } /** * Helper method for building subfolder XML output * * @param subfolders * @return String */ private String buildSubFolderXML(List subfolders) { StringBuilder xml = new StringBuilder(); if (!subfolders.isEmpty()) { for (ResourceFolder subfolder : subfolders) { xml.append(""); xml.append("").append(subfolder.getId().toString()).append(""); xml.append("").append(subfolder.getTitle()).append(""); xml.append("").append(subfolder.getCreator().getId()).append(""); xml.append("").append(subfolder.getCourse().getId()).append(""); xml.append("").append(subfolder.getCreated()).append(""); xml.append("").append(subfolder.getPosition()).append(""); xml.append(""); } } return xml.toString(); } /* * returns XML string of subfolders contained by resource folder * @param folder ResourceFolder * @return XML String */ public String getSubfolders(ResourceFolder folder) { System.out.println("LOAD SUBFOLDERS"); Query listing = em.createNamedQuery("ResourceFolder.subfolders"); listing.setParameter("parent", folder); List subfolders = (List) listing.getResultList(); return buildSubFolderXML(subfolders); } /* * returns XML string of Resources contained by course root * @param course Course * @return XML String */ public final String getFolderResources(Course course) { System.out.println("LOAD FOLDER RESOURCES"); Query resourceListing = em.createNamedQuery("LearningResource.findByCourse"); resourceListing.setParameter("course", course); List resources = (List) resourceListing.getResultList(); return buildResourceXML(resources); } private String buildResourceXML(List resources) { StringBuilder xml = new StringBuilder(); if (!resources.isEmpty()) { for (LearningResource resource : resources) { xml.append(""); xml.append("").append(resource.getId().toString()).append(""); xml.append("<![CDATA[").append(resource.getTitle()).append("]]>"); xml.append(""); xml.append("").append(resource.getType()).append(""); xml.append("").append(resource.getSubtype()).append(""); xml.append("").append(resource.getCreator().getId()).append(""); xml.append("").append(resource.getAuthor()).append(""); xml.append("").append(resource.getPublished()).append(""); xml.append("").append(resource.getPosition()).append(""); xml.append("").append(resource.getCourse().getId()).append(""); xml.append(""); } } return xml.toString(); } /* * returns XML string of Resources contianed by resource folder * @param folder ResourceFolder * @return XML String */ public final String getFolderResources(ResourceFolder folder) { System.out.println("LOAD FOLDER RESOURCES"); Query resourceListing = em.createNamedQuery("LearningResource.findByFolder"); resourceListing.setParameter("parent", folder); List resources = (List) resourceListing.getResultList(); return buildResourceXML(resources); } public String reorganiseResources(String data, User user) { System.out.println("REORGANISE RESOURCES"); ResourceFolder fd= unMarshalResourceFolder(data); if ( fd != null ) { ResourceFolder folder = folderFindById(fd.getId()); if ( folder != null ) { if (!permissionChecker.isFacilitator(user, folder.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } folder.setLearningResources(editResourcePositions(fd)); // TODO Marshal and return folder (current) OR loadFolderWithContent? return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalResourceFolder(folder)); } } return StatusCodes.respond(StatusCodes.REORGANISE_ERROR, "Reorganising resources failed"); } public String reorganiseFolders(String data, User user) { System.out.println("REORGANISE FOLDERS"); ResourceFolder fd= unMarshalResourceFolder(data); if ( fd != null ) { ResourceFolder folder = folderFindById(fd.getId()); if ( folder != null ) { if (!permissionChecker.isFacilitator(user, folder.getCourse())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner, admin or facilitator"); } folder.setSubfolders(editSubfolderPositions(fd)); // TODO Marshal and return folder (current) OR loadFolderWithContent? return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshalResourceFolder(folder)); } } return StatusCodes.respond(StatusCodes.REORGANISE_ERROR, "Reorganising folders failed"); } public final String getTagsXML(LearningResource resource) { try { Query trListing = em.createNamedQuery("TagRelationship.findByParent"); trListing.setParameter("parentId", resource.getId()); trListing.setParameter("parentType", "LearningResource"); 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 ""; } @Override public LearningResource resourceFindById(Long id) { if ( id > 0 ) { return em.find(LearningResource.class, id); } return null; } public LearningResource resourceFindByData(String data) { LearningResource fakeResource = unMarshalLearningResource(data); if ( fakeResource != null) { return resourceFindById(fakeResource.getId()); } return null; } @Override public ResourceFolder folderFindById(Long id) { if ( id > 0 ) { return em.find(ResourceFolder.class, id); } return null; } public ResourceFolder folderFindByData(String data) { ResourceFolder fakeFolder = unMarshalResourceFolder(data); if ( fakeFolder != null ) { return folderFindById(fakeFolder.getId()); } return null; } public static LearningResource unMarshalLearningResource(String data) { if ( unmarshaller_resource != null ){ try { final LearningResource learningResourceUNM = (LearningResource) unmarshaller_resource.unmarshal(new StringReader(data)); return learningResourceUNM; } catch(JAXBException e) { //something went wrong } } return null; } // TODO new fields public String marshalLearningResource (LearningResource learningResource) { String folder_id = "0"; if (learningResource.getResourceFolder() != null) { folder_id = learningResource.getResourceFolder().getId().toString(); } StringBuilder xml = new StringBuilder(); xml.append(""); xml.append("").append(learningResource.getId()).append(""); xml.append("<![CDATA[").append(learningResource.getTitle()).append("]]>"); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append(""); xml.append("").append(learningResource.getCreator().getId()).append(""); // author_id? xml.append(""); xml.append("").append(learningResource.getPublished()).append(""); xml.append("").append(learningResource.getCreated()).append(""); xml.append("").append(learningResource.getModified()).append(""); xml.append("").append(learningResource.getCourse().getId().toString()).append(""); // course_id? xml.append("").append(learningResource.getPosition()).append(""); xml.append("").append(folder_id).append(""); xml.append("").append(getTagsXML(learningResource)).append(""); xml.append("").append(getCategories(learningResource)).append(""); xml.append(""); return xml.toString(); } public static ResourceFolder unMarshalResourceFolder(String data) { if ( unmarshaller_folder != null ) { try { final ResourceFolder resourceFolderUNM = (ResourceFolder) unmarshaller_folder.unmarshal(new StringReader(data)); return resourceFolderUNM; } catch(JAXBException e) { //something went wrong } } return null; } public String marshalResourceFolder (ResourceFolder resourceFolder) { String parent_id = "0"; //String breadcrumbs = ""; try { if (resourceFolder.getParentFolder() != null) { parent_id = resourceFolder.getParentFolder().getId().toString(); //breadcrumbs = this.getBreadcrumbs(resourceFolder); } } catch (Exception e) { // silent } StringBuilder xml = new StringBuilder(); xml.append(""); xml.append("").append(resourceFolder.getId()).append(""); xml.append("<![CDATA[").append(resourceFolder.getTitle()).append("]]>"); xml.append("").append(resourceFolder.getPosition()).append(""); xml.append("").append(resourceFolder.getCreator().getId()).append(""); xml.append("").append(resourceFolder.getCreated()).append(""); xml.append("").append(resourceFolder.getCourse().getId().toString()).append(""); xml.append("").append(getSubfolders(resourceFolder)).append(""); xml.append("").append(getFolderResources(resourceFolder)).append(""); xml.append("").append(parent_id).append(""); //"").append(breadcrumbs).append(""); // no bradcrumbs anymore xml.append(""); return xml.toString(); } } /* REORGANISATION LOOPS int i = 0; for (LearningResource r : resources) { resourceFindById(r.getId).setWeight(i); i++; } int j = 0; for (ResourceFolder f : folders) { folderFindById(f.getId).setWeight(j); j++; } WARAMUT ei tule public String loadResource(String data, User u) { System.out.println("manageResource load()"); WaramuGet wg = new WaramuGet(); //wg.newSession(); //wg.getResource(u); //wg.closeSession(); return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", "data"); } public String loadFolderWithContent(String data, User user) { try { ResourceFolder folder = folderFindByData(data); if (!permissionChecker.isOwnerOrAdmin(user, folder.getCreator())) { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "Not owner or admin"); } String xml = ""+folder.getId().toString()+""+ "<![CDATA["+folder.getTitle()+"]]>"+ ""; if (!folder.getLearningResources().equals(null)) { List resources = (List) folder.getLearningResources(); for (LearningResource resource : resources) { xml += ""; xml += ""+resource.getId().toString()+""; xml += ""+resource.getTitle()+""; // Did not include content xml += ""+resource.getCreator().getId().toString()+""; xml += ""+resource.getCreated().toString()+""; xml += ""+resource.getModified().toString()+""; xml += ""+resource.getCourse().getTitle()+""; // Title instead of ID? xml += ""; } } else if (!folder.getSubfolders().equals(null)) { List subfolders = (List) folder.getSubfolders(); for (ResourceFolder subfolder : subfolders) { xml += ""; xml += ""+subfolder.getId().toString()+""; xml += ""+subfolder.getTitle()+""; xml += ""+subfolder.getDescription()+""; xml += ""+subfolder.getCreator().getId().toString()+""; xml += ""+subfolder.getCourse().getTitle()+""; xml += ""; } } else { return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Unknown folder content"); } xml += ""; return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", xml); } catch (Throwable t) { t.printStackTrace(System.out); } return StatusCodes.respond(StatusCodes.FOLDER_ERROR, "Not loaded"); } */