Commit 0a7b4ecf authored by Matija Obreza's avatar Matija Obreza

RDF crop, descriptor controllers updated for production

parent 04980747
......@@ -497,9 +497,8 @@
<dependency>
<groupId>org.bioversityinternational</groupId>
<artifactId>org.bioversityinternational.ontology</artifactId>
<version>0.0.6</version>
<version>0.6.2</version>
</dependency>
</dependencies>
<build>
......
......@@ -19,6 +19,12 @@ package org.genesys2.server.servlet.controller.rdf;
import java.util.List;
import java.util.Map;
import org.bioversityinternational.model.germplasm.CCO;
import org.bioversityinternational.model.rdf.dwc.DarwinCore;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.service.CropService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -33,19 +39,12 @@ import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.service.CropService;
import org.genesys2.spring.ResourceNotFoundException;
import org.bioversityinternational.model.germplasm.CCO;
import org.bioversityinternational.model.rdf.dwc.DarwinCore;
@Controller
@RequestMapping(value = "/crops", method = RequestMethod.GET, headers = "accept=text/turtle", produces = RdfBaseController.RDF_MEDIATYPE_TURTLE)
public class CropControllerRdf extends RdfBaseController {
public static final String CROPS_NS = getBaseUri()+"/crops/";;
public static final String SPECIES_NS = CROPS_NS+"species/" ;
private final String CROPS_NS = "/crops/";
private final String SPECIES_NS = CROPS_NS + "species/";
@Autowired
private CropService cropService;
......@@ -71,44 +70,41 @@ public class CropControllerRdf extends RdfBaseController {
.
*/
private void wrapCrop(Model model, Crop crop) {
Resource cropSubj = createSubject(model, crop, CROPS_NS+crop.getId(), crop.getShortName()+" Taxon") ;
cropSubj.addProperty(DarwinCore.scientificName, crop.getShortName()) ;
String seeAlso = crop.getRdfUri() ;
if(seeAlso!=null)
cropSubj.addProperty(
RDFS.seeAlso,
model.createResource(seeAlso)
) ;
Resource cropSubj = createSubject(model, crop, CROPS_NS + crop.getShortName(), crop.getShortName() + " Taxon");
// FIXME That's probably not right
cropSubj.addProperty(DarwinCore.scientificName, crop.getShortName());
String seeAlso = crop.getRdfUri();
if (seeAlso != null)
cropSubj.addProperty(RDFS.seeAlso, model.createResource(seeAlso));
/*
* vernacular names & descriptions in i18n
*/
Map<String,String> vernacularNameMap = crop.getLocalNameMap() ;
if(vernacularNameMap!=null)
for( String language : vernacularNameMap.keySet() ) {
Literal vernacularNameLiteral =
model.createLiteral(vernacularNameMap.get(language), language) ;
cropSubj.addLiteral(DarwinCore.vernacularName,vernacularNameLiteral) ;
*/
Map<String, String> vernacularNameMap = crop.getLocalNameMap();
if (vernacularNameMap != null)
for (String language : vernacularNameMap.keySet()) {
Literal vernacularNameLiteral = model.createLiteral(vernacularNameMap.get(language), language);
cropSubj.addLiteral(DarwinCore.vernacularName, vernacularNameLiteral);
}
else
this._logger.warn("Empty vernacular crop name map for crop "+crop.getShortName());
Map<String,String> vernacularDefinitionMap = crop.getLocalDefinitionMap() ;
if(vernacularDefinitionMap!=null)
for( String language : vernacularDefinitionMap.keySet() ) {
Literal vernacularDefinitionLiteral =
model.createLiteral(vernacularDefinitionMap.get(language), language) ;
cropSubj.addLiteral(DC.description,vernacularDefinitionLiteral) ;
this._logger.warn("Empty vernacular crop name map for crop " + crop.getShortName());
Map<String, String> vernacularDefinitionMap = crop.getLocalDefinitionMap();
if (vernacularDefinitionMap != null)
for (String language : vernacularDefinitionMap.keySet()) {
Literal vernacularDefinitionLiteral = model.createLiteral(vernacularDefinitionMap.get(language), language);
cropSubj.addLiteral(DC.description, vernacularDefinitionLiteral);
}
else
this._logger.warn("Empty vernacular crop description map for crop "+crop.getShortName());
this._logger.warn("Empty vernacular crop description map for crop " + crop.getShortName());
/* Exemplar for CropRule species...
/*
* Exemplar for CropRule species...
*
genesys:MusaAcuminata a cco:Species;
dwc:scientificName "Musa acuminata" ;
......@@ -122,55 +118,53 @@ public class CropControllerRdf extends RdfBaseController {
Note: current GENESYS data model does NOT track the description of a species...
*/
List<CropRule> rules = cropService.getCropRules(crop) ;
for(CropRule cr : rules) {
String species = cr.getSpecies() ;
String genus = cr.getGenus() ;
Boolean isIncluded = cr.isIncluded() ;
Resource speciesSubj =
createSubject(
model,
crop,
SPECIES_NS+canonicalID(species,true),
species
) ;
speciesSubj.addProperty(RDF.type, CCO.Species ) ;
speciesSubj.addProperty(DarwinCore.scientificName, species ) ;
speciesSubj.addProperty(DarwinCore.genus, genus ) ;
speciesSubj.addProperty(CCO.speciesIncluded, isIncluded.toString() ) ;
List<CropRule> rules = cropService.getCropRules(crop);
for (CropRule cr : rules) {
String species = cr.getSpecies();
String genus = cr.getGenus();
Boolean isIncluded = cr.isIncluded();
// FIXME in CropRule the Genus is required, species is not.
if (species != null) {
Resource speciesSubj = createSubject(model, crop, SPECIES_NS + canonicalID(species, true), species);
speciesSubj.addProperty(RDF.type, CCO.Species);
speciesSubj.addProperty(DarwinCore.scientificName, species);
speciesSubj.addProperty(DarwinCore.genus, genus);
speciesSubj.addProperty(CCO.speciesIncluded, isIncluded.toString());
}
}
}
@RequestMapping
public @ResponseBody
String dumpCrops() {
Model model = startModel() ;
List<Crop> crops = cropService.listCrops() ;
for(Crop crop : crops) wrapCrop( model, crop) ;
Model model = startModel();
List<Crop> crops = cropService.listCrops();
for (Crop crop : crops)
wrapCrop(model, crop);
return endModel(model) ;
return endModel(model);
}
@RequestMapping("/{shortName}")
public @ResponseBody
String dumpCrop(@PathVariable(value = "shortName") String shortName) {
_logger.debug("Dump RDF data for crop: " + shortName);
Model model = startModel() ;
Model model = startModel();
Crop crop = cropService.getCrop(shortName);
if (crop == null) {
throw new ResourceNotFoundException();
}
wrapCrop(model,crop) ;
return endModel(model) ;
wrapCrop(model, crop);
return endModel(model);
}
}
......@@ -21,17 +21,8 @@ import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.bioversityinternational.model.germplasm.CCO;
import org.bioversityinternational.model.rdf.skos.SKOS;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.genesys.ParameterCategory;
......@@ -40,9 +31,17 @@ import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.service.TraitService;
import org.genesys2.spring.ResourceNotFoundException;
//import org.genesys2.server.model.genesys.Metadata; // not yet integrated...
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.bioversityinternational.model.germplasm.CCO;
import org.bioversityinternational.model.rdf.skos.SKOS;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDFS;
/**
* Controller which simply handles RDF Turtle meta-data requests
......@@ -50,75 +49,75 @@ import org.bioversityinternational.model.rdf.skos.SKOS;
@Controller
@RequestMapping(value = "/descriptors", method = RequestMethod.GET, headers = "accept=text/turtle", produces = RdfBaseController.RDF_MEDIATYPE_TURTLE)
public class DescriptorControllerRdf extends RdfBaseController {
public static final String DESCRIPTOR_NS = getBaseUri()+"/descriptors/";;
public static final String CATEGORY_NS = DESCRIPTOR_NS+"categories/" ;
public static final String TRAIT_NS = DESCRIPTOR_NS+"trait/" ;
public static final String METHOD_NS = DESCRIPTOR_NS+"method/" ;
public static final String SCALE_NS = DESCRIPTOR_NS+"scale/" ;
public final String DESCRIPTOR_NS = "/descriptors/";;
public final String CATEGORY_NS = DESCRIPTOR_NS + "categories/";
public final String TRAIT_NS = DESCRIPTOR_NS + "trait/";
public final String METHOD_NS = DESCRIPTOR_NS + "method/";
public final String SCALE_NS = DESCRIPTOR_NS + "scale/";
@Autowired
private TraitService traitService;
/**
* Method to retrieve list of all descriptor meta-data
* recorded in the database, formatted as Turtle RDF.
* Method to retrieve list of all descriptor meta-data recorded in the
* database, formatted as Turtle RDF.
*
* @return List of RDF Turtle formatted trait properties
*/
@RequestMapping
public @ResponseBody
Object dumpAll() {
Model model = startModel() ;
wrapCategories(model) ;
for(Parameter trait : traitService.listTraits())
wrapTrait(model, trait) ;
for(Method method : traitService.listMethods()) {
Parameter trait = method.getParameter() ;
wrapMethod(model, trait, method) ;
Model model = startModel();
wrapCategories(model);
for (Parameter trait : traitService.listTraits())
wrapTrait(model, trait);
for (Method method : traitService.listMethods()) {
Parameter trait = method.getParameter();
wrapMethod(model, trait, method);
}
return endModel(model) ;
return endModel(model);
}
/*
* Creates and annotates a Method subject
*/
private Resource categorySubject(Model model, ParameterCategory category) {
return createSubject(model, category, CATEGORY_NS+category.getId(), category.getName() ) ;
return createSubject(model, category, CATEGORY_NS + category.getId(), category.getName());
}
/*
* Method generates RDF for one trait category record as RDF.
*/
private void wrapCategories(Model model) {
List<ParameterCategory> categories = traitService.listCategories() ;
for(ParameterCategory category:categories) {
List<ParameterCategory> categories = traitService.listCategories();
for (ParameterCategory category : categories) {
// Insert category record into RDF model
Resource subject = categorySubject(model, category) ;
CCO.Vocabulary.set(model, subject, CCO.Vocabulary.CATEGORY, "Plant") ;
subject.addProperty(SKOS.broader, CCO.TraitCategory) ;
Map<String,String> vernacularNameMap = category.getLocalNameMap() ;
for(String language:vernacularNameMap.keySet()) {
Literal vernacularNameLiteral =
model.createLiteral(vernacularNameMap.get(language), language) ;
subject.addLiteral(SKOS.prefLabel, vernacularNameLiteral) ;
Resource subject = categorySubject(model, category);
CCO.Vocabulary.set(model, subject, CCO.Vocabulary.CATEGORY, "Plant");
subject.addProperty(SKOS.broader, CCO.TraitCategory);
Map<String, String> vernacularNameMap = category.getLocalNameMap();
for (String language : vernacularNameMap.keySet()) {
Literal vernacularNameLiteral = model.createLiteral(vernacularNameMap.get(language), language);
subject.addLiteral(SKOS.prefLabel, vernacularNameLiteral);
}
}
}
/**
* Method to publish ParameterCategory meta-data as RDF
*
* @param model
* @return RDF formatted list of categories
*
* Exemplar:
* Exemplar:
*
* cco:MorphologyTrait rdfs:subClassOf cco:TraitCategory ;
* skos:broader cco:TraitCategory ;
......@@ -134,15 +133,15 @@ public class DescriptorControllerRdf extends RdfBaseController {
@RequestMapping(value = "/categories")
public @ResponseBody
String dumpCategories() {
Model model = startModel() ;
wrapCategories(model) ;
return endModel(model) ;
Model model = startModel();
wrapCategories(model);
return endModel(model);
}
/**
/**
* Trait Turtle RDF format Exemplar:
*
* cco:MusaTrait a skos:Concept;
......@@ -158,419 +157,374 @@ public class DescriptorControllerRdf extends RdfBaseController {
* skos:inScheme cco:MusaTraitVocabulary;
* dc:creator "Bioversity International";
*/
/*
* Creates and annotates a Method subject
*/
private Resource traitSubject(Model model, Parameter trait) {
return createProperty(model, trait, DESCRIPTOR_NS+trait.getId(), trait.getTitle() ) ;
return createProperty(model, trait, DESCRIPTOR_NS + trait.getId(), trait.getTitle());
}
/*
* Method generates RDF for one trait record as RDF.
*/
private void wrapTrait(Model model, Parameter trait) {
Resource traitSubject = traitSubject(model, trait) ;
String seeAlso = trait.getRdfUri() ;
if(seeAlso!=null)
traitSubject.addProperty(
RDFS.seeAlso,
model.createResource(seeAlso)
) ;
Crop crop = trait.getCrop() ;
CCO.Vocabulary.set(model, traitSubject, CCO.Vocabulary.TRAIT, crop.getShortName()) ;
ParameterCategory category = trait.getCategory() ;
Resource categorySubject = categorySubject(model, category) ;
traitSubject.addProperty(CCO.category, categorySubject) ;
Map<String,String> vernacularTitleMap = trait.getLocalTitleMap() ;
for( String language : vernacularTitleMap.keySet() ) {
Literal vernacularNameLiteral =
model.createLiteral(vernacularTitleMap.get(language), language) ;
traitSubject.addLiteral(SKOS.prefLabel,vernacularNameLiteral) ;
Resource traitSubject = traitSubject(model, trait);
String seeAlso = trait.getRdfUri();
if (seeAlso != null)
traitSubject.addProperty(RDFS.seeAlso, model.createResource(seeAlso));
Crop crop = trait.getCrop();
CCO.Vocabulary.set(model, traitSubject, CCO.Vocabulary.TRAIT, crop.getShortName());
ParameterCategory category = trait.getCategory();
if (category != null) {
Resource categorySubject = categorySubject(model, category);
traitSubject.addProperty(CCO.category, categorySubject);
}
/*
TODO - The Parameter table doesn't yet appear to track trait descriptions...
Map<String, String> vernacularTitleMap = trait.getLocalTitleMap();
Map<String,String> vernacularTitleMap = trait.getLocalTitleMap() ;
for (String language : vernacularTitleMap.keySet()) {
Literal vernacularNameLiteral = model.createLiteral(vernacularTitleMap.get(language), language);
traitSubject.addLiteral(SKOS.prefLabel, vernacularNameLiteral);
}
for( String language : vernacularDefinitionMap.keySet() ) {
Literal vernacularNameLiteral =
model.createLiteral(vernacularTitleMap.get(language), language) ;
subject.addLiteral(SKOS.prefLabel,vernacularNameLiteral) ;
}
/*
* TODO - The Parameter table doesn't yet appear to track trait
* descriptions...
*
* Map<String,String> vernacularTitleMap = trait.getLocalTitleMap() ;
*
* for( String language : vernacularDefinitionMap.keySet() ) { Literal
* vernacularNameLiteral =
* model.createLiteral(vernacularTitleMap.get(language), language) ;
* subject.addLiteral(SKOS.prefLabel,vernacularNameLiteral) ; }
*/
}
/**
* Method to retrieve list of all trait properties in the database, formatted as Turtle RDF
* Method to retrieve list of all trait properties in the database,
* formatted as Turtle RDF
*
* @return List of RDF Turtle formatted trait properties
*/
@RequestMapping("/traits")
public @ResponseBody
Object dumpTraits() {
Model model = startModel() ;
for(Parameter trait : traitService.listTraits()) wrapTrait(model, trait) ;
return endModel(model) ;
Model model = startModel();
for (Parameter trait : traitService.listTraits())
wrapTrait(model, trait);
return endModel(model);
}
/**
* Method to publish crop trait property ("Parameter") meta-data as multi-lingual RDF
* Method to publish crop trait property ("Parameter") meta-data as
* multi-lingual RDF
*
* @param PathVariable long traitId
* @param PathVariable
* long traitId
* @return RDF formatted list of categories
*/
@RequestMapping("/{traitId}")
public @ResponseBody
Object dumpTrait(@PathVariable long traitId, HttpServletResponse response) {
Parameter trait = traitService.getTrait(traitId);
if (trait == null) {
throw new ResourceNotFoundException();
}
Model model = startModel() ;
wrapTrait(model, trait) ;
response.setDateHeader("Last-Modified", trait.getLastModifiedDate().getTime());
Model model = startModel();
wrapTrait(model, trait);
if (trait.getLastModifiedDate() != null)
response.setDateHeader("Last-Modified", trait.getLastModifiedDate().getTime());
return endModel(model);
}
/**
* Trait Method RDF Exemplar (from the Bioversity model...
* some properties may not be generated from the GENESYS database)
* Trait Method RDF Exemplar (from the Bioversity model... some properties
* may not be generated from the GENESYS database)
*
* cco:fruitLengthMethod a skos:Concept; skos:broader cco:MusaTraitMethod;
* skos:related cco:fruitLength ; cco:trait cco:fruitLength ;
* dcterms:created "2014/01/14 12:00:00"; dcterms:modified
* "2014/01/14 12:00:00"; skos:prefLabel "Fruit length method"@en;
* skos:definition "Measured length of the fruit, without pedicel."@en;
* skos:prefLabel "Método para Longitud de los frutos [cm]"@es;
* skos:definition "Medir el longitud del fruto, sin el pedicelo."@es;
* skos:prefLabel "Фрукты метод длина"@ru; skos:definition
* "Измеряется длина плода, без плодоножки."@ru; skos:prefLabel "果长法"@zh;
* skos:definition "测得的果实长度,无花梗"@zh; skos:inScheme
* cco:MusaTraitMethodVocabulary .
*
* cco:fruitLengthMethod2 a skos:Concept; skos:broader cco:MusaTraitMethod;
* skos:related cco:fruitLength ; cco:trait cco:fruitLength ;
* dcterms:created "2014/01/14 12:00:00"; dcterms:modified
* "2014/01/14 12:00:00"; skos:prefLabel
* "Alternative fruit length method"@en; skos:definition
* "Measured length of the fruit, with the pedicel."@en; skos:prefLabel
* "Método alternativo para longitud de los frutos [cm]"@es;
* skos:definition "Medir el longitud del fruto, con el pedicelo."@es;
* skos:prefLabel "Альтернативный метод длина фрукты"@ru; skos:definition
* "Измеряется длина плода, с плодоножки."@ru; skos:prefLabel
* "另类水果长度的方法"@zh; skos:definition "测得的果实长度,与花梗"@zh; skos:inScheme
* cco:MusaTraitMethodVocabulary .
*
* cco:fruitLengthMethod a skos:Concept;
* skos:broader cco:MusaTraitMethod;
* skos:related cco:fruitLength ;
* cco:trait cco:fruitLength ;
* dcterms:created "2014/01/14 12:00:00";
* dcterms:modified "2014/01/14 12:00:00";
* skos:prefLabel "Fruit length method"@en;
* skos:definition "Measured length of the fruit, without pedicel."@en;
* skos:prefLabel "Método para Longitud de los frutos [cm]"@es;
* skos:definition "Medir el longitud del fruto, sin el pedicelo."@es;
* skos:prefLabel "Фрукты метод длина"@ru;
* skos:definition "Измеряется длина плода, без плодоножки."@ru;
* skos:prefLabel "果长法"@zh;
* skos:definition "测得的果实长度,无花梗"@zh;
* skos:inScheme cco:MusaTraitMethodVocabulary
* .
*
* cco:fruitLengthMethod2 a skos:Concept;
* skos:broader cco:MusaTraitMethod;