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 { ...@@ -43,6 +43,11 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
@Column(length = 36, unique = true, nullable = false) @Column(length = 36, unique = true, nullable = false)
private String uuid; 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) @Column(nullable = false)
@Lob @Lob
private String method; private String method;
...@@ -76,6 +81,14 @@ public class Method extends VersionedAuditedModel implements AclAwareModel { ...@@ -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() { public String getMethod() {
return this.method; return this.method;
} }
......
...@@ -21,6 +21,7 @@ import java.util.UUID; ...@@ -21,6 +21,7 @@ import java.util.UUID;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
...@@ -38,6 +39,11 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel { ...@@ -38,6 +39,11 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
// private Language language; // private Language language;
@Column(length = 36, unique = true, nullable = false) @Column(length = 36, unique = true, nullable = false)
private String uuid; 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) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "cropId") @JoinColumn(name = "cropId")
...@@ -60,6 +66,14 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel { ...@@ -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() { public Crop getCrop() {
return this.crop; return this.crop;
} }
......
...@@ -25,9 +25,14 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -25,9 +25,14 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
public interface MethodRepository extends JpaRepository<Method, Long> { public interface MethodRepository extends JpaRepository<Method, Long> {
List<Method> findByFieldType(int fieldType); List<Method> findByFieldType(int fieldType);
List<Method> findByParameter(Parameter parameter); List<Method> findByParameter(Parameter parameter);
Method findByRdfUri(String rdfUri) ;
Method findByMethodAndParameter(String method, Parameter parameter) ;
Method findByIdAndParameter(long id, Parameter findOne); Method findByIdAndParameter(long id, Parameter findOne);
......
...@@ -28,6 +28,8 @@ import org.springframework.data.jpa.repository.JpaRepository; ...@@ -28,6 +28,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface ParameterRepository extends JpaRepository<Parameter, Long> { public interface ParameterRepository extends JpaRepository<Parameter, Long> {
Parameter findByRdfUri(String rdfUri) ;
Page<Parameter> findByCrop(Crop crop, Pageable pageable); Page<Parameter> findByCrop(Crop crop, Pageable pageable);
Collection<? extends Parameter> findByCropAndCategory(Crop crop, ParameterCategory pc, Sort sort); Collection<? extends Parameter> findByCropAndCategory(Crop crop, ParameterCategory pc, Sort sort);
......
...@@ -71,7 +71,15 @@ public interface TraitService { ...@@ -71,7 +71,15 @@ public interface TraitService {
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @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 * Get a crop descriptor property by title
...@@ -99,7 +107,13 @@ public interface TraitService { ...@@ -99,7 +107,13 @@ public interface TraitService {
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @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 * List user's methods
......
...@@ -571,16 +571,18 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -571,16 +571,18 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @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 parameter = new Parameter();
parameter.setRdfUri(rdfUri);
parameter.setCrop(crop); parameter.setCrop(crop);
ParameterCategory parameterCategory = ParameterCategory parameterCategory =
parameterCategoryRepository.findByName(category); parameterCategoryRepository.findByName(category);
parameter.setCategory(parameterCategory); parameter.setCategory(parameterCategory);
parameter.setTitle(title); parameter.setTitle(title);
parameterRepository.save(parameter); parameterRepository.save(parameter);
...@@ -588,6 +590,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -588,6 +590,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return parameter; 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 @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true) @Transactional(readOnly = true)
...@@ -600,18 +615,26 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -600,18 +615,26 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public Method addMethod(String description, String unit, String fieldName, int fieldType, Integer fieldSize, String options, String range, public Method addMethod(
Parameter parameter) { String rdfUri, String description, String unit,
String fieldName, int fieldType, Integer fieldSize,
String options, String range,
Parameter parameter
) {
Method method = new Method(); Method method = new Method();
method.setRdfUri(rdfUri);
method.setMethod(htmlSanitizer.sanitize(description)); method.setMethod(htmlSanitizer.sanitize(description));
method.setUnit(StringUtils.defaultIfBlank(StringUtils.trimToNull(unit), null)); method.setUnit(StringUtils.defaultIfBlank(StringUtils.trimToNull(unit), null));
method.setFieldName(StringUtils.trimToNull(fieldName)); method.setFieldName(StringUtils.trimToNull(fieldName));
method.setFieldType(fieldType); method.setFieldType(fieldType);
if (fieldType == 0) { if (fieldType == 0) {
// Will throw NPE if not provided! // Will throw NPE if not provided!
method.setFieldSize(fieldSize.toString()); method.setFieldSize(fieldSize.toString());
} }
method.setOptions(StringUtils.defaultIfBlank(StringUtils.trimToNull(options), null)); method.setOptions(StringUtils.defaultIfBlank(StringUtils.trimToNull(options), null));
method.setRange(StringUtils.defaultIfBlank(StringUtils.trimToNull(range), null)); method.setRange(StringUtils.defaultIfBlank(StringUtils.trimToNull(range), null));
method.setParameter(parameter); method.setParameter(parameter);
...@@ -624,6 +647,33 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -624,6 +647,33 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return method; 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' * Returns datasets to which current user has 'WRITE'
*/ */
...@@ -1218,5 +1268,4 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -1218,5 +1268,4 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return paramMethods; return paramMethods;
} }
} }
...@@ -176,12 +176,16 @@ public class TraitsController extends RestController { ...@@ -176,12 +176,16 @@ public class TraitsController extends RestController {
Crop crop = cropService.getCrop(cropName) ; Crop crop = cropService.getCrop(cropName) ;
LOG.debug("Calling getParameter()"); 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) { if(property == null) {
// create if not available already? // create if not available already?
property = traitService.addParameter(crop, propertyJson.category, propertyJson.title); property = traitService.addParameter(propertyJson.rdfUri, crop, propertyJson.category, propertyJson.title);
} else } else
LOG.warn("Property '"+propertyJson.title+"' in category '"+ LOG.warn("Property '"+propertyJson.title+"' in category '"+
...@@ -193,9 +197,10 @@ public class TraitsController extends RestController { ...@@ -193,9 +197,10 @@ public class TraitsController extends RestController {
public static class PropertyJson { public static class PropertyJson {
//@NotNull // Resource Description Framework (RDF)
//@NotBlank // Uniform Resource Identifier (URI)
//public String rdfUri; // Kept optional for now (maybe null or blank)
public String rdfUri;
@NotNull @NotNull
@NotBlank @NotBlank
...@@ -235,18 +240,34 @@ public class TraitsController extends RestController { ...@@ -235,18 +240,34 @@ public class TraitsController extends RestController {
//if the (long integer) property id given in the path is zero, //if the (long integer) property id given in the path is zero,
// then the method is left "floating" in the database. // then the method is left "floating" in the database.
property = null ; 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, if(method == null) {
methodJson.options, methodJson.range, property); // 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; return method;
} }
public static class MethodJson { public static class MethodJson {
//@NotNull // Resource Description Framework (RDF)
//@NotBlank // Uniform Resource Identifier (URI)
//public String rdfUri; // Kept optional for now (maybe null or blank)
public String rdfUri;
@NotNull @NotNull
@NotBlank @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