Search in sources :

Example 11 with WorkspaceNode

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.

the class WorkspaceMaterialController method createContentNode.

private ContentNode createContentNode(WorkspaceNode rootMaterialNode, int level, boolean processHtml, boolean includeHidden) throws WorkspaceMaterialException {
    boolean viewRestricted = false;
    try {
        switch(rootMaterialNode.getType()) {
            case FOLDER:
                WorkspaceFolder workspaceFolder = (WorkspaceFolder) rootMaterialNode;
                viewRestricted = !sessionController.isLoggedIn() && workspaceFolder.getViewRestrict() == MaterialViewRestrict.LOGGED_IN;
                ContentNode folderContentNode = new ContentNode(workspaceFolder.getTitle(), "folder", rootMaterialNode.getId(), null, level, null, null, rootMaterialNode.getParent().getId(), rootMaterialNode.getHidden(), null, 0l, 0l, workspaceFolder.getPath(), null, null, workspaceFolder.getViewRestrict(), viewRestricted);
                List<WorkspaceNode> children = includeHidden ? workspaceNodeDAO.listByParentSortByOrderNumber(workspaceFolder) : workspaceNodeDAO.listByParentAndHiddenSortByOrderNumber(workspaceFolder, Boolean.FALSE);
                List<FlattenedWorkspaceNode> flattenedChildren;
                if (level >= FLATTENING_LEVEL) {
                    flattenedChildren = flattenWorkspaceNodes(children, level, includeHidden);
                } else {
                    flattenedChildren = new ArrayList<>();
                    for (WorkspaceNode node : children) {
                        flattenedChildren.add(new FlattenedWorkspaceNode(false, null, node, level, node.getParent().getId(), node.getHidden()));
                    }
                }
                for (FlattenedWorkspaceNode child : flattenedChildren) {
                    ContentNode contentNode;
                    if (child.isEmptyFolder) {
                        contentNode = new ContentNode(child.emptyFolderTitle, "folder", rootMaterialNode.getId(), null, child.level, null, null, child.parentId, child.hidden, null, 0l, 0l, child.node.getPath(), null, null, MaterialViewRestrict.NONE, false);
                    } else {
                        contentNode = createContentNode(child.node, child.level, processHtml, includeHidden);
                    }
                    folderContentNode.addChild(contentNode);
                }
                return folderContentNode;
            case MATERIAL:
                DOMParser parser = null;
                Transformer transformer = null;
                if (processHtml) {
                    parser = new DOMParser(new HTMLConfiguration());
                    parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
                    transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
                    transformer.setOutputProperty(OutputKeys.INDENT, "no");
                }
                WorkspaceMaterial workspaceMaterial = (WorkspaceMaterial) rootMaterialNode;
                Material material = materialController.findMaterialById(workspaceMaterial.getMaterialId());
                Long currentRevision = material instanceof HtmlMaterial ? htmlMaterialController.lastHtmlMaterialRevision((HtmlMaterial) material) : 0l;
                Long publishedRevision = material instanceof HtmlMaterial ? ((HtmlMaterial) material).getRevisionNumber() : 0l;
                List<String> producerNames = null;
                String html;
                List<MaterialProducer> producers = materialController.listMaterialProducers(material);
                if ((producers != null) && !producers.isEmpty()) {
                    producerNames = new ArrayList<>();
                    for (MaterialProducer producer : producers) {
                        producerNames.add(StringUtils.replace(StringEscapeUtils.escapeHtml4(producer.getName()), ",", "&#44;"));
                    }
                }
                viewRestricted = !sessionController.isLoggedIn() && material.getViewRestrict() == MaterialViewRestrict.LOGGED_IN;
                if (!viewRestricted) {
                    html = processHtml ? getMaterialHtml(material, parser, transformer) : null;
                } else {
                    html = String.format("<p class=\"content-view-restricted-message\">%s</p>", localeController.getText(sessionController.getLocale(), "plugin.workspace.materialViewRestricted"));
                }
                return new ContentNode(workspaceMaterial.getTitle(), material.getType(), rootMaterialNode.getId(), material.getId(), level, workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers(), workspaceMaterial.getParent().getId(), workspaceMaterial.getHidden(), html, currentRevision, publishedRevision, workspaceMaterial.getPath(), material.getLicense(), StringUtils.join(producerNames, ','), material.getViewRestrict(), viewRestricted);
            default:
                return null;
        }
    } catch (SAXNotRecognizedException | SAXNotSupportedException | TransformerConfigurationException e) {
        throw new WorkspaceMaterialException(e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) HTMLConfiguration(org.cyberneko.html.HTMLConfiguration) Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) MaterialProducer(fi.otavanopisto.muikku.plugins.material.model.MaterialProducer) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) DOMParser(org.apache.xerces.parsers.DOMParser) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 12 with WorkspaceNode

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.

the class WorkspaceMaterialController method moveAbove.

/* WorkspaceNode */
/**
 * Updates the order numbers of workspace nodes so that <code>workspaceNode</code> appears above <code>referenceNode</code>.
 *
 * @param workspaceNode
 *          The workspace node to be moved
 * @param referenceNode
 *          The workspace node above which <code>workspaceNode</code> is moved
 *
 * @return The updated workspace node
 */
public WorkspaceNode moveAbove(WorkspaceNode workspaceNode, WorkspaceNode referenceNode) {
    // Order number of the reference node
    Integer referenceOrderNumber = referenceNode.getOrderNumber() == null ? 0 : referenceNode.getOrderNumber();
    // Workspace nodes with order number >= reference order number
    List<WorkspaceNode> subsequentNodes = workspaceNodeDAO.listByOrderNumberEqualOrGreater(referenceNode);
    // Sort workspace nodes according to order number
    sortWorkspaceNodes(subsequentNodes);
    // node order number = referenceOrderNumber, subsequent nodes = ++referenceOrderNumber
    workspaceNode = workspaceNodeDAO.updateOrderNumber(workspaceNode, referenceOrderNumber);
    for (WorkspaceNode subsequentNode : subsequentNodes) {
        if (!(subsequentNode.getId().equals(workspaceNode.getId()))) {
            workspaceNodeDAO.updateOrderNumber(subsequentNode, ++referenceOrderNumber);
        }
    }
    return workspaceNode;
}
Also used : WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)

Example 13 with WorkspaceNode

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.

the class MaterialRESTService method createRestModel.

private fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterial createRestModel(WorkspaceMaterial workspaceMaterial) {
    WorkspaceNode workspaceNode = workspaceMaterialController.findWorkspaceNodeNextSibling(workspaceMaterial);
    Long nextSiblingId = workspaceNode != null ? workspaceNode.getId() : null;
    return new fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterial(workspaceMaterial.getId(), workspaceMaterial.getMaterialId(), workspaceMaterial.getParent() != null ? workspaceMaterial.getParent().getId() : null, nextSiblingId, workspaceMaterial.getHidden(), workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers(), workspaceMaterial.getPath(), workspaceMaterial.getTitle());
}
Also used : WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 14 with WorkspaceNode

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.

the class DeusNexMachinaController method importResource.

private void importResource(WorkspaceNode importRoot, WorkspaceNode parent, Resource resource, DeusNexDocument deusNexDocument, List<WorkspaceNode> createdNodes) throws DeusNexException {
    WorkspaceNode node = findNode(parent, resource);
    if (resource.getType() == Type.FOLDER) {
        Folder folderResource = (Folder) resource;
        WorkspaceFolder folder = null;
        if (node instanceof WorkspaceFolder) {
            folder = (WorkspaceFolder) node;
        }
        if (folder == null) {
            folder = createFolder(parent, folderResource);
            try {
                setResourceWorkspaceNodeId(resource.getNo(), folder.getId());
            } catch (IOException e) {
                throw new DeusNexInternalException("Failed to store resourceNo lookup file", e);
            }
            createdNodes.add(folder);
        }
        for (Resource childResource : folderResource.getResources()) {
            importResource(importRoot, folder, childResource, deusNexDocument, createdNodes);
        }
    } else {
        if (node == null) {
            logger.fine("importing " + resource.getPath());
            Material material = createMaterial(importRoot, resource, deusNexDocument);
            if (material != null) {
                WorkspaceMaterialAssignmentType assignmentType = null;
                if (resource instanceof Query) {
                    switch(((Query) resource).getQueryType()) {
                        case "1":
                            assignmentType = WorkspaceMaterialAssignmentType.EXERCISE;
                            break;
                        case "2":
                            assignmentType = WorkspaceMaterialAssignmentType.EVALUATED;
                            break;
                    }
                } else if (material instanceof HtmlMaterial) {
                    assignmentType = determineEmbeddedAssignmentType((HtmlMaterial) material);
                }
                WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, material, resource.getName(), assignmentType, WorkspaceMaterialCorrectAnswersDisplay.ALWAYS);
                try {
                    setResourceWorkspaceNodeId(resource.getNo(), workspaceMaterial.getId());
                } catch (IOException e) {
                    throw new DeusNexInternalException("Failed to store resourceNo lookup file", e);
                }
                if (resource instanceof ResourceContainer) {
                    List<Resource> childResources = ((ResourceContainer) resource).getResources();
                    if (childResources != null) {
                        for (Resource childResource : childResources) {
                            importResource(importRoot, workspaceMaterial, childResource, deusNexDocument, createdNodes);
                        }
                    }
                }
                if (resource.getHidden()) {
                    workspaceMaterialController.hideWorkspaceNode(workspaceMaterial);
                }
                createdNodes.add(workspaceMaterial);
            }
        } else {
            logger.info(node.getPath() + " already exists, skipping");
        }
    }
}
Also used : Query(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Query) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) Resource(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource) Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) IOException(java.io.IOException) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder) Folder(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Folder) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) ResourceContainer(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.ResourceContainer)

Example 15 with WorkspaceNode

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.

the class DeusNexMachinaController method importDeusNexDocument.

public void importDeusNexDocument(WorkspaceNode parentNode, InputStream inputStream) throws DeusNexException {
    DeusNexDocument desNexDocument = parseDeusNexDocument(inputStream);
    List<WorkspaceNode> createdNodes = new ArrayList<>();
    for (Resource resource : desNexDocument.getRootFolder().getResources()) {
        importResource(parentNode, parentNode, resource, desNexDocument, createdNodes);
    }
    try {
        postProcessResources(createdNodes);
    } catch (Exception e) {
        throw new DeusNexInternalException("PostProcesssing failed. ", e);
    }
    materialUnEmbedder.unembedWorkspaceMaterials(parentNode);
}
Also used : DeusNexDocument(fi.otavanopisto.muikku.plugins.dnm.parser.structure.DeusNexDocument) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ArrayList(java.util.ArrayList) Resource(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) DeusNexException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexException) IOException(java.io.IOException) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)41 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)14 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)11 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)11 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)9 IOException (java.io.IOException)9 Path (javax.ws.rs.Path)9 ArrayList (java.util.ArrayList)8 EntityManager (javax.persistence.EntityManager)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 DeusNexInternalException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException)6 Material (fi.otavanopisto.muikku.plugins.material.model.Material)6 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)6 TransformerException (javax.xml.transform.TransformerException)6 XPathExpressionException (javax.xml.xpath.XPathExpressionException)6 SAXException (org.xml.sax.SAXException)6 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 Resource (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource)4 WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)4