use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class AcceptanceTestsRESTService method createWorkspaceMaterial.
@POST
@Path("/workspaces/{WORKSPACEID}/htmlmaterials")
@RESTPermit(handling = Handling.UNSECURED)
public Response createWorkspaceMaterial(fi.otavanopisto.muikku.atests.WorkspaceHtmlMaterial payload) {
if (payload.getParentId() == null) {
return Response.status(Status.BAD_REQUEST).entity("Mandatory parentId is missing").build();
}
HtmlMaterial htmlMaterial = htmlMaterialController.createHtmlMaterial(payload.getTitle(), payload.getHtml(), payload.getContentType(), payload.getRevisionNumber(), payload.getLicense());
WorkspaceNode parent = workspaceMaterialController.findWorkspaceNodeById(payload.getParentId());
if (parent == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid parentId").build();
}
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, htmlMaterial);
String assignmentType = payload.getAssignmentType();
if (StringUtils.isNotBlank(assignmentType)) {
WorkspaceMaterialAssignmentType workspaceMaterialAssignmentType = WorkspaceMaterialAssignmentType.valueOf(assignmentType);
if (workspaceMaterialAssignmentType == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid assignmentType '%s'", assignmentType)).build();
}
workspaceMaterialController.updateWorkspaceMaterialAssignmentType(workspaceMaterial, workspaceMaterialAssignmentType);
}
return Response.ok(createRestEntity(workspaceMaterial, htmlMaterial)).build();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceRESTService method listWorkspaceMaterials.
@GET
@Path("/workspaces/{WORKSPACEENTITYID}/materials/")
@RESTPermitUnimplemented
public Response listWorkspaceMaterials(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @QueryParam("parentId") Long parentId, @QueryParam("assignmentType") String assignmentType) {
if (parentId == null && assignmentType == null) {
return Response.status(Status.NOT_IMPLEMENTED).entity("Listing workspace materials without parentId or assignmentType is currently not implemented").build();
}
WorkspaceMaterialAssignmentType workspaceAssignmentType = null;
if (assignmentType != null) {
workspaceAssignmentType = WorkspaceMaterialAssignmentType.valueOf(assignmentType);
if (workspaceAssignmentType == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid assignmentType parameter").build();
}
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity("Could not find a workspace entity").build();
}
List<WorkspaceMaterial> workspaceMaterials = null;
if (parentId != null) {
WorkspaceNode parent = workspaceMaterialController.findWorkspaceNodeById(parentId);
if (parent == null) {
return Response.status(Status.BAD_REQUEST).entity("Given workspace parent material does not exist").build();
}
WorkspaceRootFolder rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(parent);
if (rootFolder == null) {
return Response.status(Status.BAD_REQUEST).entity("Could not find a workspace root folder").build();
}
if (!rootFolder.getWorkspaceEntityId().equals(workspaceEntityId)) {
return Response.status(Status.BAD_REQUEST).entity("Invalid parentId").build();
}
if (assignmentType != null) {
workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByParentAndAssignmentType(parent, workspaceEntity, workspaceAssignmentType, BooleanPredicate.IGNORE);
workspaceMaterials.removeIf(material -> isHiddenMaterial(material));
} else {
workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByParent(parent);
}
} else {
workspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByAssignmentType(workspaceEntity, workspaceAssignmentType, BooleanPredicate.IGNORE);
workspaceMaterials.removeIf(material -> isHiddenMaterial(material));
}
if (workspaceMaterials.isEmpty()) {
return Response.noContent().build();
}
return Response.ok(createRestModel(workspaceMaterials.toArray(new WorkspaceMaterial[0]))).build();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode 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();
}
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceRESTService method updateWorkspaceFolder.
@PUT
@Path("/workspaces/{WORKSPACEID}/folders/{WORKSPACEFOLDERID}")
@RESTPermitUnimplemented
public Response updateWorkspaceFolder(@PathParam("WORKSPACEID") Long workspaceEntityId, @PathParam("WORKSPACEFOLDERID") Long workspaceFolderId, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceFolder restFolder) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
}
if (restFolder == null) {
return Response.status(Status.BAD_REQUEST).build();
}
// Workspace
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.BAD_REQUEST).build();
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
// WorkspaceFolder
WorkspaceFolder workspaceFolder = workspaceMaterialController.findWorkspaceFolderById(restFolder.getId());
if (workspaceFolder == null) {
return Response.status(Status.BAD_REQUEST).build();
}
// Workspace folder belongs to workspace check
Long folderWorkspaceEntityId = workspaceMaterialController.getWorkspaceEntityId(workspaceFolder);
if (!folderWorkspaceEntityId.equals(workspaceEntityId)) {
return Response.status(Status.BAD_REQUEST).build();
}
// Actual update
WorkspaceNode parentNode = restFolder.getParentId() == null ? null : workspaceMaterialController.findWorkspaceNodeById(restFolder.getParentId());
WorkspaceNode nextSibling = restFolder.getNextSiblingId() == null ? null : workspaceMaterialController.findWorkspaceNodeById(restFolder.getNextSiblingId());
Boolean hidden = restFolder.getHidden();
String title = restFolder.getTitle();
MaterialViewRestrict viewRestrict = restFolder.getViewRestrict();
workspaceFolder = workspaceMaterialController.updateWorkspaceFolder(workspaceFolder, title, parentNode, nextSibling, hidden, viewRestrict);
return Response.ok(createRestModel(workspaceFolder)).build();
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceRESTService method updateWorkspaceMaterial.
@PUT
@Path("/workspaces/{WORKSPACEID}/materials/{WORKSPACEMATERIALID}")
@RESTPermitUnimplemented
public // TODO @LoggedIn
Response updateWorkspaceMaterial(@PathParam("WORKSPACEID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId, fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterial workspaceMaterial) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).entity("Not logged in").build();
}
// Workspace
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.BAD_REQUEST).build();
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
// WorkspaceNode
WorkspaceNode workspaceNode = workspaceMaterialController.findWorkspaceNodeById(workspaceMaterial.getId());
if (workspaceNode == null) {
return Response.status(Status.BAD_REQUEST).build();
}
// Workspace material belongs to workspace check
Long materialWorkspaceEntityId = workspaceMaterialController.getWorkspaceEntityId(workspaceNode);
if (!materialWorkspaceEntityId.equals(workspaceEntityId)) {
return Response.status(Status.BAD_REQUEST).build();
}
// Actual update
Long materialId = workspaceMaterial.getMaterialId();
WorkspaceNode parentNode = workspaceMaterialController.findWorkspaceNodeById(workspaceMaterial.getParentId());
WorkspaceNode nextSibling = workspaceMaterial.getNextSiblingId() == null ? null : workspaceMaterialController.findWorkspaceNodeById(workspaceMaterial.getNextSiblingId());
String title = workspaceMaterial.getTitle();
Boolean hidden = workspaceMaterial.getHidden();
workspaceNode = workspaceMaterialController.updateWorkspaceNode(workspaceNode, materialId, parentNode, nextSibling, hidden, workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers(), title);
workspaceMaterial.setPath(workspaceNode.getPath());
return Response.ok(workspaceMaterial).build();
}
Aggregations