Search in sources :

Example 1 with HtmlMaterial

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();
        }
    }
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialDeleteError(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialDeleteError) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) IOException(java.io.IOException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Example 2 with HtmlMaterial

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;
}
Also used : HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) HtmlMaterialCreateEvent(fi.otavanopisto.muikku.plugins.material.events.HtmlMaterialCreateEvent)

Example 3 with HtmlMaterial

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());
    }
}
Also used : QueryField(fi.otavanopisto.muikku.plugins.material.model.QueryField) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 4 with HtmlMaterial

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)));
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) IOException(java.io.IOException) Date(java.util.Date) CoOpsPatchEvent(fi.otavanopisto.muikku.plugins.material.coops.event.CoOpsPatchEvent) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Patch(fi.foyt.coops.model.Patch) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 5 with HtmlMaterial

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;
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) ArrayList(java.util.ArrayList) HtmlMaterialRevisionProperty(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionProperty) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsNotFoundException(fi.foyt.coops.CoOpsNotFoundException) HtmlMaterialRevisionExtensionProperty(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionExtensionProperty) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Patch(fi.foyt.coops.model.Patch)

Aggregations

HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)29 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)14 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)10 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)7 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)7 Material (fi.otavanopisto.muikku.plugins.material.model.Material)7 Path (javax.ws.rs.Path)7 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)6 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)5 IOException (java.io.IOException)5 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)4 CoOpsNotImplementedException (fi.foyt.coops.CoOpsNotImplementedException)4 File (fi.foyt.coops.model.File)4 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)4 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)4 WorkspaceMaterialContainsAnswersExeption (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption)4 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)4 Patch (fi.foyt.coops.model.Patch)3 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)3 WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)3