Search in sources :

Example 1 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException 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 2 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException 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)

Example 3 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexMachinaController method importHelpPageDocument.

public void importHelpPageDocument(WorkspaceEntity workspaceEntity, InputStream inputStream) throws DeusNexException {
    DeusNexDocument deusNexDocument = parseDeusNexDocument(inputStream);
    List<Resource> resources = deusNexDocument.getRootFolder().getResources();
    if (!resources.isEmpty()) {
        List<WorkspaceNode> createdNodes = new ArrayList<>();
        WorkspaceFolder workspaceHelpPageFolder = workspaceMaterialController.createWorkspaceHelpPageFolder(workspaceEntity);
        for (Resource resource : deusNexDocument.getRootFolder().getResources()) {
            importResource(workspaceHelpPageFolder, workspaceHelpPageFolder, resource, deusNexDocument, createdNodes);
        }
        try {
            postProcessResources(createdNodes);
        } catch (Exception e) {
            throw new DeusNexInternalException("PostProcesssing failed. ", e);
        }
    }
}
Also used : DeusNexDocument(fi.otavanopisto.muikku.plugins.dnm.parser.structure.DeusNexDocument) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) Resource(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource) ArrayList(java.util.ArrayList) 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) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)

Example 4 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexMachinaController method importFrontPageDocument.

public void importFrontPageDocument(WorkspaceEntity workspaceEntity, InputStream inputStream) throws DeusNexException {
    DeusNexDocument deusNexDocument = parseDeusNexDocument(inputStream);
    List<Resource> resources = deusNexDocument.getRootFolder().getResources();
    if (!resources.isEmpty()) {
        List<WorkspaceNode> createdNodes = new ArrayList<>();
        WorkspaceFolder workspaceFrontPageFolder = workspaceMaterialController.createWorkspaceFrontPageFolder(workspaceEntity);
        for (Resource resource : deusNexDocument.getRootFolder().getResources()) {
            importResource(workspaceFrontPageFolder, workspaceFrontPageFolder, resource, deusNexDocument, createdNodes);
        }
        try {
            postProcessResources(createdNodes);
        } catch (Exception e) {
            throw new DeusNexInternalException("PostProcesssing failed. ", e);
        }
    }
}
Also used : DeusNexDocument(fi.otavanopisto.muikku.plugins.dnm.parser.structure.DeusNexDocument) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) Resource(fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource) ArrayList(java.util.ArrayList) 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) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)

Example 5 with DeusNexInternalException

use of fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException in project muikku by otavanopisto.

the class DeusNexContentParser method parseContent.

public Map<String, String> parseContent(Element documentElement) throws DeusNexException {
    if ("styledocument".equals(documentElement.getTagName())) {
        return parseStyleDocument(documentElement);
    }
    if (!"document".equals(documentElement.getTagName())) {
        throw new DeusNexSyntaxException("Invalid content document");
    }
    Map<String, String> contents = new HashMap<>();
    Document ownerDocument = documentElement.getOwnerDocument();
    try {
        List<Element> localeDocuments = DeusNexXmlUtils.getElementsByXPath(documentElement, "fckdocument");
        for (Element localeDocument : localeDocuments) {
            String lang = localeDocument.getAttribute("lang");
            if (StringUtils.isBlank(lang)) {
                throw new DeusNexSyntaxException("Locale document does not specify lang");
            }
            // TODO: Sometimes document and fckdocument nodes are duplicated, when
            // and why?
            NodeList embeddedNodeList = documentElement.getElementsByTagName("ix:embedded");
            for (int i = embeddedNodeList.getLength() - 1; i >= 0; i--) {
                Element embeddedElement = (Element) embeddedNodeList.item(i);
                Node replacement = handleEmbedded(ownerDocument, embeddedElement);
                replaceElement(ownerDocument, embeddedElement, replacement);
            }
            NodeList ixImageNodeList = documentElement.getElementsByTagName("ix:image");
            for (int i = ixImageNodeList.getLength() - 1; i >= 0; i--) {
                Element ixImageElement = (Element) ixImageNodeList.item(i);
                Node replacement = handleIxImage(ownerDocument, ixImageElement);
                replaceElement(ownerDocument, ixImageElement, replacement);
            }
            NodeList embeddedItemNodeList = localeDocument.getElementsByTagName("ix:embeddeditem");
            for (int i = embeddedItemNodeList.getLength() - 1; i >= 0; i--) {
                Element embeddedItemElement = (Element) embeddedItemNodeList.item(i);
                Node replacement = handleEmbeddedItem(ownerDocument, embeddedItemElement);
                replaceElement(ownerDocument, embeddedItemElement, replacement);
            }
            NodeList textFieldNodeList = localeDocument.getElementsByTagName("ixf:textfield");
            for (int i = textFieldNodeList.getLength() - 1; i >= 0; i--) {
                Element element = (Element) textFieldNodeList.item(i);
                Node replacement = handleTextField(ownerDocument, element);
                replaceElement(ownerDocument, element, replacement);
            }
            NodeList memoFieldNodeList = localeDocument.getElementsByTagName("ixf:memofield");
            for (int i = memoFieldNodeList.getLength() - 1; i >= 0; i--) {
                Element element = (Element) memoFieldNodeList.item(i);
                Node replacement = handleMemoField(ownerDocument, element);
                replaceElement(ownerDocument, element, replacement);
            }
            NodeList optionListNodeList = localeDocument.getElementsByTagName("ixf:optionlist");
            for (int i = optionListNodeList.getLength() - 1; i >= 0; i--) {
                Element element = (Element) optionListNodeList.item(i);
                Node replacement = handleOptionListField(ownerDocument, element);
                replaceElement(ownerDocument, element, replacement);
            }
            NodeList connectFieldNodeList = localeDocument.getElementsByTagName("ixf:connectfield");
            for (int i = connectFieldNodeList.getLength() - 1; i >= 0; i--) {
                Element element = (Element) connectFieldNodeList.item(i);
                Node replacement = handleConnectField(ownerDocument, element);
                replaceElement(ownerDocument, element, replacement);
            }
            // ixf:uploadfilefield
            NodeList fileFieldNodeList = localeDocument.getElementsByTagName("ixf:uploadfilefield");
            for (int i = fileFieldNodeList.getLength() - 1; i >= 0; i--) {
                Element element = (Element) fileFieldNodeList.item(i);
                Node replacement = handleFileField(ownerDocument, element);
                replaceElement(ownerDocument, element, replacement);
            }
            Element htmlElement = ownerDocument.createElement("html");
            Element bodyElement = ownerDocument.createElement("body");
            htmlElement.appendChild(bodyElement);
            NodeList childNodes = localeDocument.getChildNodes();
            for (int i = childNodes.getLength() - 1; i >= 0; i--) {
                if (bodyElement.getFirstChild() != null) {
                    bodyElement.insertBefore(childNodes.item(i), bodyElement.getFirstChild());
                } else {
                    bodyElement.appendChild(childNodes.item(i));
                }
            }
            contents.put(lang, DeusNexXmlUtils.serializeElement(htmlElement, true, false, "xml"));
        }
    } catch (XPathExpressionException | TransformerException e) {
        throw new DeusNexInternalException("Internal Error occurred while processing document", e);
    }
    return contents;
}
Also used : HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) Document(org.w3c.dom.Document) DeusNexSyntaxException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

DeusNexInternalException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException)12 XPathExpressionException (javax.xml.xpath.XPathExpressionException)10 IOException (java.io.IOException)8 TransformerException (javax.xml.transform.TransformerException)7 SAXException (org.xml.sax.SAXException)7 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)6 ArrayList (java.util.ArrayList)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 Element (org.w3c.dom.Element)5 Resource (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource)4 DeusNexException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexException)3 DeusNexSyntaxException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexSyntaxException)3 DeusNexDocument (fi.otavanopisto.muikku.plugins.dnm.parser.structure.DeusNexDocument)3 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)3 WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)3 HashMap (java.util.HashMap)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)2