@prefix this: <https://w3id.org/np/RAWD7diDNUAR0DLMS5gUMCyk9O6wAu7wRkfTGtS_Hf9Uo> .
@prefix sub: <https://w3id.org/np/RAWD7diDNUAR0DLMS5gUMCyk9O6wAu7wRkfTGtS_Hf9Uo/> .
@prefix np: <http://www.nanopub.org/nschema#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix nt: <https://w3id.org/np/o/ntemplate/> .
@prefix npx: <http://purl.org/nanopub/x/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix orcid: <https://orcid.org/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<https://w3id.org/np/RAWD7diDNUAR0DLMS5gUMCyk9O6wAu7wRkfTGtS_Hf9Uo> a np:Nanopublication;
  np:hasAssertion sub:assertion;
  np:hasProvenance sub:provenance;
  np:hasPublicationInfo sub:pubinfo;
  dct:created "2026-06-04T18:30:33Z"^^xsd:dateTime;
  dct:creator orcid:0000-0002-1267-0234;
  dct:license <https://creativecommons.org/licenses/by/4.0/>;
  npx:embeds sub:get-view-displays;
  npx:supersedes <https://w3id.org/np/RAf0zaMl6i5FrTepXQRcJRfdldZw-vrpWqKvFBCcjD1ls>;
  rdfs:label "Get view displays";
  nt:wasCreatedFromProvenanceTemplate <https://w3id.org/np/RA7lSq6MuK_TIC6JMSHvLtee3lpLoZDOqLJCLXevnrPoU>;
  nt:wasCreatedFromPubinfoTemplate <https://w3id.org/np/RA0J4vUn_dekg-U1kK3AOEt02p9mT2WO03uGxLDec1jLw>,
    <https://w3id.org/np/RAoTD7udB2KtUuOuAe74tJi1t3VzK0DyWS7rYVAq1GRvw>, <https://w3id.org/np/RAukAcWHRDlkqxk7H2XNSegc1WnHI569INvNr-xdptDGI>;
  nt:wasCreatedFromTemplate <https://w3id.org/np/RAEFAt-QcFK0ZhqfvlsmS10BnzGJA0xwOICZXkO-ai87k> .

sub:get-view-displays a <https://w3id.org/kpxl/grlc/grlc-query>;
  dct:description "Returns the views to display for a given resource: both standalone view displays and the views contributed by assigned presets (issue #302), unioned and ordered by date so latest-wins override resolution holds across both. Filtered server-side to declarations signed by an admin or maintainer of the owning space, or by the affected user themselves (for an agent's own page). Each referenced view is resolved to its latest version by following the npx:supersedes chain, so ?view is the most recent view definition (no separate latest-version lookup needed by the client). Preset-derived rows have an unbound ?display and carry the resolved ?view plus the assignment's activation mode.";
  dct:license <http://www.apache.org/licenses/LICENSE-2.0>;
  rdfs:label "Get view displays";
  <https://w3id.org/kpxl/grlc/endpoint> <https://w3id.org/np/l/nanopub-query-1.1/repo/type/11daee46fdfff957dc17b46f5dc1a618045afd4f5634d5334ce9db19c3689a3c>;
  <https://w3id.org/kpxl/grlc/sparql> """prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dct:  <http://purl.org/dc/terms/>
prefix np:   <http://www.nanopub.org/nschema#>
prefix npa:  <http://purl.org/nanopub/admin/>
prefix npx:  <http://purl.org/nanopub/x/>
prefix gen:  <https://w3id.org/kpxl/gen/terms/>

select distinct ?display ?view (coalesce(?viewKindOptional, ?view) as ?viewKind)
                ?label ?displayType ?displayMode ?np ?pubkey ?date where {
  values ?_resource_multi_iri {}
  service <https://w3id.org/np/l/nanopub-query-1.1/repo/spaces> {
    graph npa:graph { npa:thisRepo npa:hasCurrentSpaceState ?stateG . }
    {
      graph ?stateG {
        ?_resource_multi_iri npa:isMaintainedBy? ?space .
        ?ri a gen:RoleInstantiation ; npa:forSpace ?space ; npa:forAgent ?authAgent ;
            (npa:inverseProperty|npa:regularProperty) ?roleProp .
        ?authAcct a npa:AccountState ; npa:agent ?authAgent ; npa:pubkey ?pubkey .
      }
      optional {
        graph npa:spacesGraph {
          ?rd a npa:RoleDeclaration ; npa:forSpace ?space ;
              npa:hasRoleType gen:MaintainerRole ;
              (npa:inverseProperty|npa:regularProperty) ?roleProp .
          bind(true as ?isMaintainer)
        }
      }
      filter(?roleProp = gen:hasAdmin || bound(?isMaintainer))
    } union {
      graph ?stateG { ?selfAcct a npa:AccountState ; npa:agent ?_resource_multi_iri ; npa:pubkey ?pubkey . }
    }
  }
  {
    # branch (a): standalone view displays — LOCAL pattern on the endpoint repo
    graph npa:graph {
      ?np npx:hasNanopubType gen:ViewDisplay .
      ?np npa:hasValidSignatureForPublicKeyHash ?pubkey .
      filter not exists { ?npx npx:invalidates ?np ; npa:hasValidSignatureForPublicKeyHash ?pubkey . }
      ?np dct:created ?date .
      ?np npx:embeds ?display .
      ?np np:hasAssertion ?a .
      optional { ?np rdfs:label ?label }
    }
    graph ?a {
      ?display gen:isDisplayOfView ?refView .
      ?display gen:isDisplayFor   ?_resource_multi_iri .
      optional { values ?displayType { gen:PartLevelViewDisplay gen:TopLevelViewDisplay } ?display a ?displayType . }
      optional { values ?displayMode { gen:ActivatedViewDisplay gen:DeactivatedViewDisplay } ?display a ?displayMode . }
    }
  }
  union
  {
    # branch (b): preset-supplied views — wrapped in a sub-SELECT so the remote
    # service does not collapse branch (a). ?display stays unbound.
    select ?refView ?label ?displayType ?displayMode ?np ?pubkey ?date ?_resource_multi_iri {
      service <https://w3id.org/np/l/nanopub-query-1.1/repo/full> {
        graph npa:graph {
          ?np npx:hasNanopubType gen:PresetAssignment .
          ?np npa:hasValidSignatureForPublicKeyHash ?pubkey .
          filter not exists { ?npx npx:invalidates ?np ; npa:hasValidSignatureForPublicKeyHash ?pubkey . }
          ?np dct:created ?date .
          ?np npx:embeds ?assignment .
          ?np np:hasAssertion ?a .
          optional { ?np rdfs:label ?label }
        }
        graph ?a {
          ?assignment gen:isAssignmentFor      ?_resource_multi_iri .
          ?assignment gen:isAssignmentOfPreset ?presetRef .
          optional { values ?displayMode { gen:ActivatedPresetAssignment gen:DeactivatedPresetAssignment } ?assignment a ?displayMode . }
        }
        graph npa:graph { ?presetNp npx:embeds ?presetRef ; np:hasAssertion ?pa . }
        graph ?pa {
          ?presetRef a gen:Preset .
          { ?presetRef gen:hasTopLevelView ?refView . bind(gen:TopLevelViewDisplay as ?displayType) }
          union { ?presetRef gen:hasView ?refView . bind(gen:PartLevelViewDisplay as ?displayType) }
        }
      }
    }
  }
  # Resolve each referenced view to its latest version by following the
  # npx:supersedes chain (in both directions) and keeping the most recent.
  # This lets the client use ?view directly without a separate latest-version lookup.
  optional {
    service <https://w3id.org/np/l/nanopub-query-1.1/repo/type/ec6722efa3b44e0a18aa63afe5964158a1fdb7f0413ea5f23bfddf5c03ca0221> {
      graph npa:graph {
        ?vnp npx:embeds ?refView .
        ?latestNp (npx:supersedes|^npx:supersedes)* ?vnp ;
                  dct:created ?ldate ;
                  npx:embeds ?latestView ;
                  np:hasAssertion ?va .
        filter not exists {
          ?other (npx:supersedes|^npx:supersedes)* ?vnp ; dct:created ?odate .
          filter(?odate > ?ldate)
        }
      }
      graph ?va {
        ?latestView a gen:ResourceView .
        optional { ?latestView dct:isVersionOf ?viewKindOptional . }
      }
    }
  }
  bind(coalesce(?latestView, ?refView) as ?view)
}
order by desc(?date)""" .

sub:assertion prov:wasAttributedTo orcid:0000-0002-1267-0234 .

orcid:0000-0002-1267-0234 foaf:name "Tobias Kuhn" .

sub:sig npx:hasAlgorithm "RSA";
  npx:hasPublicKey "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwUtewGCpT5vIfXYE1bmf/Uqu1ojqnWdYxv+ySO80ul8Gu7m8KoyPAwuvaPj0lvPtHrg000qMmkxzKhYknEjq8v7EerxZNYp5B3/3+5ZpuWOYAs78UnQVjbHSmDdmryr4D4VvvNIiUmd0yxci47dTFUj4DvfHnGd6hVe5+goqdcwIDAQAB";
  npx:hasSignature "Wak+eZpWLi4RsFe+8ijPtZayiWDWnlf7xj6Jc0usJTb10nf4SYdOWiFMat/OiLAUBqPb9+jlvWuJ0yvbsHJQmQ2/vVj6T87ypMAByHeU8nbgLJTNcx3gevE/2DEC3JjqRg2Et9q9BZHnRvXtrnl2nMNl8e3/XHWclAO2kuG6ZcM=";
  npx:hasSignatureTarget <https://w3id.org/np/RAWD7diDNUAR0DLMS5gUMCyk9O6wAu7wRkfTGtS_Hf9Uo>;
  npx:signedBy orcid:0000-0002-1267-0234 .

