From b3a1e1635e70de3e845565d4852091b0c417ac35 Mon Sep 17 00:00:00 2001 From: mozzy11 Date: Mon, 12 Aug 2019 09:53:47 +0300 Subject: [PATCH] SYNCT-336 : Sync2 should dispaly a right message on failure or success --- .../sync2/api/service/SyncAuditService.java | 3 ++ .../service/impl/SyncAuditServiceImpl.java | 5 +++ .../ManualSyncPullPageController.java | 30 +++++++------ .../ManualSyncPushPageController.java | 27 +++++------ .../web/controller/Sync2ModuleController.java | 45 ++++++++++--------- 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/sync2/api/service/SyncAuditService.java b/api/src/main/java/org/openmrs/module/sync2/api/service/SyncAuditService.java index 628930ef..140301cb 100644 --- a/api/src/main/java/org/openmrs/module/sync2/api/service/SyncAuditService.java +++ b/api/src/main/java/org/openmrs/module/sync2/api/service/SyncAuditService.java @@ -57,4 +57,7 @@ String getPaginatedMessages(Integer page, Integer pageSize, Boolean success, Str @Authorized(SyncModuleConfig.SYNC_AUDIT_PRIVILEGE) String getJsonMessage(AuditMessage message) throws APIException, JsonParseException; + + @Authorized(SyncModuleConfig.SYNC_AUDIT_PRIVILEGE) + Long getCountOfMessages(); } diff --git a/api/src/main/java/org/openmrs/module/sync2/api/service/impl/SyncAuditServiceImpl.java b/api/src/main/java/org/openmrs/module/sync2/api/service/impl/SyncAuditServiceImpl.java index a78d7a33..9ed97dc4 100644 --- a/api/src/main/java/org/openmrs/module/sync2/api/service/impl/SyncAuditServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/sync2/api/service/impl/SyncAuditServiceImpl.java @@ -117,4 +117,9 @@ private String serializeResultsWithAuditMessage(T results) { return gson.toJson(results); } + + @Override + public Long getCountOfMessages() { + return dao.getCountOfMessages(); + } } diff --git a/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPullPageController.java b/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPullPageController.java index 58349a8b..c352c6c1 100644 --- a/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPullPageController.java +++ b/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPullPageController.java @@ -3,9 +3,11 @@ import org.openmrs.annotation.OpenmrsProfile; import org.openmrs.module.sync2.SyncConstants; import org.openmrs.module.sync2.api.exceptions.SyncValidationException; +import org.openmrs.module.sync2.api.service.SyncAuditService; import org.openmrs.module.sync2.api.service.SyncConfigurationService; import org.openmrs.module.sync2.api.utils.ContextUtils; import org.openmrs.module.sync2.api.validator.Errors; +import org.openmrs.module.sync2.client.reader.LocalFeedReader; import org.openmrs.module.sync2.client.reader.ParentFeedReader; import org.openmrs.module.uicommons.util.InfoErrorMessageUtil; import org.openmrs.ui.framework.UiUtils; @@ -29,24 +31,24 @@ public class ManualSyncPullPageController { public String controller(PageModel model, @SpringBean("sync2.syncConfigurationService") SyncConfigurationService syncConfigurationService, + @SpringBean("syncAuditService") SyncAuditService syncAuditService, HttpSession session, UiUtils ui) { - try { - LOGGER.info("Start Parent Feed Reader..."); - ParentFeedReader parentFeedReader = ContextUtils.getParentFeedReader(); - parentFeedReader.pullAndProcessAllFeeds(); - InfoErrorMessageUtil.flashInfoMessage(session, ui.message(SYNC_SUCCESS)); - } catch (SyncValidationException e) { - LOGGER.error("Error during reading feeds: ", e); - InfoErrorMessageUtil.flashErrorMessage(session, ui.message(getFirstErrorCode(e.getErrors()))); - } catch (Exception e) { - LOGGER.error("Error during reading feeds: ", e); - InfoErrorMessageUtil.flashErrorMessage(session, ui.message(SYNC_FAILURE)); - } + + LOGGER.info("Start Parent Feed Reader..."); + ParentFeedReader parentFeedReader = ContextUtils.getParentFeedReader(); + parentFeedReader.pullAndProcessAllFeeds(); + Integer lastAuditMsgId = syncAuditService.getCountOfMessages().intValue(); + Boolean succes = syncAuditService.getMessageById(lastAuditMsgId).getSuccess(); + if (succes) { + InfoErrorMessageUtil.flashInfoMessage(session, ui.message(SYNC_SUCCESS)); + } else { + LOGGER.error("Error during reading feeds.."); + InfoErrorMessageUtil.flashErrorMessage(session, ui.message(SYNC_FAILURE)); + } return "redirect:/sync2/sync2.page"; } private String getFirstErrorCode(Errors errors) { return errors.getErrorsCodes().get(SyncConstants.ZERO); } -} - +} \ No newline at end of file diff --git a/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPushPageController.java b/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPushPageController.java index 281b2d11..f2c5cd04 100644 --- a/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPushPageController.java +++ b/omod/src/main/java/org/openmrs/module/sync2/page/controller/ManualSyncPushPageController.java @@ -3,6 +3,7 @@ import org.openmrs.annotation.OpenmrsProfile; import org.openmrs.module.sync2.SyncConstants; import org.openmrs.module.sync2.api.exceptions.SyncValidationException; +import org.openmrs.module.sync2.api.service.SyncAuditService; import org.openmrs.module.sync2.api.service.SyncConfigurationService; import org.openmrs.module.sync2.api.utils.ContextUtils; import org.openmrs.module.sync2.api.validator.Errors; @@ -29,19 +30,20 @@ public class ManualSyncPushPageController { public String controller(PageModel model, @SpringBean("sync2.syncConfigurationService") SyncConfigurationService syncConfigurationService, + @SpringBean("syncAuditService") SyncAuditService syncAuditService, HttpSession session, UiUtils ui) { - try { - LOGGER.info("Start Local Feed Reader..."); - LocalFeedReader localFeedReader = ContextUtils.getLocalFeedReader(); - localFeedReader.readAndPushAllFeeds(); - InfoErrorMessageUtil.flashInfoMessage(session, ui.message(SYNC_SUCCESS)); - } catch (SyncValidationException e) { - LOGGER.error("Error during pushing objects: ", e); - InfoErrorMessageUtil.flashErrorMessage(session, ui.message(getFirstErrorCode(e.getErrors()))); - } catch (Exception e) { - LOGGER.error("Error during pushing objects: ", e); - InfoErrorMessageUtil.flashErrorMessage(session, ui.message(SYNC_FAILURE)); - } + + LOGGER.info("Start Local Feed Reader..."); + LocalFeedReader localFeedReader = ContextUtils.getLocalFeedReader(); + localFeedReader.readAndPushAllFeeds(); + Integer lastAuditMsgId = syncAuditService.getCountOfMessages().intValue(); + Boolean succes = syncAuditService.getMessageById(lastAuditMsgId).getSuccess(); + if (succes) { + InfoErrorMessageUtil.flashInfoMessage(session, ui.message(SYNC_SUCCESS)); + } else { + LOGGER.error("Error during pushing objects.."); + InfoErrorMessageUtil.flashErrorMessage(session, ui.message(SYNC_FAILURE)); + } return "redirect:/sync2/sync2.page"; } @@ -49,4 +51,3 @@ private String getFirstErrorCode(Errors errors) { return errors.getErrorsCodes().get(SyncConstants.ZERO); } } - diff --git a/omod/src/main/java/org/openmrs/module/sync2/web/controller/Sync2ModuleController.java b/omod/src/main/java/org/openmrs/module/sync2/web/controller/Sync2ModuleController.java index 05941c1a..20a1d1c1 100644 --- a/omod/src/main/java/org/openmrs/module/sync2/web/controller/Sync2ModuleController.java +++ b/omod/src/main/java/org/openmrs/module/sync2/web/controller/Sync2ModuleController.java @@ -12,13 +12,16 @@ import org.openmrs.module.sync2.SyncConstants; import org.openmrs.module.sync2.SyncMessageUtils; import org.openmrs.module.sync2.api.exceptions.SyncValidationException; +import org.openmrs.module.sync2.api.service.SyncAuditService; import org.openmrs.module.sync2.api.utils.ContextUtils; import org.openmrs.module.sync2.api.utils.SyncUtils; import org.openmrs.module.sync2.api.validator.Errors; import org.openmrs.module.sync2.client.reader.LocalFeedReader; import org.openmrs.module.sync2.client.reader.ParentFeedReader; +import org.openmrs.module.uicommons.util.InfoErrorMessageUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @@ -40,6 +43,9 @@ public class Sync2ModuleController { private static final String PUSH_FAILURE_MESSAGE = "sync2.sync.push.failure"; private static final String PULL_SUCCESS_MESSAGE = "sync2.sync.pull.success"; private static final String PULL_FAILURE_MESSAGE = "sync2.sync.pull.failure"; + + @Autowired + private SyncAuditService syncAuditService; /** * Sets the UI model attributes used to check if the parent instance URI is valid etc. @@ -71,18 +77,18 @@ public void manage(ModelMap model, */ @RequestMapping(value = "/manualPush") public String manualPush(ModelMap model) { - try { - LOGGER.info("Start Local Feed Reader..."); - LocalFeedReader localFeedReader = ContextUtils.getLocalFeedReader(); - localFeedReader.readAndPushAllFeeds(); + + LOGGER.info("Start Local Feed Reader..."); + LocalFeedReader localFeedReader = ContextUtils.getLocalFeedReader(); + localFeedReader.readAndPushAllFeeds(); + Integer lastAuditMsgId = syncAuditService.getCountOfMessages().intValue(); + Boolean succes = syncAuditService.getMessageById(lastAuditMsgId).getSuccess(); + if (succes) { SyncMessageUtils.successMessage(model, PUSH_SUCCESS_MESSAGE); - } catch (SyncValidationException e) { - LOGGER.error("Error during reading feeds: ", e); - SyncMessageUtils.errorMessage(model, getFirstErrorCode(e.getErrors())); - } catch (Exception e) { - LOGGER.error("Error during pushing objects: ", e); + } else { + LOGGER.error("Error during pushing objects.."); SyncMessageUtils.errorMessage(model, PUSH_FAILURE_MESSAGE); - } + } return "redirect:/module/sync2/sync2.form"; } @@ -94,17 +100,16 @@ public String manualPush(ModelMap model) { */ @RequestMapping(value = "/manualPull") public String manualPull(ModelMap model) { - try { - LOGGER.info("Start Parent Feed Reader..."); - ParentFeedReader parentFeedReader = ContextUtils.getParentFeedReader(); - parentFeedReader.pullAndProcessAllFeeds(); + + LOGGER.info("Start Parent Feed Reader..."); + ParentFeedReader parentFeedReader = ContextUtils.getParentFeedReader(); + parentFeedReader.pullAndProcessAllFeeds(); + Integer lastAuditMsgId = syncAuditService.getCountOfMessages().intValue(); + Boolean succes = syncAuditService.getMessageById(lastAuditMsgId).getSuccess(); + if (succes) { SyncMessageUtils.successMessage(model, PULL_SUCCESS_MESSAGE); - } catch (SyncValidationException e) { - LOGGER.error("Error during reading feeds: ", e); - SyncMessageUtils.errorMessage(model, getFirstErrorCode(e.getErrors())); - - } catch (Exception e) { - LOGGER.error("Error during reading feeds: ", e); + } else { + LOGGER.error("Error during reading feeds"); SyncMessageUtils.errorMessage(model, PULL_FAILURE_MESSAGE); } return "redirect:/module/sync2/sync2.form";