-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
1018 lines (847 loc) · 36.8 KB
/
queries.py
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
#!/usr/bin/python3
import copy
import escape_helpers
import helpers
from helpers import log
from datetime import datetime
from pytz import timezone
import re
import json
CONFIG_FILE_PATH = '/config/config.json'
TIMEZONE = timezone('Europe/Brussels')
STATUS_DELIVERED_UNCONFIRMED = \
"http://data.lblod.info/id/status/berichtencentrum/sync-with-kalliope/delivered/unconfirmed"
STATUS_DELIVERED_CONFIRMED = \
"http://data.lblod.info/id/status/berichtencentrum/sync-with-kalliope/delivered/confirmed"
STATUS_DELIVERED_CONFIRMATION_FAILED = \
"http://data.lblod.info/id/status/berichtencentrum/sync-with-kalliope/delivered/failedConfirmation"
# Inzendingen business rules : sender classification with decisionType
DECISION_TYPES_EB_HAS_CB = [
"<https://data.vlaanderen.be/id/concept/BesluitType/e44c535d-4339-4d15-bdbf-d4be6046de2c>", # Jaarrekening
"<https://data.vlaanderen.be/id/concept/BesluitDocumentType/2c9ada23-1229-4c7e-a53e-acddc9014e4e>" # Gecoordineerde inzending meerjarenplannen
]
DECISION_TYPES_EB = [
"<https://data.vlaanderen.be/id/concept/BesluitType/f56c645d-b8e1-4066-813d-e213f5bc529f>" # Meerjarenplan(aanpassing)
]
DECISION_TYPES_CB = [
"<https://data.vlaanderen.be/id/concept/BesluitDocumentType/18833df2-8c9e-4edd-87fd-b5c252337349>", # Budgetten(wijzigingen) - Indiening bij representatief orgaan
"<https://data.vlaanderen.be/id/concept/BesluitDocumentType/2c9ada23-1229-4c7e-a53e-acddc9014e4e>" # Gecoordineerde inzending meerjarenplannen
]
DECISION_TYPES_RO = [
"<https://data.vlaanderen.be/id/concept/BesluitType/2b12630f-8c4e-40a4-8a61-a0c45621a1e6>", # Advies Budget(wijziging)
"<https://data.vlaanderen.be/id/concept/BesluitType/0fc2c27d-a03c-4e3f-9db1-f10f026f76f8>" # Advies Meerjarenplan
]
DECISION_TYPES_GO = [
"<https://data.vlaanderen.be/id/concept/BesluitType/df261490-cc74-4f80-b783-41c35e720b46>", # Besluit over budget(wijziging) eredienstbestuur
"<https://data.vlaanderen.be/id/concept/BesluitType/3fcf7dba-2e5b-4955-a489-6dd8285c013b>" # Besluit over meerjarenplan(aanpassing) eredienstbestuur
]
DECISION_TYPES_PO = [
"<https://data.vlaanderen.be/id/concept/BesluitType/df261490-cc74-4f80-b783-41c35e720b46>", # Besluit over budget(wijziging) eredienstbestuur
"<https://data.vlaanderen.be/id/concept/BesluitType/3fcf7dba-2e5b-4955-a489-6dd8285c013b>" # Besluit over meerjarenplan(aanpassing) eredienstbestuur
]
def sparql_escape_string(obj):
log("""Warning: using a monkey patched
sparql_escape_string.
TODO: move this to template""")
obj = str(obj)
def replacer(a):
return "\\"+a.group(0)
return '"""' + re.sub(r'[\\\"]', replacer, obj) + '"""'
# monkey patch escape_helpers. TODO: mov
escape_helpers.sparql_escape_string = sparql_escape_string
def construct_conversatie_exists_query(graph_uri, referentieABB):
"""
Construct a query for selecting a conversatie based on referentieABB
(thereby also testing if the conversatie already exists)
:param graph_uri: string
:param referentieABB: string
:returns: string containing SPARQL query
"""
referentieABB = escape_helpers.sparql_escape_string(referentieABB)
q = """
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?conversatie
WHERE {{
GRAPH <{}> {{
?conversatie a schema:Conversation;
schema:identifier {}.
}}
}}
""".format(graph_uri, referentieABB)
return q
def construct_bestuurseenheid_exists_query(bestuurseeheid_uri):
"""
Construct a query for asking if a bestuurseenehid exists in our database.
:param bestuurseeheid_uri: string
:returns: string containing SPARQL query
"""
q = """
PREFIX besluit: <http://data.vlaanderen.be/ns/besluit#>
ASK {{
<{0}> a besluit:Bestuurseenheid .
}}
""".format(bestuurseeheid_uri)
return q
def construct_bericht_exists_query(graph_uri, bericht_uri):
"""
Construct a query for selecting a bericht based on its URI, retrieving the conversatie & referentieABB at the same time.
:param graph_uri: string
:param bericht_uri: string
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?conversatie ?referentieABB
WHERE {{
GRAPH <{0}> {{
<{1}> a schema:Message.
?conversatie a schema:Conversation;
schema:hasPart <{1}>;
schema:identifier ?referentieABB.
}}
}}
""".format(graph_uri, bericht_uri)
return q
def construct_insert_conversatie_query(graph_uri, conversatie, bericht, delivery_timestamp):
"""
Construct a SPARQL query for inserting a new conversatie with a first bericht attached.
:param graph_uri: string
:param conversatie: dict containing escaped properties for conversatie
:param bericht: dict containing escaped properties for bericht
:returns: string containing SPARQL query
"""
conversatie = copy.deepcopy(conversatie) # For not modifying the pass-by-name original
conversatie['referentieABB'] = escape_helpers.sparql_escape_string(conversatie['referentieABB'])
conversatie['betreft'] = escape_helpers.sparql_escape_string(conversatie['betreft'])
conversatie['current_type_communicatie'] =\
escape_helpers.sparql_escape_string(conversatie['current_type_communicatie'])
bericht = copy.deepcopy(bericht) # For not modifying the pass-by-name original
q = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX adms: <http://www.w3.org/ns/adms#>
INSERT DATA {{
GRAPH <{0}> {{
<{1[uri]}> a schema:Conversation;
<http://mu.semte.ch/vocabularies/core/uuid> "{1[uuid]}";
schema:identifier {1[referentieABB]};
"""
if conversatie["dossierUri"]:
q += """
ext:dossierUri "{1[dossierUri]}";
"""
q += """
schema:about {1[betreft]};
<http://mu.semte.ch/vocabularies/ext/currentType> {1[current_type_communicatie]};
schema:processingTime "{1[reactietermijn]}";
schema:hasPart <{2[uri]}>.
<{2[uri]}> a schema:Message;
<http://mu.semte.ch/vocabularies/core/uuid> "{2[uuid]}";
schema:dateSent "{2[verzonden]}"^^xsd:dateTime;
schema:dateReceived "{2[ontvangen]}"^^xsd:dateTime;
schema:text "Origineel bericht in bijlage";
<http://purl.org/dc/terms/type> "{2[type_communicatie]}";
schema:sender <{2[van]}>;
schema:recipient <{2[naar]}>;
adms:status <{3}>;
ext:deliveredAt "{4}"^^xsd:dateTime.
}}
}}
"""
q = q.format(graph_uri, conversatie, bericht, STATUS_DELIVERED_UNCONFIRMED, delivery_timestamp)
return q
def construct_insert_bericht_query(graph_uri, bericht, conversatie_uri, delivery_timestamp):
"""
Construct a SPARQL query for inserting a bericht and attaching it to an existing conversatie.
:param graph_uri: string
:param bericht: dict containing properties for bericht
:param conversatie_uri: string containing the uri of the conversatie that the bericht has to get attached to
:param delivery_timestamp: string
:returns: string containing SPARQL query
"""
bericht = copy.deepcopy(bericht) # For not modifying the pass-by-name original
q = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX adms: <http://www.w3.org/ns/adms#>
INSERT DATA {{
GRAPH <{0}> {{
<{2}> a schema:Conversation;
schema:hasPart <{1[uri]}>.
<{1[uri]}> a schema:Message;
<http://mu.semte.ch/vocabularies/core/uuid> "{1[uuid]}";
schema:dateSent "{1[verzonden]}"^^xsd:dateTime;
schema:dateReceived "{1[ontvangen]}"^^xsd:dateTime;
schema:text "Origineel bericht in bijlage";
schema:sender <{1[van]}>;
schema:recipient <{1[naar]}>;
adms:status <{3}>;
ext:deliveredAt "{4}"^^xsd:dateTime;
<http://purl.org/dc/terms/type> "{1[type_communicatie]}".
}}
}}
""".format(graph_uri, bericht, conversatie_uri,
STATUS_DELIVERED_UNCONFIRMED, delivery_timestamp)
return q
def construct_update_conversatie_type_query(graph_uri, conversatie_uri, type_communicatie):
"""
Construct a SPARQL query for updating the type-communicatie of a conversatie.
:param graph_uri: string
:param conversatie_uri: string containing the uri of the conversatie we want to update
:param type_communicatie: string containing the type-communicatie
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
DELETE {{
GRAPH <{0}> {{
<{1}> a schema:Conversation;
<http://mu.semte.ch/vocabularies/ext/currentType> ?type.
}}
}}
INSERT {{
GRAPH <{0}> {{
<{1}> a schema:Conversation;
<http://mu.semte.ch/vocabularies/ext/currentType> "{2}".
}}
}}
WHERE {{
GRAPH <{0}> {{
<{1}> a schema:Conversation;
<http://mu.semte.ch/vocabularies/ext/currentType> ?type.
}}
}}
""".format(graph_uri, conversatie_uri, type_communicatie)
return q
def construct_insert_bijlage_query(bericht_graph_uri, bericht_uri, bijlage, file):
"""
Construct a SPARQL query for inserting a bijlage and attaching it to an existing bericht.
:param bericht_graph_uri: string
:param bericht_uri: string
:param bijlage: dict containing escaped properties for bijlage
:param file: dict containing escaped properties for file (similar to bijlage, see mu-file-service)
:returns: string containing SPARQL query
"""
bijlage = copy.deepcopy(bijlage) # For not modifying the pass-by-name original
bijlage['name'] = escape_helpers.sparql_escape_string(bijlage['name'])
bijlage['mimetype'] = escape_helpers.sparql_escape_string(bijlage['mimetype'])
file = copy.deepcopy(file) # For not modifying the pass-by-name original
file['name'] = escape_helpers.sparql_escape_string(file['name'])
q = """
PREFIX schema: <http://schema.org/>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
PREFIX nie: <http://www.semanticdesktop.org/ontologies/2007/01/19/nie#>
PREFIX nmo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/ontology/>
INSERT DATA {{
GRAPH <{0}> {{
<{1}> nie:hasPart <{2[uri]}>.
<{2[uri]}> a nfo:FileDataObject;
<http://mu.semte.ch/vocabularies/core/uuid> "{2[uuid]}";
nfo:fileName {2[name]};
dct:format {2[mimetype]};
dct:created "{2[created]}"^^xsd:dateTime;
nfo:fileSize "{2[size]}"^^xsd:integer;
dbpedia:fileExtension "{2[extension]}".
<{3[uri]}> a nfo:FileDataObject;
<http://mu.semte.ch/vocabularies/core/uuid> "{3[uuid]}";
nfo:fileName {3[name]};
dct:format {2[mimetype]};
dct:created "{2[created]}"^^xsd:dateTime;
nfo:fileSize "{2[size]}"^^xsd:integer;
dbpedia:fileExtension "{2[extension]}";
nie:dataSource <{2[uri]}>.
}}
}}
""".format(bericht_graph_uri, bericht_uri, bijlage, file)
return q
def construct_update_last_bericht_query(conversatie_uri):
"""
Construct a SPARQL query for keeping the last message of a conversation up to date.
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
DELETE {{
GRAPH ?g {{
?conversation ext:lastMessage ?oldMessage.
}}
}}
INSERT {{
GRAPH ?g {{
?conversation ext:lastMessage ?message.
}}
}}
WHERE {{
{{
SELECT DISTINCT ?conversation ?message WHERE {{
BIND(<{0}> as ?conversation)
?conversation a schema:Conversation;
schema:hasPart ?message.
?message schema:dateSent ?dateSent.
}}
ORDER BY DESC(?dateSent)
LIMIT 1
}}
GRAPH ?g {{
?conversation a schema:Conversation;
schema:hasPart ?message.
OPTIONAL {{
?conversation ext:lastMessage ?oldMessage.
}}
}}
}}
""".format(conversatie_uri)
return q
def construct_unsent_berichten_query(naar_uri, max_sending_attempts):
"""
Construct a SPARQL query for retrieving all messages for a given recipient that haven't been received yet by the other party.
:param naar_uri: URI of the recipient for which we want to retrieve messages that have yet to be sent.
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT DISTINCT ?referentieABB ?dossieruri ?bericht ?betreft ?uuid ?van ?verzonden ?inhoud
WHERE {{
?conversatie a schema:Conversation;
schema:identifier ?referentieABB;
schema:about ?betreft;
schema:hasPart ?bericht.
?bericht a schema:Message;
<http://mu.semte.ch/vocabularies/core/uuid> ?uuid;
schema:dateSent ?verzonden;
schema:text ?inhoud;
schema:sender ?van;
schema:recipient <{0}>.
FILTER NOT EXISTS {{ ?bericht schema:dateReceived ?ontvangen. }}
OPTIONAL {{
?conversatie ext:dossierUri ?dossieruri.
}}
BIND(0 AS ?default_attempts)
OPTIONAL {{ ?bericht ext:failedSendingAttempts ?attempts. }}
BIND(COALESCE(?attempts, ?default_attempts) AS ?result_attempts)
FILTER(?result_attempts < {1})
}}
""".format(naar_uri, max_sending_attempts)
return q
def construct_select_bijlagen_query(bericht_uri):
"""
Construct a SPARQL query for retrieving all bijlages for a given bericht.
:param bericht_uri: URI of the bericht for which we want to retrieve bijlagen.
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
PREFIX nie: <http://www.semanticdesktop.org/ontologies/2007/01/19/nie#>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT DISTINCT ?bijlagenaam ?file ?type WHERE {{
<{0}> a schema:Message;
nie:hasPart ?bijlage.
?bijlage a nfo:FileDataObject;
nfo:fileName ?bijlagenaam;
dct:format ?type.
?file nie:dataSource ?bijlage.
}}
""".format(bericht_uri)
return q
def construct_increment_bericht_attempts_query(graph_uri, bericht_uri):
"""
Construct a SPARQL query for incrementing (+1) the counter that keeps track of how many times
the service attempted to send out a certain message without succes.
:param graph_uri: string
:param bericht_uri: URI of the bericht.
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
DELETE {{
GRAPH <{0}> {{
<{1}> ext:failedSendingAttempts ?result_attempts.
}}
}}
INSERT {{
GRAPH <{0}> {{
<{1}> ext:failedSendingAttempts ?incremented_attempts.
}}
}}
WHERE {{
GRAPH <{0}> {{
<{1}> a schema:Message.
OPTIONAL {{ <{1}> ext:failedSendingAttempts ?attempts. }}
BIND(0 AS ?default_attempts)
BIND(COALESCE(?attempts, ?default_attempts) AS ?result_attempts)
BIND((?result_attempts + 1) AS ?incremented_attempts)
}}
}}
""".format(graph_uri, bericht_uri)
return q
def construct_bericht_sent_query(graph_uri, bericht_uri, verzonden):
"""
Construct a SPARQL query for marking a bericht as received by the other party (and thus 'sent' by us)
:param graph_uri: string
:param bericht_uri: URI of the bericht we would like to mark as sent.
:param verzonden: ISO-string representation of the datetetime when the message was sent
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
INSERT {{
GRAPH <{0}> {{
<{1}> schema:dateReceived "{2}"^^xsd:dateTime.
}}
}}
WHERE {{
GRAPH <{0}> {{
<{1}> a schema:Message.
}}
}}
""".format(graph_uri, bericht_uri, verzonden)
return q
def construct_select_original_bericht_query(bericht_uri):
"""
Construct a SPARQL query for selecting the first message in a conversation
:param bericht_uri: URI of a bericht in a conversation
:returns: string containing SPARQL query
"""
q = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT DISTINCT ?origineelbericht ?dateSent WHERE {{
?conversation a schema:Conversation;
schema:hasPart ?origineelbericht;
schema:hasPart <{0}>.
?origineelbericht schema:dateSent ?dateSent.
}}
ORDER BY ASC(?dateSent)
LIMIT 1
""".format(bericht_uri)
return q
def verify_eb_has_cb_exclusion_rule(submission):
ask_query_eb_has_cb = """
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX pav: <http://purl.org/pav/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
ASK {{
BIND(<{0}> AS ?submission)
?submission a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
prov:generated ?formData ;
pav:createdBy ?bestuurseenheid .
?formData dct:type ?decisionType .
VALUES ?decisionType {{ {1} }}
?bestuurseenheid a ere:BestuurVanDeEredienst.
?centraalBestuur a ere:CentraalBestuurVanDeEredienst ;
org:hasSubOrganization ?bestuurseenheid .
}}
""".format(submission, " ".join(DECISION_TYPES_EB_HAS_CB))
return ask_query_eb_has_cb
def verify_eb_exclusion_rule(submission):
ask_query_eb = """
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX pav: <http://purl.org/pav/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
ASK {{
BIND(<{0}> AS ?submission)
?submission a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
prov:generated ?formData ;
pav:createdBy ?bestuurseenheid .
?formData dct:type ?decisionType .
VALUES ?decisionType {{ {1} }}
?bestuurseenheid a ere:BestuurVanDeEredienst.
}}
""".format(submission, " ".join(DECISION_TYPES_EB))
return ask_query_eb
def verify_cb_exclusion_rule(submission):
ask_query_cb = """
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX pav: <http://purl.org/pav/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
ASK {{
BIND(<{0}> AS ?submission)
?submission a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
prov:generated ?formData ;
pav:createdBy ?bestuurseenheid .
?formData dct:type ?decisionType .
VALUES ?decisionType {{ {1} }}
?bestuurseenheid a ere:CentraalBestuurVanDeEredienst .
}}
""".format(submission, " ".join(DECISION_TYPES_CB))
return ask_query_cb
def verify_ro_exclusion_rule(submission):
ask_query_ro = """
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX pav: <http://purl.org/pav/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
ASK {{
BIND(<{0}> AS ?submission)
?submission a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
prov:generated ?formData ;
pav:createdBy ?bestuurseenheid .
?formData dct:type ?decisionType .
VALUES ?decisionType {{ {1} }}
?bestuurseenheid a ere:RepresentatiefOrgaan .
}}
""".format(submission, " ".join(DECISION_TYPES_RO))
return ask_query_ro
def verify_go_exclusion_rule(submission):
ask_query_go = """
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX pav: <http://purl.org/pav/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX besluit: <http://data.vlaanderen.be/ns/besluit#>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
ASK {{
BIND(<{0}> AS ?submission)
?submission a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
prov:generated ?formData ;
pav:createdBy ?bestuurseenheid .
?formData dct:type ?decisionType .
VALUES ?decisionType {{ {1} }}
?bestuurseenheid besluit:classificatie <http://data.vlaanderen.be/id/concept/BestuurseenheidClassificatieCode/5ab0e9b8a3b2ca7c5e000001> .
}}
""".format(submission, " ".join(DECISION_TYPES_GO))
return ask_query_go
def verify_po_exclusion_rule(submission):
ask_query_po = """
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX pav: <http://purl.org/pav/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX besluit: <http://data.vlaanderen.be/ns/besluit#>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
ASK {{
BIND(<{0}> AS ?submission)
?submission a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
prov:generated ?formData ;
pav:createdBy ?bestuurseenheid .
?formData dct:type ?decisionType .
VALUES ?decisionType {{ {1} }}
?bestuurseenheid besluit:classificatie <http://data.vlaanderen.be/id/concept/BestuurseenheidClassificatieCode/5ab0e9b8a3b2ca7c5e000000> .
}}
""".format(submission, " ".join(DECISION_TYPES_PO))
return ask_query_po
def construct_unsent_inzendingen_query(max_sending_attempts):
"""
Construct a SPARQL query for retrieving all messages for a given recipient that haven't been received yet by the other party.
:param max_sending_attempts: the maximum number of delivery attempts that have to be done
:returns: string containing SPARQL query
"""
with open(CONFIG_FILE_PATH) as config_file:
allowedDecisionTypesList = json.load(config_file)['allowedDecisionTypes'];
separator = ' '
q = """
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX nmo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX adms: <http://www.w3.org/ns/adms#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX eli: <http://data.europa.eu/eli/ontology#>
PREFIX besluit: <http://data.vlaanderen.be/ns/besluit#>
PREFIX mandaat: <http://data.vlaanderen.be/ns/mandaat#>
PREFIX ere: <http://data.lblod.info/vocabularies/erediensten/>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX pav: <http://purl.org/pav/>
SELECT DISTINCT ?inzending ?inzendingUuid ?bestuurseenheid ?decisionType ?sessionDate
?decisionTypeLabel ?datumVanVerzenden ?boekjaar
WHERE {{
?inzending a meb:Submission ;
adms:status <http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c> ;
mu:uuid ?inzendingUuid ;
pav:createdBy ?bestuurseenheid;
nmo:sentDate ?datumVanVerzenden;
prov:generated ?formData .
?formData dct:type ?decisionType .
OPTIONAL {{ ?formData <http://linkedeconomy.org/ontology#financialYear> ?boekjaar . }}
VALUES ?decisionType {{ {1} }}
FILTER NOT EXISTS {{ ?inzending nmo:receivedDate ?receivedDate. }}
OPTIONAL {{ ?formData ext:sessionStartedAtTime ?sessionDate. }}
BIND(0 AS ?default_attempts)
OPTIONAL {{ ?inzending ext:failedSendingAttempts ?attempts. }}
BIND(COALESCE(?attempts, ?default_attempts) AS ?result_attempts)
FILTER(?result_attempts < {0})
OPTIONAL {{ ?decisionType skos:prefLabel ?decisionTypeLabel }} .
}}
""".format(max_sending_attempts, separator.join(allowedDecisionTypesList))
return q
def construct_increment_inzending_attempts_query(graph_uri, inzending_uri):
"""
Construct a SPARQL query for incrementing (+1) the counter that keeps track of how many times
the service attempted to send out a certain inzending without succes.
:param graph_uri: string
:param inzending_uri: URI of the bericht.
:returns: string containing SPARQL query
"""
q = """
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX prov: <http://www.w3.org/ns/prov#>
DELETE {{
GRAPH <{0}> {{
<{1}> ext:failedSendingAttempts ?result_attempts.
}}
}}
INSERT {{
GRAPH <{0}> {{
<{1}> ext:failedSendingAttempts ?incremented_attempts.
}}
}}
WHERE {{
GRAPH <{0}> {{
<{1}> a meb:Submission .
OPTIONAL {{ <{1}> ext:failedSendingAttempts ?attempts. }}
BIND(0 AS ?default_attempts)
BIND(COALESCE(?attempts, ?default_attempts) AS ?result_attempts)
BIND((?result_attempts + 1) AS ?incremented_attempts)
}}
}}
""".format(graph_uri, inzending_uri)
return q
def construct_inzending_sent_query(graph_uri, inzending_uri, verzonden):
"""
Construct a SPARQL query for marking a bericht as received by the other party (and thus 'sent' by us)
:param graph_uri: string
:param bericht_uri: URI of the bericht we would like to mark as sent.
:param verzonden: ISO-string representation of the datetetime when the message was sent
:returns: string containing SPARQL query
"""
q = """
PREFIX meb: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX nmo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#>
PREFIX prov: <http://www.w3.org/ns/prov#>
INSERT DATA {{
GRAPH <{0}> {{
<{1}> nmo:receivedDate "{2}"^^xsd:dateTime .
}}
}}
""".format(graph_uri, inzending_uri, verzonden)
return q
def construct_create_kalliope_sync_error_query(graph_uri, poststuk_uri, message, error):
now = datetime.now(tz=TIMEZONE).replace(microsecond=0).isoformat()
uuid = helpers.generate_uuid()
error_uri = "http://data.lblod.info/kalliope-sync-errors/" + uuid
"""
Construct a SPARQL query for creating a new KalliopeSyncError
:param graph_uri: string
:param poststuk_uri: URI of the message that triggered an error, can be None
:param message: string describing the error
:param error: error catched by the exception catcher
:returns: string containing SPARQL query
"""
q = """
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pav: <http://purl.org/pav/>
INSERT DATA {{
GRAPH <{0}> {{
<{5}> a ext:KalliopeSyncError ;
rdfs:label {2} ;
ext:errorMessage {3} ;
"""
if poststuk_uri is not None:
q += """
ext:processedMessage <{1}> ;
"""
q += """
pav:createdOn "{4}"^^xsd:dateTime ;
pav:createdBy <http://lblod.data.gift/services/berichtencentrum-sync-with-kalliope-service> .
}}
}}
"""
q = q.format(graph_uri,
poststuk_uri,
escape_helpers.sparql_escape_string(message),
escape_helpers.sparql_escape_string(error),
now,
error_uri)
return q
def construct_dossierbehandelaar_exists_query(graph_uri, dossierbehandelaar):
"""
Construct a query for
selecting a conversatie based on referentieABB (thereby also testing if the conversatie already exists)
:param graph_uri: string
:param referentieABB: string
:returns: string containing SPARQL query
"""
identifier = escape_helpers.sparql_escape_string(dossierbehandelaar['identifier'])
q = """
PREFIX schema: <http://schema.org/>
PREFIX prov: <http://www.w3.org/ns/prov#>
SELECT DISTINCT ?dossierbehandelaar
WHERE {{
GRAPH <{}> {{
?dossierbehandelaar a prov:Association ;
schema:identifier {} .
}}
}}
""".format(graph_uri, identifier)
return q
def construct_insert_dossierbehandelaar_query(graph_uri, bericht):
"""
Construct a SPARQL query for inserting a new dossierbehandelaar.
:param graph_uri: string
:param bericht: dict containing properties for bericht
:returns: string containing SPARQL query
"""
uuid = helpers.generate_uuid()
bericht['dossierbehandelaar']['uuid'] = uuid
bericht['dossierbehandelaar']['uri'] = "http://data.lblod.info/id/dossierbehandelaars/" + uuid
q = """
PREFIX schema: <http://schema.org/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX adms: <http://www.w3.org/ns/adms#>
INSERT DATA {{
GRAPH <{0}> {{
<{1[uri]}> a prov:Association;
prov:hadRole <http://data.lblod.info/association-role/249969e6-2bfa-48c2-9a37-3f0b97685a24>;
<http://mu.semte.ch/vocabularies/core/uuid> "{1[uuid]}";
adms:identifier "{1[identifier]}";
schema:email "{1[email]}".
}}
}}
""".format(graph_uri, bericht['dossierbehandelaar'])
return q
def construct_link_dossierbehandelaar_query(graph_uri, bericht):
"""
Construct a SPARQL query for linking a dossierbehandelaar to a bericht.
:param graph_uri: string
:param bericht: dict containing properties for bericht
:returns: string containing SPARQL query
"""
q = """
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
INSERT DATA {{
GRAPH <{0}> {{
<{1[uri]}> ext:heeftBehandelaar <{2[uri]}> .
}}
}}
""".format(graph_uri, bericht, bericht['dossierbehandelaar'])
return q
def construct_get_messages_by_status(status_uri, max_confirmation_attempts, bericht_uri=None):
bound_bericht_statement = ""
if bericht_uri:
bound_bericht_statement = "BIND(<{0}> as ?bericht)".format(bericht_uri)
query_str = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX adms: <http://www.w3.org/ns/adms#>
SELECT DISTINCT ?bericht
?uuid
?verzonden
?ontvangen
?inhoud
?van
?naar
?status
?deliveredAt
?typeCommunicatie
?confirmationAttempts
?g
{{
GRAPH ?g {{
BIND(<{0}> as ?status)
{1}
?bericht a schema:Message;
<http://mu.semte.ch/vocabularies/core/uuid> ?uuid;
schema:dateSent ?verzonden;
schema:dateReceived ?ontvangen;
schema:text ?inhoud;
schema:sender ?van;
schema:recipient ?naar;
adms:status ?status;
ext:deliveredAt ?deliveredAt;
<http://purl.org/dc/terms/type> ?typeCommunicatie.
OPTIONAL {{ ?bericht ext:failedConfirmationAttempts ?confirmationAttempts. }}
}}
FILTER( REGEX(STR(?g), "LoketLB-berichtenGebruiker"))
}}
""".format(status_uri, bound_bericht_statement)
return query_str
def construct_update_bericht_status(bericht_uri, status_uri):
query_str = """
PREFIX schema: <http://schema.org/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX adms: <http://www.w3.org/ns/adms#>
DELETE {{
GRAPH ?g {{
?bericht adms:status ?status.
}}
}}
INSERT {{
GRAPH ?g {{
?bericht adms:status <{1}>.
}}
}}
WHERE {{
BIND(<{0}> as ?bericht)
GRAPH ?g {{
?bericht adms:status ?status.
}}
}}
""".format(bericht_uri, status_uri)
return query_str
def construct_increment_confirmation_attempts_query(graph_uri, poststuk_uri):
"""
Construct a SPARQL query for incrementing (+1) the counter that keeps track of how many times
the service attempted to send out a conformation for a certain message without succes.
:param graph_uri: string
:param poststuk_uri: URI of the bericht.
:returns: string containing SPARQL query
"""
q = """
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
PREFIX schema: <http://schema.org/>
DELETE {{
GRAPH <{0}> {{
<{1}> ext:failedConfirmationAttempts ?result_attempts.
}}
}}