Search in sources :

Example 1 with Material

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

the class WorkspaceRESTService method deleteNode.

@DELETE
@Path("/workspaces/{WORKSPACEID}/materials/{WORKSPACEMATERIALID}")
@RESTPermitUnimplemented
public Response deleteNode(@PathParam("WORKSPACEID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId, @QueryParam("removeAnswers") Boolean removeAnswers, @QueryParam("updateLinkedMaterials") @DefaultValue("false") Boolean updateLinkedMaterials) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).build();
    } else {
        try {
            // #1261: HtmlMaterial attachments should be removed from all workspace materials sharing the same HtmlMaterial
            if (updateLinkedMaterials) {
                WorkspaceNode parentNode = workspaceMaterial.getParent();
                if (parentNode instanceof WorkspaceMaterial) {
                    Long parentMaterialId = ((WorkspaceMaterial) parentNode).getMaterialId();
                    if (parentMaterialId != null) {
                        Material parentMaterial = materialController.findMaterialById(parentMaterialId);
                        if (parentMaterial instanceof HtmlMaterial) {
                            List<WorkspaceMaterial> sharedWorkspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(parentMaterial);
                            for (WorkspaceMaterial sharedWorkspaceMaterial : sharedWorkspaceMaterials) {
                                WorkspaceMaterial childWorkspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByParentAndUrlName(sharedWorkspaceMaterial, workspaceMaterial.getUrlName());
                                if (childWorkspaceMaterial.getId().equals(workspaceMaterial.getId())) {
                                    // skip the one we delete below
                                    continue;
                                }
                                workspaceMaterialController.deleteWorkspaceMaterial(childWorkspaceMaterial, removeAnswers != null ? removeAnswers : false);
                            }
                        }
                    }
                }
            }
            workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, removeAnswers != null ? removeAnswers : false);
            return Response.noContent().build();
        } catch (WorkspaceMaterialContainsAnswersExeption e) {
            return Response.status(Status.CONFLICT).entity(new WorkspaceMaterialDeleteError(WorkspaceMaterialDeleteError.Reason.CONTAINS_ANSWERS)).build();
        } catch (Exception e) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
    }
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialDeleteError(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialDeleteError) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) 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) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) IOException(java.io.IOException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Example 2 with Material

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

the class WorkspaceHtmlMaterialDeleteListener method onHtmlMaterialDelete.

public void onHtmlMaterialDelete(@Observes HtmlMaterialDeleteEvent htmlMaterialDeleteEvent) throws WorkspaceMaterialContainsAnswersExeption {
    // TODO: This should not be limited to html materials
    Material material = htmlMaterialDeleteEvent.getMaterial();
    List<WorkspaceMaterial> workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(material);
    for (WorkspaceMaterial workspaceMaterial : workspaceMaterials) {
        workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, htmlMaterialDeleteEvent.getRemoveAnswers());
    }
}
Also used : Material(fi.otavanopisto.muikku.plugins.material.model.Material) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 3 with Material

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

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

the class WorkspaceMaterialController method revertToOriginMaterial.

public WorkspaceMaterial revertToOriginMaterial(WorkspaceMaterial workspaceMaterial, boolean updateUrlName) {
    Material originMaterial = getMaterialForWorkspaceMaterial(workspaceMaterial).getOriginMaterial();
    if (originMaterial == null) {
        throw new IllegalArgumentException("WorkSpaceMaterial has no origin material");
    }
    workspaceMaterialDAO.updateMaterialId(workspaceMaterial, originMaterial.getId());
    if (updateUrlName) {
        String urlName = generateUniqueUrlName(workspaceMaterial.getParent(), workspaceMaterial, originMaterial.getTitle());
        if (!workspaceMaterial.getUrlName().equals(urlName)) {
            workspaceMaterialDAO.updateUrlName(workspaceMaterial, urlName);
        }
    }
    return workspaceMaterial;
}
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)

Example 5 with Material

use of fi.otavanopisto.muikku.plugins.material.model.Material 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)

Aggregations

Material (fi.otavanopisto.muikku.plugins.material.model.Material)20 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)15 Path (javax.ws.rs.Path)10 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)9 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)7 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)6 GET (javax.ws.rs.GET)4 Produces (javax.ws.rs.Produces)4 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)3 MaterialMeta (fi.otavanopisto.muikku.plugins.material.model.MaterialMeta)3 MaterialMetaKey (fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey)3 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)3 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)3 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)3 IOException (java.io.IOException)3 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)2 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)2 WorkspaceMaterialField (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField)2 ArrayList (java.util.ArrayList)2 DELETE (javax.ws.rs.DELETE)2