<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- $This file is distributed under the terms of the license in LICENSE$ -->

<!-- See guidelines at https://wiki.duraspace.org/x/eYXVAw -->

<list-view-config>
    <query-select>

        PREFIX core:  &lt;http://vivoweb.org/ontology/core#&gt;
        PREFIX foaf: &lt;http://xmlns.com/foaf/0.1/&gt;
        PREFIX rdfs:  &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
        PREFIX vitro: &lt;http://vitro.mannlib.cornell.edu/ns/vitro/0.7#&gt;

        SELECT DISTINCT
            ?role
            ?activity ?activityName
            ?activityLabel
            ?awardedBy
            ?adminedBy
            ?awardedByLabel
            ?adminedByLabel
            ?dateTimeStartRole ?dateTimeEndRole
            ?dateTimeStartGrant ?dateTimeEndGrant
            ?hideThis

        WHERE
        {
            ?subject ?property ?role .
            ?role a core:PrincipalInvestigatorRole .

            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Contract .
                ?activity rdfs:label ?activityLabel .
                LET (?activityName := REPLACE(STR(?activity),"^.*(#)(.*)$", "$2")) .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Contract .
                ?activity core:assignedBy ?awardedBy .
                ?awardedBy rdfs:label ?awardedByLabel .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Contract .
                ?activity core:relates ?adminedByRole .
                ?adminedByRole &lt;http://purl.obolibrary.org/obo/RO_0000052&gt; ?adminedBy .
                ?adminedBy rdfs:label ?adminedByLabel .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Grant .
                ?activity rdfs:label ?activityLabel .
                LET (?activityName := REPLACE(STR(?activity),"^.*(#)(.*)$", "$2")) .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Grant .
                ?activity core:assignedBy ?awardedBy .
                ?awardedBy rdfs:label ?awardedByLabel .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Grant .
                ?activity core:relates ?adminedByRole .
                ?adminedByRole a core:AdminsitratorRole.
                ?adminedByRole &lt;http://purl.obolibrary.org/obo/RO_0000052&gt; ?adminedBy .
                ?adminedBy a foaf:Organization .
                ?adminedBy rdfs:label ?adminedByLabel .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:dateTimeInterval ?dateTimeIntervalRole .
                ?dateTimeIntervalRole core:start ?dateTimeStartValueRole .
                ?dateTimeStartValueRole core:dateTime ?dateTimeStartRole .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:dateTimeInterval ?dateTimeIntervalRole .
                ?dateTimeIntervalRole core:end ?dateTimeEndValueRole .
                ?dateTimeEndValueRole core:dateTime ?dateTimeEndRole .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Contract .
                ?activity core:dateTimeInterval ?dateTimeIntervalGrant .
                ?dateTimeIntervalGrant core:start ?dateTimeStartValueGrant .
                ?dateTimeStartValueGrant core:dateTime ?dateTimeStartGrant .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Contract .
                ?activity core:dateTimeInterval ?dateTimeIntervalGrant .
                ?dateTimeIntervalGrant core:end ?dateTimeEndValueGrant .
                ?dateTimeEndValueGrant core:dateTime ?dateTimeEndGrant .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Grant .
                ?activity core:dateTimeInterval ?dateTimeIntervalGrant .
                ?dateTimeIntervalGrant core:start ?dateTimeStartValueGrant .
                ?dateTimeStartValueGrant core:dateTime ?dateTimeStartGrant .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:relatedBy ?activity .
                ?activity a core:Grant .
                ?activity core:dateTimeInterval ?dateTimeIntervalGrant .
                ?dateTimeIntervalGrant core:end ?dateTimeEndValueGrant .
                ?dateTimeEndValueGrant core:dateTime ?dateTimeEndGrant .
            }
            OPTIONAL {
                <precise-subquery>?subject ?property ?role .
                ?role a core:PrincipalInvestigatorRole .</precise-subquery>
                ?role core:hideFromDisplay ?hideThis .
            }
        } ORDER BY DESC(?dateTimeEndRole) DESC(?dateTimeStartRole) DESC(?dateTimeEndGrant) DESC(?dateTimeStartGrant) ?activityLabel ?activityName
    </query-select>

    <template>propStatement-hasInvestigatorRole.ftl</template>
</list-view-config>
