21
21
import org .ict4h .atomfeed .server .service .EventServiceImpl ;
22
22
import org .ict4h .atomfeed .transaction .AFTransactionWorkWithoutResult ;
23
23
import org .joda .time .DateTime ;
24
+ import org .openmrs .DrugOrder ;
24
25
import org .openmrs .Order ;
25
26
import org .openmrs .User ;
26
27
import org .openmrs .api .context .Context ;
@@ -52,7 +53,13 @@ public DrugOrderAdvice() throws SQLException {
52
53
}
53
54
54
55
@ Override
55
- public void afterReturning (Object returnValue , Method method , Object [] arguments , Object target ) throws Throwable {
56
+ public void afterReturning (Object returnValue , Method method , Object [] args , Object target ) throws Throwable {
57
+ Order o = args .length >0 &&args [0 ] instanceof Order ?(Order )args [0 ]:null ;
58
+
59
+ if (isDrugOrder == false ){// checking for drug order again; it doesnot get populated earlier
60
+ isDrugOrder = isDrugOrder (o );
61
+ }
62
+
56
63
if (method .getName ().equals (SAVE_METHOD ) && isDrugOrder ) {
57
64
logDebug ("In method SAVE_DRUG_ORDER for atomfeed with isNewEntry=" +isNewEntry );
58
65
@@ -66,26 +73,48 @@ public void afterReturning(Object returnValue, Method method, Object[] arguments
66
73
67
74
if (isNewEntry == null ) {
68
75
logWarn ("DRUG_ORDER: Can not determine the transaction type. Data syncer Service should handle Save and Update on its own" );
69
- process (( Order ) returnValue , "Order" , formatDataEntrySource (u ));
76
+ process (o , "Order" , formatDataEntrySource (u ));
70
77
}
71
78
// if new entry and not sent from SRP i.e. skip entry sent via opensrp
72
79
else if (isNewEntry ){
73
80
logDebug ("DRUG_ORDER: Insert found. Creating drug_order_save atomfeed for " +u );
74
- process (( Order ) returnValue , "DrugOrder_Save" , formatDataEntrySource (u ));
81
+ process (o , "DrugOrder_Save" , formatDataEntrySource (u ));
75
82
}
76
83
// if not a new entry and no role for data edit found ignorable
77
84
else if (isNewEntry == false ){
78
85
logInfo ("ORDER: Creating atomfeed for order_update for " +u );
79
- process (( Order ) returnValue , "DrugOrder_Update" , formatDataEntrySource (u ));
86
+ process (o , "DrugOrder_Update" , formatDataEntrySource (u ));
80
87
}
81
88
}
82
89
}
83
90
91
+ private boolean isDrugOrder (Order o ){
92
+ boolean drugOrder = false ;
93
+ if (o != null ){
94
+ try {
95
+ if (o instanceof DrugOrder ){
96
+ drugOrder = true ;
97
+ }
98
+ else if (o .getOrderType () != null
99
+ && (o .getOrderType ().getName ().equalsIgnoreCase ("drugorder" )
100
+ || o .getOrderType ().getName ().equalsIgnoreCase ("drug order" ))){
101
+ drugOrder = true ;
102
+ }
103
+ }
104
+ catch (Exception e ) {
105
+ e .printStackTrace ();
106
+ }
107
+ }
108
+ return drugOrder ;
109
+ }
110
+
84
111
@ Override
85
112
public void before (Method method , Object [] args , Object target ) throws Throwable {
86
113
Order o = args .length >0 &&args [0 ] instanceof Order ?(Order )args [0 ]:null ;
87
- isDrugOrder = o != null && ((Order ) o ).getOrderType ().getName ().equalsIgnoreCase ("drugorder" );
88
- if (method .getName ().equals (SAVE_METHOD ) && isDrugOrder ) {
114
+
115
+ isDrugOrder = isDrugOrder (o );
116
+
117
+ if (method .getName ().equals (SAVE_METHOD )) {
89
118
logDebug ("In method SAVE_DRUG_ORDER for atomfeed before advice" );
90
119
91
120
if (o != null ){
@@ -103,6 +132,9 @@ public void before(Method method, Object[] args, Object target) throws Throwable
103
132
}
104
133
105
134
private void process (Order order , String title , final String dataEnrySource ) {
135
+ if (order == null ){
136
+ return ;
137
+ }
106
138
String contents = String .format (TEMPLATE , order .getUuid ());
107
139
final Event event = new Event (UUID .randomUUID ().toString (), title , DateTime .now (), (URI ) null , contents , CATEGORY );
108
140
atomFeedSpringTransactionManager .executeWithTransaction (
0 commit comments