Count labels and distinct languages using same query; revert changes to lib-properties.ftl.

This commit is contained in:
Brian Lowe 2021-06-04 16:59:27 +03:00
parent 63639fc852
commit 27353bfb91
2 changed files with 78 additions and 49 deletions

View file

@ -6,14 +6,13 @@ import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.apache.jena.rdf.model.RDFNode;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.jena.query.QuerySolution; import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet; import org.apache.jena.query.ResultSet;
@ -36,6 +35,7 @@ import edu.cornell.mannlib.vitro.webapp.i18n.selection.SelectedLocale;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ExecuteDataRetrieval; import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ExecuteDataRetrieval;
import edu.cornell.mannlib.vitro.webapp.web.beanswrappers.ReadOnlyBeansWrapper; import edu.cornell.mannlib.vitro.webapp.web.beanswrappers.ReadOnlyBeansWrapper;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModelBuilder;
import edu.ucsf.vitro.opensocial.OpenSocialManager; import edu.ucsf.vitro.opensocial.OpenSocialManager;
import freemarker.ext.beans.BeansWrapper; import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateModel; import freemarker.template.TemplateModel;
@ -123,8 +123,10 @@ class IndividualResponseBuilder {
* into the data model: no real data can be modified. * into the data model: no real data can be modified.
*/ */
// body.put("individual", wrap(itm, BeansWrapper.EXPOSE_SAFE)); // body.put("individual", wrap(itm, BeansWrapper.EXPOSE_SAFE));
body.put("labelCount", getLabelCount(itm.getUri(), vreq)); LabelAndLanguageCount labelAndLanguageCount = getLabelAndLanguageCount(
body.put("languageCount", getLanguagesRepresentedCount(itm.getUri(), vreq)); itm.getUri(), vreq);
body.put("labelCount", labelAndLanguageCount.getLabelCount());
body.put("languageCount", labelAndLanguageCount.getLanguageCount());
//We also need to know the number of available locales //We also need to know the number of available locales
body.put("localesCount", SelectedLocale.getSelectableLocales(vreq).size()); body.put("localesCount", SelectedLocale.getSelectableLocales(vreq).size());
body.put("profileType", getProfileType(itm.getUri(), vreq)); body.put("profileType", getProfileType(itm.getUri(), vreq));
@ -289,63 +291,96 @@ class IndividualResponseBuilder {
+ " FILTER isLiteral(?label) \n" + " FILTER isLiteral(?label) \n"
+ "}" ; + "}" ;
// Query that was previously used for a count of labels in all languages // Queries that were previously used for counts via RDFService that didn't
private static String LABEL_COUNT_QUERY = "" // filter results by language. With language filtering, aggregate
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" // functions like COUNT() cannot be used.
+ "SELECT ( str(COUNT(?label)) AS ?labelCount ) WHERE { \n"
+ " ?subject rdfs:label ?label \n"
+ " FILTER isLiteral(?label) \n"
+ "}" ;
private static String DISTINCT_LANGUAGE_QUERY = "" // private static String LABEL_COUNT_QUERY = ""
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" // + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ "SELECT ( str(COUNT(DISTINCT lang(?label))) AS ?languageCount ) WHERE { \n" // + "SELECT ( str(COUNT(?label)) AS ?labelCount ) WHERE { \n"
+ " ?subject rdfs:label ?label \n" // + " ?subject rdfs:label ?label \n"
+ " FILTER isLiteral(?label) \n" // + " FILTER isLiteral(?label) \n"
+ "}" ; // + "}" ;
private static Integer getLabelCount(String subjectUri, VitroRequest vreq) { // private static String DISTINCT_LANGUAGE_QUERY = ""
// + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
// + "SELECT ( str(COUNT(DISTINCT lang(?label))) AS ?languageCount ) WHERE { \n"
// + " ?subject rdfs:label ?label \n"
// + " FILTER isLiteral(?label) \n"
// + "}" ;
private static LabelAndLanguageCount getLabelAndLanguageCount(
String subjectUri, VitroRequest vreq) {
// 1.12.0 Now filtering to only the labels for the current locale so as // 1.12.0 Now filtering to only the labels for the current locale so as
// to be consistent with other editing forms. Because the language // to be consistent with other editing forms. Because the language
// filter can only act on a result set containing actual literals, // filter can only act on a result set containing actual literals,
// we can't do the counting with a COUNT() in the query itself. So // we can't do the counting with a COUNT() in the query itself. So
// we will now use the LABEL_QUERY instead of LABEL_COUNT_QUERY and // we will now use the LABEL_QUERY instead of LABEL_COUNT_QUERY and
// count the rows. // count the rows and the number of distinct languages represented.
Set<String> distinctLanguages = new HashSet<String>();
String queryStr = QueryUtils.subUriForQueryVar(LABEL_QUERY, "subject", subjectUri); String queryStr = QueryUtils.subUriForQueryVar(LABEL_QUERY, "subject", subjectUri);
log.debug("queryStr = " + queryStr); log.debug("queryStr = " + queryStr);
int theCount = 0; int labelCount = 0;
try { try {
ResultSet results = QueryUtils.getQueryResults(queryStr, vreq); ResultSet results = QueryUtils.getQueryResults(queryStr, vreq);
while(results.hasNext()) { while(results.hasNext()) {
results.next(); QuerySolution qsoln = results.next();
theCount++; labelCount++;
String lang = qsoln.getLiteral("label").getLanguage();
if(lang == null) {
lang = "";
}
distinctLanguages.add(lang);
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); log.error(e, e);
} }
return theCount; return new LabelAndLanguageCount(labelCount, distinctLanguages.size());
}
private static class LabelAndLanguageCount {
private Integer labelCount;
private Integer languageCount;
public LabelAndLanguageCount(Integer labelCount, Integer languageCount) {
this.labelCount = labelCount;
this.languageCount = languageCount;
}
public Integer getLabelCount() {
return this.labelCount;
}
public Integer getLanguageCount() {
return this.languageCount;
}
} }
//what is the number of languages represented across the labels //what is the number of languages represented across the labels
private static Integer getLanguagesRepresentedCount(String subjectUri, VitroRequest vreq) { // This version not compatible with language-filtering RDF services
String queryStr = QueryUtils.subUriForQueryVar(DISTINCT_LANGUAGE_QUERY, "subject", subjectUri); // private static Integer getLanguagesRepresentedCount(String subjectUri, VitroRequest vreq) {
log.debug("queryStr = " + queryStr); // String queryStr = QueryUtils.subUriForQueryVar(DISTINCT_LANGUAGE_QUERY, "subject", subjectUri);
int theCount = 0; // log.debug("queryStr = " + queryStr);
try { // int theCount = 0;
// try {
ResultSet results = QueryUtils.getLanguageNeutralQueryResults(queryStr, vreq); //
if (results.hasNext()) { // ResultSet results = QueryUtils.getLanguageNeutralQueryResults(queryStr, vreq);
QuerySolution soln = results.nextSolution(); // if (results.hasNext()) {
RDFNode languageCount = soln.get("languageCount"); // QuerySolution soln = results.nextSolution();
if (languageCount != null && languageCount.isLiteral()) { // RDFNode languageCount = soln.get("languageCount");
theCount = languageCount.asLiteral().getInt(); // if (languageCount != null && languageCount.isLiteral()) {
} // theCount = languageCount.asLiteral().getInt();
} // log.info("Language count is " + theCount);
} catch (Exception e) { // }
log.error(e, e); // }
} // } catch (Exception e) {
return theCount; // log.error(e, e);
} // }
// log.info("Returning language count " + theCount);
// return theCount;
// }
private static String PROFILE_TYPE_QUERY = "" private static String PROFILE_TYPE_QUERY = ""
+ "PREFIX display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> \n" + "PREFIX display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> \n"

View file

@ -307,23 +307,17 @@ name will be used as the label. -->
<#assign linkTitle = "${i18n().manage_list_of_labels}"> <#assign linkTitle = "${i18n().manage_list_of_labels}">
<#assign labelLink= "${urls.base}/editRequestDispatch?subjectUri=${individualUri}&editForm=${generators.ManageLabelsGenerator}&predicateUri=${labelPropertyUri}${extraParameters}"> <#assign labelLink= "${urls.base}/editRequestDispatch?subjectUri=${individualUri}&editForm=${generators.ManageLabelsGenerator}&predicateUri=${labelPropertyUri}${extraParameters}">
<#else> <#else>
<#-- For consistency of behavior, don't show view labels link to users without editing privileges .
<#assign linkTitle = "${i18n().view_list_of_labels}"> <#assign linkTitle = "${i18n().view_list_of_labels}">
<#assign imageAlt = "${i18n().view}" /> <#assign imageAlt = "${i18n().view}" />
<#assign labelLink= "${urls.base}/viewLabels?subjectUri=${individualUri}${extraParameters}"> <#assign labelLink= "${urls.base}/viewLabels?subjectUri=${individualUri}${extraParameters}">
-->
</#if> </#if>
<#if editable>
<#-- Render the link only if editable. See comment above. -->
<span class="inline"> <span class="inline">
<a class="add-label" href="${labelLink}" <a class="add-label" href="${labelLink}"
title="${linkTitle}"> title="${linkTitle}">
<img class="add-individual" src="${urls.images}/individual/manage-icon.png" alt="${imageAlt}" /></a> <img class="add-individual" src="${urls.images}/individual/manage-icon.png" alt="${imageAlt}" /></a>
</span> </span>
</#if> </#if>
</#if>
</#macro> </#macro>
<#-- Most specific types --> <#-- Most specific types -->