Skip to content

Commit 96083d5

Browse files
author
Maimoona Kausar
committed
MK: Release 3.2.1. Compatible with openmrs 2.0.x; merged with ICT4H branch with relationship and program feature as well.
1 parent 992031d commit 96083d5

File tree

14 files changed

+521
-134
lines changed

14 files changed

+521
-134
lines changed

openmrs-atomfeed-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.opensrp</groupId>
66
<artifactId>atomfeed</artifactId>
7-
<version>3.1.1</version>
7+
<version>3.2.1</version>
88
</parent>
99

1010
<artifactId>atomfeed-api</artifactId>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.openmrs.module.atomfeed.advice;
2+
3+
import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsQueueJdbcImpl;
4+
import org.ict4h.atomfeed.server.service.Event;
5+
import org.ict4h.atomfeed.server.service.EventService;
6+
import org.ict4h.atomfeed.server.service.EventServiceImpl;
7+
import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult;
8+
import org.joda.time.DateTime;
9+
import org.openmrs.PatientProgram;
10+
import org.openmrs.api.context.Context;
11+
import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager;
12+
import org.springframework.aop.AfterReturningAdvice;
13+
import org.springframework.transaction.PlatformTransactionManager;
14+
15+
import java.lang.reflect.Method;
16+
import java.net.URI;
17+
import java.sql.SQLException;
18+
import java.util.List;
19+
import java.util.UUID;
20+
21+
public class PatientProgramAdvice implements AfterReturningAdvice {
22+
private static final String CATEGORY = "programenrollment";
23+
private static final String TITLE = "Progam Enrollment";
24+
private static final String SAVE_PATIENT_PROGRAM_METHOD = "savePatientProgram";
25+
private static final String RAISE_PATIENT_PROGRAM_EVENT_GLOBAL_PROPERTY = "atomfeed.publish.eventsForPatientProgramStateChange";
26+
private static final String PATIENT_PROGRAM_EVENT_URL_PATTERN_GLOBAL_PROPERTY = "atomfeed.event.urlPatternForProgramStateChange";
27+
private static final String DEFAULT_PATIENT_PROGRAM_URL_PATTERN = "/openmrs/ws/rest/v1/programenrollment/{uuid}?v=full";
28+
private AtomFeedSpringTransactionManager atomFeedSpringTransactionManager;
29+
private EventService eventService;
30+
private final Object eventServiceMonitor = new Object();
31+
private final Object txManagerMonitor = new Object();
32+
33+
public PatientProgramAdvice() throws SQLException {
34+
35+
}
36+
37+
@Override
38+
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) throws Throwable {
39+
if (method.getName().equals(SAVE_PATIENT_PROGRAM_METHOD) && shouldRaiseRelationshipEvent()) {
40+
String contents = getUrlPattern().replace("{uuid}",((PatientProgram) returnValue).getUuid());
41+
final Event event = new Event(UUID.randomUUID().toString(), TITLE, DateTime.now(), (URI) null, contents, CATEGORY);
42+
43+
getAFTxManager().executeWithTransaction(
44+
new AFTransactionWorkWithoutResult() {
45+
@Override
46+
protected void doInTransaction() {
47+
getEventService().notify(event);
48+
}
49+
50+
@Override
51+
public PropagationDefinition getTxPropagationDefinition() {
52+
return PropagationDefinition.PROPAGATION_REQUIRED;
53+
}
54+
}
55+
);
56+
}
57+
}
58+
59+
private EventService getEventService() {
60+
if (eventService == null) { // Single Checked
61+
synchronized (eventServiceMonitor) {
62+
if (eventService == null) { // Double checked
63+
this.eventService = new EventServiceImpl(new AllEventRecordsQueueJdbcImpl(getAFTxManager()));
64+
}
65+
}
66+
}
67+
return this.eventService;
68+
}
69+
70+
private AtomFeedSpringTransactionManager getAFTxManager() {
71+
if (this.atomFeedSpringTransactionManager == null) {
72+
synchronized (txManagerMonitor) {
73+
if(this.atomFeedSpringTransactionManager == null) {
74+
this.atomFeedSpringTransactionManager = new AtomFeedSpringTransactionManager(getSpringPlatformTransactionManager());
75+
}
76+
}
77+
}
78+
return this.atomFeedSpringTransactionManager;
79+
}
80+
81+
private boolean shouldRaiseRelationshipEvent() {
82+
String raiseEvent = Context.getAdministrationService().getGlobalProperty(RAISE_PATIENT_PROGRAM_EVENT_GLOBAL_PROPERTY);
83+
return Boolean.valueOf(raiseEvent);
84+
}
85+
86+
private String getUrlPattern() {
87+
String urlPattern = Context.getAdministrationService().getGlobalProperty(PATIENT_PROGRAM_EVENT_URL_PATTERN_GLOBAL_PROPERTY);
88+
if (urlPattern == null || urlPattern.equals("")) {
89+
return DEFAULT_PATIENT_PROGRAM_URL_PATTERN;
90+
}
91+
return urlPattern;
92+
}
93+
94+
private PlatformTransactionManager getSpringPlatformTransactionManager() {
95+
List<PlatformTransactionManager> platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class);
96+
return platformTransactionManagers.get(0);
97+
}
98+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.openmrs.module.atomfeed.advice;
2+
3+
import org.ict4h.atomfeed.server.repository.AllEventRecordsQueue;
4+
import org.ict4h.atomfeed.server.repository.jdbc.AllEventRecordsQueueJdbcImpl;
5+
import org.ict4h.atomfeed.server.service.Event;
6+
import org.ict4h.atomfeed.server.service.EventService;
7+
import org.ict4h.atomfeed.server.service.EventServiceImpl;
8+
import org.ict4h.atomfeed.transaction.AFTransactionWorkWithoutResult;
9+
import org.joda.time.DateTime;
10+
import org.openmrs.Relationship;
11+
import org.openmrs.api.context.Context;
12+
import org.openmrs.module.atomfeed.transaction.support.AtomFeedSpringTransactionManager;
13+
import org.springframework.aop.AfterReturningAdvice;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
16+
import java.lang.reflect.Method;
17+
import java.net.URI;
18+
import java.sql.SQLException;
19+
import java.util.List;
20+
import java.util.UUID;
21+
22+
public class PersonRelationshipAdvice implements AfterReturningAdvice {
23+
private static final String CATEGORY = "relationship";
24+
private static final String TITLE = "Relationship";
25+
private static final String SAVE_RELATIONSHIP_METHOD = "saveRelationship";
26+
private static final String RAISE_RELATIONSHIP_EVENT_GLOBAL_PROPERTY = "atomfeed.publish.eventsForPatientRelationshipChange";
27+
private static final String RELATIONSHIP_EVENT_URL_PATTERN_GLOBAL_PROPERTY = "atomfeed.event.urlPatternForPatientRelationshipChange";
28+
private static final String DEFAULT_RELATIONSHIP_URL_PATTERN = "/openmrs/ws/rest/v1/relationship/%s";
29+
private final AtomFeedSpringTransactionManager atomFeedSpringTransactionManager;
30+
private final EventService eventService;
31+
32+
public PersonRelationshipAdvice() throws SQLException {
33+
atomFeedSpringTransactionManager = new AtomFeedSpringTransactionManager(getSpringPlatformTransactionManager());
34+
AllEventRecordsQueue allEventRecordsQueue = new AllEventRecordsQueueJdbcImpl(atomFeedSpringTransactionManager);
35+
this.eventService = new EventServiceImpl(allEventRecordsQueue);
36+
}
37+
38+
@Override
39+
public void afterReturning(Object returnValue, Method method, Object[] arguments, Object target) throws Throwable {
40+
if (method.getName().equals(SAVE_RELATIONSHIP_METHOD) && shouldRaiseRelationshipEvent()) {
41+
String contents = String.format(getUrlPattern(), ((Relationship) returnValue).getUuid());
42+
final Event event = new Event(UUID.randomUUID().toString(), TITLE, DateTime.now(), (URI) null, contents, CATEGORY);
43+
44+
atomFeedSpringTransactionManager.executeWithTransaction(
45+
new AFTransactionWorkWithoutResult() {
46+
@Override
47+
protected void doInTransaction() {
48+
eventService.notify(event);
49+
}
50+
51+
@Override
52+
public PropagationDefinition getTxPropagationDefinition() {
53+
return PropagationDefinition.PROPAGATION_REQUIRED;
54+
}
55+
}
56+
);
57+
}
58+
}
59+
60+
private boolean shouldRaiseRelationshipEvent() {
61+
String raiseEvent = Context.getAdministrationService().getGlobalProperty(RAISE_RELATIONSHIP_EVENT_GLOBAL_PROPERTY);
62+
return Boolean.valueOf(raiseEvent);
63+
}
64+
65+
private String getUrlPattern() {
66+
String urlPattern = Context.getAdministrationService().getGlobalProperty(RELATIONSHIP_EVENT_URL_PATTERN_GLOBAL_PROPERTY);
67+
if (urlPattern == null || urlPattern.equals("")) {
68+
return DEFAULT_RELATIONSHIP_URL_PATTERN;
69+
}
70+
return urlPattern;
71+
}
72+
73+
private PlatformTransactionManager getSpringPlatformTransactionManager() {
74+
List<PlatformTransactionManager> platformTransactionManagers = Context.getRegisteredComponents(PlatformTransactionManager.class);
75+
return platformTransactionManagers.get(0);
76+
}
77+
}

openmrs-atomfeed-api/src/main/resources/liquibase.xml

Lines changed: 94 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,21 @@
8888
<column name="uuid" valueComputed=" uuid() "/>
8989
</insert>
9090
</changeSet>
91-
<changeSet id="opensrp-atomfeed-20160719-7" author="Vinay, Mihir">
91+
92+
<changeSet id="20160119-1146" context="setup" author="Shashi, Hanisha">
93+
<preConditions onFail="MARK_RAN">
94+
<not>
95+
<columnExists columnName="date_created" tableName="event_records"/>
96+
</not>
97+
</preConditions>
98+
<comment>Creating column date_created for queue table. This indicates the time event was raised or created.</comment>
99+
<addColumn tableName="event_records">
100+
<column name="date_created" type="TIMESTAMP" defaultValueDate="CURRENT_TIMESTAMP"/>
101+
</addColumn>
102+
</changeSet>
103+
104+
105+
<changeSet id="20160603-1739" author="angshu">
92106
<preConditions onFail="MARK_RAN">
93107
<sqlCheck expectedResult="0">select count(*) from chunking_history;</sqlCheck>
94108
</preConditions>
@@ -99,50 +113,88 @@
99113
</insert>
100114
</changeSet>
101115

102-
<changeSet id="opensrp-atomfeed-20160721-1" author="maimoonak">
103-
<preConditions onFail="MARK_RAN">
104-
<not>
105-
<tableExists tableName="event_records_extras"/>
106-
</not>
107-
</preConditions>
108-
<createTable tableName="event_records_extras">
109-
<column name="id" type="int" autoIncrement="true">
110-
<constraints nullable="false" primaryKey="true"/>
111-
</column>
112-
<column name="event_uuid" type="varchar(40)"/>
113-
<column name="uuid" type="varchar(40)"/>
114-
<column name="name" type="varchar(255)"/>
115-
<column name="value" type="varchar(1000)"/>
116-
<column name="timestamp" type="TIMESTAMP" defaultValueDate="CURRENT_TIMESTAMP"/>
117-
</createTable>
116+
<changeSet id="20160712-1977-1" author="Jaswanth/Sanjit">
117+
<preConditions onFail="MARK_RAN">
118+
<sqlCheck expectedResult="0">
119+
SELECT COUNT(*) FROM global_property where property = 'atomfeed.publish.eventsForPatientRelationshipChange'
120+
</sqlCheck>
121+
</preConditions>
122+
<comment>Adding global property to act as switch for raising relationship events</comment>
123+
<insert tableName="global_property">
124+
<column name="property" value="atomfeed.publish.eventsForPatientRelationshipChange"/>
125+
<column name="property_value" value=""/>
126+
<column name="uuid" valueComputed="UUID()"/>
127+
<column name="description" value="If set true, events related to relationship changes are published"/>
128+
</insert>
118129
</changeSet>
119-
<changeSet id="opensrp-atomfeed-20170228-1" author="maimoonak">
120-
<preConditions onFail="MARK_RAN">
121-
<sqlCheck expectedResult="0">select count(*) from role where role='opensrp-rest-service';</sqlCheck>
130+
131+
<changeSet id="20160712-1977-2" author="Jaswanth/Sanjit">
132+
<preConditions onFail="MARK_RAN">
133+
<sqlCheck expectedResult="0">
134+
SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForPatientRelationshipChange'
135+
</sqlCheck>
136+
</preConditions>
137+
<comment>Adding global property to specify the URL pattern for published relationship events</comment>
138+
<insert tableName="global_property">
139+
<column name="property" value="atomfeed.event.urlPatternForPatientRelationshipChange"/>
140+
<column name="property_value" value=""/>
141+
<column name="uuid" valueComputed="UUID()"/>
142+
<column name="description" value="URL pattern to use for published relationship events. Default is /openmrs/ws/rest/v1/relationship/%s"/>
143+
</insert>
144+
</changeSet>
145+
146+
<changeSet id="20160705-1120" context="setup" author="angshu">
147+
<preConditions onFail="MARK_RAN">
148+
<not>
149+
<columnExists columnName="tags" tableName="event_records_queue"/>
150+
</not>
122151
</preConditions>
123-
<comment>Create opensrp-rest-service default role</comment>
124-
<sql>
125-
insert into role (role, description, uuid) values ('opensrp-rest-service', 'Default role for OpenSRP REST Service',uuid());
126-
</sql>
127-
</changeSet>
128-
<changeSet id="opensrp-atomfeed-20170404-1" author="maimoonak">
129-
<preConditions onFail="MARK_RAN">
130-
<sqlCheck expectedResult="0">select count(*) from event_records where category LIKE 'opensrp%';</sqlCheck>
152+
<comment>Creating column tags for queue table. Each event can be tagged with multiple tags; as comma separated strings</comment>
153+
<addColumn tableName="event_records_queue">
154+
<column name="tags" type="varchar(255)"></column>
155+
</addColumn>
156+
</changeSet>
157+
158+
<changeSet id="20160705-1130" context="setup" author="angshu">
159+
<preConditions onFail="MARK_RAN">
160+
<not>
161+
<columnExists columnName="tags" tableName="event_records"/>
162+
</not>
131163
</preConditions>
132-
<comment>Creating feeds for existing Patient, Encounter, DrugOrder data</comment>
133-
<sql>
134-
INSERT INTO event_records(uuid, title, timestamp, uri, object, category, date_created)
135-
SELECT UUID(), 'Patient_Migrate', NOW(), null, CONCAT('/openmrs/ws/rest/v1/patient/', pr.uuid,'?v=full'), 'OpenSRP_Patient',p.date_created
136-
FROM patient p JOIN person pr ON pr.person_id=p.patient_id;
164+
<comment>Creating column tags for event_records table. Each event can be tagged with multiple tags; as comma separated strings</comment>
165+
<addColumn tableName="event_records">
166+
<column name="tags" type="varchar(255)"></column>
167+
</addColumn>
168+
</changeSet>
137169

138-
INSERT INTO event_records(uuid, title, timestamp, uri, object, category, date_created)
139-
SELECT UUID(), 'Encounter_Migrate', NOW(), null, CONCAT('/openmrs/ws/rest/v1/encounter/', e.uuid,'?v=full'), 'OpenSRP_Encounter',e.date_created
140-
FROM encounter e;
170+
<changeSet id="20160713-1978-1" author="Pankaj/Koushik">
171+
<preConditions onFail="MARK_RAN">
172+
<sqlCheck expectedResult="0">
173+
SELECT COUNT(*) FROM global_property where property = 'atomfeed.publish.eventsForPatientProgramStateChange'
174+
</sqlCheck>
175+
</preConditions>
176+
<comment>Adding global property to act as switch for raising program events</comment>
177+
<insert tableName="global_property">
178+
<column name="property" value="atomfeed.publish.eventsForPatientProgramStateChange"/>
179+
<column name="property_value" value=""/>
180+
<column name="uuid" valueComputed="UUID()"/>
181+
<column name="description" value="If set true, events related to program changes are published"/>
182+
</insert>
183+
</changeSet>
184+
185+
<changeSet id="20160713-1978-2" author="Pankaj/Koushik">
186+
<preConditions onFail="MARK_RAN">
187+
<sqlCheck expectedResult="0">
188+
SELECT COUNT(*) FROM global_property where property = 'atomfeed.event.urlPatternForProgramStateChange'
189+
</sqlCheck>
190+
</preConditions>
191+
<comment>Adding global property to specify the URL pattern for published program events</comment>
192+
<insert tableName="global_property">
193+
<column name="property" value="atomfeed.event.urlPatternForProgramStateChange"/>
194+
<column name="property_value" value=""/>
195+
<column name="uuid" valueComputed="UUID()"/>
196+
<column name="description" value="URL pattern to use for published program events. Default is /openmrs/ws/rest/v1/programenrollment/{uuid}?v=full"/>
197+
</insert>
198+
</changeSet>
141199

142-
INSERT INTO event_records(uuid, title, timestamp, uri, object, category, date_created)
143-
SELECT UUID(), 'DrugOrder_Migrate', NOW(), null, CONCAT('/openmrs/ws/rest/v1/order/', o.uuid,'?v=full'), 'OpenSRP_DrugOrder',o.date_created
144-
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');
145-
</sql>
146-
</changeSet>
147-
148200
</databaseChangeLog>

openmrs-atomfeed-api/src/main/resources/moduleApplicationContext.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
1717
http://www.springframework.org/schema/util
1818
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
19+
<bean id="patientProgramAdviceInterceptor" class="org.openmrs.module.atomfeed.advice.PatientProgramAdvice"/>
1920
</beans>

0 commit comments

Comments
 (0)