//ATTENZIONE: ALCUNE QUERY HANNO LO STESSO NOME! CAMBIARE I NOMI

prefixes = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/> \
PREFIX owl: <http://www.w3.org/2002/07/owl#> \
PREFIX schema: <http://schema.org/> \
PREFIX foaf: <http://xmlns.com/foaf/0.1/> \
PREFIX person: <http://www.w3.org/ns/person#>"

//QUERY DA INTERFACCIA DI RICERCA
//search_graph è il grafo in cui effettuare la ricerca e selezionato attraverso il menu a tendina dell'interfaccia di ricerca
//search_name è la stringa passata dall'input nell'interfaccia di ricerca
queryMMO = prefixes + " SELECT DISTINCT "+ search_graph +" AS ?graph ?subject ?label ?object\
WHERE {GRAPH "+ search_graph +" { ?subject ?property ?object }\
?subject rdf:type crm:E22_Man-Made_Object ;\
rdfs:label ?label ;\
?property ?object .\
?object bif:contains \"\' "+ search_name + " \'\"  .\
}"

queryInfObj = prefixes + " SELECT DISTINCT "+ search_graph +" AS ?graph (SAMPLE(?type) AS ?typeName) ?subject ?label ?object \
WHERE {GRAPH "+ search_graph +" { ?subject ?property ?object }\
?subject rdf:type crm:E73_Information_Object ;\
rdfs:label ?label ;\
?property ?object .\
?object bif:contains \"\' "+ search_name + " \'\"  .\
OPTIONAL {?subject crm:P2_has_type ?entity_type . \
?entity_type rdfs:label ?type .}\
} \
GROUP BY ?subject ?object ?label "+ gg + ""

queryPerson = prefixes + " SELECT DISTINCT "+ search_graph +" AS ?graph ?subject ?label \
WHERE {GRAPH "+ search_graph +" { ?subject rdfs:label ?label ;\
rdf:type crm:E21_Person .\
?label bif:contains \"\' "+ search_name + " \'\"  .}\
}"

queryPlace = prefixes + " SELECT DISTINCT "+ search_graph +" AS ?graph ?subject ?label \
WHERE {GRAPH "+ search_graph +" { ?subject rdfs:label ?label ;\
rdf:type crm:E53_Place .\
?label bif:contains \"\' "+ search_name + " \'\"  .}\
}"


//QUERY SCHEDA LETTERA
//thisUrlParams.link è passato dalla url della pagina ed è il link della risorsa
query1 = prefixes + " SELECT DISTINCT ?mittente ?destinatario ?data_partenza ?data_arrivo ?luogo_partenza ?luogo_arrivo \
WHERE {?subject crm:P128_carries <" + thisUrlParams.link + "> \
GRAPH <http://dev.restore.ovi.cnr.it:8890/ovi/datini> {?subject crm:P25i_moved_by ?mov_ev .} \
?send rdfs:subClassOf ?mov_ev ; \
rdf:type crm:EL2_Send_Letter ; \
crm:P4_has_time-span ?time_spanA; \
crm:P27_moved_from ?placeA; \
crm:P01_has_domain ?sender . \
\
?time_spanA rdfs:label ?data_partenza . \
?placeA rdfs:label ?luogo_partenza . \
?sender crm:P02_has_range ?mittente . \
 \
?receive rdfs:subClassOf ?mov_ev; \
rdf:type crm:EL3_Receive_Letter ; \
crm:P4_has_time-span ?time_spanB; \
crm:P26_moved_to ?placeB; \
crm:P01_has_domain ?receiver . \
 \
?time_spanB rdfs:label ?data_arrivo . \
?placeB rdfs:label ?luogo_arrivo . \
?receiver crm:P02_has_range ?destinatario . \
}"

query2 = prefixes + " SELECT DISTINCT ?uriSender ?mittente ?uriReceiver ?destinatario ?timeSpanSend ?timeSpanReceive ?placeSend ?placeReceive ?identifier ?material ?dimension ?currentLocation \
WHERE {?subject crm:P128_carries <" + thisUrlParams.link + "> . \
GRAPH <http://dev.restore.ovi.cnr.it:8890/aspo/datini> {?subject crm:P25i_moved_by ?mov_ev .} \
?send rdfs:subClassOf ?mov_ev ; \
rdf:type crm:EL2_Send_Letter . \
?receive rdfs:subClassOf ?mov_ev ; \
rdf:type crm:EL3_Receive_Letter . \
\
OPTIONAL {?subject crm:P54_has_current_permanent_location ?uriCurrentLocation . \
?uriCurrentLocation rdfs:label ?currentLocation . } \
OPTIONAL {?subject crm:P45_consist_of ?uriMaterial . \
?uriMaterial rdfs:label ?material . } \
OPTIONAL {?subject crm:P43_has_dimension ?uriDimension . \
?uriDimension rdfs:label ?dimension . } \
OPTIONAL {?subject crm:P1_is_identified_by ?uriIdentifier . \
?uriIdentifier rdfs:label ?identifier . } \
\
OPTIONAL {?send crm:P4_has_time-span ?time_spanS . \
?time_spanS rdfs:label ?timeSpanSend } \
OPTIONAL {?receive crm:P4_has_time-span ?time_spanR . \
?time_spanR rdfs:label ?timeSpanReceive } \
\
OPTIONAL {?send crm:P27_moved_from ?placeS . \
?placeS rdfs:label ?placeSend } \
OPTIONAL {?receive crm:P26_moved_to ?placeR . \
?placeR rdfs:label ?placeReceive } \
\
OPTIONAL {?send crm:P01_has_domain ?pcS . \
?pcS crm:P02_has_range ?uriSender . \
?uriSender rdfs:label ?mittente } \
\
OPTIONAL {?receive crm:P01_has_domain ?pcR . \
?pcR crm:P02_has_range ?uriReceiver . \
?uriReceiver rdfs:label ?destinatario } \
}"

querySegnatura = prefixes + "SELECT DISTINCT ?segnatura_OVI \
WHERE {?subject crm:P128_carries <" + thisUrlParams.link + ">; \
crm:P1_is_identified_by ?segnatura_ASPO . \
?segnatura_ASPO crm:P139_has_alternative_form ?segnatura . \
?segnatura crm:P2_has_type ?tipo_segnatura; \
rdfs:label ?segnatura_OVI . \
?tipo_segnatura rdfs:label \"Segnatura OVI\"}"

queryAreaLinguistica = prefixes + " SELECT DISTINCT ?lingua ?area_linguistica \
WHERE {<" + thisUrlParams.link + "> crm:P72_has_language ?language . \
?language crm:P3_has_note ?area ; \
rdfs:label ?lingua . \
?area rdfs:label ?area_linguistica \
}"


queryDescrizione = prefixes + " SELECT DISTINCT ?descrizione \
WHERE {<" + thisUrlParams.link + "> crm:P3_has_note ?description . \
?description rdfs:label ?descrizione \
}"

queryTipo = prefixes + " SELECT DISTINCT ?tipologia \
WHERE {<" + thisUrlParams.link + "> crm:P2_has_type ?type . \
?type rdf:type crm:E55_Type; \
rdfs:label ?tipologia . \
}"

querySiglaOVI = prefixes + " SELECT DISTINCT ?sigla_OVI \
WHERE {<" + thisUrlParams.link + "> crm:P1_is_identified_by ?id . \
?id rdf:type crm:E42_Identifier; \
crm:P2_has_type ?type ; \
rdfs:label ?sigla_OVI . \
?type rdfs:label 'Sigla OVI'. \
}"

queryTitolo = prefixes + " SELECT DISTINCT ?titolo \
WHERE {<" + thisUrlParams.link + "> crm:P1_is_identified_by ?title . \
?title rdf:type crm:E35_Title; \
rdfs:label ?titolo . \
}"

queryTestoLemmatizzato = prefixes + " SELECT DISTINCT ?testo_lemmatizzato \
WHERE {<" + thisUrlParams.link + "> crm:P190_has_symbolic_content ?testo_lemmatizzato . \
}"

queryEdizione = prefixes + " SELECT DISTINCT ?edizione ?edizione_abbreviata \
WHERE {?edition crm:P70_documents <" + thisUrlParams.link + "> ; \
crm:P1_is_identified_by ?edition_id . \
?edition_id rdfs:label ?edizione; \
crm:P139_has_alternative_form ?ed_abbr . \
?ed_abbr rdfs:label ?edizione_abbreviata \
}"

queryRaccolta = prefixes + " SELECT DISTINCT ?raccolta \
WHERE {?racc crm:P148_has_component <" + thisUrlParams.link + "> ; \
crm:P2_has_type ?racc_type ; \
rdfs:label ?raccolta . \
?racc_type rdfs:label 'Raccolta'. \
}"

queryToponimi = prefixes + "SELECT DISTINCT ?link_toponimo ?toponimo \
WHERE {<" + thisUrlParams.link + "> crm:P67_refers_to ?link_toponimo . \
?link_toponimo rdfs:label ?toponimo ; \
crm:P2_has_type 'Toponimo' . \
}"

queryAntroponimi = prefixes + "SELECT DISTINCT * \
WHERE {<" + thisUrlParams.link + "> crm:P67_refers_to ?link_antroponimo . \
?link_antroponimo rdfs:label ?antroponimo; \
crm:P2_has_type 'Antroponimo'}"

//QUERY SCHEDA NLP
//thisUrlParams.link è passato dalla url della pagina ed è il link della risorsa

queryTesto = prefixes + " SELECT DISTINCT ?titolo ?testo WHERE {?subject crm:P128_carries <" + thisUrlParams.link + ">;\
crm:P128_carries ?object .\
?object rdf:type crm:E33_Linguistic_Object;\
crm:P190_has_symbolic_content ?testo;\
rdfs:label ?titolo .}"

//QUERY SCHEDA LUOGO
//thisUrlParams.link è passato dalla url della pagina ed è il link della risorsa

query = prefixes + " SELECT DISTINCT ?graph ?name_place ?coordinates {\
GRAPH ?graph {<" + thisUrlParams.link + "> crm:P168_place_is_defined_by ?coordinates;\
rdfs:label ?name_place .\
}\
}"

queryRiferimenti = prefixes + " SELECT DISTINCT ?references {\
<" + thisUrlParams.link + "> owl:sameAs ?references\
}"

queryToponimi = prefixes + " SELECT DISTINCT ?toponimi {\
<" + thisUrlParams.link + "> crm:P1_is_identified_by ?uri_toponym .\
?uri_toponym rdfs:label ?toponimi\
}"

queryRicezione = prefixes + " SELECT DISTINCT ?object ?label {\
<" + thisUrlParams.link + "> owl:sameAs ?place .\
?event_to crm:P26_moved_to ?place ;\
rdf:type crm:EL3_Receive_Letter ;\
rdfs:subClassOf ?event .\
?object crm:P25i_moved_by ?event ; \
rdfs:label ?label .\
} "

queryInvio = prefixes + " SELECT DISTINCT ?object ?label {\
<" + thisUrlParams.link + "> owl:sameAs ?place .\
?event_to crm:P27_moved_from ?place ;\
rdf:type crm:EL2_Send_Letter ;\
rdfs:subClassOf ?event .\
?object crm:P25i_moved_by ?event ; \
rdfs:label ?label .\
}"

queryCitazione = prefixes + " SELECT DISTINCT ?object ?label\
{<" + thisUrlParams.link + "> crm:P1_is_identified_by ?toponym .\
?object crm:P67_refers_to ?toponym ;\
rdfs:label ?label\
}"

queryPersone = prefixes + " SELECT DISTINCT ?range ?label \
WHERE{ \
  {?place owl:sameAs <" + thisUrlParams.link + "> .\
  ?event_to crm:P26_moved_to ?place ;\
  rdf:type crm:EL3_Receive_Letter ;\
  crm:P01_has_domain ?domain .\
  ?domain crm:P02_has_range ?range .\
  ?range rdfs:label ?label .\
} UNION {\
  ?place owl:sameAs <" + thisUrlParams.link + "> .\
  ?event_to crm:P27_moved_from ?place ;\
  rdf:type crm:EL2_Send_Letter ;\
  crm:P01_has_domain ?domain .\
  ?domain crm:P02_has_range ?range .\
  ?range rdfs:label ?label .\
  }\
}"

queryCount = prefixes + " SELECT ?place ?label COUNT(?label) AS ?Count \
WHERE{ \
?place_to owl:sameAs <" + thisUrlParams.link + "> . \
?event_to crm:P26_moved_to ?place_to ; \
rdf:type crm:EL3_Receive_Letter ; \
rdfs:subClassOf ?event . \
?event_from rdfs:subClassOf ?event ; \
rdf:type crm:EL2_Send_Letter ; \
crm:P27_moved_from ?place . \
?place rdfs:label ?label \
} \
ORDER BY DESC (?Count)"

queryCount2 = prefixes + " SELECT ?place ?label COUNT(?label) AS ?Count \
WHERE{ \
?place_from owl:sameAs <" + thisUrlParams.link + "> . \
?event_from crm:P27_moved_from ?place_from ; \
rdf:type crm:EL2_Send_Letter ; \
rdfs:subClassOf ?event . \
?event_to rdfs:subClassOf ?event ; \
rdf:type crm:EL2_Send_Letter ; \
crm:P27_moved_from ?place . \
?place rdfs:label ?label . \
} \
ORDER BY DESC (?Count) "


//QUERY SCHEDA PERSONA
//thisUrlParams.link è passato dalla url della pagina ed è il link della risorsa

query = prefixes + " SELECT DISTINCT ?place ?label ?coordinates \
WHERE { \
{?pc crm:P02_has_range <" + thisUrlParams.link + "> . \
?event_from crm:P01_has_domain ?pc ; \
rdf:type crm:EL3_Receive_Letter; \
crm:P26_moved_to ?place_from . \
?place_from rdf:type crm:E53_Place ; \
owl:sameAs ?place . \
?place rdfs:label ?label ; \
crm:P168_place_is_defined_by ?coordinates . \
} UNION { \
?pc crm:P02_has_range <" + thisUrlParams.link + "> . \
?event_from crm:P01_has_domain ?pc ; \
rdf:type crm:EL2_Send_Letter; \
crm:P27_moved_from ?place_from . \
?place_from rdf:type crm:E53_Place ; \
owl:sameAs ?place . \
?place rdfs:label ?label ; \
crm:P168_place_is_defined_by ?coordinates . \
} \
}"

queryInfo = prefixes + " SELECT DISTINCT ?graph ?label ?identifier ?name ?givenName ?familyName ?gender ?Birth_Date ?Birth_Place ?Death_Date ?Death_Place ?patronymic ?occupation (group_concat(distinct ?relative1 ;separator=', ') as ?relatives) ?qualification ?group \
WHERE { \
VALUES ?uri {<" + thisUrlParams.link + ">} \
GRAPH ?graph {?uri rdfs:label ?label} \
?uri foaf:name ?name . \
OPTIONAL {?uri crm:P1_is_identified_by ?id . \
?id rdfs:label ?identifier } \
OPTIONAL {?uri foaf:givenName ?givenName} \
OPTIONAL {?uri foaf:familyName ?familyName} \
OPTIONAL {?uri foaf:gender ?gender} \
OPTIONAL {?uri person:patronymicName ?patronymic } \
OPTIONAL {?uri schema:hasOccupation ?uriOccupation . \
?uriOccupation rdf:type schema:Occupation; \
rdfs:label ?occupation } \
OPTIONAL {?uri schema:honorificPrefix ?qualification} \
OPTIONAL {?uri schema:relatedTo ?uriRel1 . \
?uriRel1 rdfs:label ?relative1} \
OPTIONAL {?uri crm:P100i_died_in ?Death . \
?Death crm:P4_has_time-span ?Death_TS; \
crm:P7_took_place_at ?Place_D .\
?Death_TS rdfs:label ?Death_Date . \
?Place_D rdfs:label ?Death_Place } \
OPTIONAL {?uri crm:P98i_was_born ?Birth . \
?Birth crm:P4_has_time-span ?Birth_TS; \
crm:P7_took_place_at ?Place_B . \
?Birth_TS rdfs:label ?Birth_Date . \
?Place_B rdfs:label ?Birth_Place } \
OPTIONAL {?uri crm:P107i_is_current_or_former_member_of ?uriGroup . \
?uriGroup rdfs:label ?group } \
} \
GROUP BY ?graph ?label ?identifier ?name ?givenName ?familyName ?gender ?Birth_Date ?Birth_Place ?Death_Date ?Death_Place ?patronymic ?occupation ?qualification ?group "

queryLetters = prefixes + " SELECT DISTINCT ?type ?document_uri ?document_name \
WHERE {?pc crm:P02_has_range <" + thisUrlParams.link + "> . \
?ev_send crm:P01_has_domain ?pc ; \
rdfs:label ?type ; \
rdfs:subClassOf ?event . \
?document_uri crm:P25i_moved_by ?event ; \
rdfs:label ?document_name . \
}"

queryNetwork = prefixes + " SELECT DISTINCT ?uri ?label ?uri2 ?label2 \
WHERE { \
{?event rdf:type crm:EL1_Exchange_Letters . \
?event_to rdfs:subClassOf ?event; \
rdf:type crm:EL2_Send_Letter ; \
crm:P01_has_domain ?pc_to . \
?pc_to crm:P02_has_range ?uri . \
?uri rdfs:label ?label . \
?event_from rdfs:subClassOf ?event; \
rdf:type crm:EL3_Receive_Letter; \
crm:P01_has_domain ?pc_from . \
?pc_from crm:P02_has_range ?uri2 . \
?uri2 rdfs:label ?label2 . \
FILTER (?uri = <" + thisUrlParams.link + ">) \
} UNION { \
?event rdf:type crm:EL1_Exchange_Letters . \
?event_to rdfs:subClassOf ?event; \
rdf:type crm:EL3_Receive_Letter ; \
crm:P01_has_domain ?pc_from . \
?pc_from crm:P02_has_range ?uri . \
?uri rdfs:label ?label . \
?event_from rdfs:subClassOf ?event; \
rdf:type crm:EL2_Send_Letter; \
crm:P01_has_domain ?pc_to . \
?pc_to crm:P02_has_range ?uri2 . \
?uri2 rdfs:label ?label2 . \
FILTER (?uri = <" + thisUrlParams.link + ">) \
} \
} LIMIT 30"


//QUERY SCHEDA OPERA D'ARTE
//thisUrlParams.link è passato dalla url della pagina ed è il link della risorsa

queryInfo = prefixes + " SELECT DISTINCT ?graph ?identifier ?label ?title ?uriType ?type ?uriOwner ?current_owner ?current_location ?subject (group_concat(distinct ?dimension ; separator='<br />') as ?dimensions) (GROUP_CONCAT(DISTINCT CONCAT(?uriMaterial, '; ', ?material) ; SEPARATOR = '<br />') AS ?Materials) ?condition ?note \
WHERE { \
VALUES ?uri {<" + thisUrlParams.link + ">} \
GRAPH ?graph {?uri rdfs:label ?label} \
OPTIONAL {?uri crm:P3_has_note ?note } \
?uri crm:P128_carries ?Inf_Obj . \
OPTIONAL {?Inf_Obj crm:P1_is_identified_by ?uriTitle . \
?uriTitle rdf:type crm:E35_Title; \
rdfs:label ?title } \
OPTIONAL {?Inf_Obj crm:P2_has_type ?uriType . \
?uriType rdfs:label ?type } \
OPTIONAL {?uri crm:P43_has_dimension ?uriDimension . \
?uriDimension rdfs:label ?dimension } \
OPTIONAL {?uri crm:P1_is_identified_by ?uriIdentifier . \
?uriIdentifier rdfs:label ?identifier } \
OPTIONAL {?uri crm:P45_consists_of ?uriMaterial . \
?uriMaterial rdfs:label ?material } \
OPTIONAL {?uri crm:P44_has_condition ?uriCondition . \
?uriCondition crm:P2_has_type ?condition} \
OPTIONAL {?uri crm:P54_has_current_permanent_location ?current_location } \
OPTIONAL {?uri crm:P62_depicts ?uriSubject . \
?uriSubject rdfs:label ?subject; \
crm:P2_has_type 'Identificazione Iconografica' .} \
OPTIONAL {?uri crm:P52_has_current_owner ?uriOwner . \
?uriOwner rdfs:label ?current_owner } \
}"

queryProduction = prefixes + " SELECT DISTINCT (GROUP_CONCAT(DISTINCT CONCAT(?uriPerson, '; ', ?person, '; ', ?role) ; SEPARATOR = '<br />') AS ?Partecipants) (group_concat(distinct ?time_span ;separator='-') as ?time) (GROUP_CONCAT(DISTINCT CONCAT(?uriTecne, '; ', ?technique) ; SEPARATOR = '<br />') AS ?techniques) \
WHERE { \
VALUES ?uri {<" + thisUrlParams.link + ">} \
?uri crm:P128_carries ?Information_Object . \
?Information_Object crm:P108i_was_produced_by ?Production . \
OPTIONAL {?Production crm:P4_has_time-span ?uriTS ; \
crm:P32_used_general_technique ?uriTecne . \
?uriTS rdfs:label ?time_span . \
?uriTecne rdfs:label ?technique} \
OPTIONAL {?pc crm:P01_has_domain ?Production ; \
crm:P02_has_range ?uriPerson ; \
crm:P14.1_in_the_role_of ?uriRole . \
?uriPerson rdfs:label ?person . \
?uriRole rdfs:label ?role} \
}"