Search in sources :

Example 1 with CoOpsDiffAlgorithm

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

the class HtmlMaterialController method getAlgorithmMap.

private Map<String, CoOpsDiffAlgorithm> getAlgorithmMap() {
    Map<String, CoOpsDiffAlgorithm> result = new HashMap<>();
    Iterator<CoOpsDiffAlgorithm> iterator = this.algorithms.iterator();
    while (iterator.hasNext()) {
        CoOpsDiffAlgorithm algorithm = iterator.next();
        result.put(algorithm.getName(), algorithm);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) CoOpsDiffAlgorithm(fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm)

Example 2 with CoOpsDiffAlgorithm

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

Example 3 with CoOpsDiffAlgorithm

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

CoOpsDiffAlgorithm (fi.otavanopisto.muikku.plugins.material.coops.CoOpsDiffAlgorithm)3 HtmlMaterialRevision (fi.otavanopisto.muikku.plugins.material.coops.model.HtmlMaterialRevision)2 CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)1 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)1 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1