use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType 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.WorkspaceMaterialAssignmentType in project muikku by otavanopisto.
the class DeusNexMachinaController method importResource.
private void importResource(WorkspaceNode importRoot, WorkspaceNode parent, Resource resource, DeusNexDocument deusNexDocument, List<WorkspaceNode> createdNodes) throws DeusNexException {
WorkspaceNode node = findNode(parent, resource);
if (resource.getType() == Type.FOLDER) {
Folder folderResource = (Folder) resource;
WorkspaceFolder folder = null;
if (node instanceof WorkspaceFolder) {
folder = (WorkspaceFolder) node;
}
if (folder == null) {
folder = createFolder(parent, folderResource);
try {
setResourceWorkspaceNodeId(resource.getNo(), folder.getId());
} catch (IOException e) {
throw new DeusNexInternalException("Failed to store resourceNo lookup file", e);
}
createdNodes.add(folder);
}
for (Resource childResource : folderResource.getResources()) {
importResource(importRoot, folder, childResource, deusNexDocument, createdNodes);
}
} else {
if (node == null) {
logger.fine("importing " + resource.getPath());
Material material = createMaterial(importRoot, resource, deusNexDocument);
if (material != null) {
WorkspaceMaterialAssignmentType assignmentType = null;
if (resource instanceof Query) {
switch(((Query) resource).getQueryType()) {
case "1":
assignmentType = WorkspaceMaterialAssignmentType.EXERCISE;
break;
case "2":
assignmentType = WorkspaceMaterialAssignmentType.EVALUATED;
break;
}
} else if (material instanceof HtmlMaterial) {
assignmentType = determineEmbeddedAssignmentType((HtmlMaterial) material);
}
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, material, resource.getName(), assignmentType, WorkspaceMaterialCorrectAnswersDisplay.ALWAYS);
try {
setResourceWorkspaceNodeId(resource.getNo(), workspaceMaterial.getId());
} catch (IOException e) {
throw new DeusNexInternalException("Failed to store resourceNo lookup file", e);
}
if (resource instanceof ResourceContainer) {
List<Resource> childResources = ((ResourceContainer) resource).getResources();
if (childResources != null) {
for (Resource childResource : childResources) {
importResource(importRoot, workspaceMaterial, childResource, deusNexDocument, createdNodes);
}
}
}
if (resource.getHidden()) {
workspaceMaterialController.hideWorkspaceNode(workspaceMaterial);
}
createdNodes.add(workspaceMaterial);
}
} else {
logger.info(node.getPath() + " already exists, skipping");
}
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType 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.WorkspaceMaterialAssignmentType in project muikku by otavanopisto.
the class DeusNexMachinaController method determineEmbeddedAssignmentType.
private WorkspaceMaterialAssignmentType determineEmbeddedAssignmentType(HtmlMaterial material) throws DeusNexException {
try {
if (material.getHtml() == null) {
return null;
}
StringReader htmlReader = new StringReader(material.getHtml());
DOMParser parser = new DOMParser();
InputSource inputSource = new InputSource(htmlReader);
parser.parse(inputSource);
org.w3c.dom.Document domDocument = parser.getDocument();
List<Element> elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe[@data-type=\"embedded-document\"]");
List<WorkspaceMaterialAssignmentType> assignmentTypes = new ArrayList<>();
if (!elements.isEmpty()) {
for (Element element : elements) {
if ("EXERCISE".equals(element.getAttribute("data-assignment-type"))) {
assignmentTypes.add(WorkspaceMaterialAssignmentType.EXERCISE);
}
if ("EVALUATED".equals(element.getAttribute("data-assignment-type"))) {
assignmentTypes.add(WorkspaceMaterialAssignmentType.EVALUATED);
}
}
}
if (assignmentTypes.isEmpty() || (assignmentTypes.contains(WorkspaceMaterialAssignmentType.EXERCISE) && assignmentTypes.contains(WorkspaceMaterialAssignmentType.EVALUATED))) {
return null;
} else {
return assignmentTypes.get(0);
}
} catch (SAXException | IOException | XPathExpressionException e) {
throw new DeusNexInternalException("Embedded assignment type handling failed. ", e);
}
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType in project muikku by otavanopisto.
the class MaterialUnEmbedder method unembedHtmlMaterial.
private void unembedHtmlMaterial(WorkspaceNode parent, WorkspaceMaterial workspaceMaterial, HtmlMaterial htmlMaterial) throws DeusNexInternalException, IOException {
String html = htmlMaterial.getHtml();
if (StringUtils.isNotBlank(html)) {
InputStream is = null;
logger.info("Unembedding html material " + htmlMaterial.getId());
try {
is = new ByteArrayInputStream(html.getBytes("UTF-8"));
DocumentBuilder builder = DeusNexXmlUtils.createDocumentBuilder();
Document document = builder.parse(is);
if (document != null) {
NodeList iframes = DeusNexXmlUtils.findNodesByXPath(document.getDocumentElement(), "//iframe[@data-type='embedded-document']");
if (iframes.getLength() > 0) {
List<Document> splittedHtmlDocument = splitHtmlDocument(document);
for (int i = 0; i < splittedHtmlDocument.size(); i++) {
List<Long> pieceList = new ArrayList<Long>();
htmlMaterialPieces.put(htmlMaterial.getId(), pieceList);
HashMap<Long, WorkspaceMaterialAssignmentType> assignmentTypes = new HashMap<Long, WorkspaceMaterialAssignmentType>();
Document documentPiece = splittedHtmlDocument.get(i);
List<HtmlMaterial> pieceHtmlMaterials;
if (isEmbedPiece(documentPiece)) {
WorkspaceMaterialAssignmentType assignmentType = embeddedHtmlMaterialAssignmentType(documentPiece);
pieceHtmlMaterials = new ArrayList<HtmlMaterial>();
long embeddedHtmlMaterialId = embeddedHtmlMaterialId(documentPiece);
if (htmlMaterialPieces.containsKey(embeddedHtmlMaterialId)) {
for (Long htmlMaterialId : htmlMaterialPieces.get(embeddedHtmlMaterialId)) {
logger.info("Existing html material " + htmlMaterialId + " embedded in " + htmlMaterial.getId());
HtmlMaterial pieceHtmlMaterial = htmlMaterialController.findHtmlMaterialById(htmlMaterialId);
pieceHtmlMaterials.add(pieceHtmlMaterial);
pieceList.add(pieceHtmlMaterial.getId());
assignmentTypes.put(pieceHtmlMaterial.getId(), assignmentType);
}
} else {
HtmlMaterial pieceHtmlMaterial = htmlMaterialController.findHtmlMaterialById(embeddedHtmlMaterialId);
logger.info("Existing html material " + embeddedHtmlMaterialId + " embedded in " + htmlMaterial.getId());
pieceHtmlMaterials.add(pieceHtmlMaterial);
pieceList.add(pieceHtmlMaterial.getId());
assignmentTypes.put(pieceHtmlMaterial.getId(), assignmentType);
}
} else {
String license = null;
HtmlMaterial pieceHtmlMaterial = htmlMaterialController.createHtmlMaterial(htmlMaterial.getTitle() + " (" + i + ")", DeusNexXmlUtils.serializeElement(documentPiece.getDocumentElement(), true, false, "xml"), "text/html; editor=CKEditor", 0l, license);
logger.info("New html material piece " + pieceHtmlMaterial.getId() + " split from " + htmlMaterial.getId());
pieceHtmlMaterials = new ArrayList<HtmlMaterial>();
pieceHtmlMaterials.add(pieceHtmlMaterial);
pieceList.add(pieceHtmlMaterial.getId());
}
for (HtmlMaterial pieceHtmlMaterial : pieceHtmlMaterials) {
WorkspaceNode newNode = workspaceMaterialController.createWorkspaceMaterial(parent, pieceHtmlMaterial, assignmentTypes.get(pieceHtmlMaterial.getId()), WorkspaceMaterialCorrectAnswersDisplay.ALWAYS);
workspaceMaterialController.moveAbove(newNode, workspaceMaterial);
}
}
workspaceMaterialController.deleteWorkspaceMaterial(workspaceMaterial, true);
htmlMaterialController.deleteHtmlMaterial(htmlMaterial);
} else {
logger.info("Html material " + htmlMaterial.getId() + " has no embeds");
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Html material " + htmlMaterial.getId() + " unembed fail", e);
throw new DeusNexInternalException("MaterialUnEmbedder:unembedHtmlMaterial", e);
} finally {
if (is != null) {
is.close();
}
}
}
}
Aggregations