Skip to content

Commit

Permalink
Merge pull request #75 from Gouga34/vocabularies
Browse files Browse the repository at this point in the history
 update labels form and add comments (references #65)
  • Loading branch information
Gouga34 authored Jun 25, 2018
2 parents f8f389e + c2b6c81 commit ef64125
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 25 deletions.
77 changes: 62 additions & 15 deletions phis2-ws/src/main/java/phis2ws/service/dao/sesame/UriDaoSesame.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
//***********************************************************************************************
package phis2ws.service.dao.sesame;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Value;
Expand Down Expand Up @@ -42,6 +43,8 @@ public class UriDaoSesame extends DAOSesame<Uri> {
public String label;
//used to query the triplestore
final static String LABEL = "label";
//used to query the triplestore
final static String COMMENT = "comment";

final static String TRIPLESTORE_FIELDS_TYPE = "type";
final static String TRIPLESTORE_FIELDS_CLASS = "class";
Expand All @@ -53,6 +56,7 @@ public class UriDaoSesame extends DAOSesame<Uri> {

private final static URINamespaces NAMESPACES = new URINamespaces();
final static String TRIPLESTORE_RELATION_LABEL = NAMESPACES.getRelationsProperty("label");
final static String TRIPLESTORE_RELATION_COMMENT = NAMESPACES.getRelationsProperty("comment");


/**
Expand Down Expand Up @@ -479,15 +483,6 @@ public ArrayList<Uri> getAskTypeAnswer() {
private SPARQLQueryBuilder prepareIsSubclassOf(String rdfSubType, String rdfType) {
SPARQLQueryBuilder query = new SPARQLQueryBuilder();
query.appendDistinct(Boolean.TRUE);

String contextURI;

if (uri != null) {
contextURI = "<" + uri + ">";
} else {
contextURI = "?uri";
query.appendSelect("?uri");
}

query.appendTriplet("<" + rdfSubType + ">", NAMESPACES.getRelationsProperty("subClassOf*"), "<" + rdfType + ">", null);

Expand Down Expand Up @@ -531,6 +526,27 @@ protected SPARQLQueryBuilder prepareGetLabels() {
return query;
}

/**
* generates a query to get the list of the comments of the uri attribute
* e.g.
* SELECT DISTINCT ?comment
* WHERE {
* <http://www.phenome-fppn.fr/vocabulary/2017#hasTechnicalContact> rdfs:comment ?comment .
* }
* @return the generated query
*/
protected SPARQLQueryBuilder prepareGetComments() {
SPARQLQueryBuilder query = new SPARQLQueryBuilder();
query.appendDistinct(Boolean.TRUE);

query.appendSelect("?" + COMMENT);
query.appendTriplet(uri, TRIPLESTORE_RELATION_COMMENT, "?" + COMMENT, null);

LOGGER.debug(SPARQL_SELECT_QUERY + " " + query.toString());

return query;
}

/**
* get the labels associated to the uri attribute in the triplestore
* @return the list of labels. the key is the language
Expand All @@ -541,24 +557,55 @@ protected SPARQLQueryBuilder prepareGetLabels() {
* "none" : "dqfgdf"
* ]
*/
public HashMap<String, String> getLabels() {
public Multimap<String, String> getLabels() {
SPARQLQueryBuilder query = prepareGetLabels();
HashMap<String, String> labels = new HashMap<>();
Multimap<String, String> labels = ArrayListMultimap.create();

TupleQuery tupleQuery = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, query.toString());

try (TupleQueryResult result = tupleQuery.evaluate()) {
while (result.hasNext()) {
BindingSet bindingSet = result.next();
Literal propertyLabel = (Literal) bindingSet.getValue(LABEL);
Optional<String> labelLanguage = propertyLabel.getLanguage();
if (labelLanguage.get() != null) {
labels.put(labelLanguage.get(), bindingSet.getValue(LABEL).stringValue());
if (propertyLabel.getLanguage().isPresent()) {
Optional<String> commentLanguage = propertyLabel.getLanguage();
labels.put(commentLanguage.get(), bindingSet.getValue(LABEL).stringValue());
} else { //no language tag associated
labels.put("none", bindingSet.getValue(LABEL).stringValue());
}
}
}
return labels;
}

/**
* get the comments associated to the uri attribute in the triplestore
* @return the list of comments. the key is the language
* e.g.
* [
* "fr" : "maison",
* "en" : "home",
* "none" : "dqfgdf"
* ]
*/
public Multimap<String, String> getComments() {
SPARQLQueryBuilder query = prepareGetComments();
Multimap<String, String> comments = ArrayListMultimap.create();

TupleQuery tupleQuery = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, query.toString());

try (TupleQueryResult result = tupleQuery.evaluate()) {
while (result.hasNext()) {
BindingSet bindingSet = result.next();
Literal propertyLabel = (Literal) bindingSet.getValue(COMMENT);
if (propertyLabel.getLanguage().isPresent()) {
Optional<String> commentLanguage = propertyLabel.getLanguage();
comments.put(commentLanguage.get(), bindingSet.getValue(COMMENT).stringValue());
} else { //no language tag associated
comments.put("none", bindingSet.getValue(COMMENT).stringValue());
}
}
}
return comments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ private PropertyVocabularyDTO propertyStringToPropertyVocabularyDTO(String prope

UriDaoSesame uriDao = new UriDaoSesame();
uriDao.uri = propertyUri;
property.setLabels(uriDao.getLabels());
property.setLabels(uriDao.getLabels().asMap());
property.setComments(uriDao.getComments().asMap());

return property;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
package phis2ws.service.resources.dto;

import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import phis2ws.service.documentation.DocumentationAnnotation;
Expand All @@ -30,7 +32,10 @@ public class PropertyVocabularyDTO extends AbstractVerifiedClass {
//the list of the labels of the property. Hash Map with the languages if needed
//it is a hash map with the language and the label
//if there is no language for a label, the key is equals to none (?)
private HashMap<String, String> labels = new HashMap<>();
private Map<String, Collection<String>> labels = new HashMap<>();
//the list of the comments of the property. Map with the languages if knowned.
//if there is no language, the key will be "none"
private Map<String, Collection<String>> comments = new HashMap<>();

@Override
public Map rules() {
Expand All @@ -39,11 +44,7 @@ public Map rules() {

@Override
public Property createObjectFromDTO() {
Property property = new Property();
property.setRelation(relation);
property.setLabels(labels);

return property;
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@ApiModelProperty(example = DocumentationAnnotation.EXAMPLE_SPECIES_FROM_SPECIES)
Expand All @@ -55,15 +56,28 @@ public void setRelation(String relation) {
this.relation = relation;
}

public HashMap<String, String> getLabels() {
public Map<String, Collection<String>> getLabels() {
return labels;
}

public void setLabels(HashMap<String, String> labels) {
public void setLabels(Map<String, Collection<String>> labels) {
this.labels = labels;
}

public void addLabel(String language, String label) {
labels.put(language, label);
Collection<String> labelsByLang = labels.get(language);
if (labelsByLang == null) {
labelsByLang = new ArrayList<>();
}
labelsByLang.add(label);
labels.put(language, labelsByLang);
}

public Map<String, Collection<String>> getComments() {
return comments;
}

public void setComments(Map<String, Collection<String>> comments) {
this.comments = comments;
}
}

0 comments on commit ef64125

Please sign in to comment.