Search in sources :

Example 16 with Material

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

the class TaskRelativeWorkspaceMaterialResources method cleanElement.

@Override
protected void cleanElement(Element element) {
    boolean resourceConflict = false;
    // TODO: Treats all relative links as immediate child resources of this material ONLY
    String elementName = element.getTagName();
    if ("img".equals(elementName) || "source".equals(elementName) || "embed".equals(elementName) || "object".equals(elementName)) {
        String attributeName = "object".equals(elementName) ? "data" : "src";
        String src = element.getAttribute(attributeName);
        if (StringUtils.startsWith(src, "/workspace/")) {
            logger.info(String.format("Processing resource %s", src));
            // Resolve WorkspaceMaterial and Material the link points to
            WorkspaceMaterial resourceWorkspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByRootPath(src);
            if (resourceWorkspaceMaterial != null) {
                String resourceUrlName = resourceWorkspaceMaterial.getUrlName();
                Material resourceMaterial = resourceWorkspaceMaterial == null ? null : workspaceMaterialController.getMaterialForWorkspaceMaterial(resourceWorkspaceMaterial);
                if (resourceMaterial != null) {
                    // Ensure that all workspace materials using this HTML material will have the link target as a child resource
                    Material htmlMaterial = workspaceMaterialController.getMaterialForWorkspaceMaterial(getWorkspaceMaterial());
                    List<WorkspaceMaterial> workspaceHtmlMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(htmlMaterial);
                    for (WorkspaceMaterial workspaceHtmlMaterial : workspaceHtmlMaterials) {
                        WorkspaceMaterial childMaterial = workspaceMaterialController.findWorkspaceMaterialByParentAndUrlName(workspaceHtmlMaterial, resourceUrlName);
                        if (childMaterial == null) {
                            childMaterial = workspaceMaterialController.createWorkspaceMaterial(workspaceHtmlMaterial, resourceMaterial, resourceUrlName, null, null);
                            logger.info(String.format("Created resource %d with relative url %s", childMaterial.getId(), resourceUrlName));
                        } else {
                            resourceConflict = !resourceMaterial.getId().equals(childMaterial.getMaterialId());
                            if (resourceConflict) {
                                logger.warning(String.format("Skipping resource because WorkspaceMaterial %d has conflicting child resource %s", workspaceHtmlMaterial.getId(), resourceUrlName));
                                break;
                            }
                        }
                    }
                    if (!resourceConflict) {
                        element.setAttribute(attributeName, resourceUrlName);
                        markModified();
                    }
                } else {
                    logger.warning("Resource does not resolve to Material");
                }
            } else {
                logger.warning("Resource does not resolve to WorkspaceMaterial");
            }
        }
    }
}
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 17 with Material

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

the class MaterialDAO method listByOriginMaterial.

public List<Material> listByOriginMaterial(Material material) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Material> criteria = criteriaBuilder.createQuery(Material.class);
    Root<Material> root = criteria.from(Material.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(Material_.originMaterial), material));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Material(fi.otavanopisto.muikku.plugins.material.model.Material)

Example 18 with Material

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

the class MaterialMetaRESTService method updateMaterialMeta.

@PUT
@Path("/materials/{id}/meta/{KEY}")
@Produces(MediaType.APPLICATION_JSON)
@RESTPermitUnimplemented
public Response updateMaterialMeta(@PathParam("id") Long id, @PathParam("KEY") String key, fi.otavanopisto.muikku.plugins.material.rest.MaterialMeta payload) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIAL_META)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    if (payload.getKey() != null && !StringUtils.equals(payload.getKey(), key)) {
        return Response.status(Status.BAD_REQUEST).entity("Can not update material meta key").build();
    }
    Material material = materialController.findMaterialById(id);
    if (material == null) {
        return Response.status(Status.NOT_FOUND).entity("Material not found").build();
    }
    MaterialMetaKey materialMetaKey = materialController.findMaterialMetaKey(key);
    if (materialMetaKey == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid key").build();
    }
    MaterialMeta materialMeta = materialController.findMaterialMeta(material, materialMetaKey);
    if (materialMeta == null) {
        return Response.status(Status.BAD_REQUEST).entity("MaterialMeta not found").build();
    }
    return Response.ok(createRestModel(materialController.updateMaterialMeta(materialMeta, payload.getValue()))).build();
}
Also used : MaterialMeta(fi.otavanopisto.muikku.plugins.material.model.MaterialMeta) MaterialMetaKey(fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey) Material(fi.otavanopisto.muikku.plugins.material.model.Material) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) PUT(javax.ws.rs.PUT)

Example 19 with Material

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

the class MaterialMetaRESTService method findMaterialMeta.

@GET
@Path("/materials/{id}/meta/{KEY}")
@Produces(MediaType.APPLICATION_JSON)
@RESTPermitUnimplemented
public Response findMaterialMeta(@PathParam("id") Long id, @PathParam("KEY") String key) {
    Material material = materialController.findMaterialById(id);
    if (material == null) {
        return Response.status(Status.NOT_FOUND).entity("Material not found").build();
    }
    MaterialMetaKey materialMetaKey = materialController.findMaterialMetaKey(key);
    if (materialMetaKey == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid key").build();
    }
    MaterialMeta materialMeta = materialController.findMaterialMeta(material, materialMetaKey);
    if (materialMeta == null) {
        return Response.status(Status.NOT_FOUND).entity("Material meta not found").build();
    }
    return Response.ok(createRestModel(materialMeta)).build();
}
Also used : MaterialMeta(fi.otavanopisto.muikku.plugins.material.model.MaterialMeta) MaterialMetaKey(fi.otavanopisto.muikku.plugins.material.model.MaterialMetaKey) Material(fi.otavanopisto.muikku.plugins.material.model.Material) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Example 20 with Material

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

the class WorkspaceRESTService method createWorkspaceMaterial.

@POST
@Path("/workspaces/{ID}/materials/")
@RESTPermitUnimplemented
public Response createWorkspaceMaterial(@PathParam("ID") Long workspaceEntityId, @QueryParam("sourceNodeId") Long sourceNodeId, @QueryParam("targetNodeId") Long targetNodeId, @QueryParam("sourceWorkspaceEntityId") Long sourceWorkspaceEntityId, @QueryParam("targetWorkspaceEntityId") Long targetWorkspaceEntityId, @QueryParam("copyOnlyChildren") Boolean copyOnlyChildren, @QueryParam("cloneMaterials") @DefaultValue("false") Boolean cloneMaterials, @QueryParam("updateLinkedMaterials") @DefaultValue("false") Boolean updateLinkedMaterials, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterial entity) {
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if ((sourceNodeId != null) || (sourceWorkspaceEntityId != null)) {
        if (sourceNodeId == null) {
            WorkspaceEntity sourceWorkspaceEntity = workspaceController.findWorkspaceEntityById(sourceWorkspaceEntityId);
            if (sourceWorkspaceEntity == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid sourceWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
            }
            WorkspaceRootFolder sourceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(sourceWorkspaceEntity);
            if (sourceRootFolder == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid sourceWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
            }
            sourceNodeId = sourceRootFolder.getId();
        }
        if (targetNodeId == null) {
            if (targetWorkspaceEntityId != null) {
                WorkspaceEntity targetWorkspaceEntity = workspaceController.findWorkspaceEntityById(targetWorkspaceEntityId);
                if (targetWorkspaceEntity == null) {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid targetWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
                }
                WorkspaceRootFolder targetRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(targetWorkspaceEntity);
                if (targetRootFolder == null) {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid targetWorkspaceEntity %d", sourceWorkspaceEntityId)).build();
                }
                targetNodeId = targetRootFolder.getId();
            }
        }
        if (targetNodeId == null) {
            return Response.status(Status.BAD_REQUEST).entity("targetNodeId is required when sourceNodeId is specified").build();
        }
        if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.COPY_WORKSPACE)) {
            return Response.status(Status.FORBIDDEN).build();
        }
        // Source
        WorkspaceNode sourceNode = workspaceMaterialController.findWorkspaceNodeById(sourceNodeId);
        if (sourceNode == null) {
            return Response.status(Status.BAD_REQUEST).entity("null source").build();
        }
        // Target
        WorkspaceNode targetNode = workspaceMaterialController.findWorkspaceNodeById(targetNodeId);
        if (targetNode == null) {
            return Response.status(Status.BAD_REQUEST).entity("null target").build();
        }
        WorkspaceRootFolder targetRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(targetNode);
        if (!targetRootFolder.getWorkspaceEntityId().equals(workspaceEntity.getId())) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("targetNode does not belong to workspace entity %d", workspaceEntity.getId())).build();
        }
        // Circular reference check
        WorkspaceNode node = targetNode;
        while (node != null) {
            if (node.getId().equals(sourceNode.getId())) {
                return Response.status(Status.BAD_REQUEST).entity("Circular copy reference").build();
            }
            node = node.getParent();
        }
        // Copy
        WorkspaceNode createdNode = null;
        WorkspaceMaterial createdMaterial = null;
        if (copyOnlyChildren) {
            List<WorkspaceNode> sourceChildren = workspaceMaterialController.listWorkspaceNodesByParent(sourceNode);
            for (WorkspaceNode sourceChild : sourceChildren) {
                workspaceMaterialController.cloneWorkspaceNode(sourceChild, targetNode, cloneMaterials);
            }
        } else {
            createdNode = workspaceMaterialController.cloneWorkspaceNode(sourceNode, targetNode, cloneMaterials);
            if (createdNode.getType() == WorkspaceNodeType.MATERIAL) {
                createdMaterial = workspaceMaterialController.findWorkspaceMaterialById(createdNode.getId());
                if (entity != null && entity.getNextSiblingId() != null) {
                    WorkspaceNode nextSibling = workspaceMaterialController.findWorkspaceNodeById(entity.getNextSiblingId());
                    if (nextSibling == null) {
                        return Response.status(Status.BAD_REQUEST).entity("Specified next sibling does not exist").build();
                    }
                    workspaceMaterialController.moveAbove(createdNode, nextSibling);
                }
            }
        }
        return createdMaterial == null ? Response.noContent().build() : Response.ok(createRestModel(createdMaterial)).build();
    } else {
        if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
            return Response.status(Status.FORBIDDEN).build();
        }
        if (entity.getMaterialId() == null) {
            return Response.status(Status.BAD_REQUEST).entity("material_id is required when creating new WorkspaceMaterial").build();
        }
        WorkspaceNode parent = null;
        if (entity.getParentId() != null) {
            parent = workspaceMaterialController.findWorkspaceNodeById(entity.getParentId());
            if (parent == null) {
                return Response.status(Status.NOT_FOUND).entity("parent not found").build();
            }
        } else {
            parent = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
        }
        Material material = materialController.findMaterialById(entity.getMaterialId());
        if (material == null) {
            return Response.status(Status.NOT_FOUND).entity("material not found").build();
        }
        WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, material, entity.getAssignmentType(), entity.getCorrectAnswers());
        if (entity.getNextSiblingId() != null) {
            WorkspaceNode nextSibling = workspaceMaterialController.findWorkspaceNodeById(entity.getNextSiblingId());
            if (nextSibling == null) {
                return Response.status(Status.BAD_REQUEST).entity("Specified next sibling does not exist").build();
            }
            if (!nextSibling.getParent().getId().equals(parent.getId())) {
                return Response.status(Status.BAD_REQUEST).entity("Specified next sibling does not share parent with created workspace material").build();
            }
            workspaceMaterialController.moveAbove(workspaceMaterial, nextSibling);
        }
        // #1261: HtmlMaterial attachments should be added to all workspace materials sharing the same HtmlMaterial
        if (updateLinkedMaterials && parent instanceof WorkspaceMaterial) {
            Long parentMaterialId = ((WorkspaceMaterial) parent).getMaterialId();
            if (parentMaterialId != null) {
                Material parentMaterial = materialController.findMaterialById(parentMaterialId);
                if (parentMaterial instanceof HtmlMaterial) {
                    List<WorkspaceMaterial> sharedWorkspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(parentMaterial);
                    for (WorkspaceMaterial sharedWorkspaceMaterial : sharedWorkspaceMaterials) {
                        if (sharedWorkspaceMaterial.getId().equals(workspaceMaterial.getId())) {
                            // skip the one we created above
                            continue;
                        }
                        WorkspaceMaterial sharedAttachment = workspaceMaterialController.findWorkspaceMaterialByParentAndUrlName(sharedWorkspaceMaterial, workspaceMaterial.getUrlName());
                        if (sharedAttachment == null) {
                            workspaceMaterialController.createWorkspaceMaterial(sharedWorkspaceMaterial, material, workspaceMaterial.getUrlName(), workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers());
                        }
                    }
                }
            }
        }
        return Response.ok(createRestModel(workspaceMaterial)).build();
    }
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) 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) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

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