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 @@ ...@@ -16,6 +16,8 @@
package org.genesys2.server.model.genesys; package org.genesys2.server.model.genesys;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID; import java.util.UUID;
import javax.persistence.Column; import javax.persistence.Column;
...@@ -30,6 +32,9 @@ import javax.persistence.Transient; ...@@ -30,6 +32,9 @@ import javax.persistence.Transient;
import org.genesys2.server.model.AclAwareModel; import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel; import org.genesys2.server.model.VersionedAuditedModel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Entity @Entity
@Table(name = "method") @Table(name = "method")
public class Method extends VersionedAuditedModel implements AclAwareModel { public class Method extends VersionedAuditedModel implements AclAwareModel {
...@@ -51,6 +56,12 @@ public class Method extends VersionedAuditedModel implements AclAwareModel { ...@@ -51,6 +56,12 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
@Column(nullable = false) @Column(nullable = false)
@Lob @Lob
private String method; private String method;
@Lob
private String i18n;
@Transient
private JsonNode methodJ;
@Column(length = 32, nullable = true) @Column(length = 32, nullable = true)
private String unit; private String unit;
...@@ -93,9 +104,32 @@ public class Method extends VersionedAuditedModel implements AclAwareModel { ...@@ -93,9 +104,32 @@ public class Method extends VersionedAuditedModel implements AclAwareModel {
return this.method; 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) { public void setMethod(final String method) {
this.method = method; this.method = method;
} }
public void setI18n(String i18n) {
this.i18n = i18n;
}
public String getUnit() { public String getUnit() {
return this.unit; return this.unit;
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.genesys2.server.model.genesys; package org.genesys2.server.model.genesys;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID; import java.util.UUID;
import javax.persistence.Column; import javax.persistence.Column;
...@@ -25,11 +27,15 @@ import javax.persistence.Lob; ...@@ -25,11 +27,15 @@ 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;
import javax.persistence.Transient;
import org.genesys2.server.model.AclAwareModel; import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel; import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Entity @Entity
@Table(name = "parameter") @Table(name = "parameter")
public class Parameter extends VersionedAuditedModel implements AclAwareModel { public class Parameter extends VersionedAuditedModel implements AclAwareModel {
...@@ -56,6 +62,12 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel { ...@@ -56,6 +62,12 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
@Column(nullable = false, length = 32) @Column(nullable = false, length = 32)
private String title; private String title;
@Lob
private String i18n;
@Transient
private JsonNode titleJ;
public Parameter() { public Parameter() {
} }
...@@ -97,6 +109,29 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel { ...@@ -97,6 +109,29 @@ public class Parameter extends VersionedAuditedModel implements AclAwareModel {
public void setTitle(final String title) { public void setTitle(final String title) {
this.title = title; 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 // @Override
// public String toString() { // public String toString() {
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +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;
...@@ -112,6 +113,48 @@ public class Crop extends AuditedModel { ...@@ -112,6 +113,48 @@ public class Crop extends AuditedModel {
this.i18n = i18n; 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) { public String getName(Locale locale) {
return StringUtils.defaultIfBlank(translate("name", locale), this.name); return StringUtils.defaultIfBlank(translate("name", locale), this.name);
} }
......
...@@ -48,6 +48,16 @@ public interface CropService { ...@@ -48,6 +48,16 @@ public interface CropService {
* @return * @return
*/ */
Crop addCrop(String shortName, String name, String description, String i18n); 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); CropRule addCropRule(Crop crop, String genus, String species, boolean included);
......
...@@ -51,7 +51,10 @@ public interface TraitService { ...@@ -51,7 +51,10 @@ public interface TraitService {
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @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 * Get an existing crop descriptor Property Category by name
...@@ -68,10 +71,11 @@ public interface TraitService { ...@@ -68,10 +71,11 @@ public interface TraitService {
* @param crop * @param crop
* @param category * @param category
* @param title * @param title
* @param i18n
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @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 { ...@@ -107,7 +111,18 @@ public interface TraitService {
* @return * @return
*/ */
@PreAuthorize("isAuthenticated()") @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()") @PreAuthorize("isAuthenticated()")
Method getMethod(String rdfUri); Method getMethod(String rdfUri);
......
...@@ -223,6 +223,33 @@ public class CropServiceImpl implements CropService { ...@@ -223,6 +223,33 @@ public class CropServiceImpl implements CropService {
return crop; 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 @Override
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
......
...@@ -548,14 +548,11 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -548,14 +548,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) { public ParameterCategory addCategory(String name, String i18n) {
ParameterCategory category = new ParameterCategory(); ParameterCategory category = new ParameterCategory();
category.setName(name); category.setName(name);
category.setNameL(i18n);
parameterCategoryRepository.save(category); parameterCategoryRepository.save(category);
return category; return category;
} }
...@@ -573,17 +570,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -573,17 +570,19 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@Transactional(readOnly = false) @Transactional(readOnly = false)
public Parameter addParameter( public Parameter addParameter(
String rdfUri, String rdfUri,
Crop crop, String category, String title Crop crop, String category,
String title, String i18n
) { ) {
Parameter parameter = new Parameter(); Parameter parameter = new Parameter();
parameter.setRdfUri(rdfUri); parameter.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
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);
parameter.setI18n(StringUtils.defaultIfBlank(i18n, null));
parameterRepository.save(parameter); parameterRepository.save(parameter);
...@@ -616,7 +615,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -616,7 +615,7 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public Method addMethod( public Method addMethod(
String rdfUri, String description, String unit, String rdfUri, String description, String i18n, String unit,
String fieldName, int fieldType, Integer fieldSize, String fieldName, int fieldType, Integer fieldSize,
String options, String range, String options, String range,
Parameter parameter Parameter parameter
...@@ -624,8 +623,9 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset ...@@ -624,8 +623,9 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
Method method = new Method(); Method method = new Method();
method.setRdfUri(rdfUri); method.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
method.setMethod(htmlSanitizer.sanitize(description)); method.setMethod(htmlSanitizer.sanitize(description));
method.setI18n(StringUtils.defaultIfBlank(i18n, null));
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);
......
...@@ -89,7 +89,14 @@ public class CropsController extends RestController { ...@@ -89,7 +89,14 @@ public class CropsController extends RestController {
// TODO We could do better messages on validation error // TODO We could do better messages on validation error
throw new ModelValidationException("Crop does not validate", violations); 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) { for (CropTaxonomyJson ctj : cropJson.taxonomies) {
CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included); CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included);
if (cropRule != null) if (cropRule != null)
......
...@@ -125,11 +125,10 @@ public class TraitsController extends RestController { ...@@ -125,11 +125,10 @@ public class TraitsController extends RestController {
ParameterCategory category = traitService.getCategory(categoryJson.name); ParameterCategory category = traitService.getCategory(categoryJson.name);
if(category == null) // create if not available already? if(category == null) // create if not available already?
category= traitService.addCategory(categoryJson.name); 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;
} }
...@@ -143,6 +142,10 @@ public class TraitsController extends RestController { ...@@ -143,6 +142,10 @@ public class TraitsController extends RestController {
@NotBlank @NotBlank
public String name; public String name;
@NotNull
@NotBlank
public String i18n;
} }
/** /**
...@@ -185,12 +188,19 @@ public class TraitsController extends RestController { ...@@ -185,12 +188,19 @@ public class TraitsController extends RestController {
if(property == null) { if(property == null) {
// create if not available already? // 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 } else
LOG.warn("Property '"+propertyJson.title+"' in category '"+ LOG.warn("Property '"+propertyJson.title+"' in category '"+
propertyJson.category+"' for crop '"+ propertyJson.category+"' for crop '"+
cropName+"' already exists?"); cropName+"' already exists?");
// TODO perhaps update parameter.i18n value here?
return property; return property;
} }
...@@ -210,6 +220,10 @@ public class TraitsController extends RestController { ...@@ -210,6 +220,10 @@ public class TraitsController extends RestController {
@NotBlank @NotBlank
public String title; public String title;
@NotNull
@NotBlank
public String i18n;
} }
/** /**
...@@ -251,7 +265,7 @@ public class TraitsController extends RestController { ...@@ -251,7 +265,7 @@ public class TraitsController extends RestController {
// create if not available already? // create if not available already?
method = method =
traitService.addMethod( traitService.addMethod(
methodJson.rdfUri, methodJson.description, methodJson.unit, methodJson.rdfUri, methodJson.description, methodJson.i18n, methodJson.unit,
methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize, methodJson.fieldName, methodJson.fieldType, methodJson.fieldSize,
methodJson.options, methodJson.range, property methodJson.options, methodJson.range, property
); );
...@@ -274,6 +288,10 @@ public class TraitsController extends RestController { ...@@ -274,6 +288,10 @@ public class TraitsController extends RestController {
@MinLength(10) @MinLength(10)
public String description; public String description;
@NotNull
@NotBlank
public String i18n;
@NotBlank @NotBlank
public String unit; public String unit;
......