Search in sources :

Example 1 with CoOpsSession

use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession 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 2 with CoOpsSession

use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession 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)

Example 3 with CoOpsSession

use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession in project muikku by otavanopisto.

the class CoOpsApiImpl method fileJoin.

public Join fileJoin(String fileId, List<String> algorithms, String protocolVersion) throws CoOpsNotFoundException, CoOpsNotImplementedException, CoOpsInternalErrorException, CoOpsForbiddenException, CoOpsUsageException {
    HtmlMaterial htmlMaterial = findFile(fileId);
    if (!COOPS_PROTOCOL_VERSION.equals(protocolVersion)) {
        throw new CoOpsNotImplementedException("Protocol version mismatch. Client is using " + protocolVersion + " and server " + COOPS_PROTOCOL_VERSION);
    }
    if (algorithms == null || algorithms.isEmpty()) {
        throw new CoOpsInternalErrorException("Invalid request");
    }
    CoOpsDiffAlgorithm algorithm = htmlMaterialController.findAlgorithm(algorithms);
    if (algorithm == null) {
        throw new CoOpsNotImplementedException("Server and client do not have a commonly supported algorithm.");
    }
    Long currentRevision = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
    String data = htmlMaterialController.getRevisionHtml(htmlMaterial, currentRevision);
    if (data == null) {
        data = "";
    }
    Map<String, String> properties = htmlMaterialController.getRevisionProperties(htmlMaterial, currentRevision);
    // TODO: Extension properties...
    Map<String, Object> extensions = new HashMap<>();
    String sessionId = UUID.randomUUID().toString();
    CoOpsSession coOpsSession = coOpsSessionController.createSession(htmlMaterial, sessionController.getLoggedUserEntity(), sessionId, currentRevision, algorithm.getName());
    addSessionEventsExtension(htmlMaterial, extensions);
    addWebSocketExtension(htmlMaterial, extensions, coOpsSession);
    return new Join(coOpsSession.getSessionId(), coOpsSession.getAlgorithm(), coOpsSession.getJoinRevision(), data, htmlMaterial.getContentType(), properties, extensions);
}
Also used : CoOpsNotImplementedException(fi.foyt.coops.CoOpsNotImplementedException) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) HashMap(java.util.HashMap) Join(fi.foyt.coops.model.Join) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 4 with CoOpsSession

use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession in project muikku by otavanopisto.

the class CoOpsSessionDAO method findBySessionId.

public CoOpsSession findBySessionId(String sessionId) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CoOpsSession> criteria = criteriaBuilder.createQuery(CoOpsSession.class);
    Root<CoOpsSession> root = criteria.from(CoOpsSession.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(CoOpsSession_.sessionId), sessionId));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)

Example 5 with CoOpsSession

use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession in project muikku by otavanopisto.

the class CoOpsSessionDAO method create.

public CoOpsSession create(HtmlMaterial htmlMaterial, Long userEntityId, String sessionId, CoOpsSessionType type, Long joinRevision, String algorithm, Boolean closed, Date accessed) {
    CoOpsSession coOpsSession = new CoOpsSession();
    coOpsSession.setAccessed(accessed);
    coOpsSession.setAlgorithm(algorithm);
    coOpsSession.setClosed(closed);
    coOpsSession.setHtmlMaterial(htmlMaterial);
    coOpsSession.setUserEntityId(userEntityId);
    coOpsSession.setJoinRevision(joinRevision);
    coOpsSession.setSessionId(sessionId);
    coOpsSession.setType(type);
    return persist(coOpsSession);
}
Also used : CoOpsSession(fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)

Aggregations

CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)13 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)4 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)4 Patch (fi.foyt.coops.model.Patch)4 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)4 EntityManager (javax.persistence.EntityManager)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)2 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)2 ErrorMessage (fi.foyt.coops.extensions.websocket.ErrorMessage)2 HtmlMaterialRevision (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CloseReason (javax.websocket.CloseReason)2 CoOpsNotImplementedException (fi.foyt.coops.CoOpsNotImplementedException)1 PatchMessage (fi.foyt.coops.extensions.websocket.PatchMessage)1 Join (fi.foyt.coops.model.Join)1