Search in sources :

Example 11 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class HtmlMaterialCloneOperation method clone.

@Override
public HtmlMaterial clone(HtmlMaterial material) {
    HtmlMaterial clonedMaterial = htmlMaterialController.createHtmlMaterial(material.getTitle(), material.getHtml(), material.getContentType(), new Long(0), material, material.getLicense(), material.getViewRestrict());
    List<MaterialMeta> materialMetas = materialController.listMaterialMetas(material);
    for (MaterialMeta materialMeta : materialMetas) {
        materialController.createMaterialMeta(clonedMaterial, materialMeta.getKey(), materialMeta.getValue());
    }
    List<MaterialProducer> materialProducers = materialController.listMaterialProducers(material);
    for (MaterialProducer materialProducer : materialProducers) {
        materialController.createMaterialProducer(clonedMaterial, materialProducer.getName());
    }
    return clonedMaterial;
}
Also used : MaterialMeta(fi.otavanopisto.muikku.plugins.material.model.MaterialMeta) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) MaterialProducer(fi.otavanopisto.muikku.plugins.material.model.MaterialProducer)

Example 12 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class DeusNexMachinaController method postProcessHtml.

private void postProcessHtml(HtmlMaterial material) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
    StringReader htmlReader = new StringReader(material.getHtml());
    DOMParser parser = new DOMParser(new HTMLConfiguration());
    parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
    InputSource inputSource = new InputSource(htmlReader);
    parser.parse(inputSource);
    org.w3c.dom.Document domDocument = parser.getDocument();
    boolean modified = false;
    // Embedded YouTube clips; strip protocol
    List<Element> elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe");
    if (!elements.isEmpty()) {
        for (Element element : elements) {
            String src = element.getAttribute("src");
            if (src != null && src.startsWith("http://www.youtube.com/")) {
                element.setAttribute("src", src.substring(5));
                modified = true;
            }
        }
    }
    // Embedded documents; add data attributes and determine correct material title
    elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe[@data-type=\"embedded-document\"]");
    if (!elements.isEmpty()) {
        modified = true;
        for (Element element : elements) {
            Integer resourceNo = Integer.valueOf(element.getAttribute("data-resource-no"));
            WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(getResourceWorkspaceNodeId(resourceNo));
            HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(workspaceMaterial.getMaterialId());
            // If a header precedes an embedded document, use its text as the embedded
            // document's title and remove it from the parent document altogether
            Node possibleHeaderNode = getPreviousSiblingElement(element);
            if (isHeader(possibleHeaderNode)) {
                String headerText = StringUtils.trim(possibleHeaderNode.getTextContent());
                if (!StringUtils.isBlank(headerText)) {
                    htmlMaterialController.updateHtmlMaterialTitle(htmlMaterial, headerText);
                    possibleHeaderNode.getParentNode().removeChild(possibleHeaderNode);
                }
            }
            element.setAttribute("data-material-id", String.valueOf(htmlMaterial.getId()));
            element.setAttribute("data-material-type", htmlMaterial.getType());
            element.setAttribute("data-workspace-material-id", String.valueOf(workspaceMaterial.getId()));
        }
    }
    // Update to post-processed version, if applicable
    if (modified) {
        StringWriter writer = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.transform(new DOMSource(domDocument), new StreamResult(writer));
        htmlMaterialController.updateHtmlMaterialHtml(material, writer.getBuffer().toString());
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HTMLConfiguration(org.cyberneko.html.HTMLConfiguration) Element(org.w3c.dom.Element) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) Node(org.w3c.dom.Node) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) DOMParser(org.apache.xerces.parsers.DOMParser) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 13 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial 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 14 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class DeusNexMachinaController method postProcessResources.

private void postProcessResources(List<WorkspaceNode> createdNodes) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
    for (WorkspaceNode node : createdNodes) {
        if (node.getType() == WorkspaceNodeType.MATERIAL) {
            WorkspaceMaterial workspaceMaterial = (WorkspaceMaterial) node;
            HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(workspaceMaterial.getMaterialId());
            if (htmlMaterial != null && StringUtils.isNotBlank(htmlMaterial.getHtml())) {
                postProcessHtml(htmlMaterial);
            }
        }
    }
}
Also used : HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 15 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class MaterialAttachmentUploadServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String materialUrl = req.getPathInfo();
    if (StringUtils.isBlank(materialUrl)) {
        sendResponse(resp, "Missing material path", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    if (!sessionController.isLoggedIn()) {
        sendResponse(resp, "Unauthorized", HttpServletResponse.SC_UNAUTHORIZED);
        return;
    }
    Part file = req.getPart("upload");
    if (file == null) {
        sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
    if (file.getSize() > fileSizeLimit) {
        sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
        return;
    }
    WorkspaceMaterial parentWorkspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByRootPath(materialUrl);
    if (parentWorkspaceMaterial == null) {
        sendResponse(resp, "Material not found", HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(parentWorkspaceMaterial);
    if (workspaceRootFolder == null) {
        sendResponse(resp, "Workspace root folder not found", HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
    if (workspaceEntity == null) {
        sendResponse(resp, "Workspace entity not found", HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
        sendResponse(resp, "Forbidden", HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    HtmlMaterial parentMaterial = htmlMaterialController.findHtmlMaterialById(parentWorkspaceMaterial.getMaterialId());
    if (parentMaterial == null) {
        sendResponse(resp, "Parent material is not html material", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String license = null;
    BinaryMaterial uploadedMaterial = binaryMaterialController.createBinaryMaterial(file.getSubmittedFileName(), file.getContentType(), IOUtils.toByteArray(file.getInputStream()), license);
    String uploadedUrl = null;
    List<WorkspaceMaterial> parentWorkspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(parentMaterial);
    for (WorkspaceMaterial sharedWorkspaceMaterial : parentWorkspaceMaterials) {
        WorkspaceMaterial uploadedWorkspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(sharedWorkspaceMaterial, uploadedMaterial);
        if (sharedWorkspaceMaterial.getId().equals(parentWorkspaceMaterial.getId())) {
            uploadedUrl = uploadedWorkspaceMaterial.getUrlName();
        }
    }
    if (StringUtils.isBlank(uploadedUrl)) {
        sendResponse(resp, "Could not resolve uploaded file url", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    UploadMeta uploadMeta = new UploadMeta(file.getName(), 1, uploadedUrl);
    resp.setContentType("application/json");
    ServletOutputStream servletOutputStream = resp.getOutputStream();
    try {
        (new ObjectMapper()).writeValue(servletOutputStream, uploadMeta);
    } finally {
        servletOutputStream.flush();
    }
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ServletOutputStream(javax.servlet.ServletOutputStream) Part(javax.servlet.http.Part) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)

Aggregations

HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)29 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)14 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)10 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)7 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)7 Material (fi.otavanopisto.muikku.plugins.material.model.Material)7 Path (javax.ws.rs.Path)7 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)6 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)5 IOException (java.io.IOException)5 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)4 CoOpsNotImplementedException (fi.foyt.coops.CoOpsNotImplementedException)4 File (fi.foyt.coops.model.File)4 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)4 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)4 WorkspaceMaterialContainsAnswersExeption (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption)4 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)4 Patch (fi.foyt.coops.model.Patch)3 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)3 WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)3