forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLOVDownloader.cpp
1096 lines (931 loc) · 35.3 KB
/
LOVDownloader.cpp
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QStandardPaths>
#include <QDir>
#include <QSqlQuery>
#include <QTimer>
#include <QNetworkReply>
#include <QSqlError>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QApplication>
#include <QRegularExpression>
#include "LogParam.h"
#include "LOVDownloader.h"
#include "debug.h"
MODULE_IDENTIFICATION("qlog.core.lovdownloader");
LOVDownloader::LOVDownloader(QObject *parent) :
QObject(parent),
currentReply(nullptr),
abortRequested(false),
/* https://stackoverflow.com/questions/18144431/regex-to-split-a-csv */
CSVRe("(?:^|,)(?=[^\"]|(\")?)\"?((?(1)(?:[^\"]|\"\")*|[^,\"]*))\"?(?=,|$)"),
CTYPrefixSeperatorRe("[\\s;]"),
CTYPrefixFormatRe("(=?)([A-Z0-9/]+)(?:\\((\\d+)\\))?(?:\\[(\\d+)\\])?$")
{
FCT_IDENTIFICATION;
nam = new QNetworkAccessManager(this);
connect(nam, &QNetworkAccessManager::finished,
this, &LOVDownloader::processReply);
}
LOVDownloader::~LOVDownloader()
{
FCT_IDENTIFICATION;
if ( currentReply )
{
currentReply->abort();
currentReply->deleteLater();
}
nam->deleteLater();
}
void LOVDownloader::update(const SourceType & sourceType)
{
FCT_IDENTIFICATION;
abortRequested = false;
const SourceDefinition &sourceDef = sourceMapping[sourceType];
Q_ASSERT(sourceDef.type == sourceType);
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
const QDate &last_update = LogParam::getLOVaParam(sourceDef.lastTimeConfigName);
if ( dir.exists(sourceDef.fileName)
&& last_update.isValid()
&& last_update.daysTo(QDate::currentDate()) < sourceDef.ageTime )
{
if ( isTableFilled(sourceDef.tableName) )
{
// nothing to do.
qCDebug(runtime) << "Not needed to update " << sourceDef.fileName;
emit noUpdate();
return;
}
qCDebug(runtime) << "using cached " << sourceDef.fileName << " at" << dir.path();
QTimer::singleShot(0, this, [this, sourceDef]() {loadData(sourceDef);});
}
else
{
qCDebug(runtime) << sourceDef.fileName << " is too old or not exist - downloading";
download(sourceDef);
}
}
void LOVDownloader::abortRequest()
{
FCT_IDENTIFICATION;
if ( currentReply )
{
currentReply->abort();
currentReply = nullptr;
}
abortRequested = true;
}
void LOVDownloader::loadData(const LOVDownloader::SourceDefinition &sourceDef)
{
FCT_IDENTIFICATION;
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation));
QFile file(dir.filePath(sourceDef.fileName));
emit processingSize(file.size());
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
parseData(sourceDef, stream);
file.close();
emit finished(true);
}
bool LOVDownloader::isTableFilled(const QString &tableName)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << tableName;
QSqlQuery query(QString("select exists( select 1 from %1)").arg(tableName));
int i = query.first() ? query.value(0).toInt() : 0;
qCDebug(runtime) << i;
return i==1;
}
bool LOVDownloader::deleteTable(const QString &tableName)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << tableName;
QSqlQuery query;
QString queryStatement("delete from %1");
if ( ! query.exec(queryStatement.arg(tableName)) )
{
qWarning() << "Cannot delete " << tableName << query.lastError();
return false;
}
return true;
}
void LOVDownloader::download(const LOVDownloader::SourceDefinition &sourceDef)
{
FCT_IDENTIFICATION;
QUrl url(sourceDef.URL);
QNetworkRequest request(url);
QString rheader = QString("QLog/%1").arg(VERSION);
request.setRawHeader("User-Agent", rheader.toUtf8());
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->get(request);
currentReply->setProperty("sourceType", sourceDef.type);
qCDebug(runtime) << "Downloading " << sourceDef.fileName << "from " << url.toString();
}
void LOVDownloader::parseData(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
qCDebug(runtime) << "Parsing file " << sourceDef.fileName;
switch ( sourceDef.type )
{
case CTY:
parseCTY(sourceDef, data);
break;
case SATLIST:
parseSATLIST(sourceDef, data);
break;
case SOTASUMMITS:
parseSOTASummits(sourceDef, data);
break;
case WWFFDIRECTORY:
parseWWFFDirectory(sourceDef, data);
break;
case IOTALIST:
parseIOTA(sourceDef, data);
break;
case POTADIRECTORY:
parsePOTA(sourceDef, data);
break;
case MEMBERSHIPCONTENTLIST:
parseMembershipContent(sourceDef, data);
break;
default:
qWarning() << "Unssorted type to download" << sourceDef.type << sourceDef.fileName;
}
}
void LOVDownloader::parseCTY(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QRegularExpressionMatch matchExp;
QSqlDatabase::database().transaction();
if ( ! deleteTable("dxcc_prefixes") )
{
qCWarning(runtime) << "dxcc_prefixes delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << sourceDef.tableName << " delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
QSqlQuery insertEntityQuery;
if ( ! insertEntityQuery.prepare("INSERT INTO dxcc_entities (id,"
" name,"
" prefix,"
" cont,"
" cqz,"
" ituz,"
" lat,"
" lon,"
" tz) "
" VALUES ( :id,"
" :name,"
" :prefix,"
" :cont,"
" :cqz,"
" :ituz,"
" :lat,"
" :lon,"
" :tz)") )
{
qWarning() << "cannot prepare Insert statement - Entity";
abortRequested = true;
}
QSqlQuery insertPrefixesQuery;
if ( ! insertPrefixesQuery.prepare("INSERT INTO dxcc_prefixes ("
" prefix,"
" exact,"
" dxcc,"
" cqz,"
" ituz) "
" VALUES ( :prefix,"
" :exact,"
" :dxcc,"
" :cqz,"
" :ituz)") )
{
qWarning() << "cannot prepare Insert statement - Prefixes";
abortRequested = true;
}
unsigned int count = 0;
while ( !data.atEnd() && !abortRequested )
{
const QString &line = data.readLine();
const QStringList &fields = line.split(',');
if ( fields.count() != 10 )
{
qCDebug(runtime) << "Invalid line in the input file " << line;
continue;
}
else if ( fields.at(0).startsWith("*") )
continue;
qCDebug(runtime) << fields;
int dxcc_id = fields.at(2).toInt();
insertEntityQuery.bindValue(":id", dxcc_id);
insertEntityQuery.bindValue(":prefix", fields.at(0));
insertEntityQuery.bindValue(":name", fields.at(1));
insertEntityQuery.bindValue(":cont", fields.at(3));
insertEntityQuery.bindValue(":cqz", fields.at(4));
insertEntityQuery.bindValue(":ituz", fields.at(5));
insertEntityQuery.bindValue(":lat", fields.at(6).toFloat());
insertEntityQuery.bindValue(":lon", -fields.at(7).toFloat());
insertEntityQuery.bindValue(":tz", fields.at(8).toFloat());
if ( ! insertEntityQuery.exec() )
{
qWarning() << "DXCC Entity insert error "
<< insertEntityQuery.lastError().text()
<< insertEntityQuery.lastQuery();
qCDebug(runtime) << fields;
abortRequested = true;
continue;
}
else
count++;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
const QStringList &prefixList = fields.at(9).split(CTYPrefixSeperatorRe, Qt::SkipEmptyParts);
#else /* Due to ubuntu 20.04 where qt5.12 is present */
const QStringList &prefixList = fields.at(9).split(CTYPrefixSeperatorRe, QString::SkipEmptyParts);
#endif
qCDebug(runtime) << prefixList;
QStringList dup;
for ( auto &prefix : prefixList )
{
matchExp = CTYPrefixFormatRe.match(prefix);
if ( matchExp.hasMatch() )
{
// removing duplicities in CTY file.
const QString &pfx = matchExp.captured(2);
if ( !dup.contains(pfx) )
{
dup << pfx;
insertPrefixesQuery.bindValue(":prefix", pfx);
insertPrefixesQuery.bindValue(":exact", !matchExp.captured(1).isEmpty());
insertPrefixesQuery.bindValue(":dxcc", dxcc_id);
insertPrefixesQuery.bindValue(":cqz", matchExp.captured(3).toInt());
insertPrefixesQuery.bindValue(":ituz", matchExp.captured(4).toInt());
if ( ! insertPrefixesQuery.exec() )
{
qWarning() << "DXCC Prefix insert error "
<< insertPrefixesQuery.lastError().text()
<< insertPrefixesQuery.lastQuery();
qCDebug(runtime) << prefix << prefixList;
abortRequested = true;
}
}
else
qCDebug(runtime) << "Removing non-unique prefix" << pfx;
}
else
qCDebug(runtime) << "Failed to match " << prefix;
}
if ( count% 20 == 0 )
{
emit progress(data.pos());
QCoreApplication::processEvents();
}
emit progress(data.pos());
QCoreApplication::processEvents();
}
if ( !abortRequested )
{
qCDebug(runtime) << "DXCC update finished:" << count << "entities loaded.";
QSqlDatabase::database().commit();
}
else
{
//can be a result of abort
qCWarning(runtime) << "DXCC update failed - rollback";
QSqlDatabase::database().rollback();
}
}
void LOVDownloader::parseSATLIST(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QSqlDatabase::database().transaction();
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << "Satlist delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
QSqlTableModel entityTableModel;
entityTableModel.setTable(sourceDef.tableName);
entityTableModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
QSqlRecord entityRecord = entityTableModel.record();
int count = 0;
while ( !data.atEnd() && !abortRequested )
{
QString line = data.readLine();
QStringList fields = line.split(';');
if ( fields.count() != 8 )
{
qCDebug(runtime) << "Invalid line in the input file " << line;
continue;
}
qCDebug(runtime) << fields;
entityRecord.clearValues();
entityRecord.setValue("name", fields.at(0));
entityRecord.setValue("number", fields.at(1));
entityRecord.setValue("uplink", fields.at(2));
entityRecord.setValue("downlink", fields.at(3));
entityRecord.setValue("beacon", fields.at(4));
entityRecord.setValue("mode", fields.at(5));
entityRecord.setValue("callsign", fields.at(6));
entityRecord.setValue("status", fields.at(7));
if ( !entityTableModel.insertRecord(-1, entityRecord) )
{
qWarning() << "Cannot insert a record to SATList Table - " << entityTableModel.lastError();
qCDebug(runtime) << entityRecord;
}
else
{
count++;
}
emit progress(data.pos());
QCoreApplication::processEvents();
}
if ( entityTableModel.submitAll()
&& !abortRequested )
{
QSqlDatabase::database().commit();
qCDebug(runtime) << "Satlist update finished:" << count << "entities loaded.";
}
else
{
//can be a result of abort
qCWarning(runtime) << "Satlist update failed - rollback" << entityTableModel.lastError();
QSqlDatabase::database().rollback();
}
}
void LOVDownloader::parseSOTASummits(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QSqlDatabase::database().transaction();
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << "SOTA Summits delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
int count = 0;
QSqlQuery insertQuery;
if ( ! insertQuery.prepare("INSERT INTO sota_summits(summit_code,"
" association_name,"
" region_name,"
" summit_name,"
" altm,"
" altft,"
" gridref1,"
" gridref2,"
" longitude,"
" latitude,"
" points,"
" bonus_points,"
" valid_from,"
" valid_to) "
" VALUES ( ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?)") )
{
qWarning() << "cannot prepare Insert statement";
abortRequested = true;
}
QVariantList sota_summit_code;
QVariantList sota_association_name;
QVariantList sota_region_name;
QVariantList sota_summit_name;
QVariantList sota_altm;
QVariantList sota_altft;
QVariantList sota_gridref1;
QVariantList sota_gridref2;
QVariantList sota_longitude;
QVariantList sota_latitude;
QVariantList sota_points;
QVariantList sota_bonus_points;
QVariantList sota_valid_from;
QVariantList sota_valid_to;
while ( !data.atEnd() && !abortRequested )
{
QString line = data.readLine();
if ( count == 0 || count ==1 )
{
QString checkingString = (count == 0 ) ?
"SOTA Summits List" :
"SummitCode,AssociationName,RegionName,"
"SummitName,AltM,AltFt,GridRef1,GridRef2,"
"Longitude,Latitude,Points,BonusPoints,"
"ValidFrom,ValidTo,ActivationCount,"
"ActivationDate,ActivationCall";
//read the first line
if ( !line.contains(checkingString) )
{
qCDebug(runtime) << line;
qWarning() << "Unexpected header for SOTA Summit CSV file - aborting";
abortRequested = true;
}
count++;
continue;
}
QRegularExpressionMatchIterator i = CSVRe.globalMatch(line);
QStringList fields;
while ( i.hasNext() )
{
QRegularExpressionMatch match = i.next();
fields << match.captured(2);
}
if ( fields.size() >= 14 )
{
qCDebug(runtime) << fields;
sota_summit_code << fields.at(0);
sota_association_name << fields.at(1);
sota_region_name << fields.at(2);
sota_summit_name << fields.at(3);
sota_altm << fields.at(4);
sota_altft << fields.at(5);
sota_gridref1 << fields.at(6);
sota_gridref2 << fields.at(7);
sota_longitude << fields.at(8);
sota_latitude << fields.at(9);
sota_points << fields.at(10);
sota_bonus_points << fields.at(11);
sota_valid_from << fields.at(12);
sota_valid_to << fields.at(13);
if ( count%10000 == 0 )
{
emit progress(data.pos());
QCoreApplication::processEvents();
}
}
else
{
qCDebug(runtime) << "Invalid line in the input file " << line;
}
count++;
}
insertQuery.addBindValue(sota_summit_code);
insertQuery.addBindValue(sota_association_name);
insertQuery.addBindValue(sota_region_name);
insertQuery.addBindValue(sota_summit_name);
insertQuery.addBindValue(sota_altm);
insertQuery.addBindValue(sota_altft);
insertQuery.addBindValue(sota_gridref1);
insertQuery.addBindValue(sota_gridref2);
insertQuery.addBindValue(sota_longitude);
insertQuery.addBindValue(sota_latitude);
insertQuery.addBindValue(sota_points);
insertQuery.addBindValue(sota_bonus_points);
insertQuery.addBindValue(sota_valid_from);
insertQuery.addBindValue(sota_valid_to);
if ( ! insertQuery.execBatch() )
{
qInfo() << "SOTA Summit insert error " << insertQuery.lastError().text() << insertQuery.lastQuery();
abortRequested = true;
}
if ( !abortRequested )
{
QSqlDatabase::database().commit();
qCDebug(runtime) << "SOTA Summits update finished:" << count << "entities loaded.";
}
else
{
qCWarning(runtime) << "SOTA Summits update failed - rollback";
QSqlDatabase::database().rollback();
}
}
void LOVDownloader::parseWWFFDirectory(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QSqlDatabase::database().transaction();
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << "WWFT Directory delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
int count = 0;
QSqlQuery insertQuery;
if ( ! insertQuery.prepare("INSERT INTO wwff_directory(reference,"
" status,"
" name,"
" program,"
" dxcc,"
" state,"
" county,"
" continent,"
" iota,"
" iaruLocator,"
" latitude,"
" longitude,"
" iucncat,"
" valid_from,"
" valid_to) "
" VALUES ( ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?) ") )
{
qWarning() << "cannot prepare Insert statement";
abortRequested = true;
}
QVariantList wwff_reference;
QVariantList wwff_status;
QVariantList wwff_name;
QVariantList wwff_program;
QVariantList wwff_dxcc;
QVariantList wwff_state;
QVariantList wwff_county;
QVariantList wwff_continent;
QVariantList wwff_iota;
QVariantList wwff_iaruLocator;
QVariantList wwff_latitude;
QVariantList wwff_longitude;
QVariantList wwff_iucncat;
QVariantList wwff_valid_from;
QVariantList wwff_valid_to;
while ( !data.atEnd() && !abortRequested )
{
QString line = data.readLine();
if ( count == 0 )
{
QString checkingString = "reference,status,"
"name,program,dxcc,state,"
"county,continent,iota,"
"iaruLocator,latitude,"
"longitude,IUCNcat,validFrom,"
"validTo,notes,lastMod,changeLog,"
"reviewFlag,specialFlags,website,"
"country,region";
//read the first line
if ( !line.contains(checkingString) )
{
qCDebug(runtime) << line;
qWarning() << "Unexpected header for WWFF Directory CSV file - aborting";
abortRequested = true;
}
count++;
continue;
}
QRegularExpressionMatchIterator i = CSVRe.globalMatch(line);
QStringList fields;
while ( i.hasNext() )
{
QRegularExpressionMatch match = i.next();
fields << match.captured(2);
}
if ( fields.size() >= 15 )
{
qCDebug(runtime) << fields;
wwff_reference << fields.at(0);
wwff_status << fields.at(1);
wwff_name << fields.at(2);
wwff_program << fields.at(3);
wwff_dxcc << fields.at(4);
wwff_state << fields.at(5);
wwff_county << fields.at(6);
wwff_continent << fields.at(7);
wwff_iota << fields.at(8);
wwff_iaruLocator << fields.at(9);
wwff_latitude << fields.at(10);
wwff_longitude << fields.at(11);
wwff_iucncat << fields.at(12);
wwff_valid_from << fields.at(13);
wwff_valid_to << fields.at(14);
if ( count%10000 == 0 )
{
emit progress(data.pos());
QCoreApplication::processEvents();
}
}
else
{
qCDebug(runtime) << "Invalid line in the input file " << line;
}
count++;
}
insertQuery.addBindValue(wwff_reference);
insertQuery.addBindValue(wwff_status);
insertQuery.addBindValue(wwff_name);
insertQuery.addBindValue(wwff_program);
insertQuery.addBindValue(wwff_dxcc);
insertQuery.addBindValue(wwff_state);
insertQuery.addBindValue(wwff_county);
insertQuery.addBindValue(wwff_continent);
insertQuery.addBindValue(wwff_iota);
insertQuery.addBindValue(wwff_iaruLocator);
insertQuery.addBindValue(wwff_latitude);
insertQuery.addBindValue(wwff_longitude);
insertQuery.addBindValue(wwff_iucncat);
insertQuery.addBindValue(wwff_valid_from);
insertQuery.addBindValue( wwff_valid_to);
if ( ! insertQuery.execBatch() )
{
qInfo() << "WWFT Directory insert error " << insertQuery.lastError().text() << insertQuery.lastQuery();
abortRequested = true;
}
if ( !abortRequested )
{
QSqlDatabase::database().commit();
qCDebug(runtime) << "WWFT Directory update finished:" << count << "entities loaded.";
}
else
{
qCWarning(runtime) << "WWFT Directory update failed - rollback";
QSqlDatabase::database().rollback();
}
}
void LOVDownloader::parseIOTA(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QSqlQuery insertQuery;
if ( ! insertQuery.prepare("INSERT INTO IOTA(iotaid,"
" islandname)"
" VALUES (?, ?)") )
{
qWarning() << "cannot prepare Insert statement";
abortRequested = true;
return;
}
QSqlDatabase::database().transaction();
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << "IOTA List delete failed - rollback";
abortRequested = true;
QSqlDatabase::database().rollback();
return;
}
unsigned int count = 0;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data.readAll().toUtf8());
if ( !jsonDoc.isArray() )
{
qCDebug(runtime) << jsonDoc;
qWarning() << "Unexpected IOTA JSON - aborting";
abortRequested = true;
}
else
{
QVariantList iota_id;
QVariantList iota_islandname;
const QJsonArray &jsonArray = jsonDoc.array();
for ( const QJsonValue &value : jsonArray )
{
if ( !value.isObject() ) continue;
const QJsonObject &obj = value.toObject();
const QJsonValue &refno = obj.value("refno");
const QJsonValue &name = obj.value("name");
qCDebug(runtime) << "IOTA Record" << refno << name;
iota_id << refno;
iota_islandname << name;
if ( count%500 == 0 )
{
emit progress(data.pos());
QCoreApplication::processEvents();
}
count++;
}
insertQuery.addBindValue(iota_id);
insertQuery.addBindValue(iota_islandname);
if ( ! insertQuery.execBatch() )
{
qInfo() << "IOTA Directory insert error " << insertQuery.lastError().text() << insertQuery.lastQuery();
abortRequested = true;
}
}
if ( !abortRequested )
{
QSqlDatabase::database().commit();
qCDebug(runtime) << "IOTA update finished:" << count << "entities loaded.";
}
else
{
qCWarning(runtime) << "IOTA update failed - rollback";
QSqlDatabase::database().rollback();
}
}
void LOVDownloader::parsePOTA(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QSqlDatabase::database().transaction();
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << "POTA List delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
int count = 0;
QSqlQuery insertQuery;
if ( ! insertQuery.prepare("INSERT INTO POTA_DIRECTORY(reference,"
" name,"
" active,"
" entityID,"
" locationDesc,"
" latitude,"
" longitude,"
" grid"
")"
" VALUES (?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?,"
" ?"
")") )
{
qWarning() << "cannot prepare Insert statement";
abortRequested = true;
}
QVariantList pota_reference;
QVariantList pota_name;
QVariantList pota_active;
QVariantList pota_entityID;
QVariantList pota_locationDesc;
QVariantList pota_latitude;
QVariantList pota_longitude;
QVariantList pota_grid;
while ( !data.atEnd() && !abortRequested )
{
QString line = data.readLine();
if ( count == 0 )
{
QString checkingString = "\"reference\",\"name\",\"active\",\"entityId\",\"locationDesc\",\"latitude\",\"longitude\",\"grid\"";
//read the first line
if ( !line.contains(checkingString) )
{
qCDebug(runtime) << line;
qWarning() << "Unexpected header for POTA CSV file - aborting";
abortRequested = true;
}
count++;
continue;
}
QRegularExpressionMatchIterator i = CSVRe.globalMatch(line);
QStringList fields;
while ( i.hasNext() )
{
QRegularExpressionMatch match = i.next();
fields << match.captured(2);
}
if ( fields.size() >= 8 )
{
qCDebug(runtime) << fields;
pota_reference << fields.at(0);
pota_name << fields.at(1);
pota_active << fields.at(2);
pota_entityID << fields.at(3);
pota_locationDesc << fields.at(4);
pota_latitude << fields.at(5);
pota_longitude << fields.at(6);
pota_grid << fields.at(7);
if ( count%3000 == 0 )
{
emit progress(data.pos());
QCoreApplication::processEvents();
}
}
else
{
qCDebug(runtime) << "Invalid line in the input file " << line;
}
count++;
}
insertQuery.addBindValue(pota_reference);
insertQuery.addBindValue(pota_name);
insertQuery.addBindValue(pota_active);
insertQuery.addBindValue(pota_entityID);
insertQuery.addBindValue(pota_locationDesc);
insertQuery.addBindValue(pota_latitude);
insertQuery.addBindValue(pota_longitude);
insertQuery.addBindValue(pota_grid);
if ( ! insertQuery.execBatch() )
{
qInfo() << "POTA Directory insert error " << insertQuery.lastError().text() << insertQuery.lastQuery();
abortRequested = true;
}
if ( !abortRequested )
{
QSqlDatabase::database().commit();
qCDebug(runtime) << "POTA update finished:" << count << "entities loaded.";
}
else
{
qCWarning(runtime) << "POTA update failed - rollback";
QSqlDatabase::database().rollback();
}
}
void LOVDownloader::parseMembershipContent(const SourceDefinition &sourceDef, QTextStream &data)
{
FCT_IDENTIFICATION;
QSqlDatabase::database().transaction();
if ( ! deleteTable(sourceDef.tableName) )
{
qCWarning(runtime) << "Membership Directory delete failed - rollback";
QSqlDatabase::database().rollback();
return;
}
QSqlTableModel entityTableModel;
entityTableModel.setTable(sourceDef.tableName);
entityTableModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
QSqlRecord entityRecord = entityTableModel.record();