use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial 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.material.model.HtmlMaterial in project muikku by otavanopisto.
the class HtmlMaterialController method createHtmlMaterial.
public HtmlMaterial createHtmlMaterial(String title, String html, String contentType, Long revisionNumber, HtmlMaterial originMaterial, String license, MaterialViewRestrict visibility) {
HtmlMaterial material = htmlMaterialDAO.create(title, html, contentType, revisionNumber, originMaterial, license, visibility);
materialCreateEvent.fire(new HtmlMaterialCreateEvent(material));
return material;
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class HtmlMaterialFieldChangeListener method onHtmlMaterialFieldDeleted.
// Delete
public void onHtmlMaterialFieldDeleted(@Observes HtmlMaterialFieldDeleteEvent event) {
HtmlMaterial material = event.getMaterial();
QueryField queryField = queryFieldController.findQueryFieldByMaterialAndName(material, event.getField().getName());
if (queryField != null) {
queryFieldController.deleteQueryField(queryField, event.getRemoveAnswers());
}
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class CoOpsApiImpl method filePatch.
public void filePatch(String fileId, String sessionId, Long revisionNumber, String patch, Map<String, String> properties, Map<String, Object> extensions) throws CoOpsInternalErrorException, CoOpsUsageException, CoOpsNotFoundException, CoOpsConflictException, CoOpsForbiddenException {
CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
if (session == null) {
throw new CoOpsUsageException("Invalid session id");
}
CoOpsDiffAlgorithm algorithm = htmlMaterialController.findAlgorithm(session.getAlgorithm());
if (algorithm == null) {
throw new CoOpsUsageException("Algorithm is not supported by this server");
}
HtmlMaterial htmlMaterial = findFile(fileId);
Long maxRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
if (!maxRevision.equals(revisionNumber)) {
throw new CoOpsConflictException();
}
ObjectMapper objectMapper = new ObjectMapper();
String checksum = null;
if (StringUtils.isNotBlank(patch)) {
String data = htmlMaterialController.getRevisionHtml(htmlMaterial, maxRevision);
if (data == null) {
data = "";
}
String patched = algorithm.patch(data, patch);
checksum = DigestUtils.md5Hex(patched);
}
Long patchRevisionNumber = maxRevision + 1;
HtmlMaterialRevision htmlMaterialRevision = htmlMaterialController.createRevision(htmlMaterial, sessionId, patchRevisionNumber, new Date(), patch, checksum);
if (properties != null) {
for (String key : properties.keySet()) {
String value = properties.get(key);
htmlMaterialController.createRevisionProperty(htmlMaterialRevision, key, value);
}
}
if (extensions != null) {
for (String key : extensions.keySet()) {
String value;
try {
value = objectMapper.writeValueAsString(extensions.get(key));
} catch (IOException e) {
throw new CoOpsInternalErrorException(e);
}
htmlMaterialController.createRevisionExtensionProperty(htmlMaterialRevision, key, value);
}
}
patchEvent.fire(new CoOpsPatchEvent(fileId, new Patch(sessionId, patchRevisionNumber, checksum, patch, properties, extensions)));
}
use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.
the class CoOpsApiImpl method fileUpdate.
public List<Patch> fileUpdate(String fileId, String sessionId, Long revisionNumber) throws CoOpsNotFoundException, CoOpsInternalErrorException, CoOpsUsageException, CoOpsForbiddenException {
CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
if (session == null) {
throw new CoOpsUsageException("Invalid session id");
}
if (revisionNumber == null) {
throw new CoOpsUsageException("revisionNumber parameter is missing");
}
HtmlMaterial htmlMaterial = findFile(fileId);
if (htmlMaterial == null) {
throw new CoOpsNotFoundException();
}
List<Patch> updateResults = new ArrayList<>();
List<HtmlMaterialRevision> htmlMaterialRevisions = htmlMaterialController.listRevisionsAfter(htmlMaterial, revisionNumber);
if (!htmlMaterialRevisions.isEmpty()) {
for (HtmlMaterialRevision htmlMaterialRevision : htmlMaterialRevisions) {
String patch = htmlMaterialRevision.getData();
Map<String, String> properties = null;
Map<String, Object> extensions = null;
List<HtmlMaterialRevisionProperty> revisionProperties = htmlMaterialController.listRevisionProperties(htmlMaterialRevision);
if (revisionProperties.size() > 0) {
properties = new HashMap<>();
for (HtmlMaterialRevisionProperty revisionProperty : revisionProperties) {
properties.put(revisionProperty.getKey(), revisionProperty.getValue());
}
}
List<HtmlMaterialRevisionExtensionProperty> revisionExtensionProperties = htmlMaterialController.listRevisionExtensionProperties(htmlMaterialRevision);
if (revisionExtensionProperties.size() > 0) {
extensions = new HashMap<>();
for (HtmlMaterialRevisionExtensionProperty revisionExtensionProperty : revisionExtensionProperties) {
extensions.put(revisionExtensionProperty.getKey(), revisionExtensionProperty.getValue());
}
}
if (patch != null) {
updateResults.add(new Patch(htmlMaterialRevision.getSessionId(), htmlMaterialRevision.getRevision(), htmlMaterialRevision.getChecksum(), patch, properties, extensions));
} else {
updateResults.add(new Patch(htmlMaterialRevision.getSessionId(), htmlMaterialRevision.getRevision(), null, null, properties, extensions));
}
}
}
return updateResults;
}
Aggregations