diff --git a/openmrs-atomfeed-api/pom.xml b/openmrs-atomfeed-api/pom.xml index e5a8fe5..5357d50 100644 --- a/openmrs-atomfeed-api/pom.xml +++ b/openmrs-atomfeed-api/pom.xml @@ -4,7 +4,7 @@ org.ict4h.openmrs openmrs-atomfeed - 2.5.1-SNAPSHOT + 2.6.3 openmrs-atomfeed-api @@ -17,6 +17,7 @@ org.ict4h atomfeed-server + ${atomfeed.version} @@ -24,7 +25,17 @@ openmrs-api jar - + + + org.apache.logging.log4j + log4j-api + + + + org.apache.logging.log4j + log4j-core + + org.openmrs.web openmrs-web diff --git a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/EventPublishFilterHook.java b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/EventPublishFilterHook.java index 63c8c45..2b53a04 100644 --- a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/EventPublishFilterHook.java +++ b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/EventPublishFilterHook.java @@ -1,7 +1,8 @@ package org.openmrs.module.atomfeed; import groovy.lang.GroovyClassLoader; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.openmrs.module.atomfeed.advice.EncounterSaveAdvice; import org.openmrs.module.atomfeed.filter.DefaultEventPublishFilter; import org.openmrs.module.atomfeed.filter.EventPublishFilter; @@ -14,7 +15,7 @@ public class EventPublishFilterHook { - private static final Logger log = Logger.getLogger(EncounterSaveAdvice.class); + private static final Logger log = LogManager.getLogger(EncounterSaveAdvice.class); private static GroovyClassLoader groovyClassLoader = new GroovyClassLoader(); private static Map eventPublishFilterMap = new HashMap(); diff --git a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterSaveAdvice.java b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterSaveAdvice.java index fcb26ec..8a124f8 100644 --- a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterSaveAdvice.java +++ b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterSaveAdvice.java @@ -7,7 +7,6 @@ import org.ict4h.atomfeed.server.service.EventService; import org.ict4h.atomfeed.server.service.EventServiceImpl; import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult; -import org.joda.time.DateTime; import org.openmrs.api.context.Context; import org.openmrs.module.atomfeed.EventPublishFilterHook; import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager; @@ -17,6 +16,7 @@ import java.lang.reflect.Method; import java.net.URI; import java.sql.SQLException; +import java.time.LocalDateTime; import java.util.List; import java.util.UUID; @@ -46,7 +46,7 @@ public void afterReturning(Object returnValue, Method method, Object[] args, Obj if (method.getName().equals(SAVE_METHOD)) { Object encounterUuid = PropertyUtils.getProperty(returnValue, "encounterUuid"); String url = String.format(ENCOUNTER_REST_URL, encounterUuid); - final Event event = new Event(UUID.randomUUID().toString(), TITLE, DateTime.now(), (URI) null, url, CATEGORY); + final Event event = new Event(UUID.randomUUID().toString(), TITLE, LocalDateTime.now(), (URI) null, url, CATEGORY); if (EventPublishFilterHook.shouldPublish(returnValue, args, "EncounterPublishCondition.groovy")) { atomFeedSpringTransactionManager.executeWithTransaction( new AFTransactionWorkWithoutResult() { diff --git a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterServiceSaveAdvice.java b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterServiceSaveAdvice.java new file mode 100644 index 0000000..edf530a --- /dev/null +++ b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/EncounterServiceSaveAdvice.java @@ -0,0 +1,81 @@ +package org.openmrs.module.atomfeed.advice; + + +import org.ict4h.atomfeed.server.repository.AllEventRecordsQueue; +import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsQueueJdbcImpl; +import org.ict4h.atomfeed.server.service.Event; +import org.ict4h.atomfeed.server.service.EventService; +import org.ict4h.atomfeed.server.service.EventServiceImpl; +import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult; +import org.openmrs.Encounter; +import org.openmrs.api.context.Context; +import org.openmrs.module.atomfeed.EventPublishFilterHook; +import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager; +import org.springframework.aop.AfterReturningAdvice; +import org.springframework.transaction.PlatformTransactionManager; + +import java.lang.reflect.Method; +import java.net.URI; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +public class EncounterServiceSaveAdvice implements AfterReturningAdvice { + + public static final String ENCOUNTER_REST_URL = getEncounterFeedUrl(); + public static final String TITLE = "Encounter"; + public static final String CATEGORY = "Encounter"; + private final AtomFeedSpringTransactionManager atomFeedSpringTransactionManager; + + private static final String SAVE_METHOD = "saveEncounter"; + private static final String ENCOUNTER_TYPE_INVESTIGATION = "INVESTIGATION"; + + private EventService eventService; + + public EncounterServiceSaveAdvice() throws SQLException { + PlatformTransactionManager platformTransactionManager = getSpringPlatformTransactionManager(); + atomFeedSpringTransactionManager = new AtomFeedSpringTransactionManager(platformTransactionManager); + AllEventRecordsQueue allEventRecordsQueue = new AllEventRecordsQueueJdbcImpl(atomFeedSpringTransactionManager); + this.eventService = new EventServiceImpl(allEventRecordsQueue); + } + + + @Override + public void afterReturning(Object returnValue, Method method, Object[] args, Object encounterService) throws Throwable { + if (method.getName().equals(SAVE_METHOD)) { + Encounter encounter = (Encounter) args[0]; // Assuming encounter is the first argument + String encounterType = encounter.getEncounterType().getName(); // Assuming encounterType is a string + if (ENCOUNTER_TYPE_INVESTIGATION.equals(encounterType)) { + + String url = String.format(ENCOUNTER_REST_URL, encounter.getUuid()); + final Event event = new Event(UUID.randomUUID().toString(), TITLE, LocalDateTime.now(), (URI) null, url, CATEGORY); + if (EventPublishFilterHook.shouldPublish(returnValue, args, "EncounterPublishCondition.groovy")) { + atomFeedSpringTransactionManager.executeWithTransaction( + new AFTransactionWorkWithoutResult() { + @Override + protected void doInTransaction() { + eventService.notify(event); + } + + @Override + public PropagationDefinition getTxPropagationDefinition() { + return PropagationDefinition.PROPAGATION_REQUIRED; + } + } + ); + } + } + } + } + + private static String getEncounterFeedUrl() { + return Context.getAdministrationService().getGlobalProperty("encounter.feed.publish.url"); + } + + private PlatformTransactionManager getSpringPlatformTransactionManager() { + List platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class); + return platformTransactionManagers.get(0); + } +} + diff --git a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientAdvice.java b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientAdvice.java index 64c1ee2..6facfa0 100644 --- a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientAdvice.java +++ b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientAdvice.java @@ -6,7 +6,6 @@ import org.ict4h.atomfeed.server.service.EventService; import org.ict4h.atomfeed.server.service.EventServiceImpl; import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult; -import org.joda.time.DateTime; import org.openmrs.Patient; import org.openmrs.api.context.Context; import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager; @@ -16,6 +15,7 @@ import java.lang.reflect.Method; import java.net.URI; import java.sql.SQLException; +import java.time.LocalDateTime; import java.util.List; import java.util.UUID; @@ -38,7 +38,7 @@ public PatientAdvice() throws SQLException { public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) throws Throwable { if (method.getName().equals(SAVE_PATIENT_METHOD)) { String contents = String.format(TEMPLATE, ((Patient) returnValue).getUuid()); - final Event event = new Event(UUID.randomUUID().toString(), TITLE, DateTime.now(), (URI) null, contents, CATEGORY); + final Event event = new Event(UUID.randomUUID().toString(), TITLE, LocalDateTime.now(), (URI) null, contents, CATEGORY); atomFeedSpringTransactionManager.executeWithTransaction( new AFTransactionWorkWithoutResult() { diff --git a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientProgramAdvice.java b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientProgramAdvice.java new file mode 100644 index 0000000..f792aa8 --- /dev/null +++ b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientProgramAdvice.java @@ -0,0 +1,98 @@ +package org.openmrs.module.atomfeed.advice; + +import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsQueueJdbcImpl; +import org.ict4h.atomfeed.server.service.Event; +import org.ict4h.atomfeed.server.service.EventService; +import org.ict4h.atomfeed.server.service.EventServiceImpl; +import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult; +import org.openmrs.PatientProgram; +import org.openmrs.api.context.Context; +import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager; +import org.springframework.aop.AfterReturningAdvice; +import org.springframework.transaction.PlatformTransactionManager; + +import java.lang.reflect.Method; +import java.net.URI; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +public class PatientProgramAdvice implements AfterReturningAdvice { + private static final String CATEGORY = "programenrollment"; + private static final String TITLE = "Progam Enrollment"; + private static final String SAVE_PATIENT_PROGRAM_METHOD = "savePatientProgram"; + private static final String RAISE_PATIENT_PROGRAM_EVENT_GLOBAL_PROPERTY = "atomfeed.publish.eventsForPatientProgramStateChange"; + private static final String PATIENT_PROGRAM_EVENT_URL_PATTERN_GLOBAL_PROPERTY = "atomfeed.event.urlPatternForProgramStateChange"; + private static final String DEFAULT_PATIENT_PROGRAM_URL_PATTERN = "/openmrs/ws/rest/v1/programenrollment/{uuid}?v=full"; + private AtomFeedSpringTransactionManager atomFeedSpringTransactionManager; + private EventService eventService; + private final Object eventServiceMonitor = new Object(); + private final Object txManagerMonitor = new Object(); + + public PatientProgramAdvice() throws SQLException { + + } + + @Override + public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) throws Throwable { + if (method.getName().equals(SAVE_PATIENT_PROGRAM_METHOD) && shouldRaiseRelationshipEvent()) { + String contents = getUrlPattern().replace("{uuid}",((PatientProgram) returnValue).getUuid()); + final Event event = new Event(UUID.randomUUID().toString(), TITLE, LocalDateTime.now(), (URI) null, contents, CATEGORY); + + getAFTxManager().executeWithTransaction( + new AFTransactionWorkWithoutResult() { + @Override + protected void doInTransaction() { + getEventService().notify(event); + } + + @Override + public PropagationDefinition getTxPropagationDefinition() { + return PropagationDefinition.PROPAGATION_REQUIRED; + } + } + ); + } + } + + private EventService getEventService() { + if (eventService == null) { // Single Checked + synchronized (eventServiceMonitor) { + if (eventService == null) { // Double checked + this.eventService = new EventServiceImpl(new AllEventRecordsQueueJdbcImpl(getAFTxManager())); + } + } + } + return this.eventService; + } + + private AtomFeedSpringTransactionManager getAFTxManager() { + if (this.atomFeedSpringTransactionManager == null) { + synchronized (txManagerMonitor) { + if(this.atomFeedSpringTransactionManager == null) { + this.atomFeedSpringTransactionManager = new AtomFeedSpringTransactionManager(getSpringPlatformTransactionManager()); + } + } + } + return this.atomFeedSpringTransactionManager; + } + + private boolean shouldRaiseRelationshipEvent() { + String raiseEvent = Context.getAdministrationService().getGlobalProperty(RAISE_PATIENT_PROGRAM_EVENT_GLOBAL_PROPERTY); + return Boolean.valueOf(raiseEvent); + } + + private String getUrlPattern() { + String urlPattern = Context.getAdministrationService().getGlobalProperty(PATIENT_PROGRAM_EVENT_URL_PATTERN_GLOBAL_PROPERTY); + if (urlPattern == null || urlPattern.equals("")) { + return DEFAULT_PATIENT_PROGRAM_URL_PATTERN; + } + return urlPattern; + } + + private PlatformTransactionManager getSpringPlatformTransactionManager() { + List platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class); + return platformTransactionManagers.get(0); + } +} diff --git a/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdvice.java b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdvice.java new file mode 100644 index 0000000..b5e970c --- /dev/null +++ b/openmrs-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdvice.java @@ -0,0 +1,77 @@ +package org.openmrs.module.atomfeed.advice; + +import org.ict4h.atomfeed.server.repository.AllEventRecordsQueue; +import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsQueueJdbcImpl; +import org.ict4h.atomfeed.server.service.Event; +import org.ict4h.atomfeed.server.service.EventService; +import org.ict4h.atomfeed.server.service.EventServiceImpl; +import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult; +import org.openmrs.Relationship; +import org.openmrs.api.context.Context; +import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager; +import org.springframework.aop.AfterReturningAdvice; +import org.springframework.transaction.PlatformTransactionManager; + +import java.lang.reflect.Method; +import java.net.URI; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +public class PersonRelationshipAdvice implements AfterReturningAdvice { + private static final String CATEGORY = "relationship"; + private static final String TITLE = "Relationship"; + private static final String SAVE_RELATIONSHIP_METHOD = "saveRelationship"; + private static final String RAISE_RELATIONSHIP_EVENT_GLOBAL_PROPERTY = "atomfeed.publish.eventsForPatientRelationshipChange"; + private static final String RELATIONSHIP_EVENT_URL_PATTERN_GLOBAL_PROPERTY = "atomfeed.event.urlPatternForPatientRelationshipChange"; + private static final String DEFAULT_RELATIONSHIP_URL_PATTERN = "/openmrs/ws/rest/v1/relationship/%s"; + private final AtomFeedSpringTransactionManager atomFeedSpringTransactionManager; + private final EventService eventService; + + public PersonRelationshipAdvice() throws SQLException { + atomFeedSpringTransactionManager = new AtomFeedSpringTransactionManager(getSpringPlatformTransactionManager()); + AllEventRecordsQueue allEventRecordsQueue = new AllEventRecordsQueueJdbcImpl(atomFeedSpringTransactionManager); + this.eventService = new EventServiceImpl(allEventRecordsQueue); + } + + @Override + public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) throws Throwable { + if (method.getName().equals(SAVE_RELATIONSHIP_METHOD) && shouldRaiseRelationshipEvent()) { + String contents = String.format(getUrlPattern(), ((Relationship) returnValue).getUuid()); + final Event event = new Event(UUID.randomUUID().toString(), TITLE, LocalDateTime.now(), (URI) null, contents, CATEGORY); + + atomFeedSpringTransactionManager.executeWithTransaction( + new AFTransactionWorkWithoutResult() { + @Override + protected void doInTransaction() { + eventService.notify(event); + } + + @Override + public PropagationDefinition getTxPropagationDefinition() { + return PropagationDefinition.PROPAGATION_REQUIRED; + } + } + ); + } + } + + private boolean shouldRaiseRelationshipEvent() { + String raiseEvent = Context.getAdministrationService().getGlobalProperty(RAISE_RELATIONSHIP_EVENT_GLOBAL_PROPERTY); + return Boolean.valueOf(raiseEvent); + } + + private String getUrlPattern() { + String urlPattern = Context.getAdministrationService().getGlobalProperty(RELATIONSHIP_EVENT_URL_PATTERN_GLOBAL_PROPERTY); + if (urlPattern == null || urlPattern.equals("")) { + return DEFAULT_RELATIONSHIP_URL_PATTERN; + } + return urlPattern; + } + + private PlatformTransactionManager getSpringPlatformTransactionManager() { + List platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class); + return platformTransactionManagers.get(0); + } +} diff --git a/openmrs-atomfeed-api/src/main/resources/liquibase.xml b/openmrs-atomfeed-api/src/main/resources/liquibase.xml index 456182b..0734e39 100644 --- a/openmrs-atomfeed-api/src/main/resources/liquibase.xml +++ b/openmrs-atomfeed-api/src/main/resources/liquibase.xml @@ -96,4 +96,100 @@ + + + + select count(*) from chunking_history; + + Default chunking history entry if doesn't exist. + + + + + + + + + + SELECT COUNT(*) FROM global_property where property = 'atomfeed.publish.eventsForPatientRelationshipChange' + + + Adding global property to act as switch for raising relationship events + + + + + + + + + + + + SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForPatientRelationshipChange' + + + Adding global property to specify the URL pattern for published relationship events + + + + + + + + + + + + + + + Creating column tags for queue table. Each event can be tagged with multiple tags; as comma separated strings + + + + + + + + + + + + Creating column tags for event_records table. Each event can be tagged with multiple tags; as comma separated strings + + + + + + + + + SELECT COUNT(*) FROM global_property where property = 'atomfeed.publish.eventsForPatientProgramStateChange' + + + Adding global property to act as switch for raising program events + + + + + + + + + + + + SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForProgramStateChange' + + + Adding global property to specify the URL pattern for published program events + + + + + + + + \ No newline at end of file diff --git a/openmrs-atomfeed-api/src/main/resources/moduleApplicationContext.xml b/openmrs-atomfeed-api/src/main/resources/moduleApplicationContext.xml index b09df10..77d167a 100644 --- a/openmrs-atomfeed-api/src/main/resources/moduleApplicationContext.xml +++ b/openmrs-atomfeed-api/src/main/resources/moduleApplicationContext.xml @@ -16,4 +16,5 @@ http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> + diff --git a/openmrs-atomfeed-api/src/test/java/org/openmrs/module/atomfeed/advice/PatientProgramAdviceTest.java b/openmrs-atomfeed-api/src/test/java/org/openmrs/module/atomfeed/advice/PatientProgramAdviceTest.java new file mode 100644 index 0000000..18ca01f --- /dev/null +++ b/openmrs-atomfeed-api/src/test/java/org/openmrs/module/atomfeed/advice/PatientProgramAdviceTest.java @@ -0,0 +1,81 @@ +package org.openmrs.module.atomfeed.advice; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.openmrs.PatientProgram; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.context.Context; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.transaction.PlatformTransactionManager; + +import java.lang.reflect.Method; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Context.class, Method.class}) +public class PatientProgramAdviceTest { + + @Mock + private PatientProgram returnValue; + + @Mock + private PlatformTransactionManager platformTransactionManager; + + @Mock + private AdministrationService administrationService; + + private PatientProgramAdvice patientProgramAdvice; + + @Before + public void setUp() throws SQLException { + mockStatic(Context.class); + List listA = new ArrayList(); + listA.add(platformTransactionManager); + PowerMockito.when(Context.getRegisteredComponents(PlatformTransactionManager.class)) + .thenReturn(listA); + PowerMockito.when(Context.getAdministrationService()).thenReturn(administrationService); + this.patientProgramAdvice = new PatientProgramAdvice(); + } + + @Test + public void shouldCheckNameOfTheMethod() throws Throwable { + Method method = this.getClass().getMethod("abcd"); + when(administrationService.getGlobalProperty("atomfeed.publish.eventsForPatientProgramStateChange")).thenReturn("true"); + patientProgramAdvice.afterReturning(returnValue, method, null, null); + + verify(administrationService, times(0)).getGlobalProperty("atomfeed.publish.eventsForPatientProgramStateChange"); + } + + @Test + @Ignore + public void shouldCheckForGlobalPropertyToRaiseTheEvent() throws Throwable { + Method method = this.getClass().getMethod("savePatientProgram"); + when(administrationService.getGlobalProperty("atomfeed.publish.eventsForPatientProgramStateChange")).thenReturn("true"); + when(administrationService.getGlobalProperty("atomfeed.event.urlPatternForProgramStateChange")).thenReturn("/url/{uuid}"); + when(returnValue.getUuid()).thenReturn("1289313"); + patientProgramAdvice.afterReturning(returnValue, method, null, null); + + verify(administrationService, times(0)).getGlobalProperty("atomfeed.publish.eventsForPatientProgramStateChange"); + } + + public void abcd() { + + } + + public void savePatientProgram() { + + } +} \ No newline at end of file diff --git a/openmrs-atomfeed-api/src/test/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdviceTest.java b/openmrs-atomfeed-api/src/test/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdviceTest.java new file mode 100644 index 0000000..9fb3db6 --- /dev/null +++ b/openmrs-atomfeed-api/src/test/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdviceTest.java @@ -0,0 +1,83 @@ +package org.openmrs.module.atomfeed.advice; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.openmrs.Relationship; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.context.Context; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.transaction.PlatformTransactionManager; + +import java.lang.reflect.Method; +import java.sql.SQLException; +import java.util.Collections; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Context.class, Method.class}) +public class PersonRelationshipAdviceTest { + + @Mock + private Relationship returnValue; + + @Mock + private PlatformTransactionManager platformTransactionManager; + + @Mock + private AdministrationService administrationService; + + private PersonRelationshipAdvice personRelationshipAdvice; + + @Before + public void setUp() throws SQLException { + mockStatic(Context.class); + PowerMockito.when(Context.getRegisteredComponents(PlatformTransactionManager.class)).thenReturn(Collections.singletonList(platformTransactionManager)); + + this.personRelationshipAdvice = new PersonRelationshipAdvice(); + } + + @Test + public void shouldCheckNameOfTheMethod() throws Throwable { + Method method = this.getClass().getMethod("abcd"); + PowerMockito.when(Context.getRegisteredComponents(PlatformTransactionManager.class)).thenReturn(Collections.singletonList(platformTransactionManager)); + PowerMockito.when(Context.getAdministrationService()).thenReturn(administrationService); + when(administrationService.getGlobalProperty("atomfeed.publish.eventsForPatientRelationshipChange")).thenReturn("true"); + personRelationshipAdvice.afterReturning(returnValue, method, null, null); + + verify(administrationService, times(0)).getGlobalProperty("atomfeed.publish.eventsForPatientRelationshipChange"); + } + + @Test + @Ignore + public void shouldCheckForGlobalPropertyToRaiseTheEvent() throws Throwable { + Method method = this.getClass().getMethod("saveRelationship"); + mockStatic(Context.class); + PowerMockito.when(Context.getRegisteredComponents(PlatformTransactionManager.class)).thenReturn(Collections.singletonList(platformTransactionManager)); + PowerMockito.when(Context.getAdministrationService()).thenReturn(administrationService); + when(administrationService.getGlobalProperty("atomfeed.publish.eventsForPatientRelationshipChange")).thenReturn("true"); + when(administrationService.getGlobalProperty("atomfeed.event.urlPatternForPatientRelationshipChange")).thenReturn("/url/%s"); + when(returnValue.getUuid()).thenReturn("1289313"); + + personRelationshipAdvice.afterReturning(returnValue, method, null, null); + + verify(administrationService, times(1)).getGlobalProperty("atomfeed.publish.eventsForPatientRelationshipChange"); + } + + public void abcd() { + + } + + public void saveRelationship() { + + } +} \ No newline at end of file diff --git a/openmrs-atomfeed-common/pom.xml b/openmrs-atomfeed-common/pom.xml index 30fedda..218387c 100644 --- a/openmrs-atomfeed-common/pom.xml +++ b/openmrs-atomfeed-common/pom.xml @@ -4,7 +4,7 @@ org.ict4h.openmrs openmrs-atomfeed - 2.5.1-SNAPSHOT + 2.6.3 openmrs-atomfeed-common @@ -14,8 +14,7 @@ UTF-8 - 1.8.4 - 3.0.5.RELEASE + 5.2.9.RELEASE @@ -25,12 +24,6 @@ ${atomfeed.version} provided - - org.openmrs.hibernate - hibernate-core - 3.6.5.Final-mod - provided - org.springframework spring-beans diff --git a/openmrs-atomfeed-common/src/main/java/org/openmrs/module/atomfeed/transaction/support/AtomFeedSpringTransactionManager.java b/openmrs-atomfeed-common/src/main/java/org/openmrs/module/atomfeed/transaction/support/AtomFeedSpringTransactionManager.java index 03559b1..1c5296c 100644 --- a/openmrs-atomfeed-common/src/main/java/org/openmrs/module/atomfeed/transaction/support/AtomFeedSpringTransactionManager.java +++ b/openmrs-atomfeed-common/src/main/java/org/openmrs/module/atomfeed/transaction/support/AtomFeedSpringTransactionManager.java @@ -1,7 +1,8 @@ package org.openmrs.module.atomfeed.transaction.support; import org.hibernate.SessionFactory; -import org.hibernate.classic.Session; +import org.hibernate.Session; +import org.hibernate.internal.SessionImpl; import org.ict4h.atomfeed.jdbc.JdbcConnectionProvider; import org.ict4h.atomfeed.transaction.AFTransactionManager; import org.ict4h.atomfeed.transaction.AFTransactionWork; @@ -54,7 +55,7 @@ private Integer getTxPropagation(AFTransactionWork.PropagationDefinition propaga @Override public Connection getConnection() throws SQLException { //TODO: ensure that only connection associated with current thread current transaction is given - return getSession().connection(); + return ((SessionImpl)getSession()).connection(); } private Session getSession() { diff --git a/openmrs-atomfeed-omod/pom.xml b/openmrs-atomfeed-omod/pom.xml index b5527fd..ed2062d 100644 --- a/openmrs-atomfeed-omod/pom.xml +++ b/openmrs-atomfeed-omod/pom.xml @@ -4,7 +4,7 @@ org.ict4h.openmrs openmrs-atomfeed - 2.5.1-SNAPSHOT + 2.6.3 @@ -33,12 +33,27 @@ jar + + org.apache.logging.log4j + log4j-api + + + + org.apache.logging.log4j + log4j-core + + org.openmrs.web openmrs-web jar + + org.openmrs.module + legacyui-omod + + org.openmrs.api openmrs-api diff --git a/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/utils/UrlUtil.java b/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/utils/UrlUtil.java new file mode 100644 index 0000000..54915c7 --- /dev/null +++ b/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/utils/UrlUtil.java @@ -0,0 +1,56 @@ +package org.openmrs.module.atomfeed.utils; + +import org.apache.commons.lang.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.openmrs.api.context.Context; + +import javax.servlet.http.HttpServletRequest; +import java.net.URI; +import java.net.URISyntaxException; + +public class UrlUtil { + private static Logger logger = LogManager.getLogger(UrlUtil.class); + + public String getRequestURL(HttpServletRequest request) { + String requestUrl = getServiceUriFromRequest(request); + if (requestUrl == null) { + requestUrl = getBaseUrlFromOpenMrsGlobalProperties(request); + } + return requestUrl != null ? requestUrl : formUrl(request.getScheme(), request.getServerName(), request.getServerPort(), request.getRequestURI(), request.getQueryString()); + } + + private String getBaseUrlFromOpenMrsGlobalProperties(HttpServletRequest request) { + String restUri = Context.getAdministrationService().getGlobalProperty("webservices.rest.uriPrefix"); + if (StringUtils.isNotBlank(restUri)) { + try { + URI uri = new URI(restUri); + return formUrl(uri.getScheme(), uri.getHost(), uri.getPort(), request.getRequestURI(), request.getQueryString()); + } catch (URISyntaxException e) { + logger.warn("Invalid url is set in global property webservices.rest.uriPrefix"); + } + } + return null; + } + + private String getServiceUriFromRequest(HttpServletRequest request) { + String scheme = request.getHeader("X-Forwarded-Proto"); + if (scheme == null) { + return null; + } + return formUrl(scheme, request.getServerName(), request.getServerPort(), request.getRequestURI(), request.getQueryString()); + } + + private String formUrl(String scheme, String hostname, int port, String path, String queryString) { + String url = null; + if (port != 80 && port != 443 && port != -1) { + url = scheme + "://" + hostname + ":" + port + path; + } else { + url = scheme + "://" + hostname + path; + } + if (queryString != null) { + return url + "?" + queryString; + } + return url; + } +} diff --git a/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/web/controller/AtomFeedController.java b/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/web/controller/AtomFeedController.java index 286f358..1f40ef0 100644 --- a/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/web/controller/AtomFeedController.java +++ b/openmrs-atomfeed-omod/src/main/java/org/openmrs/module/atomfeed/web/controller/AtomFeedController.java @@ -1,6 +1,7 @@ package org.openmrs.module.atomfeed.web.controller; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsJdbcImpl; import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsOffsetMarkersJdbcImpl; import org.ict4h.atomfeed.server.repository.jdbc.ChunkingEntriesJdbcImpl; @@ -10,6 +11,7 @@ import org.ict4h.atomfeed.server.service.helper.EventFeedServiceHelper; import org.ict4h.atomfeed.server.service.helper.ResourceHelper; import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager; +import org.openmrs.module.atomfeed.utils.UrlUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.PlatformTransactionManager; @@ -23,7 +25,7 @@ @Controller @RequestMapping(value = "/atomfeed") public class AtomFeedController { - private static Logger logger = Logger.getLogger(AtomFeedController.class); + private static Logger logger = LogManager.getLogger(AtomFeedController.class); private AtomFeedSpringTransactionManager atomTxManager; private EventFeedService eventFeedService; @@ -44,7 +46,7 @@ public AtomFeedController(EventFeedService eventFeedService) { @RequestMapping(method = RequestMethod.GET, value = "/{category}/recent") @ResponseBody public String getRecentEventFeedForCategory(HttpServletRequest httpServletRequest, @PathVariable String category) { - return EventFeedServiceHelper.getRecentFeed(eventFeedService, httpServletRequest.getRequestURL().toString(), + return EventFeedServiceHelper.getRecentFeed(eventFeedService, new UrlUtil().getRequestURL(httpServletRequest), category, logger, atomTxManager); } @@ -52,7 +54,7 @@ public String getRecentEventFeedForCategory(HttpServletRequest httpServletReques @ResponseBody public String getEventFeedWithCategory(HttpServletRequest httpServletRequest, @PathVariable String category, @PathVariable int n) { - return EventFeedServiceHelper.getEventFeed(eventFeedService, httpServletRequest.getRequestURL().toString(), + return EventFeedServiceHelper.getEventFeed(eventFeedService, new UrlUtil().getRequestURL(httpServletRequest), category, n, logger, atomTxManager); } } \ No newline at end of file diff --git a/openmrs-atomfeed-omod/src/main/resources/config.xml b/openmrs-atomfeed-omod/src/main/resources/config.xml index 10b806d..d682ca5 100644 --- a/openmrs-atomfeed-omod/src/main/resources/config.xml +++ b/openmrs-atomfeed-omod/src/main/resources/config.xml @@ -15,7 +15,10 @@ ${openMRSVersion} - + + org.openmrs.module.legacyui + + org.openmrs.module.atomfeed.AtomFeedActivator @@ -25,10 +28,24 @@ org.openmrs.module.atomfeed.advice.PatientAdvice + + org.openmrs.api.PersonService + org.openmrs.module.atomfeed.advice.PersonRelationshipAdvice + + + + org.openmrs.api.ProgramWorkflowService + org.openmrs.module.atomfeed.advice.PatientProgramAdvice + + org.openmrs.module.emrapi.encounter.EmrEncounterService org.openmrs.module.atomfeed.advice.EncounterSaveAdvice + + org.openmrs.api.EncounterService + org.openmrs.module.atomfeed.advice.EncounterServiceSaveAdvice + diff --git a/openmrs-atomfeed-omod/src/main/resources/webModuleApplicationContext.xml b/openmrs-atomfeed-omod/src/main/resources/webModuleApplicationContext.xml index 02af8c9..f714797 100644 --- a/openmrs-atomfeed-omod/src/main/resources/webModuleApplicationContext.xml +++ b/openmrs-atomfeed-omod/src/main/resources/webModuleApplicationContext.xml @@ -8,9 +8,5 @@ http://www.springframework.org/schema/context/spring-context-3.0.xsd"> - - - - diff --git a/pom.xml b/pom.xml index 05b1771..f886a8a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.ict4h.openmrs openmrs-atomfeed - 2.5.1-SNAPSHOT + 2.6.3 pom AtomFeed Module Atomfeed publisher and consumer for OpenMRS. @@ -39,16 +39,17 @@ - + openmrs-atomfeed-api openmrs-atomfeed-omod openmrs-atomfeed-common - 1.9.2 + 2.4.2 UTF-8 - 1.9.1 + 1.10.1 + 2.17.1 @@ -75,6 +76,14 @@ provided + + org.openmrs.module + legacyui-omod + 1.3.3 + jar + provided + + org.openmrs.api openmrs-api @@ -99,6 +108,19 @@ test + + org.apache.logging.log4j + log4j-api + ${log4j.version} + provided + + + + org.apache.logging.log4j + log4j-core + ${log4j.version} + provided + @@ -129,7 +151,6 @@ 2.4 true - false @@ -150,11 +171,61 @@ + + + nexus-sonatype + https://oss.sonatype.org/content/repositories/snapshots + + + nexus-sonatype + https://oss.sonatype.org/service/local/staging/deploy/maven2 + + + + + + release + + + performRelease + true + + + + + + org.apache.maven.plugins + maven-release-plugin + 2.4 + + false + true + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.4 + + + sign-artifacts + verify + + sign + + + + + + + + + openmrs-repo OpenMRS Nexus Repository - http://mavenrepo.openmrs.org/nexus/content/repositories/public + https://mavenrepo.openmrs.org/nexus/content/repositories/public sonatype-nexus-snapshots @@ -167,7 +238,6 @@ true - sonatype-nexus-releases Sonatype Nexus Releases @@ -179,7 +249,7 @@ openmrs-repo OpenMRS Nexus Repository - http://mavenrepo.openmrs.org/nexus/content/repositories/public + https://mavenrepo.openmrs.org/nexus/content/repositories/public false