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

Added quite a bit of I18N support for Crop, ParameterCategory, Parameter

and Method data, however, new code is essentially untested: you've been
warned!!!
parent 48c0e8d6
......@@ -16,6 +16,8 @@
package org.genesys2.server.model.genesys;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID;
import javax.persistence.Column;
......@@ -30,6 +32,9 @@ import javax.persistence.Transient;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "method")
public class Method extends VersionedAuditedModel implements AclAwareModel {
......@@ -52,6 +57,12 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
@Lob
private String method;
@Lob
private String i18n;
@Transient
private JsonNode methodJ;
@Column(length = 32, nullable = true)
private String unit;
......@@ -93,10 +104,33 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
return this.method;
}
public String getMethod(Locale locale) {
return getMethodLocal(locale);
}
private synchronized String getMethodLocal(Locale locale) {
if (this.methodJ == null && this.i18n != null) {
ObjectMapper mapper = new ObjectMapper();
try {
this.methodJ = mapper.readTree(i18n);
} catch (IOException e) {
e.printStackTrace();
}
}
return this.methodJ != null &&
this.methodJ.has(locale.getLanguage())
? this.methodJ.get(locale.getLanguage()).textValue()
: this.method;
}
public void setMethod(final String method) {
this.method = method;
}
public void setI18n(String i18n) {
this.i18n = i18n;
}
public String getUnit() {
return this.unit;
}
......
......@@ -16,6 +16,8 @@
package org.genesys2.server.model.genesys;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID;
import javax.persistence.Column;
......@@ -25,11 +27,15 @@ import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.model.impl.Crop;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "parameter")
public class Parameter extends VersionedAuditedModel implements AclAwareModel {
......@@ -56,6 +62,12 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
@Column(nullable = false, length = 32)
private String title;
@Lob
private String i18n;
@Transient
private JsonNode titleJ;
public Parameter() {
}
......@@ -98,6 +110,29 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
this.title = title;
}
public String getTitle(Locale locale) {
return getTitleLocal(locale);
}
private synchronized String getTitleLocal(Locale locale) {
if (this.titleJ == null && this.i18n != null) {
ObjectMapper mapper = new ObjectMapper();
try {
this.titleJ = mapper.readTree(i18n);
} catch (IOException e) {
e.printStackTrace();
}
}
return this.titleJ != null &&
this.titleJ.has(locale.getLanguage())
? this.titleJ.get(locale.getLanguage()).textValue()
: this.title;
}
public void setI18n(String i18n) {
this.i18n = i18n;
}
// @Override
// public String toString() {
// return MessageFormat
......
......@@ -17,6 +17,7 @@
package org.genesys2.server.model.impl;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
......@@ -112,6 +113,48 @@ public class Crop extends AuditedModel {
this.i18n = i18n;
}
public void updateI18n(String i18n) {
// additional sanity check (but already checked elsewhere?)
if(i18n != null && !StringUtils.isBlank(i18n))
update(i18n);
}
@Transient
private JsonNode i18nJO ;
@Transient
private JsonNode i18nJU ;
/*
* Complex language tag merge operation... is there an easier way?
*/
private synchronized void update(String i18n) {
ObjectMapper mapper = new ObjectMapper();
try {
this.i18nJO = mapper.readTree(this.i18n);
this.i18nJU = mapper.readTree(i18n);
} catch (IOException e) {
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
}
}
}
this.i18n = this.i18nJO.asText() ;
}
}
public String getName(Locale locale) {
return StringUtils.defaultIfBlank(translate("name", locale), this.name);
}
......
......@@ -49,6 +49,16 @@ public interface CropService {
*/
Crop addCrop(String shortName, String name, String description, String i18n);
/**
* Updates a crop record
* @param shortName
* @param name
* @param description
* @param i18n
* @return
*/
Crop updateCrop(Crop crop, String name, String description, String i18n);
CropRule addCropRule(Crop crop, String genus, String species, boolean included);
List<CropRule> getCropRules(Crop crop);
......
......@@ -51,7 +51,10 @@ public interface TraitService {
* @return
*/
@PreAuthorize("isAuthenticated()")
ParameterCategory addCategory(String name);
ParameterCategory addCategory(String name, String i18n);
// TODO implement i18n functionality
// ParameterCategory addCategory(String name, String i18n);
/**
* Get an existing crop descriptor Property Category by name
......@@ -68,10 +71,11 @@ public interface TraitService {
* @param crop
* @param category
* @param title
* @param i18n
* @return
*/
@PreAuthorize("isAuthenticated()")
Parameter addParameter(String rdfUri, Crop crop, String category, String title);
Parameter addParameter(String rdfUri, Crop crop, String category, String title, String i18n);
/**
*
......@@ -107,7 +111,18 @@ public interface TraitService {
* @return
*/
@PreAuthorize("isAuthenticated()")
Method addMethod(String rdfUri, String description, 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);
......
......@@ -224,6 +224,33 @@ public class CropServiceImpl implements CropService {
return crop;
}
/**
* Update a crop record in Genesys
*/
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
public Crop updateCrop(Crop crop, String name, String description, String i18n) {
LOG.info("Updating crop " + crop);
if(crop != null) {
name = StringUtils.defaultIfBlank(name, null) ;
if(name != null) crop.setName(name);
description = StringUtils.defaultIfBlank(htmlSanitizer.sanitize(description),null) ;
if(description != null) crop.setDescription(description);
i18n = StringUtils.defaultIfBlank(i18n, null);
if(i18n != null) crop.updateI18n(i18n);
cropRepository.save(crop);
LOG.info("Updated crop: " + crop);
}
return crop;
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
......
......@@ -548,14 +548,11 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public ParameterCategory addCategory(String name) {
public ParameterCategory addCategory(String name, String i18n) {
ParameterCategory category = new ParameterCategory();
category.setName(name);
category.setNameL(i18n);
parameterCategoryRepository.save(category);
return category;
}
......@@ -573,17 +570,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Transactional(readOnly = false)
public Parameter addParameter(
String rdfUri,
Crop crop, String category, String title
Crop crop, String category,
String title, String i18n
) {
Parameter parameter = new Parameter();
parameter.setRdfUri(rdfUri);
parameter.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
parameter.setCrop(crop);
ParameterCategory parameterCategory =
parameterCategoryRepository.findByName(category);
parameter.setCategory(parameterCategory);
parameter.setTitle(title);
parameter.setI18n(StringUtils.defaultIfBlank(i18n, null));
parameterRepository.save(parameter);
......@@ -616,7 +615,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Method addMethod(
String rdfUri, String description, String unit,
String rdfUri, String description, String i18n, String unit,
String fieldName, int fieldType, Integer fieldSize,
String options, String range,
Parameter parameter
......@@ -624,8 +623,9 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
Method method = new Method();
method.setRdfUri(rdfUri);
method.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
method.setMethod(htmlSanitizer.sanitize(description));
method.setI18n(StringUtils.defaultIfBlank(i18n, null));
method.setUnit(StringUtils.defaultIfBlank(StringUtils.trimToNull(unit), null));
method.setFieldName(StringUtils.trimToNull(fieldName));
method.setFieldType(fieldType);
......
......@@ -89,7 +89,14 @@ public class CropsController extends RestController {
// TODO We could do better messages on validation error
throw new ModelValidationException("Crop does not validate", violations);
}
Crop crop = cropService.addCrop(cropJson.shortName, cropJson.name, cropJson.description, cropJson.i18n);
Crop crop = cropService.getCrop(cropJson.shortName) ;
if(crop == null)
crop = cropService.addCrop(cropJson.shortName, cropJson.name, cropJson.description, cropJson.i18n);
else
crop = cropService.updateCrop(crop, cropJson.name, cropJson.description, cropJson.i18n);
for (CropTaxonomyJson ctj : cropJson.taxonomies) {
CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included);
if (cropRule != null)
......
......@@ -125,11 +125,10 @@ public class TraitsController extends RestController {
ParameterCategory category = traitService.getCategory(categoryJson.name);
if(category == null) // create if not available already?
category= traitService.addCategory(categoryJson.name);
category= traitService.addCategory(categoryJson.name, categoryJson.i18n);
else
LOG.warn("Property category "+categoryJson.name+" already exists?");
return category;
}
......@@ -143,6 +142,10 @@ public class TraitsController extends RestController {
@NotBlank
public String name;
@NotNull
@NotBlank
public String i18n;
}
/**
......@@ -185,12 +188,19 @@ public class TraitsController extends RestController {
if(property == null) {
// create if not available already?
property = traitService.addParameter(propertyJson.rdfUri, crop, propertyJson.category, propertyJson.title);
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?
return property;
}
......@@ -210,6 +220,10 @@ public class TraitsController extends RestController {
@NotBlank
public String title;
@NotNull
@NotBlank
public String i18n;
}
/**
......@@ -251,7 +265,7 @@ public class TraitsController extends RestController {
// create if not available already?
method =
traitService.addMethod(
methodJson.rdfUri, methodJson.description, methodJson.unit,
methodJson.rdfUri, methodJson.description, methodJson.i18n, methodJson.unit,
methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, property
);
......@@ -274,6 +288,10 @@ public class TraitsController extends RestController {
@MinLength(10)
public String description;
@NotNull
@NotBlank
public String i18n;
@NotBlank
public String unit;
......
......@@ -32,7 +32,7 @@
<c:forEach items="${traitMethods}" var="method">
<tr>
<td><c:out value="${method.parameter.title}" /></td>
<td><a href="<c:url value="/descriptors/${trait.id}/${method.id}" />"><c:out value="${method.method}" /></a></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>
</tr>
......
......@@ -40,7 +40,7 @@
<c:forEach items="${pagedData.content}" var="trait" varStatus="status">
<tr class="${status.count % 2 == 0 ? 'even' : 'odd'}">
<td class="idx-col">${status.count + pagedData.size * pagedData.number}</td>
<td><a href="<c:url value="/descriptors/${trait.id}" />">${trait.title}</a></td>
<td><a href="<c:url value="/descriptors/${trait.id}" />">${trait.getTitle(pageContext.response.locale)}</a></td>
<td><a href="<c:url value="/c/${trait.crop.shortName}" />">${trait.crop.getName(pageContext.response.locale)}</a></td>
<td><c:out value="${trait.category.getName(pageContext.response.locale)}" /></td>
</tr>
......
......@@ -16,8 +16,8 @@
<table>
<tbody>
<tr>
<td><a href="<c:url value="/descriptors/${trait.id}" />"><c:out value="${method.parameter.title}" /></a></td>
<td><a href="<c:url value="/descriptors/${trait.id}/${method.id}" />"><c:out value="${method.method}" /></a></td>
<td><a href="<c:url value="/descriptors/${trait.id}" />"><c:out value="${method.parameter.getTitle(pageContext.response.locale)}" /></a></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.fieldName}" /></td>
<td><c:out value="${method.unit}" /></td>
</tr>
......
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