diff --git a/phis2-ws/src/main/java/phis2ws/service/dao/sesame/UriDaoSesame.java b/phis2-ws/src/main/java/phis2ws/service/dao/sesame/UriDaoSesame.java index 105cc0f06..482e4291a 100644 --- a/phis2-ws/src/main/java/phis2ws/service/dao/sesame/UriDaoSesame.java +++ b/phis2-ws/src/main/java/phis2ws/service/dao/sesame/UriDaoSesame.java @@ -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; @@ -42,6 +43,8 @@ public class UriDaoSesame extends DAOSesame { 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"; @@ -53,6 +56,7 @@ public class UriDaoSesame extends DAOSesame { 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"); /** @@ -479,15 +483,6 @@ public ArrayList 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); @@ -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 { + * 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 @@ -541,9 +557,9 @@ protected SPARQLQueryBuilder prepareGetLabels() { * "none" : "dqfgdf" * ] */ - public HashMap getLabels() { + public Multimap getLabels() { SPARQLQueryBuilder query = prepareGetLabels(); - HashMap labels = new HashMap<>(); + Multimap labels = ArrayListMultimap.create(); TupleQuery tupleQuery = getConnection().prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); @@ -551,9 +567,9 @@ public HashMap getLabels() { while (result.hasNext()) { BindingSet bindingSet = result.next(); Literal propertyLabel = (Literal) bindingSet.getValue(LABEL); - Optional labelLanguage = propertyLabel.getLanguage(); - if (labelLanguage.get() != null) { - labels.put(labelLanguage.get(), bindingSet.getValue(LABEL).stringValue()); + if (propertyLabel.getLanguage().isPresent()) { + Optional commentLanguage = propertyLabel.getLanguage(); + labels.put(commentLanguage.get(), bindingSet.getValue(LABEL).stringValue()); } else { //no language tag associated labels.put("none", bindingSet.getValue(LABEL).stringValue()); } @@ -561,4 +577,35 @@ public HashMap getLabels() { } 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 getComments() { + SPARQLQueryBuilder query = prepareGetComments(); + Multimap 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 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; + } } diff --git a/phis2-ws/src/main/java/phis2ws/service/dao/sesame/VocabularyDAOSesame.java b/phis2-ws/src/main/java/phis2ws/service/dao/sesame/VocabularyDAOSesame.java index 47a8e0fe2..dcb7077a2 100644 --- a/phis2-ws/src/main/java/phis2ws/service/dao/sesame/VocabularyDAOSesame.java +++ b/phis2-ws/src/main/java/phis2ws/service/dao/sesame/VocabularyDAOSesame.java @@ -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; } diff --git a/phis2-ws/src/main/java/phis2ws/service/resources/dto/PropertyVocabularyDTO.java b/phis2-ws/src/main/java/phis2ws/service/resources/dto/PropertyVocabularyDTO.java index ba22bf80a..044d6c8f3 100644 --- a/phis2-ws/src/main/java/phis2ws/service/resources/dto/PropertyVocabularyDTO.java +++ b/phis2-ws/src/main/java/phis2ws/service/resources/dto/PropertyVocabularyDTO.java @@ -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; @@ -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 labels = new HashMap<>(); + private Map> 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> comments = new HashMap<>(); @Override public Map rules() { @@ -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) @@ -55,15 +56,28 @@ public void setRelation(String relation) { this.relation = relation; } - public HashMap getLabels() { + public Map> getLabels() { return labels; } - public void setLabels(HashMap labels) { + public void setLabels(Map> labels) { this.labels = labels; } public void addLabel(String language, String label) { - labels.put(language, label); + Collection labelsByLang = labels.get(language); + if (labelsByLang == null) { + labelsByLang = new ArrayList<>(); + } + labelsByLang.add(label); + labels.put(language, labelsByLang); + } + + public Map> getComments() { + return comments; + } + + public void setComments(Map> comments) { + this.comments = comments; } }