Commit 48c0e8d6 authored by Richard Bruskiewich's avatar Richard Bruskiewich Committed by Matija Obreza
Browse files

Added 'rdfUri' to both descriptor property ('parameter') and method data

models, propagating changes through all three levels and adding suitable
methods to the TraitService to suit.

I now also test for the existence of Property ('Parameter') and Method
objects based on sensible match criteria. When such meta-data records
already exist, I simply return them back to the caller (without
attempting to create a duplicate, which triggers a database constraint
error... but such existing records are logged).
parent fe77f459
......@@ -43,6 +43,11 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
@Column(length = 36, unique = true, nullable = false)
private String uuid;
// Resource Description Framework (RDF) Uniform Resource Identifier (URI)
// to link method records with semantic web linked open (meta-)data
@Lob
private String rdfUri;
@Column(nullable = false)
@Lob
private String method;
......@@ -76,6 +81,14 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
}
}
public String getRdfUri() {
return this.rdfUri;
}
public void setRdfUri(final String rdfUri) {
this.rdfUri = rdfUri;
}
public String getMethod() {
return this.method;
}
......
......@@ -21,6 +21,7 @@ import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
......@@ -38,6 +39,11 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
// private Language language;
@Column(length = 36, unique = true, nullable = false)
private String uuid;
// Resource Description Framework (RDF) Uniform Resource Identifier (URI)
// to link method records with semantic web linked open (meta-)data
@Lob
private String rdfUri;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "cropId")
......@@ -60,6 +66,14 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
}
}
public String getRdfUri() {
return this.rdfUri;
}
public void setRdfUri(final String rdfUri) {
this.rdfUri = rdfUri;
}
public Crop getCrop() {
return this.crop;
}
......
......@@ -25,9 +25,14 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface MethodRepository extends JpaRepository<Method, Long> {
List<Method> findByFieldType(int fieldType);
List<Method> findByParameter(Parameter parameter);
Method findByRdfUri(String rdfUri) ;
Method findByMethodAndParameter(String method, Parameter parameter) ;
Method findByIdAndParameter(long id, Parameter findOne);
......
......@@ -28,6 +28,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface ParameterRepository extends JpaRepository<Parameter, Long> {
Parameter findByRdfUri(String rdfUri) ;
Page<Parameter> findByCrop(Crop crop, Pageable pageable);
Collection<? extends Parameter> findByCropAndCategory(Crop crop, ParameterCategory pc, Sort sort);
......
......@@ -71,7 +71,15 @@ public interface TraitService {
* @return
*/
@PreAuthorize("isAuthenticated()")
Parameter addParameter(Crop crop, String category, String title);
Parameter addParameter(String rdfUri, Crop crop, String category, String title);
/**
*
* @param rdfUri is the Resource Description Framework (RDF) Uniform Resource Identifier (URI) of the Parameter concept
* @return
*/
@PreAuthorize("isAuthenticated()")
Parameter getParameter(String rdfUri);
/**
* Get a crop descriptor property by title
......@@ -99,7 +107,13 @@ public interface TraitService {
* @return
*/
@PreAuthorize("isAuthenticated()")
Method addMethod(String description, String unit, String fieldName, int fieldType, Integer fieldSize, String options, String range, Parameter parameter);
Method addMethod(String rdfUri, String description, String unit, String fieldName, int fieldType, Integer fieldSize, String options, String range, Parameter parameter);
@PreAuthorize("isAuthenticated()")
Method getMethod(String rdfUri);
@PreAuthorize("isAuthenticated()")
Method getMethod(String description, Parameter parameter);
/**
* List user's methods
......
......@@ -571,16 +571,18 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Parameter addParameter(Crop crop, String category, String title) {
public Parameter addParameter(
String rdfUri,
Crop crop, String category, String title
) {
Parameter parameter = new Parameter();
parameter.setRdfUri(rdfUri);
parameter.setCrop(crop);
ParameterCategory parameterCategory =
parameterCategoryRepository.findByName(category);
parameter.setCategory(parameterCategory);
parameter.setTitle(title);
parameterRepository.save(parameter);
......@@ -588,6 +590,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return parameter;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public Parameter getParameter(String rdfUri) {
if(rdfUri == null || rdfUri.isEmpty() )
return null ;
Parameter parameter =
parameterRepository.findByRdfUri(rdfUri) ; // assumes that crop x title is unique(?)
return parameter;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
......@@ -600,18 +615,26 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Method addMethod(String description, String unit, String fieldName, int fieldType, Integer fieldSize, String options, String range,
Parameter parameter) {
public Method addMethod(
String rdfUri, String description, String unit,
String fieldName, int fieldType, Integer fieldSize,
String options, String range,
Parameter parameter
) {
Method method = new Method();
method.setRdfUri(rdfUri);
method.setMethod(htmlSanitizer.sanitize(description));
method.setUnit(StringUtils.defaultIfBlank(StringUtils.trimToNull(unit), null));
method.setFieldName(StringUtils.trimToNull(fieldName));
method.setFieldType(fieldType);
if (fieldType == 0) {
// Will throw NPE if not provided!
method.setFieldSize(fieldSize.toString());
}
method.setOptions(StringUtils.defaultIfBlank(StringUtils.trimToNull(options), null));
method.setRange(StringUtils.defaultIfBlank(StringUtils.trimToNull(range), null));
method.setParameter(parameter);
......@@ -624,6 +647,33 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return method;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public Method getMethod(String rdfUri) {
if(rdfUri == null || rdfUri.isEmpty() )
return null ;
Method method =
methodRepository.findByRdfUri(rdfUri) ;
return method;
}
@Override
public Method getMethod(String description, Parameter parameter) {
if(description == null || description.isEmpty() )
return null ;
Method method =
methodRepository.findByMethodAndParameter(description,parameter) ;
return method;
}
/**
* Returns datasets to which current user has 'WRITE'
*/
......@@ -1218,5 +1268,4 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return paramMethods;
}
}
......@@ -176,12 +176,16 @@ public class TraitsController extends RestController {
Crop crop = cropService.getCrop(cropName) ;
LOG.debug("Calling getParameter()");
Parameter property = traitService.getParameter(crop, propertyJson.title);
Parameter property ;
if(propertyJson.rdfUri != null && !propertyJson.rdfUri.isEmpty() )
property = traitService.getParameter(propertyJson.rdfUri);
else
property = traitService.getParameter(crop, propertyJson.title);
if(property == null) {
// create if not available already?
property = traitService.addParameter(crop, propertyJson.category, propertyJson.title);
property = traitService.addParameter(propertyJson.rdfUri, crop, propertyJson.category, propertyJson.title);
} else
LOG.warn("Property '"+propertyJson.title+"' in category '"+
......@@ -193,9 +197,10 @@ public class TraitsController extends RestController {
public static class PropertyJson {
//@NotNull
//@NotBlank
//public String rdfUri;
// Resource Description Framework (RDF)
// Uniform Resource Identifier (URI)
// Kept optional for now (maybe null or blank)
public String rdfUri;
@NotNull
@NotBlank
......@@ -235,18 +240,34 @@ public class TraitsController extends RestController {
//if the (long integer) property id given in the path is zero,
// then the method is left "floating" in the database.
property = null ;
Method method ;
if(methodJson.rdfUri != null && !methodJson.rdfUri.isEmpty() )
method = traitService.getMethod(methodJson.rdfUri);
else
method = traitService.getMethod(methodJson.description, property);
Method method = traitService.addMethod(methodJson.description, methodJson.unit, methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, property);
if(method == null) {
// create if not available already?
method =
traitService.addMethod(
methodJson.rdfUri, methodJson.description, methodJson.unit,
methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, property
);
} else
LOG.warn("Method '"+methodJson.description+
"' already exists for property '"+property.getTitle()+"'?");
return method;
}
public static class MethodJson {
//@NotNull
//@NotBlank
//public String rdfUri;
// Resource Description Framework (RDF)
// Uniform Resource Identifier (URI)
// Kept optional for now (maybe null or blank)
public String rdfUri;
@NotNull
@NotBlank
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment