-
Notifications
You must be signed in to change notification settings - Fork 0
/
APIIntegrationReadJson.java
566 lines (466 loc) · 21.9 KB
/
APIIntegrationReadJson.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package org.hisp.dhis.scheduleapiintegration;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodService;
import org.hisp.dhis.period.PeriodType;
import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
/**
* @author Mithilesh Kumar Thakur
*/
public class APIIntegration implements Runnable
{
// -------------------------------------------------------------------------
// Dependencies
// -------------------------------------------------------------------------
@Autowired
private PeriodService periodService;
@Autowired
private JdbcTemplate jdbcTemplate;
public static final String KEY_TASK = "scheduleAPIIntegrationTask";
// -------------------------------------------------------------------------
// Properties
// -------------------------------------------------------------------------
private List<Period> selectedPeriodList = new ArrayList<>();
private Map<String, String> dataElementsMappingMap = new HashMap<String, String>();
private Map<String, String> dataElementsMappingLabMap = new HashMap<String, String>();
private Map<String, String> orgUnitMappingMap = new HashMap<String, String>();
private Map<String, String> orgUnitMappingLabMap = new HashMap<String, String>();
private List<String> dataValueList = new ArrayList<String>();
private List<String> dataValueListLab = new ArrayList<String>();
private int metaAttributeId = 64992555;
private int categoryOptionComboId = 15;
private int attributeoptioncomboid = 15;
private SimpleDateFormat simpleDateFormat;
String currentDate = "";
String currentMonth = "";
String currentYear = "";
String todayDate = "";
@Override
public void run()
{
System.out.println( " API Integration Scheduler Started at : " + new Date() );
initializeDataElementMap();
initializeOrgUnitMap();
dataValueList = new ArrayList<String>( );
simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Date date = new Date();
todayDate = simpleDateFormat.format( date );
currentDate = simpleDateFormat.format( date ).split( "-" )[2];
currentMonth = simpleDateFormat.format( date ).split( "-" )[1];
currentYear = simpleDateFormat.format( date ).split( "-" )[0];
//JSONObject json = readJsonFromUrl("http://182.156.208.43:85/faber_maharashtra/services/service_phd2");
try
{
// for bioMedical
dataValueList = new ArrayList<String>( readJsonFromUrl("http://182.156.208.43:85/faber_maharashtra/services/service_phd2") );
insertUpdateDataValue( dataValueList );
System.out.println( " API Integration Done for bioMedical -- " + dataValueList.size());
// for lab
String apiUrlForLab = "https://mahahindlabs.com/api/data_districtwise_patientdetails.php?month=" + currentMonth + "&year=" + currentYear;
String isoPeriod = currentYear+currentMonth;
Integer periodId = null;
if( isoPeriod != null )
{
Period period = PeriodType.getPeriodFromIsoString( isoPeriod );
period = periodService.reloadPeriod( period );
periodId = period.getId();
}
dataValueListLab = new ArrayList<String>( readJsonFromUrlLab( apiUrlForLab, periodId ) );
insertUpdateDataValue( dataValueListLab );
System.out.println( " API Integration Done for lab -- " + dataValueListLab.size() );
}
catch ( JSONException | IOException | ParseException e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println( " API Integration Scheduler Ended at : " + new Date() );
}
// read JsonFrom URL
public List<String> readJsonFromUrl(String url) throws IOException, JSONException, ParseException
{
dataValueList = new ArrayList<String>();
InputStream is = new URL(url).openStream();
try
{
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
//FileReader reader = new FileReader("employees.json");
Object obj = jsonParser.parse(rd);
JSONArray jsonResponseList = (JSONArray) obj;
//System.out.println(jsonResponseList);
System.out.println(jsonResponseList.size());
JSONArray outerArray = new JSONArray();
outerArray = jsonResponseList;
for (int i = 0; i < outerArray.size(); i++)
{
//System.out.println(outerArray.get(i));
JSONObject jobject = (JSONObject) outerArray.get(i);
String year = (String) jobject.get("Year");
//System.out.println("Year - " + year );
if( year.equalsIgnoreCase( "2018-19" ) || year.equalsIgnoreCase("2019-20" ) )
{
//System.out.println("Year - " + year );
String district = (String) jobject.get("District");
//System.out.println("District - " + district );
String mon = (String) jobject.get("month");
//System.out.println("month - " + mon );
int periodId = getPeriodId( year, mon );
String orgUnitId = orgUnitMappingMap.get( district );
for ( Map.Entry<String,String> de : dataElementsMappingMap.entrySet() )
{
if( de.getKey().equalsIgnoreCase( "Open_Calls" ))
{
String deId = de.getValue();
String openCalls = (String) jobject.get("Open_Calls");
//System.out.println("Open_Calls - " + openCalls );
dataValueList.add( deId + ":" + periodId + ":" + orgUnitId + ":" + openCalls );
}
if( de.getKey().equalsIgnoreCase( "Closed_Calls" ))
{
String deId = de.getValue();
String closedCalls = (String) jobject.get("Closed_Calls");
//System.out.println("Closed_Calls - " + closedCalls );
dataValueList.add( deId + ":" + periodId + ":" + orgUnitId + ":" + closedCalls );
}
if( de.getKey().equalsIgnoreCase( "Total_Calls" ))
{
String deId = de.getValue();
String totalCalls = (String) jobject.get("Total_Calls");
//System.out.println("Total_Calls - " + totalCalls );
dataValueList.add( deId + ":" + periodId + ":" + orgUnitId + ":" + totalCalls );
}
//System.out.println("Key = " + de.getKey() + ", Value = " + de.getValue());
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
return dataValueList;
}
private String readAll(Reader rd) throws IOException
{
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1)
{
sb.append((char) cp);
}
return sb.toString();
}
private void jsonResponseList(JSONObject employee)
{
//Get employee object within list
JSONObject employeeObject = (JSONObject) employee.get("employee");
//Get employee first name
String firstName = (String) employeeObject.get("firstName");
System.out.println(firstName);
//Get employee last name
String lastName = (String) employeeObject.get("lastName");
System.out.println(lastName);
//Get employee website name
String website = (String) employeeObject.get("website");
System.out.println(website);
}
private Integer getPeriodId( String financialYear, String month)
{
Period period = new Period();
Integer periodId = null;
String isoPeriod = null;
String startYear = financialYear.split("-")[0];
String endYear = financialYear.split("-")[0].substring(0, 2) + financialYear.split("-")[1];
if ( month.equalsIgnoreCase( "April" ) )
{
isoPeriod = startYear+"04";
}
else if ( month.equalsIgnoreCase( "May" ) )
{
isoPeriod = startYear+"05";
}
else if ( month.equalsIgnoreCase( "June" ) )
{
isoPeriod = startYear+"06";
}
else if ( month.equalsIgnoreCase("July" ) )
{
isoPeriod = startYear+"07";
}
else if ( month.equalsIgnoreCase( "August" ) )
{
isoPeriod = startYear+"08";
}
else if ( month.equalsIgnoreCase( "September" ) )
{
isoPeriod = startYear+"09";
}
else if ( month.equalsIgnoreCase( "October" ) )
{
isoPeriod = startYear+"10";
}
else if ( month.equalsIgnoreCase( "November" ) )
{
isoPeriod = startYear+"11";
}
else if ( month.equalsIgnoreCase( "December" ) )
{
isoPeriod = startYear+"12";
}
else if ( month.equalsIgnoreCase( "January" ) )
{
isoPeriod = endYear+"01";
}
else if ( month.equalsIgnoreCase( "February" ))
{
isoPeriod = endYear+"02";
}
else if ( month.equalsIgnoreCase( "March" ) )
{
isoPeriod = endYear+"03";
}
if( isoPeriod != null )
{
period = PeriodType.getPeriodFromIsoString( isoPeriod );
period = periodService.reloadPeriod( period );
periodId = period.getId();
}
return periodId;
}
public void initializeDataElementMap()
{
dataElementsMappingMap = new HashMap<String, String>();
dataElementsMappingMap.put( "Open_Calls", "64982715" );
dataElementsMappingMap.put( "Closed_Calls", "64915059" );
dataElementsMappingMap.put( "Total_Calls", "64915030" );
dataElementsMappingLabMap = new HashMap<String, String>();
dataElementsMappingLabMap.put( "TotalPatients", "-1" );
dataElementsMappingLabMap.put( "TATMET", "-2" );
dataElementsMappingLabMap.put( "TATFAIL", "-3" );
}
public void initializeOrgUnitMap()
{
orgUnitMappingMap = new HashMap<String, String>();
try
{
String query = " SELECT attrValue.value, orgUnitAttrValue.organisationunitid from attributevalue attrValue " +
" INNER JOIN organisationunitattributevalues orgUnitAttrValue ON orgUnitAttrValue.attributevalueid = attrValue.attributevalueid " +
" WHERE attrValue.attributeid = " + metaAttributeId +" ORDER BY attrValue.value ";
//System.out.println( "query = " + query );
SqlRowSet rs = jdbcTemplate.queryForRowSet( query );
while ( rs.next() )
{
String orgUnitName = rs.getString( 1 );
String orgUnitId = rs.getString( 2 );
if( orgUnitName != null && orgUnitId != null )
{
orgUnitMappingMap.put( orgUnitName, orgUnitId );
}
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
public void initializeOrgUnitLabMap()
{
orgUnitMappingLabMap = new HashMap<String, String>();
try
{
String query = " SELECT attrValue.value, orgUnitAttrValue.organisationunitid from attributevalue attrValue " +
" INNER JOIN organisationunitattributevalues orgUnitAttrValue ON orgUnitAttrValue.attributevalueid = attrValue.attributevalueid " +
" WHERE attrValue.attributeid = " + metaAttributeId +" ORDER BY attrValue.value ";
//System.out.println( "query = " + query );
SqlRowSet rs = jdbcTemplate.queryForRowSet( query );
while ( rs.next() )
{
String orgUnitName = rs.getString( 1 );
String orgUnitId = rs.getString( 2 );
if( orgUnitName != null && orgUnitId != null )
{
orgUnitMappingLabMap.put( orgUnitName, orgUnitId );
}
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
// read JSON form lab URL
public List<String> readJsonFromUrlLab( String url, Integer periodId ) throws IOException, JSONException, ParseException
{
//System.out.println("API for patient -- " + url );
dataValueListLab = new ArrayList<String>();
InputStream is = new URL(url).openStream();
if( periodId != null )
{
try
{
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
JSONObject obj = (JSONObject) jsonParser.parse(rd);
//System.out.println(obj.size());
JSONArray results = (JSONArray) obj.get( "result" );
for (int i = 0; i < results.size(); i++)
{
JSONObject jobject = (JSONObject) results.get(i);
String districtName = (String) jobject.get("DISTNAME");
String orgUnitId = orgUnitMappingLabMap.get( districtName );
for ( Map.Entry<String,String> de : dataElementsMappingLabMap.entrySet() )
{
if( de.getKey().equalsIgnoreCase( "TotalPatients" ))
{
String deId = de.getValue();
String totalPatients = (String) jobject.get("TotalPatients");
//System.out.println("Open_Calls - " + openCalls );
dataValueListLab.add( deId + ":" + periodId + ":" + orgUnitId + ":" + totalPatients );
}
if( de.getKey().equalsIgnoreCase( "TATMET" ))
{
String deId = de.getValue();
String tATMET = (String) jobject.get("TATMET");
//System.out.println("Closed_Calls - " + closedCalls );
dataValueListLab.add( deId + ":" + periodId + ":" + orgUnitId + ":" + tATMET );
}
if( de.getKey().equalsIgnoreCase( "TATFAIL" ))
{
String deId = de.getValue();
String tATFAIL = (String) jobject.get("TATFAIL");
//System.out.println("Total_Calls - " + totalCalls );
dataValueListLab.add( deId + ":" + periodId + ":" + orgUnitId + ":" + tATFAIL );
}
//System.out.println("Key = " + de.getKey() + ", Value = " + de.getValue());
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
}
return dataValueListLab;
}
// insert dataValue
public void insertUpdateDataValue( List<String> dataValueList )
{
String importStatus = "";
Integer updateCount = 0;
Integer insertCount = 0;
System.out.println(" DataValue List Size - " + dataValueList.size() );
if( dataValueList != null && dataValueList.size() > 0 )
{
String storedBy = "admin";
int count = 1;
int slNo = 1;
long t;
Date d = new Date();
t = d.getTime();
java.sql.Date created = new java.sql.Date( t );
java.sql.Date lastUpdatedDate = new java.sql.Date( t );
String query = "";
int insertFlag = 1;
String insertQuery = "INSERT INTO datavalue ( dataelementid, periodid, sourceid, categoryoptioncomboid, attributeoptioncomboid, value, storedby, created, lastupdated, deleted ) VALUES ";
try
{
for( String combinedString : dataValueList )
{
String dataElementId = combinedString.split( ":" )[0];
String periodId = combinedString.split( ":" )[1];
String sourceId = combinedString.split( ":" )[2];
String value = combinedString.split( ":" )[3];
//System.out.println( slNo + " -- update Query - " + combinedString );
query = "SELECT value FROM datavalue WHERE dataelementid = " + dataElementId + " AND categoryoptioncomboid = " + categoryOptionComboId + " AND attributeoptioncomboid = " + attributeoptioncomboid + " AND periodid = " + periodId + " AND sourceid = " + sourceId;
SqlRowSet sqlResultSet1 = jdbcTemplate.queryForRowSet( query );
if ( sqlResultSet1 != null && sqlResultSet1.next() )
{
String updateQuery = "UPDATE datavalue SET value = '" + value + "', storedby = '" + storedBy + "',lastupdated='" + lastUpdatedDate + "' WHERE dataelementid = " + dataElementId + " AND periodid = "
+ periodId + " AND sourceid = " + sourceId + " AND categoryoptioncomboid = " + categoryOptionComboId + " AND attributeoptioncomboid = " + attributeoptioncomboid;
jdbcTemplate.update( updateQuery );
//System.out.println(" update Query - " + updateQuery );
updateCount++;
}
else
{
if ( value != null && !value.trim().equals( "" ) )
{
insertQuery += "( " + dataElementId + ", " + periodId + ", " + sourceId + ", " + categoryOptionComboId + ", " + attributeoptioncomboid + ", '" + value + "', '" + storedBy + "', '" + created + "', '" + lastUpdatedDate + "', false ), ";
insertFlag = 2;
insertCount++;
}
}
if ( count == 1000 )
{
count = 1;
if ( insertFlag != 1 )
{
insertQuery = insertQuery.substring( 0, insertQuery.length() - 2 );
System.out.println( " insert Query 2 - " );
jdbcTemplate.update( insertQuery );
}
insertFlag = 1;
insertQuery = "INSERT INTO datavalue ( dataelementid, periodid, sourceid, categoryoptioncomboid, attributeoptioncomboid, value, storedby, created, lastupdated, deleted ) VALUES ";
}
count++;
slNo++;
}
System.out.println(" Count - " + count + " -- Insert Count : " + insertCount + " Update Count -- " + updateCount );
if ( insertFlag != 1 )
{
insertQuery = insertQuery.substring( 0, insertQuery.length() - 2 );
System.out.println(" insert Query 1 - ");
jdbcTemplate.update( insertQuery );
}
importStatus = "Successfully populated aggregated data : ";
importStatus += "<br/> Total new records : " + insertCount;
importStatus += "<br/> Total updated records : " + updateCount;
//System.out.println( importStatus );
}
catch ( Exception e )
{
importStatus = "Exception occured while import, please check log for more details" + e.getMessage();
}
}
System.out.println("Insert Count : " + insertCount + " Update Count -- " + updateCount);
}
}