Search in sources :

Example 1 with WorkspaceMaterialDeleteError

use of fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialDeleteError 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)

Aggregations

WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)1 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)1 Material (fi.otavanopisto.muikku.plugins.material.model.Material)1 WorkspaceMaterialContainsAnswersExeption (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption)1 WorkspaceMaterialDeleteError (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialDeleteError)1 WorkspaceFieldIOException (fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException)1 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)1 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 IOException (java.io.IOException)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1