Skip to content

Commit

Permalink
MK: Release 3.2.1. Compatible with openmrs 2.0.x; merged with ICT4H b…
Browse files Browse the repository at this point in the history
…ranch with relationship and program feature as well.
  • Loading branch information
Maimoona Kausar committed Jan 1, 2018
1 parent 992031d commit 96083d5
Show file tree
Hide file tree
Showing 14 changed files with 521 additions and 134 deletions.
2 changes: 1 addition & 1 deletion openmrs-atomfeed-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.opensrp</groupId>
<artifactId>atomfeed</artifactId>
<version>3.1.1</version>
<version>3.2.1</version>
</parent>

<artifactId>atomfeed-api</artifactId>
Expand Down
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);
}
}
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);
}
}
136 changes: 94 additions & 42 deletions openmrs-atomfeed-api/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,21 @@
<column name="uuid" valueComputed=" uuid() "/>
</insert>
</changeSet>
<changeSet id="opensrp-atomfeed-20160719-7" author="Vinay, Mihir">

<changeSet id="20160119-1146" context="setup" author="Shashi, Hanisha">
<preConditions onFail="MARK_RAN">
<not>
<columnExists columnName="date_created" tableName="event_records"/>
</not>
</preConditions>
<comment>Creating column date_created for queue table. This indicates the time event was raised or created.</comment>
<addColumn tableName="event_records">
<column name="date_created" type="TIMESTAMP" defaultValueDate="CURRENT_TIMESTAMP"/>
</addColumn>
</changeSet>


<changeSet id="20160603-1739" author="angshu">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">select count(*) from chunking_history;</sqlCheck>
</preConditions>
Expand All @@ -99,50 +113,88 @@
</insert>
</changeSet>

<changeSet id="opensrp-atomfeed-20160721-1" author="maimoonak">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="event_records_extras"/>
</not>
</preConditions>
<createTable tableName="event_records_extras">
<column name="id" type="int" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="event_uuid" type="varchar(40)"/>
<column name="uuid" type="varchar(40)"/>
<column name="name" type="varchar(255)"/>
<column name="value" type="varchar(1000)"/>
<column name="timestamp" type="TIMESTAMP" defaultValueDate="CURRENT_TIMESTAMP"/>
</createTable>
<changeSet id="20160712-1977-1" author="Jaswanth/Sanjit">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM global_property where property = 'atomfeed.publish.eventsForPatientRelationshipChange'
</sqlCheck>
</preConditions>
<comment>Adding global property to act as switch for raising relationship events</comment>
<insert tableName="global_property">
<column name="property" value="atomfeed.publish.eventsForPatientRelationshipChange"/>
<column name="property_value" value=""/>
<column name="uuid" valueComputed="UUID()"/>
<column name="description" value="If set true, events related to relationship changes are published"/>
</insert>
</changeSet>
<changeSet id="opensrp-atomfeed-20170228-1" author="maimoonak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">select count(*) from role where role='opensrp-rest-service';</sqlCheck>

<changeSet id="20160712-1977-2" author="Jaswanth/Sanjit">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForPatientRelationshipChange'
</sqlCheck>
</preConditions>
<comment>Adding global property to specify the URL pattern for published relationship events</comment>
<insert tableName="global_property">
<column name="property" value="atomfeed.event.urlPatternForPatientRelationshipChange"/>
<column name="property_value" value=""/>
<column name="uuid" valueComputed="UUID()"/>
<column name="description" value="URL pattern to use for published relationship events. Default is /openmrs/ws/rest/v1/relationship/%s"/>
</insert>
</changeSet>

<changeSet id="20160705-1120" context="setup" author="angshu">
<preConditions onFail="MARK_RAN">
<not>
<columnExists columnName="tags" tableName="event_records_queue"/>
</not>
</preConditions>
<comment>Create opensrp-rest-service default role</comment>
<sql>
insert into role (role, description, uuid) values ('opensrp-rest-service', 'Default role for OpenSRP REST Service',uuid());
</sql>
</changeSet>
<changeSet id="opensrp-atomfeed-20170404-1" author="maimoonak">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">select count(*) from event_records where category LIKE 'opensrp%';</sqlCheck>
<comment>Creating column tags for queue table. Each event can be tagged with multiple tags; as comma separated strings</comment>
<addColumn tableName="event_records_queue">
<column name="tags" type="varchar(255)"></column>
</addColumn>
</changeSet>

<changeSet id="20160705-1130" context="setup" author="angshu">
<preConditions onFail="MARK_RAN">
<not>
<columnExists columnName="tags" tableName="event_records"/>
</not>
</preConditions>
<comment>Creating feeds for existing Patient, Encounter, DrugOrder data</comment>
<sql>
INSERT INTO event_records(uuid, title, timestamp, uri, object, category, date_created)
SELECT UUID(), 'Patient_Migrate', NOW(), null, CONCAT('/openmrs/ws/rest/v1/patient/', pr.uuid,'?v=full'), 'OpenSRP_Patient',p.date_created
FROM patient p JOIN person pr ON pr.person_id=p.patient_id;
<comment>Creating column tags for event_records table. Each event can be tagged with multiple tags; as comma separated strings</comment>
<addColumn tableName="event_records">
<column name="tags" type="varchar(255)"></column>
</addColumn>
</changeSet>

INSERT INTO event_records(uuid, title, timestamp, uri, object, category, date_created)
SELECT UUID(), 'Encounter_Migrate', NOW(), null, CONCAT('/openmrs/ws/rest/v1/encounter/', e.uuid,'?v=full'), 'OpenSRP_Encounter',e.date_created
FROM encounter e;
<changeSet id="20160713-1978-1" author="Pankaj/Koushik">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM global_property where property = 'atomfeed.publish.eventsForPatientProgramStateChange'
</sqlCheck>
</preConditions>
<comment>Adding global property to act as switch for raising program events</comment>
<insert tableName="global_property">
<column name="property" value="atomfeed.publish.eventsForPatientProgramStateChange"/>
<column name="property_value" value=""/>
<column name="uuid" valueComputed="UUID()"/>
<column name="description" value="If set true, events related to program changes are published"/>
</insert>
</changeSet>

<changeSet id="20160713-1978-2" author="Pankaj/Koushik">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForProgramStateChange'
</sqlCheck>
</preConditions>
<comment>Adding global property to specify the URL pattern for published program events</comment>
<insert tableName="global_property">
<column name="property" value="atomfeed.event.urlPatternForProgramStateChange"/>
<column name="property_value" value=""/>
<column name="uuid" valueComputed="UUID()"/>
<column name="description" value="URL pattern to use for published program events. Default is /openmrs/ws/rest/v1/programenrollment/{uuid}?v=full"/>
</insert>
</changeSet>

INSERT INTO event_records(uuid, title, timestamp, uri, object, category, date_created)
SELECT UUID(), 'DrugOrder_Migrate', NOW(), null, CONCAT('/openmrs/ws/rest/v1/order/', o.uuid,'?v=full'), 'OpenSRP_DrugOrder',o.date_created
FROM orders o JOIN order_type ot ON o.order_type_id=ot.order_type_id WHERE ot.name IN ('drug order', 'drug_order', 'drug-order', 'drugorder');
</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<bean id="patientProgramAdviceInterceptor" class="org.openmrs.module.atomfeed.advice.PatientProgramAdvice"/>
</beans>
Loading

0 comments on commit 96083d5

Please sign in to comment.