@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/> .

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

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

@prefix sd-nace-item-no:    <http://no.data.stirdata.eu/resource/nace/sic2007-no/item/> .

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

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

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

<#CountriesSource>
   a dris:HTTPSource ;
   dris:cacheable true ;
   dris:uri "https://stirdata-semantic.ails.ece.ntua.gr/api/content/index/countries/search?text={@@TEXT@@}&keys=label,alt-label&type=match-phrase&fields=label-en,nal,alpha2" .
   
<#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" .
   
<#EnheterSource>
   a dris:HTTPSource ;
   dris:uri "https://data.brreg.no/enhetsregisteret/api/enheter/lastned" ;
   dris:inContainerPath "enheter_alle.json" .
#   a dris:FileSource ;
#   dris:path "D:/data/stirdata/data/no/enheter_alle.json" .
    
######## TRANSFORMATIONS ###########

<#CountryTransformation>
   dr:logicalArray [ 
      a dr:JSONItemArray ;
      dr:source <#CountriesSource> ;
	  dr:iterator "$";
      dr:limit 1;
   ] .
   
<#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 ###########

<#EnheterMapping>  

# ++ Organisasjonsnummer                 
# ++ Navn	
# ++ Organisasjonsform.kode	
# -- Organisasjonsform.beskrivelse	
# ++ Næringskode 1	
# -- Næringskode 1.beskrivelse	
# ++ Næringskode 2	
# -- Næringskode 2.beskrivelse	
# ++ Næringskode 3	
# -- Næringskode 3.beskrivelse	
# ++ Hjelpeenhetskode	
# -- Hjelpeenhetskode.beskrivelse	
#    Antall ansatte	
# ++ Hjemmeside	
# ++ Postadresse.adresse	
# ++ Postadresse.poststed	
# ++ Postadresse.postnummer	
# -- Postadresse.kommune	
# ++ Postadresse.kommunenummer	
# ++ Postadresse.land	
# ++ Postadresse.landkode	
# ++ Forretningsadresse.adresse	
# ++ Forretningsadresse.poststed	
# ++ Forretningsadresse.postnummer	
# ++ Forretningsadresse.kommune	
# -- Forretningsadresse.kommunenummer	
# ++ Forretningsadresse.land	
# ++ Forretningsadresse.landkode	
#    Institusjonell sektorkode	
#    Institusjonell sektorkode.beskrivelse	
#    Siste innsendte årsregnskap	
# ++ Registreringsdato i Enhetsregisteret	
# ++ Stiftelsesdato	
#    FrivilligRegistrertIMvaregisteret	
#    Registrert i MVA-registeret	
#    Registrert i Frivillighetsregisteret	
#    Registrert i Foretaksregisteret	
#    Registrert i Stiftelsesregisteret	
# ++ Konkurs	
# ++ Under avvikling	
# ++ Under tvangsavvikling eller tvangsoppløsning	
#    Overordnet enhet i offentlig sektor	
#    Målform

   dr:logicalArray [ 
      a dr:JSONItemArray;
      dr:source <#EnheterSource> ;
      dr:iteratorFormulation dris:JSONElement ;
      dr:columnFormulation dris:JSONPath ;
#	  dr:limit 1 ;
   ] ;
   
   dr:transformations ( 
   [
      dr:dataref <#CountryTransformation> ;
      dr:transformationName "PostalCountryTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "$.postadresse.land" ;
      ]  ;
   ]
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "PostalPostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "NO" ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "$.postadresse.postnummer" ;
         dr:condition [
            rr:column "$.postadresse.postnummer" ;
            drop:neq "";
         ] ; 		 
      ]  ;
   ][
      dr:dataref <#NutsTransformation> ;
      dr:transformationName "PostalNutsTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "PostalPostCodeTransformation~~$.NUTS3" ;
      ]  ;
   ]   
  [
      dr:dataref <#CountryTransformation> ;
      dr:transformationName "BusinessCountryTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "$.forretningsadresse.land" ;
      ]  ;
   ]
   [
      dr:dataref <#PostCodeTransformation> ;
      dr:transformationName "BusinessPostCodeTransformation";
      dr:parameterBinding [ 
         dr:parameter "COUNTRY" ;
         rr:constant "NO" ;
      ] ;
      dr:parameterBinding [ 
         dr:parameter "POSTCODE" ;
         rr:column "$.forretningsadresse.postnummer" ;
         dr:condition [
            rr:column "$.forretningsadresse.postnummer" ;
            drop:neq "";
         ] ; 		 
      ]  ;
   ]
   [
      dr:dataref <#NutsTransformation> ;
      dr:transformationName "BusinessNutsTransformation";
      dr:parameterBinding [ 
         dr:parameter "TEXT" ;
         rr:column "BusinessPostCodeTransformation~~$.NUTS3" ;
      ]  ;
   ]   
   ) ;
   
   # POSTAL ADDRESS
   dr:definedColumns ( [
      dr:name "FULL_POSTAL_ADDRESS" ;
      dr:function drop:identity ; 
      dr:parameterBinding [ 
         dr:parameter "input" ;
         rr:column "$.postadresse.adresse" ;   
		 dr:joinString ", " ;
      ] ;
   ] [
      dr:name "POSTAL_ADDRESS" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
         dr:parameter "input" ;
         rr:column "FULL_POSTAL_ADDRESS" ;   
      ] ;
      dr:parameterBinding [ 
         dr:parameter "regex" ;
         rr:constant "^(.*?)(?:\\s+([0-9]+\\s?[A-Z]?))?$"  ;
      ] ; 
   ] [
      dr:name "FULL_BUSINESS_ADDRESS" ;
      dr:function drop:identity ; 
      dr:parameterBinding [ 
         dr:parameter "input" ;
         rr:column "$.forretningsadresse.adresse" ;   
		 dr:joinString ", " ;
      ] ;
   ] [
      dr:name "BUSINESS_ADDRESS" ;
      dr:function drop:extractMatch ; 
      dr:parameterBinding [ 
         dr:parameter "input" ;
         rr:column "FULL_BUSINESS_ADDRESS" ;   
      ] ;
      dr:parameterBinding [ 
         dr:parameter "regex" ;
         rr:constant "^(.*?)(?:\\s+([0-9]+\\s?[A-Z]?))?$"  ;
      ] ; 
   ] ) ;   

   dr:triplesMap [
      # ID - CLASS
      rr:subjectMap [ 
         rr:template  "{@sd-organization-no}{$.organisasjonsnummer}" ;
         rr:class legal:LegalEntity ;
#	     dr:condition [
#	        rr:column "$.organisasjonsform.kode" ;
#           drop:neq "UTLA"
#        ]
      ] ;
   
   
#   # SYSTEM IDENTIFIER
#   rr:predicateObjectMap [ 
#      rr:predicate  dcterms:identifier ;
#      rr:objectMap  [ 
#         rr:column "$.organisasjonsnummer" ;
#         rr:termType  rr:Literal ;
#      ] ;
#   ] ;   
   
      # THIRD PARTY WEBPAGES 
      rr:predicateObjectMap [ 
         rr:predicate  schema:sameAs ;
         rr:objectMap  [ 
            rr:template "https://data.brreg.no/enhetsregisteret/oppslag/enheter/{$.organisasjonsnummer}" ;
            rr:termType rr:IRI ;
         ] ;
#        rr:objectMap  [ 
#           rr:template "https://opencorporates.com/companies/no/{$.organisasjonsnummer}" ;
#           rr:termType rr:IRI ;
#        ] ;   
      ] ;      
   
      # BRREG REGISTRATION IDENTIFIER
      rr:predicateObjectMap [
         rr:predicate legal:legalIdentifier ;
         rr:objectMap [
            rr:parentTriplesMap [
               rr:subjectMap [
                  rr:template "{@sd-identifier-no}brreg/{$.organisasjonsnummer}" ;
                  rr:class adms:Identifier ;
               ] ;

               # DATE ISSUED
               rr:predicateObjectMap [
                  rr:predicate dct:issued;
                  rr:objectMap [
                     rr:column "$.registreringsdatoEnhetsregisteret" ;
                     rr:datatype xsd:date;
                  ] ;
               ] ;

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

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

      # COMPANY NAME
      rr:predicateObjectMap [ 
         rr:predicate  legal:legalName ;
         rr:objectMap  [ 
            rr:column "$.navn" ;
            rr:termType  rr:Literal ;
            rr:language "no" ;
         ] ;
      ] ;   
   
#   # JURISDICTION (ASSUME NORWAY FOR ALL)
#   rr:predicateObjectMap [ 
#      rr:predicate  dbo:jurisdiction ;
#      rr:objectMap  [ 
#         rr:constant <http://publications.europa.eu/resource/authority/country/NOR> ;
#         rr:termType  rr:IRI ;
#      ] ;
#   ] ;  
   
      # LEGAL FORM (TEMPORARY UNTIL SKOS VOCABULARY DECIDED)
      rr:predicateObjectMap [ 
         rr:predicate  legal:companyType ;
         rr:objectMap  [ 
            rr:template "{@sd-companytype-item-no}{$.organisasjonsform.kode}" ;
            rr:termType  rr:IRI ;
         ] ;
      ] ;
	  
	  # STATUS
	  rr:predicateObjectMap [ 
         rr:predicate  legal:companyStatus ;
         rr:objectMap  [ 
            rr:template  "{@sd-companystatus-item-no}KON" ;
            rr:termType  rr:IRI ;
            dr:condition [
               rr:column  "$.konkurs" ;
               drop:eq true ;
            ] ;		 
         ] ;
      ] ;
	  
	  rr:predicateObjectMap [ 
         rr:predicate  legal:companyStatus ;
         rr:objectMap  [ 
            rr:template  "{@sd-companystatus-item-no}AVV" ;
            rr:termType  rr:IRI ;
            dr:condition [
               rr:column  "$.underAvvikling" ;
               drop:eq true ;
            ] ;		 
         ] ;
      ] ;

	  rr:predicateObjectMap [ 
         rr:predicate  legal:companyStatus ;
         rr:objectMap  [ 
            rr:template  "{@sd-companystatus-item-no}TVA" ;
            rr:termType  rr:IRI ;
            dr:condition [
               rr:column  "$.underTvangsavviklingEllerTvangsopplosning" ;
               drop:eq true ;
            ] ;		 
         ] ;
      ] ;

      # ACTIVITY   
      rr:predicateObjectMap [
         rr:predicate legal:companyActivity ;
         rr:objectMap [
            rr:template "{@sd-nace-item-no}{$.naeringskode1.kode}" ;
            rr:termType rr:IRI ;
            dr:condition [
               rr:column "$.naeringskode1.kode" ;
               drop:neq "00.000" ;
            ] ;
         ] ;
         rr:objectMap [
            rr:template "{@sd-nace-item-no}{$.naeringskode2.kode}" ;
            rr:termType rr:IRI ;
            dr:condition [
               rr:column "$.naeringskode2.kode" ;
               drop:neq "00.000" ;
            ] ;
         ] ;
         rr:objectMap [
            rr:template "{@sd-nace-item-no}{$.naeringskode3.kode}" ;
            rr:termType rr:IRI ;
            dr:condition [
               rr:column "$.naeringskode3.kode" ;
               drop:neq "00.000" ;
            ] ;
         ] ;
      ]  ;
   
      # AUXILIARY ACTIVITY   
      rr:predicateObjectMap [
         rr:predicate legal:companyActivity ;
         rr:predicate sd-voc:auxiliaryActivity ;
         rr:objectMap [
            rr:template "{@sd-nace-item-no}{$.hjelpeenhetskode.kode}" ;
            rr:termType rr:IRI ;
            dr:condition [
               rr:column "$.hjelpeenhetskode.kode" ;
               drop:neq "00.000" ;
            ] ;
         ] ;
      ]  ;

      # WEBSITE
      rr:predicateObjectMap [ 
         rr:predicate  schema:url ;
         rr:objectMap  [ 
            rr:column "$.hjemmeside" ;
            rr:datatype  xsd:anyURI ;
         ] ;      
      ] ;  

      rr:predicateObjectMap [ 
         rr:predicateMap [  
            rr:constant sd-voc:postalAddress;
            dr:condition [
               rr:column "$.postadresse.landkode" ;
               drop:neq "";
            ] ;
         ] ;    
#        rr:predicateMap [  
#           rr:constant m8g:registeredAddress;
#           dr:condition [
#              rr:column "$.postadresse.landkode" ;
#              drop:neq "";
#           ] ;
#        ] ;    
         rr:objectMap [
                   rr:parentTriplesMap [
                     rr:subjectMap [
                        rr:template "{@sd-address-no}{$.organisasjonsnummer}/p";
                        rr:class locn:Address ;
                    ] ;

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

                    rr:predicateObjectMap [ 
                       rr:predicate  locn:fullAddress ;
                       rr:objectMap  [ 
                          rr:language  "no" ;
                          dr:cases ( [
                             rr:template  "<<{FULL_POSTAL_ADDRESS}, >>{$.postadresse.poststed}, {$.postadresse.land}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column  "$.postadresse.landkode" ;
                                drop:neq "NO" ;
                             ] ;
                           ] [
                              rr:template  "<<{FULL_POSTAL_ADDRESS}, >>{$.postadresse.postnummer} {$.postadresse.poststed}, {$.postadresse.land}" ;
                              rr:termType  rr:Literal ;
                              dr:condition [
                                 rr:column  "$.postadresse.landkode" ;
                                 drop:eq "NO" ;
                              ] ;     
                           ] ) ;
                        ] ; 
                       rr:objectMap  [ 
                          rr:language  "en" ;
                          dr:cases ( [
                             rr:template  "<<{FULL_POSTAL_ADDRESS}, >>{$.postadresse.poststed}, {PostalCountryTransformation~~$.['label-en'][0]}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column  "$.postadresse.landkode" ;
                                drop:neq "NO" ;
                             ] ;
                           ] [
                             rr:template  "<<{FULL_POSTAL_ADDRESS}, >>{$.postadresse.postnummer} {$.postadresse.poststed}, {PostalCountryTransformation~~$.['label-en'][0]}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column  "$.postadresse.landkode" ;
                                drop:eq "NO" ;
                             ] ;
                           ] ) ;
                        ] ; 

                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:postCode ;
                       rr:objectMap  [ 
                          rr:column "$.postadresse.postnummer" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:postName ;
                       rr:objectMap  [ 
                          rr:column "$.postadresse.poststed" ;
                          rr:termType  rr:Literal ;
                          dr:condition [
                             rr:column  "$.postadresse.landkode" ;
                             drop:eq "NO" ;
                          ] ;                          
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:adminUnitL1 ;
                       rr:objectMap  [ 
                          rr:column "$.postadresse.landkode" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:adminUnitL2 ;
                       rr:objectMap  [ 
                          rr:column "$.postadresse.kommune" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;                     
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:column "PostalCountryTransformation~~$.nal[0]" ;
                          rr:termType  rr:IRI ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:template "{@nuts-item}{PostalPostCodeTransformation~~$.NUTS3}" ;
                          rr:termType  rr:IRI ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:template "{@lau-item}{PostalPostCodeTransformation~~$.LAU}" ;
                          rr:termType  rr:IRI ;
                       ] ;      
                    ] ;
                 ] ;   
         ] ;
      ] ;

      rr:predicateObjectMap [ 
         rr:predicateMap [  
            rr:constant m8g:registeredAddress;
            dr:condition [
               rr:column "$.forretningsadresse.landkode" ;
               drop:neq "";
            ] ;
         ] ;    
#        rr:predicateMap [  
#           rr:constant sd-model:businessAddress;
#           dr:condition [
#              rr:column "$.forretningsadresse.landkode" ;
#              drop:neq "";
#           ] ;
#        ] ;    
         rr:objectMap [
                  rr:parentTriplesMap [
                     rr:subjectMap [
                        rr:template "{@sd-address-no}{$.organisasjonsnummer}/b" ;
                        rr:class locn:Address ;
                    ] ;

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

                    rr:predicateObjectMap [ 
                       rr:predicate  locn:fullAddress ;
                       rr:objectMap  [ 
                          rr:language  "no" ;
                          dr:cases ( [
                             rr:template  "<<{FULL_BUSINESS_ADDRESS}, >>{$.forretningsadresse.poststed}, {$.forretningsadresse.land}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column  "$.forretningsadresse.landkode" ;
                                drop:neq "NO" ;
                             ] ;
                           ] [
                              rr:template  "<<{FULL_BUSINESS_ADDRESS}, >>{$.forretningsadresse.postnummer} {$.forretningsadresse.poststed}, {$.forretningsadresse.land}" ;
                              rr:termType  rr:Literal ;
                              dr:condition [
                                 rr:column  "$.forretningsadresse.landkode" ;
                                 drop:eq "NO" ;
                              ] ;     
                           ] ) ;
                        ] ; 
                       rr:objectMap  [ 
                          rr:language  "en" ;
                          dr:cases ( [
                             rr:template  "<<{FULL_BUSINESS_ADDRESS}, >>{$.forretningsadresse.poststed}, {BusinessCountryTransformation~~$.['label-en'][0]}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column  "$.forretningsadresse.landkode" ;
                                drop:neq "NO" ;
                             ] ;
                           ] [
                             rr:template  "<<{FULL_BUSINESS_ADDRESS}, >>{$.forretningsadresse.postnummer} {$.forretningsadresse.poststed}, {BusinessCountryTransformation~~$.['label-en'][0]}" ;
                             rr:termType  rr:Literal ;
                             dr:condition [
                                rr:column  "$.forretningsadresse.landkode" ;
                                drop:eq "NO" ;
                             ] ;
                           ] ) ;
                        ] ; 

                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:postCode ;
                       rr:objectMap  [ 
                          rr:column "$.forretningsadresse.postnummer" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:postName ;
                       rr:objectMap  [ 
                          rr:column "$.forretningsadresse.poststed" ;
                          rr:termType  rr:Literal ;
                          dr:condition [
                             rr:column  "$.forretningsadresse.landkode" ;
                             drop:eq "NO" ;
                          ] ;                          
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:adminUnitL1 ;
                       rr:objectMap  [ 
                          rr:column "$.forretningsadresse.landkode" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  locn:adminUnitL2 ;
                       rr:objectMap  [ 
                          rr:column "$.forretningsadresse.kommune" ;
                          rr:termType  rr:Literal ;
                       ] ;      
                    ] ;                     

                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:column "BusinessCountryTransformation~~$.nal[0]" ;
                          rr:termType  rr:IRI ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:template "{@nuts-item}{BusinessPostCodeTransformation~~$.NUTS3}" ;
                          rr:termType  rr:IRI ;
                       ] ;      
                    ] ;   
                    rr:predicateObjectMap [ 
                       rr:predicate  m8g:adminUnit ;
                       rr:objectMap  [ 
                          rr:template "{@lau-item}{BusinessPostCodeTransformation~~$.LAU}" ;
                          rr:termType  rr:IRI ;
                       ] ;      
                    ] ;
             ];
         ] ;
      ] ;
   
      # FOUNDATION DATE (if explicitly given used it, else use registration date)
      rr:predicateObjectMap [ 
         rr:predicate  m8g:registrationDate ;
         rr:objectMap  [ 
            dr:cases ( [
               rr:column "$.stiftelsesdato" ;
               rr:datatype xsd:date ;
               dr:condition [
                  rr:column "$.stiftelsesdato" ;
                  drop:neq "";
               ] ;
            ] [
               rr:column "$.registreringsdatoEnhetsregisteret" ;
               rr:datatype xsd:date ;
            ] )
         ] ;      
      ] ;   
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
           rr:column "PostalCountryTransformation~~$.nal[0]" ;
           rr:class sd-voc-adminlevel:Country ;
      ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@nuts-item}{PostalPostCodeTransformation~~$.NUTS3}";
         rr:class sd-voc-adminlevel:NUTS-3 ;
      ] ;
   ] ;
	  
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@lau-item}{PostalPostCodeTransformation~~$.LAU}";
         rr:class sd-voc-adminlevel:LAU ;
      ] ;
   ] ;

   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
           rr:column "BusinessCountryTransformation~~$.nal[0]" ;
           rr:class sd-voc-adminlevel:Country ;
      ] ;
   ] ;
   
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@nuts-item}{BusinessPostCodeTransformation~~$.NUTS3}";
         rr:class sd-voc-adminlevel:NUTS-3 ;
      ] ;
   ] ;
	  
   dr:triplesMap [
      dr:logicalOutput <#AdminLevelModel> ;
      rr:subjectMap [ 
         rr:template "{@lau-item}{BusinessPostCodeTransformation~~$.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 ;
         ]
      ] ;   
   ] .