use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class HtmlMaterialCloneOperation method clone.
@Override
public HtmlMaterial clone(HtmlMaterial material) {
HtmlMaterial clonedMaterial = htmlMaterialController.createHtmlMaterial(material.getTitle(), material.getHtml(), material.getContentType(), new Long(0), material, material.getLicense(), material.getViewRestrict());
List<MaterialMeta> materialMetas = materialController.listMaterialMetas(material);
for (MaterialMeta materialMeta : materialMetas) {
materialController.createMaterialMeta(clonedMaterial, materialMeta.getKey(), materialMeta.getValue());
}
List<MaterialProducer> materialProducers = materialController.listMaterialProducers(material);
for (MaterialProducer materialProducer : materialProducers) {
materialController.createMaterialProducer(clonedMaterial, materialProducer.getName());
}
return clonedMaterial;
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class DeusNexMachinaController method postProcessHtml.
private void postProcessHtml(HtmlMaterial material) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
StringReader htmlReader = new StringReader(material.getHtml());
DOMParser parser = new DOMParser(new HTMLConfiguration());
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
InputSource inputSource = new InputSource(htmlReader);
parser.parse(inputSource);
org.w3c.dom.Document domDocument = parser.getDocument();
boolean modified = false;
// Embedded YouTube clips; strip protocol
List<Element> elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe");
if (!elements.isEmpty()) {
for (Element element : elements) {
String src = element.getAttribute("src");
if (src != null && src.startsWith("http://www.youtube.com/")) {
element.setAttribute("src", src.substring(5));
modified = true;
}
}
}
// Embedded documents; add data attributes and determine correct material title
elements = DeusNexXmlUtils.getElementsByXPath(domDocument.getDocumentElement(), "//iframe[@data-type=\"embedded-document\"]");
if (!elements.isEmpty()) {
modified = true;
for (Element element : elements) {
Integer resourceNo = Integer.valueOf(element.getAttribute("data-resource-no"));
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(getResourceWorkspaceNodeId(resourceNo));
HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(workspaceMaterial.getMaterialId());
// If a header precedes an embedded document, use its text as the embedded
// document's title and remove it from the parent document altogether
Node possibleHeaderNode = getPreviousSiblingElement(element);
if (isHeader(possibleHeaderNode)) {
String headerText = StringUtils.trim(possibleHeaderNode.getTextContent());
if (!StringUtils.isBlank(headerText)) {
htmlMaterialController.updateHtmlMaterialTitle(htmlMaterial, headerText);
possibleHeaderNode.getParentNode().removeChild(possibleHeaderNode);
}
}
element.setAttribute("data-material-id", String.valueOf(htmlMaterial.getId()));
element.setAttribute("data-material-type", htmlMaterial.getType());
element.setAttribute("data-workspace-material-id", String.valueOf(workspaceMaterial.getId()));
}
}
// Update to post-processed version, if applicable
if (modified) {
StringWriter writer = new StringWriter();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.transform(new DOMSource(domDocument), new StreamResult(writer));
htmlMaterialController.updateHtmlMaterialHtml(material, writer.getBuffer().toString());
}
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial 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.material.model.HtmlMaterial in project muikku by otavanopisto.
the class DeusNexMachinaController method postProcessResources.
private void postProcessResources(List<WorkspaceNode> createdNodes) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException {
for (WorkspaceNode node : createdNodes) {
if (node.getType() == WorkspaceNodeType.MATERIAL) {
WorkspaceMaterial workspaceMaterial = (WorkspaceMaterial) node;
HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(workspaceMaterial.getMaterialId());
if (htmlMaterial != null && StringUtils.isNotBlank(htmlMaterial.getHtml())) {
postProcessHtml(htmlMaterial);
}
}
}
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class MaterialAttachmentUploadServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String materialUrl = req.getPathInfo();
if (StringUtils.isBlank(materialUrl)) {
sendResponse(resp, "Missing material path", HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (!sessionController.isLoggedIn()) {
sendResponse(resp, "Unauthorized", HttpServletResponse.SC_UNAUTHORIZED);
return;
}
Part file = req.getPart("upload");
if (file == null) {
sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
return;
}
long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
if (file.getSize() > fileSizeLimit) {
sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
WorkspaceMaterial parentWorkspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByRootPath(materialUrl);
if (parentWorkspaceMaterial == null) {
sendResponse(resp, "Material not found", HttpServletResponse.SC_NOT_FOUND);
return;
}
WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(parentWorkspaceMaterial);
if (workspaceRootFolder == null) {
sendResponse(resp, "Workspace root folder not found", HttpServletResponse.SC_NOT_FOUND);
return;
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
if (workspaceEntity == null) {
sendResponse(resp, "Workspace entity not found", HttpServletResponse.SC_NOT_FOUND);
return;
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
sendResponse(resp, "Forbidden", HttpServletResponse.SC_FORBIDDEN);
return;
}
HtmlMaterial parentMaterial = htmlMaterialController.findHtmlMaterialById(parentWorkspaceMaterial.getMaterialId());
if (parentMaterial == null) {
sendResponse(resp, "Parent material is not html material", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String license = null;
BinaryMaterial uploadedMaterial = binaryMaterialController.createBinaryMaterial(file.getSubmittedFileName(), file.getContentType(), IOUtils.toByteArray(file.getInputStream()), license);
String uploadedUrl = null;
List<WorkspaceMaterial> parentWorkspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(parentMaterial);
for (WorkspaceMaterial sharedWorkspaceMaterial : parentWorkspaceMaterials) {
WorkspaceMaterial uploadedWorkspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(sharedWorkspaceMaterial, uploadedMaterial);
if (sharedWorkspaceMaterial.getId().equals(parentWorkspaceMaterial.getId())) {
uploadedUrl = uploadedWorkspaceMaterial.getUrlName();
}
}
if (StringUtils.isBlank(uploadedUrl)) {
sendResponse(resp, "Could not resolve uploaded file url", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
UploadMeta uploadMeta = new UploadMeta(file.getName(), 1, uploadedUrl);
resp.setContentType("application/json");
ServletOutputStream servletOutputStream = resp.getOutputStream();
try {
(new ObjectMapper()).writeValue(servletOutputStream, uploadMeta);
} finally {
servletOutputStream.flush();
}
}
Aggregations