forked from ICT4H/openmrs-atomfeed
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MK: Release 3.2.1. Compatible with openmrs 2.0.x; merged with ICT4H b…
…ranch with relationship and program feature as well.
- Loading branch information
Maimoona Kausar
committed
Jan 1, 2018
1 parent
992031d
commit 96083d5
Showing
14 changed files
with
521 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...s-atomfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PatientProgramAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.joda.time.DateTime; | ||
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.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, DateTime.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<PlatformTransactionManager> platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class); | ||
return platformTransactionManagers.get(0); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...omfeed-api/src/main/java/org/openmrs/module/atomfeed/advice/PersonRelationshipAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.joda.time.DateTime; | ||
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.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, DateTime.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<PlatformTransactionManager> platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class); | ||
return platformTransactionManagers.get(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.