11import time
22from typing import List , Set
3-
43from pymongo import MongoClient
54from ted_sws .core .model .notice import Notice , NoticeStatus
65from ted_sws .core .service .batch_processing import chunks
76from ted_sws .data_manager .adapters .notice_repository import NoticeRepository
8- from ted_sws .data_manager .adapters .sparql_endpoint import SPARQLTripleStoreEndpoint
7+ from ted_sws .data_manager .adapters .sparql_endpoint import SPARQLTripleStoreEndpoint , SPARQLStringEndpoint
8+ from ted_sws .event_manager .services .log import log_notice_error
99from ted_sws .notice_validator .resources import NOTICE_AVAILABILITY_SPARQL_QUERY_TEMPLATE_PATH , \
10- NOTICES_AVAILABILITY_SPARQL_QUERY_TEMPLATE_PATH
10+ NOTICES_AVAILABILITY_SPARQL_QUERY_TEMPLATE_PATH , GET_NOTICE_URI_SPARQL_QUERY_TEMPLATE_PATH
1111
1212WEBAPI_SPARQL_URL = "https://publications.europa.eu/webapi/rdf/sparql"
1313WEBAPI_SPARQL_RUN_FORMAT = "application/sparql-results+json"
@@ -44,13 +44,22 @@ def check_availability_of_notices_in_cellar(notice_uries: List[str], endpoint_ur
4444 return set (result ['s' ].to_list ())
4545
4646
47- def generate_notice_uri_from_notice_id ( notice_id : str ) -> str :
47+ def generate_notice_uri_from_notice ( notice : Notice ) -> str :
4848 """
4949 This service generates Cellar URI for a notice, determined by notice_id
50- :param notice_id :
50+ :param notice :
5151 :return:
5252 """
53- # TODO: implement notice_uri logic
53+ if notice .distilled_rdf_manifestation and notice .distilled_rdf_manifestation .object_data :
54+ sparql_endpoint = SPARQLStringEndpoint (rdf_content = notice .distilled_rdf_manifestation .object_data )
55+ sparql_query = GET_NOTICE_URI_SPARQL_QUERY_TEMPLATE_PATH .read_text (encoding = "utf-8" )
56+ notice_uries = sparql_endpoint .with_query (sparql_query = sparql_query ).fetch_tabular ()["s" ].to_list ()
57+ if len (notice_uries ) == 1 :
58+ return notice_uries [0 ]
59+ else :
60+ log_notice_error (message = "Invalid extraction of notice URI from distilled RDF manifestation!" ,
61+ notice_id = notice .ted_id )
62+
5463 return INVALID_NOTICE_URI
5564
5665
@@ -63,7 +72,7 @@ def validate_notice_availability_in_cellar(notice: Notice, notice_uri: str = Non
6372 """
6473 if notice .status in [NoticeStatus .PUBLISHED , NoticeStatus .PUBLICLY_UNAVAILABLE ]:
6574 if not notice_uri :
66- notice_uri = generate_notice_uri_from_notice_id ( notice_id = notice . ted_id )
75+ notice_uri = generate_notice_uri_from_notice ( notice = notice )
6776 if check_availability_of_notice_in_cellar (notice_uri = notice_uri ):
6877 notice .update_status_to (new_status = NoticeStatus .PUBLICLY_AVAILABLE )
6978 else :
@@ -85,7 +94,7 @@ def validate_notices_availability_in_cellar(notice_statuses: List[NoticeStatus],
8594 selected_notices = notice_repository .get_notices_by_status (notice_status = notice_status )
8695 for selected_notices_chunk in chunks (selected_notices , chunk_size = DEFAULT_NOTICES_BATCH_SIZE ):
8796 selected_notices_map = {
88- generate_notice_uri_from_notice_id ( notice_id = notice . ted_id ): notice
97+ generate_notice_uri_from_notice ( notice = notice ): notice
8998 for notice in selected_notices_chunk
9099 }
91100 selected_notices_uries = list (selected_notices_map .keys ())
0 commit comments