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

Troubleshooting of diverse aspects of the crop/category/parameter/method REST uploading processes.

parent dc09a340
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
package org.genesys2.server.model.impl; package org.genesys2.server.model.impl;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
...@@ -30,6 +30,8 @@ import javax.persistence.Table; ...@@ -30,6 +30,8 @@ import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.AuditedModel; import org.genesys2.server.model.AuditedModel;
import org.genesys2.server.servlet.controller.rest.serialization.CropSerializer; import org.genesys2.server.servlet.controller.rest.serialization.CropSerializer;
import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Field;
...@@ -45,7 +47,10 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; ...@@ -45,7 +47,10 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Indexed @Indexed
@JsonSerialize(using = CropSerializer.class) @JsonSerialize(using = CropSerializer.class)
public class Crop extends AuditedModel { public class Crop extends AuditedModel {
private static final long serialVersionUID = -2686341831839109257L; private static final long serialVersionUID = -2686341831839109257L;
public static final Log LOG = LogFactory.getLog(Crop.class);
/** /**
* Crop short name used as short name in URLs * Crop short name used as short name in URLs
...@@ -115,20 +120,22 @@ public class Crop extends AuditedModel { ...@@ -115,20 +120,22 @@ public class Crop extends AuditedModel {
public void updateI18n(String i18n) { public void updateI18n(String i18n) {
// additional sanity check (but already checked elsewhere?) // additional sanity check (but already checked elsewhere?)
if (i18n != null && !StringUtils.isBlank(i18n)) if(!(i18n == null || StringUtils.isBlank(i18n)))
update(i18n); update(i18n);
} }
@Transient @Transient
private JsonNode i18nJO; private JsonNode i18nJO ;
@Transient @Transient
private JsonNode i18nJU; private JsonNode i18nJU ;
/* /*
* Complex language tag merge operation... is there an easier way? * This is meant to be a complex language tag merge operation... is there an easier way?
*/ */
private synchronized void update(String i18n) { private synchronized void update(String i18n) {
this.i18n = i18n ;
/** TODO Need to properly code and test this!
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
try { try {
this.i18nJO = mapper.readTree(this.i18n); this.i18nJO = mapper.readTree(this.i18n);
...@@ -137,23 +144,23 @@ public class Crop extends AuditedModel { ...@@ -137,23 +144,23 @@ public class Crop extends AuditedModel {
System.err.println("I18n = " + i18n); System.err.println("I18n = " + i18n);
e.printStackTrace(); e.printStackTrace();
} }
if (this.i18nJU != null) { if(this.i18nJU != null) {
Iterator<String> jui = this.i18nJU.fieldNames(); Iterator<String> jui = this.i18nJU.fieldNames() ;
while (jui.hasNext()) { while(jui.hasNext()) {
String uField = jui.next(); String uField = jui.next() ;
if (this.i18nJO.has(uField)) { if(this.i18nJO.has(uField)) {
JsonNode juf = this.i18nJU.get(uField); JsonNode juf = this.i18nJU.get(uField) ;
JsonNode jof = this.i18nJO.get(uField); JsonNode jof = this.i18nJO.get(uField) ;
Iterator<String> ulocales = juf.fieldNames(); Iterator<String> ulocales = juf.fieldNames() ;
while (ulocales.hasNext()) { while(ulocales.hasNext()) {
String locale = ulocales.next(); String locale = ulocales.next() ;
// TODO - append/overwrite jof.ufield.locale with // TODO - append/overwrite jof.ufield.locale with juf.ufield.locale
// juf.ufield.locale }
}
} }
} }
this.i18n = this.i18nJO.asText(); this.i18n = this.i18nJO.asText() ;
} }
*/
} }
public String getName(Locale locale) { public String getName(Locale locale) {
......
...@@ -52,9 +52,9 @@ public interface TraitService { ...@@ -52,9 +52,9 @@ public interface TraitService {
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
ParameterCategory addCategory(String name, String i18n); ParameterCategory addCategory(String name, String i18n);
// TODO implement i18n functionality // TODO implement rdfUri functionality
// ParameterCategory addCategory(String name, String i18n); // ParameterCategory addCategory(String rdfUri, String name, String i18n);
/** /**
* Get an existing crop descriptor Property Category by name * Get an existing crop descriptor Property Category by name
...@@ -79,9 +79,7 @@ public interface TraitService { ...@@ -79,9 +79,7 @@ public interface TraitService {
/** /**
* *
* @param rdfUri * @param rdfUri is the Resource Description Framework (RDF) Uniform Resource Identifier (URI) of the Parameter concept
* is the Resource Description Framework (RDF) Uniform Resource
* Identifier (URI) of the Parameter concept
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
...@@ -113,8 +111,18 @@ public interface TraitService { ...@@ -113,8 +111,18 @@ public interface TraitService {
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
Method addMethod(String rdfUri, String description, String i18n, String unit, String fieldName, int fieldType, Integer fieldSize, String options, Method addMethod(
String range, Parameter parameter); String rdfUri,
String description,
String i18n,
String unit,
String fieldName,
int fieldType,
Integer fieldSize,
String options,
String range,
Parameter parameter
);
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
Method getMethod(String rdfUri); Method getMethod(String rdfUri);
......
...@@ -595,11 +595,11 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -595,11 +595,11 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public ParameterCategory addCategory(String name, String i18n) { public ParameterCategory addCategory(String name, String i18n) {
ParameterCategory category = new ParameterCategory(); ParameterCategory category = new ParameterCategory();
category.setName(name); category.setName(name);
category.setNameL(i18n); category.setNameL(i18n);
parameterCategoryRepository.save(category); parameterCategoryRepository.save(category);
return category; return category;
} }
...@@ -607,24 +607,33 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -607,24 +607,33 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true) @Transactional(readOnly = true)
public ParameterCategory getCategory(String name) { public ParameterCategory getCategory(String name) {
ParameterCategory category = parameterCategoryRepository.findByName(name); ParameterCategory category =
parameterCategoryRepository.findByName(name);
return category; return category;
} }
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public Parameter addParameter(String rdfUri, Crop crop, String category, String title, String i18n) { public Parameter addParameter(
String rdfUri,
Crop crop, String category,
String title, String i18n
) {
Parameter parameter = new Parameter(); Parameter parameter = new Parameter();
parameter.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null)); parameter.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
parameter.setCrop(crop); parameter.setCrop(crop);
ParameterCategory parameterCategory = parameterCategoryRepository.findByName(category);
// In an ideal world, we should search for category by URI, not name...
ParameterCategory parameterCategory =
parameterCategoryRepository.findByName(category);
parameter.setCategory(parameterCategory); parameter.setCategory(parameterCategory);
parameter.setTitle(title); parameter.setTitle(title);
parameter.setI18n(StringUtils.defaultIfBlank(i18n, null)); parameter.setI18n(StringUtils.defaultIfBlank(i18n, null));
parameterRepository.save(parameter); parameterRepository.save(parameter);
return parameter; return parameter;
...@@ -634,17 +643,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -634,17 +643,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Parameter getParameter(String rdfUri) { public Parameter getParameter(String rdfUri) {
if (rdfUri == null || rdfUri.isEmpty()) if(rdfUri == null || rdfUri.isEmpty() )
return null; return null ;
Parameter parameter = parameterRepository.findByRdfUri(rdfUri); // assumes Parameter parameter =
// that parameterRepository.findByRdfUri(rdfUri) ; // assumes that crop x title is unique(?)
// crop
// x
// title
// is
// unique(?)
return parameter; return parameter;
} }
...@@ -652,22 +656,21 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -652,22 +656,21 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Parameter getParameter(Crop crop, String title) { public Parameter getParameter(Crop crop, String title) {
Parameter parameter = parameterRepository.findByCropAndTitle(crop, title); // assumes Parameter parameter =
// that parameterRepository.findByCropAndTitle(crop, title) ; // assumes that crop x title is unique(?)
// crop
// x
// title
// is
// unique(?)
return parameter; return parameter;
} }
@Override @Override
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public Method addMethod(String rdfUri, String description, String i18n, String unit, String fieldName, int fieldType, Integer fieldSize, String options, public Method addMethod(
String range, Parameter parameter) { String rdfUri, String description, String i18n, String unit,
String fieldName, int fieldType, Integer fieldSize,
String options, String range,
Parameter parameter
) {
Method method = new Method(); Method method = new Method();
method.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null)); method.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
...@@ -676,12 +679,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -676,12 +679,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
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);
...@@ -698,23 +701,26 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -698,23 +701,26 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Method getMethod(String rdfUri) { public Method getMethod(String rdfUri) {
if (rdfUri == null || rdfUri.isEmpty()) if(rdfUri == null || rdfUri.isEmpty() )
return null; return null ;
Method method = methodRepository.findByRdfUri(rdfUri); Method method =
methodRepository.findByRdfUri(rdfUri) ;
return method; return method;
} }
@Override @Override
public Method getMethod(String description, Parameter parameter) { public Method getMethod(String description, Parameter parameter) {
if (description == null || description.isEmpty()) if(description == null || description.isEmpty() )
return null; return null ;
Method method = methodRepository.findByMethodAndParameter(description, parameter); Method method =
methodRepository.findByMethodAndParameter(description,parameter) ;
return method; return method;
} }
...@@ -1312,5 +1318,4 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -1312,5 +1318,4 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return paramMethods; return paramMethods;
} }
} }
...@@ -101,41 +101,47 @@ public class TraitsController extends RestController { ...@@ -101,41 +101,47 @@ public class TraitsController extends RestController {
return OAuth2Cleanup.clean(methods); return OAuth2Cleanup.clean(methods);
} }
/** /**
* Create a new crop property category (ParameterCategory) to use with * Create a new crop property category (ParameterCategory) to
* descriptors (property/method/scale) meta-data * use with descriptors (property/method/scale) meta-data
* *
* @return * @return
* @throws ValidationException * @throws ValidationException
*/ */
@RequestMapping(value = "/category", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) @RequestMapping(value = "/category", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody public @ResponseBody
Object createCategory(@RequestBody CategoryJson categoryJson) throws ValidationException { Object createCategory( @RequestBody CategoryJson categoryJson ) throws ValidationException {
LOG.info("Creating trait property category:"); LOG.info("Creating trait property category:");
Validator validator = new Validator(); Validator validator = new Validator();
List<ConstraintViolation> violations = validator.validate(categoryJson); List<ConstraintViolation> violations = validator.validate(categoryJson);
if (violations.size() > 0) { if (violations.size() > 0) {
// TODO We could do better messages on validation error // TODO We could do better messages on validation error
throw new ModelValidationException("Validation problem", violations); throw new ModelValidationException("Validation problem", violations);
} }
ParameterCategory category = traitService.getCategory(categoryJson.name); ParameterCategory category = traitService.getCategory(categoryJson.name);
if (category == null) // create if not available already? if(category == null)
category = traitService.addCategory(categoryJson.name, categoryJson.i18n); // create if not available already?
// Ignoring rdfUri for the time being
// TODO: decide if adding rdfURI will be useful in the future?
// category= traitService.addCategory(categoryJson.rdfUri, categoryJson.name, categoryJson.i18n);
category= traitService.addCategory(categoryJson.name, categoryJson.i18n);
else else
LOG.warn("Property category " + categoryJson.name + " already exists?"); LOG.warn("Property category "+categoryJson.name+" already exists?");
return category; return category;
} }
public static class CategoryJson { public static class CategoryJson {
// @NotNull // Resource Description Framework (RDF)
// @NotBlank // Uniform Resource Identifier (URI)
// public String uri; // Kept optional for now (maybe null or blank)
public String rdfUri;
@NotNull @NotNull
@NotBlank @NotBlank
...@@ -143,27 +149,28 @@ public class TraitsController extends RestController { ...@@ -143,27 +149,28 @@ public class TraitsController extends RestController {
@NotNull @NotNull
@NotBlank @NotBlank
public String i18n; public String i18n;
} }
/** /**
* Adds a new property by crop, if such a property for a given crop and * Adds a new property by crop, if such a property for a given crop
* title doesn't already exist. If it does exist, then the existing record * and title doesn't already exist. If it does exist, then the
* is retrieved and sent back. * existing record is retrieved and sent back.
* *
* @param cropName * @param cropName to which the property belongs (given as a string in the path of the API call)
* to which the property belongs (given as a string in the path
* of the API call)
* @param propertyJson * @param propertyJson
* @return * @return
* @throws ValidationException * @throws ValidationException
*/ */
@RequestMapping(value = "/{crop}/property", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE }) @RequestMapping(value = "/{crop}/property", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody public @ResponseBody
Object createProperty(@PathVariable("crop") String cropName, @RequestBody PropertyJson propertyJson) throws ValidationException { Object createProperty(
@PathVariable("crop") String cropName,
@RequestBody PropertyJson propertyJson
) throws ValidationException {
LOG.info("Creating property for crop '" + cropName + "':"); LOG.info("Creating property for crop '"+cropName+"':");
Validator validator = new Validator(); Validator validator = new Validator();
List<ConstraintViolation> violations = validator.validate(propertyJson); List<ConstraintViolation> violations = validator.validate(propertyJson);
...@@ -172,31 +179,47 @@ public class TraitsController extends RestController { ...@@ -172,31 +179,47 @@ public class TraitsController extends RestController {
throw new ModelValidationException("Validation problem", violations); throw new ModelValidationException("Validation problem", violations);
} }
LOG.info("Calling cropService.getCrop('" + cropName + "'):"); LOG.info("Calling cropService.getCrop('"+cropName+"'):");
Crop crop = cropService.getCrop(cropName);
LOG.debug("Calling getParameter()"); Crop crop = cropService.getCrop(cropName) ;
Parameter property; LOG.info("Calling getParameter("+propertyJson.rdfUri+")");
if (propertyJson.rdfUri != null && !propertyJson.rdfUri.isEmpty())
Parameter property ;
if(!(propertyJson.rdfUri == null || propertyJson.rdfUri.isEmpty()) )
property = traitService.getParameter(propertyJson.rdfUri); property = traitService.getParameter(propertyJson.rdfUri);
else else
property = traitService.getParameter(crop, propertyJson.title); property = traitService.getParameter(crop, propertyJson.title);
if (property == null) { LOG.info("Property found is "+(property!=null?property.getTitle():"null")+".");
if(property == null) {
LOG.info("Calling addParameter '"+
propertyJson.title+
"' in category '"+
propertyJson.category+"'.") ;
// create if not available already? // create if not available already?
property = traitService.addParameter(propertyJson.rdfUri, crop, propertyJson.category, propertyJson.title, propertyJson.i18n); property = traitService.addParameter(
propertyJson.rdfUri,
crop,
propertyJson.category,
propertyJson.title,
propertyJson.i18n
);
} else } else
LOG.warn("Property '" + propertyJson.title + "' in category '" + propertyJson.category + "' for crop '" + cropName + "' already exists?"); LOG.warn("Property '"+propertyJson.title+"' in category '"+
// TODO perhaps update parameter.i18n value here?