@prefix adms: <http://www.w3.org/ns/adms#> .
@prefix dbo: <https://dbpedia.org/ontology/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix locn: <http://www.w3.org/ns/locn#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix schema: <https://schema.org/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix legal: <http://www.w3.org/ns/legal#> .
@prefix m8g: <http://data.europa.eu/m8g/> .
@prefix fn: <http://www.w3.org/2005/xpath-functions/> .

######## D2RML NAMESPACES ###########

@prefix dr: <http://islab.ntua.gr/ns/d2rml#> .
@prefix drop: <http://islab.ntua.gr/ns/d2rml-op#> .
@prefix dris: <http://islab.ntua.gr/ns/d2rml-is#> .

######## STIRDATA TMP NAMESPACES ###########

@prefix nuts-item: <http://data.europa.eu/nuts/code/> .
@prefix lau-item: <https://w3id.org/stirdata/resource/lau/item/> .

@prefix sd-voc: <https://w3id.org/stirdata/vocabulary/> .
@prefix sd-voc-adminlevel: <https://w3id.org/stirdata/resource/adminUnitLevel/> .

@prefix sd-agency-ro: <http://ro.data.stirdata.eu/resource/agency/> .
@prefix sd-organization-ro: <http://ro.data.stirdata.eu/resource/organization/> .
@prefix sd-unit-ro: <http://ro.data.stirdata.eu/resource/unit/> .
@prefix sd-site-ro: <http://ro.data.stirdata.eu/resource/site/> .
@prefix sd-address-ro: <http://ro.data.stirdata.eu/resource/address/> .
@prefix sd-identifier-ro: <http://ro.data.stirdata.eu/resource/identifier/> .

#@prefix sd-companytype-item-ro: <http://ro.data.stirdata.eu/resource/companyType/item/> .
@prefix sd-companystatus-item-ro: <http://ro.data.stirdata.eu/resource/companyStatus/item/> .

######## EXECUTION ORDER ###########

<#Order>
   a dr:D2RMLSpecification ;
   dr:logicalDatasets ( 
   	                    <#ONRCListPageMapping>  
   	                    <#ONRCLatestDataPageMapping>  
                        <#OrganizationsMapping> 
                        <#NutsMapping>
                      ) .

######## DATA SOURCES ###########

<#ONRCListPageSource>
   a dris:HTTPSource ;
   dris:uri "https://data.gov.ro/organization/onrc" .
   
<#ONRCLatestDataPageSource>   
   a dris:HTTPSource .
   
<#PostCodeSource>
   a dris:HTTPSource ;
   dris:cacheable true ;
   dris:uri "https://apps.islab.ntua.gr/geo/api/postcode?country-code={@@COUNTRY@@}&postcode={@@POSTCODE@@}" .
   
<#NutsSource>    
   a dris:HTTPSource ;
   dris:cacheable true ;
   dris:uri "https://stirdata-semantic.ails.ece.ntua.gr/api/content/index/nuts/search?text={@@TEXT@@}&keys=notation&type=match&fields=label" .

<#BussinessRegistryOpenDataSource>
   a dris:HTTPSource .
#   dris:uri "{@@NO_URL_ENCODE@@NERADIATE_CU_SEDIU_CSV_URI@@}" .
#   dris:uri "https://data.gov.ro/dataset/8b0fe23c-def2-415c-a097-eb087949435e/resource/292aef61-79c2-4b91-afa7-94d35d9d9f0c/download/3open_data-neradiate_cu_sediu-10.09.2021.csv" .
#   a dris:FileSource ;
#   dris:path "d:/data/stirdata/data/ro/3open_data-neradiate_cu_sediu-09.02.2022.csv" .

######## TRANSFORMATIONS ###########

<#PostCodeTransformation>
   dr:logicalArray [ 
      a dr:JSONItemArray ;
      dr:source <#PostCodeSource> ;
	  dr:iterator "$";
	  dr:limit 1;
   ] . 

<#NutsTransformation>
   dr:logicalArray [ 
      a dr:JSONItemArray ;
      dr:source <#NutsSource> ;
	  dr:iterator "$";
	  dr:limit 1;
   ] . 
   
######## AUXILIARY SOURCE ###########

<#AdminLevelModel>
    a dris:AuxiliaryModel .
	
######## MAIN DATASETS AND MAPPINGS ###########

<#ONRCListPageMapping>  
   dr:logicalArray [ 
      a dr:RegExItemArray ;
      dr:source <#ONRCListPageSource> ;
      dr:iterator "(?s)(^.*$)" ;
   ] ;

   dr:definedColumn [
     dr:name "LINK" ;
     dr:function drop:extractMatch ; 
     dr:selector dr:firstElement ;
     dr:parameterBinding [ 
	    dr:parameter "input" ;
        rr:column "##1"  ;
     ] ;
     dr:parameterBinding [  
	    dr:parameter "regex" ;
		rr:constant "<a href=\"(/dataset/firme-inregistrate-la-registrul-comertului-.*?)\">"  ;
     ] ;    
   ] ;

   dr:triplesMap [
      dr:logicalOutput dris:CurrentD2RMLDocument ;
	  
	  rr:subject <#ONRCLatestDataPageSource> ;
	  
	  rr:predicateObjectMap [
	     rr:predicate dris:uri ;
		 rr:objectMap [
		    rr:template "https://data.gov.ro{LINK}" ;
			rr:termType rr:Literal ;
		 ] ;
	  ] ;
   ] .
   
<#ONRCLatestDataPageMapping>  
   dr:logicalArray [ 
      a dr:RegExItemArray ;
      dr:source <#ONRCLatestDataPageSource> ;
      dr:iterator "(?s)(^.*$)" ;
   ] ;

   dr:definedColumn [
     dr:name "LINK" ;
     dr:function drop:extractMatch ; 
     dr:selector dr:firstElement ;
     dr:parameterBinding [ 
	    dr:parameter "input" ;
        rr:column "##1"  ;
     ] ;
     dr:parameterBinding [  
	    dr:parameter "regex" ;
		#rr:constant "(?s)title=\".*?neradiate cu sediu\">.*?<a href=\"(https://data.gov.ro/dataset/.*?\\.csv)\""  ;
     rr:constant "(?s)<li>\\s*<a href=\"(https://data\\.gov\\.ro/dataset/.{36}/resource/.{36}/download/3firme_neradiate_cu_sediu_.*?\\.csv)\" class" ;
     ] ;    
   ] ;

   dr:triplesMap [
      dr:logicalOutput dris:CurrentD2RMLDocument ;
	  
	  rr:subject <#BussinessRegistryOpenDataSource> ;
	  
	  rr:predicateObjectMap [
	     rr:predicate dris:uri ;
		 rr:objectMap [
		    rr:template "{LINK}" ;
			rr:termType rr:Literal ;
		 ] ;
	  ] ;
   ] .   
   
# ++ DENUMIRE
# ++ CUI
#    COD_INMATRICULARE
#    EUID
# ++ STARE_FIRMA
# ++ ADRESA_COMPLETA
#    ADR_TARA
#    ADR_LOCALITATE	
#    ADR_JUDET
# ++ ADR_DEN_STRADA
# ++ ADR_DEN_NR_STRADA
#    ADR_BLOC
#    ADR_SCARA
#    ADR_ETAJ
#    ADR_APARTAMENT
# ++ ADR_COD_POSTAL
#    ADR_SECTOR
#    ADR_COMPLETARE


<#OrganizationsMapping>  
   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#BussinessRegistryOpenDataSource> ;
	  dr:headerRecord true;
	  dr:delimiter "^";
#      dr:offset 6 ;
#	  dr:limit 1 ;
   ] ;
   
   dr:definedColumns (  
   [ 
      dr:name "POSTCODE_LENGTH" ;
      dr:function drop:strlength ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "ADR_COD_POSTAL"  ;
      ] ; 
   ]
   [ 
      dr:name "POSTCODE" ;
      dr:function drop:if ; 
      dr:parameterBinding [ 
	     dr:parameter "condition" ;
         rr:constant "_x1 == 5"  ;
      ] ; 
      dr:parameterBinding [ 
	     dr:parameter "_x1" ;
         rr:column "POSTCODE_LENGTH"  ;
      ] ;      
      dr:parameterBinding [ 
	     dr:parameter "then" ;
         rr:template "0{ADR_COD_POSTAL}"  ;
      ] ;       
      dr:parameterBinding [ 
	     dr:parameter "else" ;
         rr:column "ADR_COD_POSTAL"  ;
      ] ;       
   ]
   [ 
      dr:name "COMPANY_STATUS" ;
      dr:function fn:tokenize ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "STARE_FIRMA"  ;
      ] ; 
      dr:parameterBinding [ 
	     dr:parameter "pattern" ;
         rr:constant ","  ;
      ] 	  
   ]   
#   [ 
#      dr:name "NR" ;
#      dr:function drop:extractMatch ; 
#      dr:parameterBinding [ 
#	     dr:parameter "input" ;
#         rr:column "ADRESA"  ;
#      ] ;
#      dr:parameterBinding [
#	     dr:parameter "regex" ;
#         rr:constant "(?i)^.*(Nr\\.\\s+.*?),.*$"  ;
#      ] ; 
#   ]
#   [ 
#      dr:name "THOROUGHFARE" ;
#      dr:function drop:extractMatch ; 
#      dr:parameterBinding [ 
#	     dr:parameter "input" ;
#         rr:column "ADRESA"  ;
#      ] ;
#      dr:parameterBinding [
#	     dr:parameter "regex" ;
#         rr:constant "(?i)^.*((?:Str\\.?|Stradela|Parc|Prel\\.?|Calea|Şos\\.?|Aleea|Cart\\.?|B-dul|Sos\\.?|Spl\\.?|Intr\\.?|P-ţa\\.?|P-ta\\.?|Sat|Drumul)\\s+.*?),.*$"  ;
#      ] ; 
#   ]
   ) ;

   dr:transformations ( 
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "PostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "RO" ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "POSTCODE" ;
         dr:condition [
            rr:column "POSTCODE" ;
            drop:neq "";
         ] ; 		 
      ]  ;
   ]
   [
      dr:dataref <#NutsTransformation> ;
      dr:transformationName "NutsTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "PostCodeTransformation~~$.NUTS3" ;
      ]  ;
   ]   ) ;   
   
   dr:triplesMap [
      # ID - CLASS
      rr:subjectMap [ 
         rr:template  "{@sd-organization-ro}{CUI}" ;
         rr:class legal:LegalEntity ;
         dr:condition [
            rr:column "CUI" ;
            drop:matches "[0-9]{8}" ;
	     ]
      ] ;
   
#      # SYSTEM IDENTIFIER
#      rr:predicateObjectMap [ 
#         rr:predicate  dcterms:identifier ;
#         rr:objectMap  [ 
#            rr:column "regcode" ;
#            rr:termType  rr:Literal ;
#         ] ;
#      ] ; 
   
      # ONRC REGISTRATION IDENTIFIER
      rr:predicateObjectMap [
         rr:predicate legal:legalIdentifier ;
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
			      rr:template "{@sd-identifier-ro}onrc/{CUI}" ;
                  rr:class adms:Identifier ;
               ] ;

               # REGISTRATION NUMBER 
               rr:predicateObjectMap [
               rr:predicate skos:notation;
                  rr:objectMap [
                     rr:column "CUI" ;
                     rr:termType rr:Literal;
                  ] ;
               ] ;

               # ISSUING BODY
               rr:predicateObjectMap [
                  rr:predicate dct:creator;
                  rr:objectMap [
                     rr:constant sd-agency-ro:onrc ;
                  ] ;
               ] ;
            ] ;
         ] ;
      ] ;    

      # COMPANY NAME
      rr:predicateObjectMap [ 
         rr:predicate  legal:legalName ;
         rr:objectMap  [ 
            rr:column "DENUMIRE" ;
            rr:termType  rr:Literal ;
         ] ;
      ] ;   
   
#      # JURISDICTION (ASSUME ROMANIA FOR ALL) // check if
#      rr:predicateObjectMap [ 
#         rr:predicate  dbo:jurisdiction ;
#         rr:objectMap  [ 
#            rr:constant <http://publications.europa.eu/resource/authority/country/ROU> ;
#            rr:termType  rr:IRI ;
#         ] ;
#      ] ;     
   
      # LEGAL STATUS (TEMPORARY UNTIL SKOS VOCABULARY DECIDED) -- 
      rr:predicateObjectMap [ 
         rr:predicate  legal:companyStatus ;
         rr:objectMap  [ 
            rr:template "{@sd-companystatus-item-ro}{COMPANY_STATUS}" ;
            rr:termType  rr:IRI ;
         ] ;
      ] ;
	  
      # ADDRESS 
      rr:predicateObjectMap [ 
         rr:predicateMap [
            rr:constant m8g:registeredAddress ;
            dr:condition [
               rr:column "ADRESA_COMPLETA" ;
               drop:neq "";
            ] ;
         ] ;    
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-address-ro}{CUI}";
                  rr:class locn:Address ;
               ] ;
			
               rr:predicateObjectMap [ 
                  rr:predicate  locn:fullAddress ;
                  rr:objectMap  [ 
                     rr:column  "ADRESA_COMPLETA" ;
                     rr:termType  rr:Literal ;
                  ] ;
               ]  ;

               rr:predicateObjectMap [ 
                  rr:predicate  locn:thoroughfare ;
                  rr:objectMap  [ 
                     rr:column "ADR_DEN_STRADA" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:locatorDesignator ;
                  rr:objectMap  [ 
                     rr:column "ADR_DEN_NR_STRADA" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:postCode ;
                  rr:objectMap  [ 
                     rr:column "POSTCODE" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   
			
               rr:predicateObjectMap [ 
                  rr:predicate  locn:postName ;
                  rr:objectMap  [ 
                     rr:column "PostCodeTransformation~~$.LAU-native" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;                       
			
               rr:predicateObjectMap [ 
                  rr:predicate  locn:adminUnitL1 ;
                  rr:objectMap  [ 
                     rr:constant "RO" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;                    

               rr:predicateObjectMap [ 
                  rr:predicate  locn:adminUnitL2 ;
                  rr:objectMap  [ 
                     rr:column "NutsTransformation~~$.label[0]" ;
                     rr:termType  rr:Literal ;
                  ] ;
               ] ; 

               rr:predicateObjectMap [ 
                  rr:predicate  m8g:adminUnit ;
                  rr:objectMap  [ 
                     rr:constant <http://publications.europa.eu/resource/authority/country/ROU> ;
                 ] ;    
               ] ;

               rr:predicateObjectMap [ 
                 rr:predicate  m8g:adminUnit ;
                 rr:objectMap  [ 
                    rr:template "{@nuts-item}{PostCodeTransformation~~$.NUTS3}" ;
                    rr:termType  rr:IRI ;
                 ]
               ] ;

               rr:predicateObjectMap [ 
                  rr:predicate  m8g:adminUnit ;
                  rr:objectMap  [ 
                     rr:template "{@lau-item}{PostCodeTransformation~~$.LAU}" ;
                     rr:termType  rr:IRI ;
                  ]
               ] ;
            ] ;
         ] ;
      ] ;
   ];
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:constant <http://publications.europa.eu/resource/authority/country/ROU> ;
         rr:class sd-voc-adminlevel:Country ;
      ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@nuts-item}{PostCodeTransformation~~$.NUTS3}";
         rr:class sd-voc-adminlevel:NUTS-3 ;
      ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@lau-item}{PostCodeTransformation~~$.LAU}" ;
         rr:class sd-voc-adminlevel:LAU ;
      ] ;
   ] .

<#NutsMapping>
   rr:logicalTable [ 
      dr:source <#AdminLevelModel> ;
      dr:sparqlQuery "SELECT ?uri ?type WHERE { ?uri a ?type } "; 
   ] ;
   
   dr:triplesMap[ 
      rr:subjectMap [ 
         rr:column "uri";
         rr:class m8g:AdminUnit ;
      ] ;

      rr:predicateObjectMap [ 
         rr:predicate  m8g:code ;
         rr:objectMap  [ 
            rr:column "uri" ;
            rr:termType  rr:IRI ;
         ]
      ] ;   

      rr:predicateObjectMap [ 
         rr:predicate  m8g:level ;
         rr:objectMap  [ 
            rr:column "type" ;
            rr:termType rr:IRI ;
         ]
      ] ;   
   ] .

