Search in sources :

Example 1 with WorkspaceMaterialAssignmentType

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

the class WorkspaceRESTService method listWorkspaceMaterials.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/")
@RESTPermitUnimplemented
public Response listWorkspaceMaterials(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @QueryParam("parentId") Long parentId, @QueryParam("assignmentType") String assignmentType) {
    if (parentId == null && assignmentType == null) {
        return Response.status(Status.NOT_IMPLEMENTED).entity("Listing workspace materials without parentId or assignmentType is currently not implemented").build();
    }
    WorkspaceMaterialAssignmentType workspaceAssignmentType = null;
    if (assignmentType != null) {
        workspaceAssignmentType = WorkspaceMaterialAssignmentType.valueOf(assignmentType);
        if (workspaceAssignmentType == null) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid assignmentType parameter").build();
        }
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("Could not find a workspace entity").build();
    }
    List<WorkspaceMaterial> workspaceMaterials = null;
    if (parentId != null) {
        WorkspaceNode parent = workspaceMaterialController.findWorkspaceNodeById(parentId);
        if (parent == null) {
            return Response.status(Status.BAD_REQUEST).entity("Given workspace parent material does not exist").build();
        }
        WorkspaceRootFolder rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(parent);
        if (rootFolder == null) {
            return Response.status(Status.BAD_REQUEST).entity("Could not find a workspace root folder").build();
        }
        if (!rootFolder.getWorkspaceEntityId().equals(workspaceEntityId)) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid parentId").build();
        }
        if (assignmentType != null) {
            workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByParentAndAssignmentType(parent, workspaceEntity, workspaceAssignmentType, BooleanPredicate.IGNORE);
            workspaceMaterials.removeIf(material -> isHiddenMaterial(material));
        } else {
            workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByParent(parent);
        }
    } else {
        workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByAssignmentType(workspaceEntity, workspaceAssignmentType, BooleanPredicate.IGNORE);
        workspaceMaterials.removeIf(material -> isHiddenMaterial(material));
    }
    if (workspaceMaterials.isEmpty()) {
        return Response.noContent().build();
    }
    return Response.ok(createRestModel(workspaceMaterials.toArray(new WorkspaceMaterial[0]))).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 2 with WorkspaceMaterialAssignmentType

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType 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 3 with WorkspaceMaterialAssignmentType

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

the class AcceptanceTestsRESTService method createWorkspaceMaterial.

@POST
@Path("/workspaces/{WORKSPACEID}/htmlmaterials")
@RESTPermit(handling = Handling.UNSECURED)
public Response createWorkspaceMaterial(fi.otavanopisto.muikku.atests.WorkspaceHtmlMaterial payload) {
    if (payload.getParentId() == null) {
        return Response.status(Status.BAD_REQUEST).entity("Mandatory parentId is missing").build();
    }
    HtmlMaterial htmlMaterial = htmlMaterialController.createHtmlMaterial(payload.getTitle(), payload.getHtml(), payload.getContentType(), payload.getRevisionNumber(), payload.getLicense());
    WorkspaceNode parent = workspaceMaterialController.findWorkspaceNodeById(payload.getParentId());
    if (parent == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid parentId").build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, htmlMaterial);
    String assignmentType = payload.getAssignmentType();
    if (StringUtils.isNotBlank(assignmentType)) {
        WorkspaceMaterialAssignmentType workspaceMaterialAssignmentType = WorkspaceMaterialAssignmentType.valueOf(assignmentType);
        if (workspaceMaterialAssignmentType == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid assignmentType '%s'", assignmentType)).build();
        }
        workspaceMaterialController.updateWorkspaceMaterialAssignmentType(workspaceMaterial, workspaceMaterialAssignmentType);
    }
    return Response.ok(createRestEntity(workspaceMaterial, htmlMaterial)).build();
}
Also used : HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 4 with WorkspaceMaterialAssignmentType

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

the class DeusNexMachinaController method determineEmbeddedAssignmentType.

private WorkspaceMaterialAssignmentType determineEmbeddedAssignmentType(HtmlMaterial material) throws DeusNexException {
    try {
        if (material.getHtml() == null) {
            return null;
        }
        StringReader htmlReader = new StringReader(material.getHtml());
        DOMParser parser = new DOMParser();
        InputSource inputSource = new InputSource(htmlReader);
        parser.parse(inputSource);
        org.w3c.dom.Document domDocument = parser.getDocument();
        List<Element> elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe[@data-type=\"embedded-document\"]");
        List<WorkspaceMaterialAssignmentType> assignmentTypes = new ArrayList<>();
        if (!elements.isEmpty()) {
            for (Element element : elements) {
                if ("EXERCISE".equals(element.getAttribute("data-assignment-type"))) {
                    assignmentTypes.add(WorkspaceMaterialAssignmentType.EXERCISE);
                }
                if ("EVALUATED".equals(element.getAttribute("data-assignment-type"))) {
                    assignmentTypes.add(WorkspaceMaterialAssignmentType.EVALUATED);
                }
            }
        }
        if (assignmentTypes.isEmpty() || (assignmentTypes.contains(WorkspaceMaterialAssignmentType.EXERCISE) && assignmentTypes.contains(WorkspaceMaterialAssignmentType.EVALUATED))) {
            return null;
        } else {
            return assignmentTypes.get(0);
        }
    } catch (SAXException | IOException | XPathExpressionException e) {
        throw new DeusNexInternalException("Embedded assignment type handling failed. ", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ArrayList(java.util.ArrayList) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) DOMParser(org.apache.xerces.parsers.DOMParser)

Example 5 with WorkspaceMaterialAssignmentType

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

the class MaterialUnEmbedder method unembedHtmlMaterial.

private void unembedHtmlMaterial(WorkspaceNode parent, WorkspaceMaterial workspaceMaterial, HtmlMaterial htmlMaterial) throws DeusNexInternalException, IOException {
    String html = htmlMaterial.getHtml();
    if (StringUtils.isNotBlank(html)) {
        InputStream is = null;
        logger.info("Unembedding html material " + htmlMaterial.getId());
        try {
            is = new ByteArrayInputStream(html.getBytes("UTF-8"));
            DocumentBuilder builder = DeusNexXmlUtils.createDocumentBuilder();
            Document document = builder.parse(is);
            if (document != null) {
                NodeList iframes = DeusNexXmlUtils.findNodesByXPath(document.getDocumentElement(), "//iframe[@data-type='embedded-document']");
                if (iframes.getLength() > 0) {
                    List<Document> splittedHtmlDocument = splitHtmlDocument(document);
                    for (int i = 0; i < splittedHtmlDocument.size(); i++) {
                        List<Long> pieceList = new ArrayList<Long>();
                        htmlMaterialPieces.put(htmlMaterial.getId(), pieceList);
                        HashMap<Long, WorkspaceMaterialAssignmentType> assignmentTypes = new HashMap<Long, WorkspaceMaterialAssignmentType>();
                        Document documentPiece = splittedHtmlDocument.get(i);
                        List<HtmlMaterial> pieceHtmlMaterials;
                        if (isEmbedPiece(documentPiece)) {
                            WorkspaceMaterialAssignmentType assignmentType = embeddedHtmlMaterialAssignmentType(documentPiece);
                            pieceHtmlMaterials = new ArrayList<HtmlMaterial>();
                            long embeddedHtmlMaterialId = embeddedHtmlMaterialId(documentPiece);
                            if (htmlMaterialPieces.containsKey(embeddedHtmlMaterialId)) {
                                for (Long htmlMaterialId : htmlMaterialPieces.get(embeddedHtmlMaterialId)) {
                                    logger.info("Existing html material " + htmlMaterialId + " embedded in " + htmlMaterial.getId());
                                    HtmlMaterial pieceHtmlMaterial = htmlMaterialController.findHtmlMaterialById(htmlMaterialId);
                                    pieceHtmlMaterials.add(pieceHtmlMaterial);
                                    pieceList.add(pieceHtmlMaterial.getId());
                                    assignmentTypes.put(pieceHtmlMaterial.getId(), assignmentType);
                                }
                            } else {
                                HtmlMaterial pieceHtmlMaterial = htmlMaterialController.findHtmlMaterialById(embeddedHtmlMaterialId);
                                logger.info("Existing html material " + embeddedHtmlMaterialId + " embedded in " + htmlMaterial.getId());
                                pieceHtmlMaterials.add(pieceHtmlMaterial);
                                pieceList.add(pieceHtmlMaterial.getId());
                                assignmentTypes.put(pieceHtmlMaterial.getId(), assignmentType);
                            }
                        } else {
                            String license = null;
                            HtmlMaterial pieceHtmlMaterial = htmlMaterialController.createHtmlMaterial(htmlMaterial.getTitle() + " (" + i + ")", DeusNexXmlUtils.serializeElement(documentPiece.getDocumentElement(), true, false, "xml"), "text/html; editor=CKEditor", 0l, license);
                            logger.info("New html material piece " + pieceHtmlMaterial.getId() + " split from " + htmlMaterial.getId());
                            pieceHtmlMaterials = new ArrayList<HtmlMaterial>();
                            pieceHtmlMaterials.add(pieceHtmlMaterial);
                            pieceList.add(pieceHtmlMaterial.getId());
                        }
                        for (HtmlMaterial pieceHtmlMaterial : pieceHtmlMaterials) {
                            WorkspaceNode newNode = workspaceMaterialController.createWorkspaceMaterial(parent, pieceHtmlMaterial, assignmentTypes.get(pieceHtmlMaterial.getId()), WorkspaceMaterialCorrectAnswersDisplay.ALWAYS);
                            workspaceMaterialController.moveAbove(newNode, workspaceMaterial);
                        }
                    }
                    workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, true);
                    htmlMaterialController.deleteHtmlMaterial(htmlMaterial);
                } else {
                    logger.info("Html material " + htmlMaterial.getId() + " has no embeds");
                }
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Html material " + htmlMaterial.getId() + " unembed fail", e);
            throw new DeusNexInternalException("MaterialUnEmbedder:unembedHtmlMaterial", e);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) ArrayList(java.util.ArrayList) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) Document(org.w3c.dom.Document) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) DeusNexInternalException(fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException) JsonParseException(org.codehaus.jackson.JsonParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Aggregations

WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)5 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)4 DeusNexInternalException (fi.otavanopisto.muikku.plugins.dnm.parser.DeusNexInternalException)3 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)3 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 SAXException (org.xml.sax.SAXException)2 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1 Folder (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Folder)1 Query (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Query)1 Resource (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.Resource)1 ResourceContainer (fi.otavanopisto.muikku.plugins.dnm.parser.structure.model.ResourceContainer)1 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)1 Material (fi.otavanopisto.muikku.plugins.material.model.Material)1 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)1 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1