@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 void: <http://rdfs.org/ns/void#> .

######## 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-ee: <http://ee.data.stirdata.eu/resource/agency/> .
@prefix sd-organization-ee: <http://ee.data.stirdata.eu/resource/organization/> .
@prefix sd-unit-ee: <http://ee.data.stirdata.eu/resource/unit/> .
@prefix sd-site-ee: <http://ee.data.stirdata.eu/resource/site/> .
@prefix sd-address-ee: <http://ee.data.stirdata.eu/resource/address/> .
@prefix sd-identifier-ee: <http://ee.data.stirdata.eu/resource/identifier/> .

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

@prefix sd-dataset-ee: <http://ee.data.stirdata.eu/resource/dataset/> .

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

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

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

<#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@@}&fields=label&type=match&keys=notation" .

<#BussinessRegistryOpenDataSource>
   a dris:HTTPSource ;
   #dris:uri "https://avaandmed.rik.ee/andmed/ARIREGISTER/ariregister_csv.zip" ;
   dris:uri "https://avaandmed.ariregister.rik.ee/sites/default/files/avaandmed/ettevotja_rekvisiidid__lihtandmed.csv.zip" ;
   dris:inContainerPath "*" .
#   a dris:FileSource ;
#   dris:path "d:/data/stirdata/data/ee/ettevotja_rekvisiidid_2022-03-11.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 ###########

# ++ nimi
# ++ ariregistri_kood
# +  ettevotja_oiguslik_vorm
#    ettevotja_oigusliku_vormi_alaliik
# ++ kmkr_nr
# ++ ettevotja_staatus 
#    ettevotja_staatus_tekstina
# ++ ettevotja_esmakande_kpv
# -- ettevotja_aadress
#    asukoht_ettevotja_aadressis
# -- asukoha_ehak_kood
# -- asukoha_ehak_tekstina
# ++ indeks_ettevotja_aadressis
#    ads_adr_id
# -- ads_ads_oid
# ++ ads_normaliseeritud_taisaadress
# ++ teabesysteemi_link

<#OrganizationsMapping>  
   rr:logicalTable [ 
      a dr:CSVTable;
      dr:source <#BussinessRegistryOpenDataSource> ;
	  dr:headerRecord true;
	  dr:delimiter ";";
	  dr:quoteCharacter "\"";	  
#      dr:offset 2 ;
#	  dr:limit 1 ;
   ] ;
   
   dr:definedColumns ( [ 
      dr:name "REGISTRATION_DATE_SPLIT" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "ettevotja_esmakande_kpv"  ;
      ] ;
      dr:parameterBinding [			
	     dr:parameter "regex" ;
         rr:constant "^([0-9]{2})\\.([0-9]{2})\\.([0-9]{4})$"  ;
      ] ; 
   ]   
   [ 
      dr:name "ADDRESS_SPLIT" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
	     dr:parameter "input" ;
         rr:column "asukoht_ettevotja_aadressis"  ;
      ] ;
      dr:parameterBinding [
	     dr:parameter "regex" ;
         rr:constant "^(.*?)(?:\\s+([0-9][0-9A-z/\\-]*))?$"  ;
      ] ; 
   ]   
   ) ;

   dr:transformations ( 
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "PostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "EE" ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "indeks_ettevotja_aadressis" ;
         dr:condition [
            rr:column "indeks_ettevotja_aadressis" ;
            drop:matches "[0-9]{5,5}";
         ] ; 		 
      ]  ;
   ]
   [
      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-ee}{ariregistri_kood}" ;
         rr:class legal:LegalEntity ;
      ] ;
   
#      # SYSTEM IDENTIFIER
#      rr:predicateObjectMap [ 
#         rr:predicate  dct:identifier ;
#         rr:objectMap  [ 
#            rr:column "ariregistri_kood" ;
#            rr:termType  rr:Literal ;
#         ] ;
#      ] ; 
   
      # THIRD PARTY WEBPAGES ( NON-LINKED DATA )
      rr:predicateObjectMap [ 
         rr:predicate  schema:sameAs ;
#        rr:predicate  owl:sameAs ;
         rr:objectMap  [ 
            rr:column "teabesysteemi_link" ;
#            rr:datatype xsd:anyURI ;
            rr:termType rr:IRI ;
         ] ;
      ] ;   
   
      # ARIREGISTER REGISTRATION IDENTIFIER
      rr:predicateObjectMap [
         rr:predicate legal:legalIdentifier ;
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-identifier-ee}ariregister/{ariregistri_kood}" ;
                  rr:class adms:Identifier ;
               ] ;

               # DATE ISSUED
               rr:predicateObjectMap [
                  rr:predicate dct:issued;
                  rr:objectMap [
                     rr:template "{REGISTRATION_DATE_SPLIT.match#3}-{REGISTRATION_DATE_SPLIT.match#2}-{REGISTRATION_DATE_SPLIT.match#1}" ;
                     rr:datatype xsd:date;
                  ] ;
               ] ;

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

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

      rr:predicateObjectMap [
         rr:predicateMap [
            rr:constant sd-voc:taxIdentifier ;
            dr:condition [
               rr:column "kmkr_nr" ;
               drop:neq "";
            ]
         ] ;
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-identifier-ee}emta/{ariregistri_kood}" ;
                  rr:class adms:Identifier ;
               ] ;

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

               # ISSUING BODY
               rr:predicateObjectMap [
                  rr:predicate dct:creator;
                  rr:objectMap [
                     rr:constant sd-agency-ee:emta ;
                  ] ;
               ] ;
            ] ;
         ] ;
      ] ;        
   
      # COMPANY NAME
      rr:predicateObjectMap [ 
         rr:predicate  legal:legalName ;
         rr:objectMap  [ 
            rr:column "nimi" ;
            rr:termType  rr:Literal ;
         ] ;
      ] ;   
   
#      # JURISDICTION (ASSUME ESTONIA FOR ALL) // check if
#      rr:predicateObjectMap [ 
#         rr:predicate  dbo:jurisdiction ;
#         rr:objectMap  [ 
#            rr:constant <http://publications.europa.eu/resource/authority/country/EST> ;
#            rr:termType  rr:IRI ;
#         ] ;
#      ] ;     
   
     # LEGAL FORM (TEMPORARY UNTIL SKOS VOCABULARY DECIDED) -- 
     # Values in csv are : 
     # Osaühing; Usaldusühing; Mittetulundusühing; Tulundusühistu; Aktsiaselts; Täisühing; Füüsilisest isikust ettevõtja; Kohaliku omavalitsuse asutus; Maaparandusühistu
     # Välismaa äriühingu filiaal; Täidesaatva riigivõimu asutus või riigi muu institutsioon; Sihtasutus; Korteriühistu; Euroopa äriühing (Societas Europaea); 
     # Avalik-õiguslik juriidiline isik, põhiseaduslik institutsioon või nende asutus; Euroopa majandushuviühing; Euroopa ühistu
  
      rr:predicateObjectMap [ 
         rr:predicate  legal:companyType ;
         rr:objectMap  [ 
            dr:cases ( [  # THIS DOES NOT COVER ALL CASES
               rr:template "{@sd-companytype-item-ee}OÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Osaühing" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}AS" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Aktsiaselts" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}TÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Täisühing" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}UÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Usaldusühing" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}TuÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Tulundusühistu" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}FIE" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Füüsilisest isikust ettevõtja" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}MTÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Mittetulundusühing" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}SA" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Sihtasutus" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}KÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Korteriühistu" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}EÜ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Euroopa ühistu" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}EÄ" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Euroopa äriühing (Societas Europaea)" ;
               ] ;
            ] [
               rr:template "{@sd-companytype-item-ee}EM" ;
               rr:termType  rr:IRI ;
               dr:condition [
                  rr:column "ettevotja_oiguslik_vorm" ;
                  drop:eq "Euroopa majandushuviühing" ;
               ] ;
            ] ) ;
         ] ;
      ] ;   
   
      # LEGAL STATUS (TEMPORARY UNTIL SKOS VOCABULARY DECIDED) -- 
      # Values in csv are : 
      # R - Registrisse kantud; L - Likvideerimisel; P - Pankrotis;
      rr:predicateObjectMap [ 
         rr:predicate  legal:companyStatus ;
         rr:objectMap  [ 
            rr:template "{@sd-companystatus-item-ee}{ettevotja_staatus}" ;
            rr:termType  rr:IRI ;
         ] ;
      ] ;

      # FOUNDATION DATE (use registration date)
      rr:predicateObjectMap [ 
         rr:predicate  m8g:registrationDate ;
         rr:objectMap  [ 
            rr:template "{REGISTRATION_DATE_SPLIT.match#3}-{REGISTRATION_DATE_SPLIT.match#2}-{REGISTRATION_DATE_SPLIT.match#1}" ;
            rr:datatype xsd:date ;
         ] ;         
      ] ;          
   
      # ADDRESS 
      rr:predicateObjectMap [ 
         rr:predicateMap [
            rr:constant m8g:registeredAddress ;
            dr:condition [
               rr:column "indeks_ettevotja_aadressis" ;
               drop:matches "[0-9]{5,5}";
            ] ;
         ] ;    
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-address-ee}{ariregistri_kood}";
                  rr:class locn:Address ;
               ] ;

               rr:predicateObjectMap [ 
                  rr:predicate  locn:fullAddress ;
                  rr:objectMap  [ 
                     rr:column  "ads_normaliseeritud_taisaadress" ;
                     rr:termType  rr:Literal ;
                  ] ;
               ] ;

               rr:predicateObjectMap [ 
                  rr:predicate  locn:thoroughfare ;
                  rr:objectMap  [ 
                     rr:column "ADDRESS_SPLIT.match#1" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:locatorDesignator ;
                  rr:objectMap  [ 
                     rr:column "ADDRESS_SPLIT.match#2" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:postCode ;
                  rr:objectMap  [ 
                     rr:column "indeks_ettevotja_aadressis" ;
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;   

               rr:predicateObjectMap [ 
                  rr:predicate  locn:postName ;
                  rr:objectMap  [ 
                     rr:column "asukoha_ehak_tekstina" ; # not exactly corresponding to post code but to ehak code
                     rr:termType  rr:Literal ;
                  ] ;      
               ] ;                       

               rr:predicateObjectMap [ 
                  rr:predicate  locn:adminUnitL1 ;
                  rr:objectMap  [ 
                     rr:constant "EE" ;
                     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/EST> ;
                 ] ;    
               ] ;

               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/EST> ;
         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 ;
         ]
      ] ;   
   ] .
