diff --git a/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java index b71919a1..ace94c35 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/listener/async/DocumentCreatedAsyncListener.java @@ -1,14 +1,13 @@ package com.sismics.docs.core.listener.async; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.common.eventbus.Subscribe; import com.sismics.docs.core.dao.jpa.ContributorDao; import com.sismics.docs.core.dao.lucene.LuceneDao; import com.sismics.docs.core.event.DocumentCreatedAsyncEvent; import com.sismics.docs.core.model.jpa.Contributor; import com.sismics.docs.core.util.TransactionUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Listener on document created. @@ -25,10 +24,9 @@ public class DocumentCreatedAsyncListener { * Document created. * * @param event Document created event - * @throws Exception */ @Subscribe - public void on(final DocumentCreatedAsyncEvent event) throws Exception { + public void on(final DocumentCreatedAsyncEvent event) { if (log.isInfoEnabled()) { log.info("Document created event: " + event.toString()); } diff --git a/docs-core/src/main/java/com/sismics/docs/core/service/InboxService.java b/docs-core/src/main/java/com/sismics/docs/core/service/InboxService.java index d568b690..d1622b95 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/service/InboxService.java +++ b/docs-core/src/main/java/com/sismics/docs/core/service/InboxService.java @@ -34,6 +34,13 @@ public class InboxService extends AbstractScheduledService { */ private static final Logger log = LoggerFactory.getLogger(InboxService.class); + /** + * Last synchronization data. + */ + private Date lastSyncDate; + private int lastSyncMessageCount = 0; + private String lastSyncError; + public InboxService() { } @@ -60,18 +67,23 @@ public class InboxService extends AbstractScheduledService { } Folder inbox = null; + lastSyncError = null; + lastSyncDate = new Date(); + lastSyncMessageCount = 0; try { inbox = openInbox(); - int count = inbox.getMessageCount(); Message[] messages = inbox.getMessages(1, count); + for (Message message : messages) { if (!message.getFlags().contains(Flags.Flag.SEEN)) { importMessage(message); + lastSyncMessageCount++; } } } catch (Exception e) { log.error("Error synching the inbox", e); + lastSyncError = e.getMessage(); } finally { try { if (inbox != null) { @@ -207,4 +219,16 @@ public class InboxService extends AbstractScheduledService { FileUtil.createFile(fileContent.getName(), fileContent.getFile(), fileContent.getSize(), "eng", "admin", document.getId()); } } + + public Date getLastSyncDate() { + return lastSyncDate; + } + + public int getLastSyncMessageCount() { + return lastSyncMessageCount; + } + + public String getLastSyncError() { + return lastSyncError; + } } diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java index f5001795..bc5c47c1 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java @@ -12,6 +12,7 @@ import com.sismics.docs.core.model.context.AppContext; import com.sismics.docs.core.model.jpa.Config; import com.sismics.docs.core.model.jpa.File; import com.sismics.docs.core.model.jpa.User; +import com.sismics.docs.core.service.InboxService; import com.sismics.docs.core.util.ConfigUtil; import com.sismics.docs.core.util.DirectoryUtil; import com.sismics.docs.core.util.jpa.PaginatedList; @@ -21,6 +22,7 @@ import com.sismics.rest.exception.ClientException; import com.sismics.rest.exception.ForbiddenClientException; import com.sismics.rest.exception.ServerException; import com.sismics.rest.util.ValidationUtil; +import com.sismics.util.JsonUtil; import com.sismics.util.context.ThreadLocalContext; import com.sismics.util.log4j.LogCriteria; import com.sismics.util.log4j.LogEntry; @@ -358,6 +360,18 @@ public class AppResource extends BaseResource { response.add("tag", tagConfig.getValue()); } + // Informations about the last synchronization + InboxService inboxService = AppContext.getInstance().getInboxService(); + JsonObjectBuilder lastSync = Json.createObjectBuilder(); + if (inboxService.getLastSyncDate() == null) { + lastSync.addNull("date"); + } else { + lastSync.add("date", inboxService.getLastSyncDate().getTime()); + } + lastSync.add("error", JsonUtil.nullable(inboxService.getLastSyncError())); + lastSync.add("count", inboxService.getLastSyncMessageCount()); + response.add("last_sync", lastSync); + return Response.ok().entity(response.build()).build(); } diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java index 2446ea02..8688aae8 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java @@ -249,6 +249,10 @@ public class TestAppResource extends BaseJerseyTest { Assert.assertTrue(json.isNull("username")); Assert.assertTrue(json.isNull("password")); Assert.assertTrue(json.isNull("tag")); + JsonObject lastSync = json.getJsonObject("last_sync"); + Assert.assertTrue(lastSync.isNull("date")); + Assert.assertTrue(lastSync.isNull("error")); + Assert.assertEquals(0, lastSync.getJsonNumber("count").intValue()); // Change inbox configuration target().path("/app/config_inbox").request() @@ -299,6 +303,15 @@ public class TestAppResource extends BaseJerseyTest { .get(JsonObject.class); Assert.assertEquals(1, json.getJsonArray("documents").size()); + // Get inbox configuration + json = target().path("/app/config_inbox").request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .get(JsonObject.class); + lastSync = json.getJsonObject("last_sync"); + Assert.assertFalse(lastSync.isNull("date")); + Assert.assertTrue(lastSync.isNull("error")); + Assert.assertEquals(1, lastSync.getJsonNumber("count").intValue()); + // Trigger an inbox sync AppContext.getInstance().getInboxService().syncInbox(); @@ -310,6 +323,15 @@ public class TestAppResource extends BaseJerseyTest { .get(JsonObject.class); Assert.assertEquals(1, json.getJsonArray("documents").size()); + // Get inbox configuration + json = target().path("/app/config_inbox").request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .get(JsonObject.class); + lastSync = json.getJsonObject("last_sync"); + Assert.assertFalse(lastSync.isNull("date")); + Assert.assertTrue(lastSync.isNull("error")); + Assert.assertEquals(0, lastSync.getJsonNumber("count").intValue()); + greenMail.stop(); } } \ No newline at end of file