use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class AcceptanceTestsRESTService method deleteWorkspaceMaterial.
@DELETE
@Path("/workspaces/{WORKSPACEENTITYID}/htmlmaterials/{WORKSPACEMATERIALID}")
@RESTPermit(handling = Handling.UNSECURED)
public Response deleteWorkspaceMaterial(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId) {
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
if (workspaceMaterial == null) {
return Response.status(Status.NOT_FOUND).entity("Not Found").build();
}
HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(workspaceMaterial.getMaterialId());
if (htmlMaterial == null) {
return Response.status(Status.BAD_REQUEST).entity("Not a html material").build();
}
try {
workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, true);
} catch (WorkspaceMaterialContainsAnswersExeption e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
List<WorkspaceMaterialEvaluation> evaluations = evaluationController.listWorkspaceMaterialEvaluationsByWorkspaceMaterialId(workspaceMaterialId);
for (WorkspaceMaterialEvaluation evaluation : evaluations) {
evaluationController.deleteWorkspaceMaterialEvaluation(evaluation);
}
htmlMaterialController.deleteHtmlMaterial(htmlMaterial);
return Response.noContent().build();
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class HtmlMaterialRESTService method revertMaterial.
@PUT
@Path("/{id}/revert/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response revertMaterial(@PathParam("id") Long id, HtmlRestMaterialRevert 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();
}
Long currentRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
if (!currentRevision.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).entity("Specified revision could not be found").build();
}
htmlMaterialController.updateHtmlMaterialToRevision(htmlMaterial, fileRevision.getContent(), entity.getToRevision(), true, 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();
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class HtmlMaterialRESTService method findMaterial.
@GET
@Path("/{id}")
@RESTPermitUnimplemented
public Response findMaterial(@PathParam("id") Long id, @QueryParam("revision") Long revision, @Context Request request) {
HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
if (htmlMaterial == null) {
return Response.status(Status.NOT_FOUND).build();
} else {
EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(revision == null ? htmlMaterial.getRevisionNumber() : revision)));
ResponseBuilder builder = request.evaluatePreconditions(tag);
if (builder != null) {
return builder.build();
}
CacheControl cacheControl = new CacheControl();
cacheControl.setMustRevalidate(true);
if (revision == null) {
return Response.ok(createRestModel(htmlMaterial)).build();
} else {
File fileRevision;
try {
fileRevision = coOpsApi.fileGet(id.toString(), revision);
} catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
if (fileRevision == null) {
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(createRestModel(htmlMaterial, fileRevision)).build();
}
}
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial 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();
}
}
Aggregations