Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
3311: Refactored visits/encounter listeners into advice classes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
druchniewicz authored Jun 3, 2024
1 parent 4cec7e0 commit 7c8b942
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 1,051 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
* <p>
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/

package org.openmrs.module.cflcore.advice;

import org.openmrs.Encounter;
import org.openmrs.Visit;
import org.openmrs.api.context.Context;
import org.openmrs.module.cflcore.api.service.ConfigService;
import org.openmrs.module.cflcore.api.service.VaccinationService;
import org.openmrs.module.cflcore.api.util.VisitUtil;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
import java.util.Date;

public class UpdateEncounterAdvice implements AfterReturningAdvice {

private static final String SAVE_ENCOUNTER_METHOD_NAME = "saveEncounter";

@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) {
if (SAVE_ENCOUNTER_METHOD_NAME.equals(method.getName())) {
Encounter encounter = (Encounter) objects[0];
updateEncounterVisit(encounter.getVisit());
createFutureVisits(encounter);
}
}

private void updateEncounterVisit(Visit encounterVisit) {
if (encounterVisit != null) {
encounterVisit.setDateChanged(new Date());
encounterVisit.setChangedBy(Context.getAuthenticatedUser());
Context.getVisitService().saveVisit(encounterVisit);
}
}

private void createFutureVisits(Encounter newEncounter) {
if (isVaccinationEncounter(newEncounter) && newEncounter.getVisit() != null) {
final Visit updatedVisit = newEncounter.getVisit();
final String visitStatus = VisitUtil.getVisitStatus(updatedVisit);

if (visitStatus.equals(VisitUtil.getOccurredVisitStatus())) {
Context.getService(VaccinationService.class)
.createFutureVisits(updatedVisit, newEncounter.getEncounterDatetime());
}
}
}

private boolean isVaccinationEncounter(Encounter newEncounter) {
return getConfigService()
.getVaccinationEncounterTypeUUIDs()
.contains(newEncounter.getEncounterType().getUuid());
}

private ConfigService getConfigService() {
return Context.getService(ConfigService.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,47 @@
* graphic logo is a trademark of OpenMRS Inc.
*/

package org.openmrs.module.cflcore.api.event.listener.subscribable;
package org.openmrs.module.cflcore.advice;

import org.openmrs.Visit;
import org.openmrs.api.context.Context;
import org.openmrs.event.Event;
import org.openmrs.module.cflcore.CFLConstants;
import org.openmrs.module.cflcore.api.service.ConfigService;
import org.openmrs.module.cflcore.api.service.VaccinationService;
import org.openmrs.module.cflcore.api.util.LockByKeyUtil;
import org.openmrs.module.cflcore.api.util.VisitUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.Message;
import java.util.Collections;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
* The UpdatingVisitListener class.
*
* <p>This listener is responsible for scheduling following visits according to the vaccination
* program defined in Global Property {@link CFLConstants#VACCINATION_PROGRAM_KEY}.
*
* <p>This listener must be enabled via Global Parameter: {@link
* CFLConstants#VACCINATION_LISTENER_KEY}.
*
* <p>The following visits are scheduled based on the Visits start date.
*
* <p>The listener observes the update of an Visit event and runs it's logic only when:
*
* <ul>
* <li>the Vaccination information is enabled ({@link
* CFLConstants#VACCINATION_INFORMATION_ENABLED_KEY} is true)
* <li>the Visit has occurred status
* <li>the Visit is the last dosage visit scheduled for its patient
* </ul>
*
* @see VaccinationEncounterListener
*/
public class UpdatingVisitListener extends VisitActionListener {
public class UpdateVisitAdvice implements AfterReturningAdvice {

private static final Logger LOGGER = LoggerFactory.getLogger(UpdateVisitAdvice.class);

private static final Logger LOGGER = LoggerFactory.getLogger(UpdatingVisitListener.class);
private static final LockByKeyUtil LOCK_BY_KEY = new LockByKeyUtil();

private static final List<String> SAVE_UPDATE_VISIT_METHOD_NAMES =
Arrays.asList("saveVisit", "updateVisit");

@Override
public List<String> subscribeToActions() {
return Collections.singletonList(Event.Action.UPDATED.name());
public void afterReturning(Object o, Method method, Object[] objects, Object o1) {
if (SAVE_UPDATE_VISIT_METHOD_NAMES.contains(method.getName())) {
Object visitObject = objects[0];
String visitUuid = null;
if (visitObject instanceof Visit) {
visitUuid = ((Visit) objects[0]).getUuid();
} else if (visitObject instanceof String) {
visitUuid = (String) visitObject;
}

createFutureVisits(visitUuid);
}
}

@Override
public void performAction(Message message) {
private void createFutureVisits(String visitUuid) {
// is listener not enabled or is vaccination info not enabled
if (!getConfigService()
.isVaccinationListenerEnabled(CFLConstants.VACCINATION_VISIT_LISTENER_NAME)
Expand All @@ -69,12 +59,10 @@ public void performAction(Message message) {
return;
}

final String visitUuid = getVisitUuid(message);

LOCK_BY_KEY.lock(visitUuid);

try {
final Visit updatedVisit = getVisit(visitUuid);
final Visit updatedVisit = Context.getVisitService().getVisitByUuid(visitUuid);
final String visitStatus = VisitUtil.getVisitStatus(updatedVisit);

if (visitStatus.equals(VisitUtil.getOccurredVisitStatus())) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7c8b942

Please sign in to comment.