use of fi.otavanopisto.muikku.plugins.material.coops.event.CoOpsPatchEvent 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)));
}
Aggregations