Search in sources :

Example 6 with HtmlMaterial

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

the class CoOpsApiImpl method fileJoin.

public Join fileJoin(String fileId, List<String> algorithms, String protocolVersion) throws CoOpsNotFoundException, CoOpsNotImplementedException, CoOpsInternalErrorException, CoOpsForbiddenException, CoOpsUsageException {
    HtmlMaterial htmlMaterial = findFile(fileId);
    if (!COOPS_PROTOCOL_VERSION.equals(protocolVersion)) {
        throw new CoOpsNotImplementedException("Protocol version mismatch. Client is using " + protocolVersion + " and server " + COOPS_PROTOCOL_VERSION);
    }
    if (algorithms == null || algorithms.isEmpty()) {
        throw new CoOpsInternalErrorException("Invalid request");
    }
    CoOpsDiffAlgorithm algorithm = htmlMaterialController.findAlgorithm(algorithms);
    if (algorithm == null) {
        throw new CoOpsNotImplementedException("Server and client do not have a commonly supported algorithm.");
    }
    Long currentRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
    String data = htmlMaterialController.getRevisionHtml(htmlMaterial, currentRevision);
    if (data == null) {
        data = "";
    }
    Map<String, String> properties = htmlMaterialController.getRevisionProperties(htmlMaterial, currentRevision);
    // TODO: Extension properties...
    Map<String, Object> extensions = new HashMap<>();
    String sessionId = UUID.randomUUID().toString();
    CoOpsSession coOpsSession = coOpsSessionController.createSession(htmlMaterial, sessionController.getLoggedUserEntity(), sessionId, currentRevision, algorithm.getName());
    addSessionEventsExtension(htmlMaterial, extensions);
    addWebSocketExtension(htmlMaterial, extensions, coOpsSession);
    return new Join(coOpsSession.getSessionId(), coOpsSession.getAlgorithm(), coOpsSession.getJoinRevision(), data, htmlMaterial.getContentType(), properties, extensions);
}
Also used : CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) HashMap(java.util.HashMap) Join(fi.foyt.coops.model.Join) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 7 with HtmlMaterial

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

the class CoOpsApiImpl method findFile.

private HtmlMaterial findFile(String fileId) throws CoOpsUsageException, CoOpsNotFoundException {
    if (!StringUtils.isNumeric(fileId)) {
        throw new CoOpsUsageException("fileId must be a number");
    }
    Long id = NumberUtils.createLong(fileId);
    if (id == null) {
        throw new CoOpsUsageException("fileId must be a number");
    }
    HtmlMaterial file = htmlMaterialController.findHtmlMaterialById(id);
    if (file == null) {
        throw new CoOpsNotFoundException();
    }
    return file;
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 8 with HtmlMaterial

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

the class WorkspaceMaterialController method cloneWorkspaceNode.

private WorkspaceNode cloneWorkspaceNode(WorkspaceNode workspaceNode, WorkspaceNode parent, boolean cloneMaterials, boolean overrideCloneMaterials) {
    WorkspaceNode newNode;
    boolean isHtmlMaterial = false;
    Integer index = workspaceNodeDAO.getMaximumOrderNumber(parent);
    index = index == null ? 0 : ++index;
    if (workspaceNode instanceof WorkspaceMaterial) {
        WorkspaceMaterial workspaceMaterial = (WorkspaceMaterial) workspaceNode;
        Material material = getMaterialForWorkspaceMaterial(workspaceMaterial);
        isHtmlMaterial = material instanceof HtmlMaterial;
        Material clonedMaterial = cloneMaterials && !overrideCloneMaterials ? materialController.cloneMaterial(material) : material;
        // Implementation of feature #1232 (front and help pages should always be copies)
        if (isHtmlMaterial && !cloneMaterials) {
            WorkspaceNode parentNode = workspaceMaterial.getParent();
            if (parentNode instanceof WorkspaceFolder) {
                WorkspaceFolder parentFolder = (WorkspaceFolder) parentNode;
                if (parentFolder.getFolderType() == WorkspaceFolderType.FRONT_PAGE || parentFolder.getFolderType() == WorkspaceFolderType.HELP_PAGE) {
                    clonedMaterial = materialController.cloneMaterial(material);
                }
            }
        }
        newNode = createWorkspaceMaterial(parent, clonedMaterial, workspaceMaterial.getTitle(), generateUniqueUrlName(parent, workspaceMaterial.getUrlName()), index, workspaceMaterial.getHidden(), workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers());
    } else if (workspaceNode instanceof WorkspaceFolder) {
        newNode = createWorkspaceFolder(parent, ((WorkspaceFolder) workspaceNode).getTitle(), generateUniqueUrlName(parent, workspaceNode.getUrlName()), index, workspaceNode.getHidden(), ((WorkspaceFolder) workspaceNode).getFolderType(), ((WorkspaceFolder) workspaceNode).getViewRestrict());
    } else {
        throw new IllegalArgumentException("Uncloneable workspace node " + workspaceNode.getClass());
    }
    List<WorkspaceNode> childNodes = workspaceNodeDAO.listByParentSortByOrderNumber(workspaceNode);
    for (WorkspaceNode childNode : childNodes) {
        cloneWorkspaceNode(childNode, newNode, cloneMaterials, isHtmlMaterial);
    }
    return newNode;
}
Also used : Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)

Example 9 with HtmlMaterial

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

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

the class HtmlMaterialRESTService method publishMaterial.

@POST
@Path("/{id}/publish/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response publishMaterial(@PathParam("id") Long id, HtmlRestMaterialPublish entity) {
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIALS)) {
        return Response.status(Status.FORBIDDEN).entity("Permission denied").build();
    }
    HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
    if (htmlMaterial == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!htmlMaterial.getRevisionNumber().equals(entity.getFromRevision())) {
        return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONCURRENT_MODIFICATIONS)).build();
    }
    try {
        File fileRevision = coOpsApi.fileGet(id.toString(), entity.getToRevision());
        if (fileRevision == null) {
            return Response.status(Status.NOT_FOUND).build();
        }
        htmlMaterialController.updateHtmlMaterialToRevision(htmlMaterial, fileRevision.getContent(), entity.getToRevision(), false, entity.getRemoveAnswers() != null ? entity.getRemoveAnswers() : false);
    } catch (WorkspaceMaterialContainsAnswersExeption e) {
        return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONTAINS_ANSWERS)).build();
    } catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    return Response.noContent().build();
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) CoOpsForbiddenException(fi.foyt.coops.CoOpsForbiddenException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) File(fi.foyt.coops.model.File) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

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