Search in sources :

Example 11 with HtmlMaterialRevision

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

the class HtmlMaterialRevisionPropertyDAO method findByHtmlMaterialAndKeyRevisionLeAndMaxRevision.

public HtmlMaterialRevisionProperty findByHtmlMaterialAndKeyRevisionLeAndMaxRevision(HtmlMaterial htmlMaterial, String key, Long revisionLe) {
    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), criteriaBuilder.lessThanOrEqualTo(revisionJoin.get(HtmlMaterialRevision_.revision), revisionLe)));
    // 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 12 with HtmlMaterialRevision

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

the class HtmlMaterialCleaner method patch.

public void patch(HtmlMaterial material, String newHtml) throws CoOpsUsageException, CoOpsInternalErrorException, WorkspaceMaterialContainsAnswersExeption {
    CoOpsDiffAlgorithm algorithm = htmlMaterialController.findAlgorithm(COOPS_PATCH_ALGORITHM);
    if (algorithm == null) {
        throw new CoOpsUsageException("Algorithm is not supported by this server");
    }
    Long maxRevision = getMaterialRevision(material);
    boolean published = material.getRevisionNumber().equals(maxRevision);
    String oldHtml = htmlMaterialController.getRevisionHtml(material, maxRevision);
    if (oldHtml == null) {
        oldHtml = "";
    }
    String checksum = DigestUtils.md5Hex(newHtml);
    String patch = createPatch(oldHtml, newHtml);
    Long patchRevisionNumber = maxRevision + 1;
    HtmlMaterialRevision htmlMaterialRevision = htmlMaterialController.createRevision(material, "dnm-cleaner", patchRevisionNumber, new Date(), patch, checksum);
    if (published) {
        htmlMaterialController.updateHtmlMaterialToRevision(material, newHtml, htmlMaterialRevision.getRevision(), false, false);
    }
}
Also used : CoOpsUsageException(fi.foyt.coops.CoOpsUsageException) HtmlMaterialRevision(fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision) CoOpsDiffAlgorithm(fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm) Date(java.util.Date)

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