Search in sources :

Example 1 with HtmlMaterialRevision

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

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

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

the class HtmlMaterialRevisionDAO method listByFileAndRevisionGreaterThanOrderedByRevision.

public List<HtmlMaterialRevision> listByFileAndRevisionGreaterThanOrderedByRevision(HtmlMaterial htmlMaterial, Long revision) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<HtmlMaterialRevision> criteria = criteriaBuilder.createQuery(HtmlMaterialRevision.class);
    Root<HtmlMaterialRevision> root = criteria.from(HtmlMaterialRevision.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(HtmlMaterialRevision_.htmlMaterial), htmlMaterial), criteriaBuilder.greaterThan(root.get(HtmlMaterialRevision_.revision), revision)));
    criteria.orderBy(criteriaBuilder.asc(root.get(HtmlMaterialRevision_.revision)));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision)

Example 4 with HtmlMaterialRevision

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

the class HtmlMaterialRevisionPropertyDAO method findByHtmlMaterialAndKeyMaxRevision.

public HtmlMaterialRevisionProperty findByHtmlMaterialAndKeyMaxRevision(HtmlMaterial htmlMaterial, String key) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<HtmlMaterialRevisionProperty> criteria = criteriaBuilder.createQuery(HtmlMaterialRevisionProperty.class);
    Root<HtmlMaterialRevisionProperty> root = criteria.from(HtmlMaterialRevisionProperty.class);
    Join<HtmlMaterialRevisionProperty, HtmlMaterialRevision> revisionJoin = root.join(HtmlMaterialRevisionProperty_.htmlMaterialRevision);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(HtmlMaterialRevisionProperty_.key), key), criteriaBuilder.equal(revisionJoin.get(HtmlMaterialRevision_.htmlMaterial), htmlMaterial)));
    // TODO: This could be optimized
    criteria.orderBy(criteriaBuilder.desc(revisionJoin.get(HtmlMaterialRevision_.revision)));
    TypedQuery<HtmlMaterialRevisionProperty> query = entityManager.createQuery(criteria);
    query.setMaxResults(1);
    return getSingleResult(query);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) HtmlMaterialRevisionProperty(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionProperty)

Example 5 with HtmlMaterialRevision

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

the class HtmlMaterialController method getRevisionHtml.

public String getRevisionHtml(HtmlMaterial htmlMaterial, long revision) throws CoOpsInternalErrorException {
    String result = htmlMaterial.getHtml();
    if (result == null) {
        result = "";
    }
    long baselineRevision = htmlMaterial.getRevisionNumber();
    CoOpsDiffAlgorithm algorithm = findAlgorithm("dmp");
    if (revision < baselineRevision) {
        List<HtmlMaterialRevision> revisions = htmlMaterialRevisionDAO.listByFileAndRevisionGeAndRevisonLtOrderedByRevision(htmlMaterial, revision, baselineRevision);
        for (int i = revisions.size() - 1; i >= 0; i--) {
            HtmlMaterialRevision patchingRevision = revisions.get(i);
            try {
                if (patchingRevision.getData() != null) {
                    result = algorithm.unpatch(result, patchingRevision.getData());
                }
            } catch (CoOpsConflictException e) {
                throw new CoOpsInternalErrorException("Patch failed when building material revision number " + revision);
            }
        }
    } else {
        List<HtmlMaterialRevision> revisions = htmlMaterialRevisionDAO.listByFileAndRevisionGtAndRevisonLeOrderedByRevision(htmlMaterial, baselineRevision, revision);
        for (HtmlMaterialRevision patchingRevision : revisions) {
            try {
                if (patchingRevision.getData() != null) {
                    result = algorithm.patch(result, patchingRevision.getData());
                }
            } catch (CoOpsConflictException e) {
                throw new CoOpsInternalErrorException("Patch failed when building material revision number " + revision);
            }
        }
    }
    return result;
}
Also used : HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsInternalErrorException(fi.foyt.coops.CoOpsInternalErrorException) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) CoOpsDiffAlgorithm(fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm)

Aggregations

HtmlMaterialRevision (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision)12 EntityManager (javax.persistence.EntityManager)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 HtmlMaterialRevisionProperty (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionProperty)4 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)3 CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)2 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)2 Patch (fi.foyt.coops.model.Patch)2 CoOpsDiffAlgorithm (fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm)2 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)2 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)2 Date (java.util.Date)2 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)1 CoOpsPatchEvent (fi.otavanopisto.muikku.plugins.material.coops.event.CoOpsPatchEvent)1 HtmlMaterialRevisionExtensionProperty (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevisionExtensionProperty)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1