From 508a1230e975e60f9f563aa67e64adba0e20f2ef Mon Sep 17 00:00:00 2001 From: jendib Date: Sun, 21 Feb 2016 14:21:20 +0100 Subject: [PATCH] Document updated event on file create/delete --- .../docs/rest/resource/DocumentResource.java | 2 +- .../docs/rest/resource/FileResource.java | 26 ++++++++++++++++--- .../docs/rest/resource/UserResource.java | 4 +-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java index 06ccd8ea..e7e73a46 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java @@ -649,7 +649,7 @@ public class DocumentResource extends BaseResource { // Delete the document documentDao.delete(document.getId(), principal.getId()); - // Raise file deleted events + // Raise file deleted events (don't bother sending document updated event) for (File file : fileList) { FileDeletedAsyncEvent fileDeletedAsyncEvent = new FileDeletedAsyncEvent(); fileDeletedAsyncEvent.setUserId(principal.getId()); diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java index 1f01564b..4c97756c 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java @@ -44,6 +44,7 @@ import com.sismics.docs.core.dao.jpa.DocumentDao; import com.sismics.docs.core.dao.jpa.FileDao; import com.sismics.docs.core.dao.jpa.UserDao; import com.sismics.docs.core.dao.jpa.dto.DocumentDto; +import com.sismics.docs.core.event.DocumentUpdatedAsyncEvent; import com.sismics.docs.core.event.FileCreatedAsyncEvent; import com.sismics.docs.core.event.FileDeletedAsyncEvent; import com.sismics.docs.core.model.context.AppContext; @@ -160,7 +161,7 @@ public class FileResource extends BaseResource { user.setStorageCurrent(user.getStorageCurrent() + fileData.length); userDao.updateQuota(user); - // Raise a new file created event if we have a document + // Raise a new file created event and document updated event if we have a document if (documentId != null) { FileCreatedAsyncEvent fileCreatedAsyncEvent = new FileCreatedAsyncEvent(); fileCreatedAsyncEvent.setUserId(principal.getId()); @@ -169,6 +170,11 @@ public class FileResource extends BaseResource { fileCreatedAsyncEvent.setInputStream(fileInputStream); fileCreatedAsyncEvent.setPdfInputStream(pdfIntputStream); AppContext.getInstance().getAsyncEventBus().post(fileCreatedAsyncEvent); + + DocumentUpdatedAsyncEvent documentUpdatedAsyncEvent = new DocumentUpdatedAsyncEvent(); + documentUpdatedAsyncEvent.setUserId(principal.getId()); + documentUpdatedAsyncEvent.setDocument(document); + AppContext.getInstance().getAsyncEventBus().post(documentUpdatedAsyncEvent); } // Always return OK @@ -223,7 +229,7 @@ public class FileResource extends BaseResource { file.setOrder(fileDao.getByDocumentId(principal.getId(), documentId).size()); fileDao.update(file); - // Raise a new file created event (it wasn't sent during file creation) + // Raise a new file created event and document updated event (it wasn't sent during file creation) try { java.nio.file.Path storedFile = DirectoryUtil.getStorageDirectory().resolve(id); InputStream fileInputStream = Files.newInputStream(storedFile); @@ -234,6 +240,11 @@ public class FileResource extends BaseResource { fileCreatedAsyncEvent.setFile(file); fileCreatedAsyncEvent.setInputStream(responseInputStream); AppContext.getInstance().getAsyncEventBus().post(fileCreatedAsyncEvent); + + DocumentUpdatedAsyncEvent documentUpdatedAsyncEvent = new DocumentUpdatedAsyncEvent(); + documentUpdatedAsyncEvent.setUserId(principal.getId()); + documentUpdatedAsyncEvent.setDocument(document); + AppContext.getInstance().getAsyncEventBus().post(documentUpdatedAsyncEvent); } catch (Exception e) { throw new ClientException("AttachError", "Error attaching file to document", e); } @@ -353,13 +364,14 @@ public class FileResource extends BaseResource { return Response.status(Status.NOT_FOUND).build(); } + Document document = null; if (file.getDocumentId() == null) { // It's an orphan file if (!file.getUserId().equals(principal.getId())) { // But not ours throw new ForbiddenClientException(); } - } else if (documentDao.getDocument(file.getDocumentId(), PermType.WRITE, principal.getId()) == null) { + } else if ((document = documentDao.getDocument(file.getDocumentId(), PermType.WRITE, principal.getId())) == null) { return Response.status(Status.NOT_FOUND).build(); } @@ -383,6 +395,14 @@ public class FileResource extends BaseResource { fileDeletedAsyncEvent.setFile(file); AppContext.getInstance().getAsyncEventBus().post(fileDeletedAsyncEvent); + if (document != null) { + // Raise a new document updated + DocumentUpdatedAsyncEvent documentUpdatedAsyncEvent = new DocumentUpdatedAsyncEvent(); + documentUpdatedAsyncEvent.setUserId(principal.getId()); + documentUpdatedAsyncEvent.setDocument(document); + AppContext.getInstance().getAsyncEventBus().post(documentUpdatedAsyncEvent); + } + // Always return OK JsonObjectBuilder response = Json.createObjectBuilder() .add("status", "ok"); diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java index 28af8b4e..dbc35005 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/UserResource.java @@ -366,7 +366,7 @@ public class UserResource extends BaseResource { AppContext.getInstance().getAsyncEventBus().post(documentDeletedAsyncEvent); } - // Raise deleted events for files + // Raise deleted events for files (don't bother sending document updated event) for (File file : fileList) { FileDeletedAsyncEvent fileDeletedAsyncEvent = new FileDeletedAsyncEvent(); fileDeletedAsyncEvent.setUserId(principal.getId()); @@ -425,7 +425,7 @@ public class UserResource extends BaseResource { AppContext.getInstance().getAsyncEventBus().post(documentDeletedAsyncEvent); } - // Raise deleted events for files + // Raise deleted events for files (don't bother sending document updated event) for (File file : fileList) { FileDeletedAsyncEvent fileDeletedAsyncEvent = new FileDeletedAsyncEvent(); fileDeletedAsyncEvent.setUserId(principal.getId());