@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix vitro:   <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix ts_:    <https://litvinovg.pro/text_structures#> .
@prefix display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> .

 
<https://litvinovg.pro/text_structures#elenphArticle> display:hasDataGetter display:getArticleExcerptsContentDataGetter .

<https://litvinovg.pro/text_structures#elenphArticle> display:hasDataGetter display:getArticleExcerptPathsDataGetter .

<https://litvinovg.pro/text_structures#elenphArticle> display:hasDataGetter display:getArticleWorksDataGetter .

<https://litvinovg.pro/text_structures#elenphArticle> display:hasDataGetter display:getArticleBibliographyDataGetter .

<https://litvinovg.pro/text_structures#elenphArticle> display:hasDataGetter display:getArticleAuthorsDataGetter .

<https://litvinovg.pro/text_structures#elenphArticle> display:hasDataGetter display:getArticleRubricsDataGetter .

display:getArticleExcerptsContentDataGetter
    a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
    display:saveToVar "excerpts";
    display:query
        """
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX ts_:  <https://litvinovg.pro/text_structures#> 
        SELECT ?elenphExcerpt ?htmlLabel ?htmlContent (COUNT (distinct ?excerptWorks) as ?worksCounter) (COUNT (distinct ?excerptBibliography) as ?bibliographyCounter)
        WHERE {
              ?individualURI ts_:hasTOC ?toc .
              ?toc (ts_:hasTOCItem/ts_:pointsTo)+/ts_:hasText ?elenphExcerpt .
              ?elenphExcerpt ts_:htmlExcerpt ?htmlContent .
              ?elenphExcerpt rdfs:label ?htmlLabel .
  			  OPTIONAL{ ?elenphExcerpt ts_:works ?excerptWorks . }
              OPTIONAL{ ?elenphExcerpt ts_:bibliography ?excerptBibliography . }
        } GROUP BY ?elenphExcerpt ?htmlLabel ?htmlContent ORDER BY ?elenphExcerpt
        """ .

display:getArticleExcerptPathsDataGetter
    a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
    display:saveToVar "paths";
    display:query
        """
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX ts_: <https://litvinovg.pro/text_structures#>
        SELECT (GROUP_CONCAT(substr(concat("000",str(?outline)),strlen(str(?outline))); SEPARATOR = ".") AS ?path ) (COUNT(?outline) AS ?level) ?tocElement ?elenphExcerpt
        WHERE {
              ?individualURI ts_:hasTOC ?toc .
              ?toc ts_:hasTOCItem/(ts_:pointsTo/ts_:hasTOCItem)* ?tmpTOCItem .
              ?tmpTOCItem rdf:type ts_:TOCItem .
              ?tmpTOCItem ts_:pointsTo/(ts_:hasTOCItem/ts_:pointsTo)* ?tocElement .
              ?tmpTOCItem ts_:itemNumber ?outline .
              OPTIONAL {
                       ?tocElement ts_:hasText ?elenphExcerpt .
              }
              {
                SELECT ?elenphArticle ?tocElement
                WHERE {
                      ?individualURI ts_:hasTOC ?toc .
                      ?toc (ts_:hasTOCItem/ts_:pointsTo)+ ?tocElement .
                      ?tocElement rdf:type ts_:TOCLevel .
                }
              }
        } GROUP BY ?tocElement ?elenphExcerpt ORDER BY ?path
        """ .

display:getArticleBibliographyDataGetter
    a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
    display:saveToVar "bibliography";
    display:query
        """
		PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX ts_:  <https://litvinovg.pro/text_structures#> 
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        SELECT DISTINCT ?bibliography ?excerpt
        WHERE {
              ?individualURI ts_:hasTOC ?toc .
              ?toc (ts_:hasTOCItem/ts_:pointsTo)+/ts_:hasText ?excerpt .
              ?excerpt ts_:bibliography ?bibliography .
        } ORDER BY ?bibliography
        """ .

display:getArticleWorksDataGetter
    a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
    display:saveToVar "works";
    display:query
        """
		PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX ts_:  <https://litvinovg.pro/text_structures#> 
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        SELECT DISTINCT ?works ?excerpt
        WHERE {
              ?individualURI ts_:hasTOC ?toc .
              ?toc (ts_:hasTOCItem/ts_:pointsTo)+/ts_:hasText ?excerpt .
              ?excerpt ts_:works ?works .
        } ORDER BY ?works
        """ .

display:getArticleAuthorsDataGetter
    a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
    display:saveToVar "authors";
    display:query
        """
		PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
		PREFIX ts_:  <https://litvinovg.pro/text_structures#> 
		PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
		SELECT DISTINCT ?authorInitials ?authorFamily ?authorGivenName ?orgName ?orgPostalCode ?orgAddress
		       WHERE {
                       ?individualURI ts_:hasTOC ?toc .
                       ?toc (ts_:hasTOCItem/ts_:pointsTo)+/ts_:hasText ?elenphExcerpt .
		               ?elenphExcerpt ts_:hasAuthor ?participant .
                       ?participant ts_:participantFamily ?authorFamily .
                       ?participant ts_:participantInitials ?authorInitials .
                       ?participant ts_:participantGivenName ?authorGivenName .
                  OPTIONAL { ?participant ts_:affiliatedWith ?organization .
                    OPTIONAL { ?organization ts_:officialOrganizationName ?orgName } .
                    OPTIONAL { ?organization ts_:organizationPostalCode ?orgPostalCode } .
                    OPTIONAL { ?organization ts_:organizationAddress ?orgAddress } .
                  }
                
		} ORDER BY ?authorFamily
        """ .

display:getArticleRubricsDataGetter
    a <java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter>;
    display:saveToVar "rubrics";
    display:query
        """
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX ts_:  <https://litvinovg.pro/text_structures#> 
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX pr:   <https://iph.ras.ru/relationships#>
    SELECT DISTINCT ?rubricID ?rubricName
	WHERE {
      ?uri ts_:hasTOC ?toc .
      ?toc (ts_:hasTOCItem/ts_:pointsTo)+/ts_:hasText ?artExcerpt .
      ?artExcerpt pr:belongsTo ?rubricID .
      ?rubricID rdfs:label ?rubricName .
    } ORDER BY ?rubricID
    """ .


