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