Commit e30ce564 authored by Matija Obreza's avatar Matija Obreza

Removed crop.language, translations for all fields are stored in field

i18n 
parent 53bef5dd
......@@ -16,7 +16,9 @@
package org.genesys2.server.model.impl;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import javax.persistence.Column;
import javax.persistence.Entity;
......@@ -24,21 +26,23 @@ import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.BusinessModel;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Entity
@Table(name = "crop")
@Indexed
public class Crop extends BusinessModel {
private static final long serialVersionUID = -2686341831839109257L;
@Column(nullable = false, length = 2)
private String language;
/**
* Crop short name used as short name in URLs
*/
......@@ -46,19 +50,25 @@ public class Crop extends BusinessModel {
private String shortName;
@Column(nullable = false, length = 200)
@Field(name="title", store = Store.NO)
@Field(name = "title", store = Store.NO)
private String name;
@Lob
@Field(name="body", store = Store.NO)
@Field(name = "body", store = Store.NO)
private String description;
@Lob
private String i18n;
/**
* Rules
*/
@OneToMany(mappedBy = "crop", fetch = FetchType.EAGER, cascade = {}, orphanRemoval = true)
private List<CropRule> cropRules;
@Transient
private JsonNode i18nJ;
public String getName() {
return name;
}
......@@ -83,14 +93,6 @@ public class Crop extends BusinessModel {
this.description = description;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public List<CropRule> getCropRules() {
return cropRules;
}
......@@ -99,4 +101,32 @@ public class Crop extends BusinessModel {
this.cropRules = cropRules;
}
public String getI18n() {
return i18n;
}
public void setI18n(String i18n) {
this.i18n = i18n;
}
public String getName(Locale locale) {
return StringUtils.defaultIfBlank(translate("name", locale), this.name);
}
public String getDescription(Locale locale) {
return StringUtils.defaultIfBlank(translate("description", locale), this.name);
}
private synchronized String translate(String field, Locale locale) {
if (this.i18nJ == null && ! StringUtils.isBlank(this.i18n)) {
ObjectMapper mapper = new ObjectMapper();
try {
this.i18nJ = mapper.readTree(this.i18n);
} catch (IOException e) {
System.err.println("I18n = " + this.i18n);
e.printStackTrace();
}
}
return this.i18nJ!=null && this.i18nJ.has(field) && this.i18nJ.get(field).has(locale.getLanguage()) ? this.i18nJ.get(field).get(locale.getLanguage()).textValue() : null;
}
}
......@@ -16,15 +16,10 @@
package org.genesys2.server.persistence.domain;
import java.util.List;
import org.genesys2.server.model.impl.Crop;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CropRepository extends JpaRepository<Crop, Long> {
Crop findByShortName(String shortName);
List<Crop> findByLanguage(String language, Sort sort);
}
......@@ -42,8 +42,8 @@ public interface CropTaxonomyRepository extends JpaRepository<CropTaxonomy, Long
@Query("select distinct t from CropTaxonomy ct inner join ct.taxonomy t where ct.crop = ?1")
List<Taxonomy> findTaxonomiesByCrop(Crop crop);
@Query("select distinct ct.crop from CropTaxonomy ct where ct.taxonomy= ?1 and ct.crop.language = ?2")
List<Crop> findByTaxonomyAndLanguage(Taxonomy taxonomy, String language);
@Query("select distinct ct.crop from CropTaxonomy ct where ct.taxonomy= ?1")
List<Crop> findByTaxonomy(Taxonomy taxonomy);
@Modifying
@Query("delete from CropTaxonomy ct where ct.crop = ?1")
......
......@@ -32,7 +32,7 @@ public interface CropService {
List<Crop> list(Locale locale);
List<Crop> getCrop(Locale locale, Taxonomy taxonomy);
List<Crop> getCrops(Taxonomy taxonomy);
void rebuildTaxonomies();
......
......@@ -14,10 +14,11 @@
* limitations under the License.
**/
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
......@@ -35,7 +36,6 @@ import org.genesys2.server.service.CropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
......@@ -65,13 +65,20 @@ public class CropServiceImpl implements CropService {
}
@Override
public List<Crop> list(Locale locale) {
return cropRepository.findByLanguage(locale.getLanguage(), new Sort("name"));
public List<Crop> list(final Locale locale) {
List<Crop> crops = cropRepository.findAll();
Collections.sort(crops, new Comparator<Crop>() {
@Override
public int compare(Crop o1, Crop o2) {
return o1.getName(locale).compareTo(o2.getName(locale));
}
});
return crops;
}
@Override
public List<Crop> getCrop(Locale locale, Taxonomy taxonomy) {
return cropTaxonomyRepository.findByTaxonomyAndLanguage(taxonomy, locale.getLanguage());
public List<Crop> getCrops(Taxonomy taxonomy) {
return cropTaxonomyRepository.findByTaxonomy(taxonomy);
}
@Override
......
......@@ -77,7 +77,7 @@ public class AccessionController extends BaseController {
model.addAttribute("methods", genesysService.listMethods(accession));
model.addAttribute("methodValues", genesysService.getAccessionTraitValues(accession));
model.addAttribute("crop", cropService.getCrop(getLocale(), accession.getTaxonomy()));
model.addAttribute("crops", cropService.getCrops(accession.getTaxonomy()));
model.addAttribute("selection", selectionBean);
......
......@@ -20,7 +20,6 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
......@@ -72,16 +71,16 @@ public class HtmlController extends BaseController {
private String captchaPublicKey;
@RequestMapping("/")
public String welcome() {
public String index() {
return "redirect:/welcome";
}
@RequestMapping(value = "welcome")
public String index(ModelMap model) {
public String welcome(ModelMap model) {
List<Permissions> permissionsList = Arrays.asList(Permissions.values());
model.addAttribute("permissionsList", permissionsList);
// FIXME get user locale!
model.addAttribute("cropList", cropService.list(new Locale("en")));
model.addAttribute("cropList", cropService.list(getLocale()));
model.addAttribute("lastNews", activityPostService.lastNews());
return "/index";
}
......
......@@ -28,7 +28,7 @@
${filters[by].taxonName}
</c:when>
<c:when test="${clazz eq 'Crop'}">
<a href="<c:url value="/c/${filters[by].shortName}" />">${filters[by].name}</a>
<a href="<c:url value="/c/${filters[by].shortName}" />">${filters[by].getName(pageContext.response.locale)}</a>
</c:when>
<c:when test="${clazz eq 'Country'}">
<a href="<c:url value="/geo/${filters[by].code3.toLowerCase()}" />"><c:out value="${filters[by].getName(pageContext.response.locale)}" /></a>
......
......@@ -64,8 +64,8 @@
<c:if test="${crop ne null}">
<tr>
<td><spring:message code="accession.crop" /></td>
<td><c:forEach items="${crop}" var="c">
<a href="<c:url value="/c/${c.shortName}" />"><c:out value="${c.name}" /></a>
<td><c:forEach items="${crops}" var="crop">
<a href="<c:url value="/c/${crop.shortName}" />"><c:out value="${crop.getName(pageContext.response.locale)}" /></a>
</c:forEach></td>
</tr>
</c:if>
......
......@@ -4,7 +4,7 @@
<html>
<head>
<title><spring:message code="crop.page.profile.title" arguments="${crop.name}" /></title>
<title><spring:message code="crop.page.profile.title" arguments="${crop.getName(pageContext.response.locale)}" /></title>
</head>
<body>
<c:if test="${crop eq null}">
......@@ -14,11 +14,11 @@
</c:if>
<h1>
<c:out value="${crop.name}" />
<c:out value="${crop.getName(pageContext.response.locale)}" />
</h1>
<div class="free-text">
<c:out value="${crop.description}" />
<c:out value="${crop.getDescription(pageContext.response.locale)}" />
</div>
<div>
......
......@@ -9,10 +9,10 @@
<body>
<h1>
<c:out value="${trait.title}" />
<small>${trait.crop.name}</small>
<small>${trait.crop.getName(pageContext.response.locale)}</small>
</h1>
<div>
<spring:message code="filter.crop" />: <b><a href="<c:url value="/c/${trait.crop.shortName}" />">${trait.crop.name}</a></b>
<spring:message code="filter.crop" />: <b><a href="<c:url value="/c/${trait.crop.shortName}" />">${trait.crop.getName(pageContext.response.locale)}</a></b>
</div>
<h4>
<spring:message code="ce.methods" />
......
......@@ -13,7 +13,7 @@
<c:if test="${crop ne null}">
<div>
<spring:message code="filter.crop" />: <b><a href="<c:url value="/c/${crop.shortName}" />">${crop.name}</a></b>
<spring:message code="filter.crop" />: <b><a href="<c:url value="/c/${crop.shortName}" />">${crop.getName(pageContext.response.locale)}</a></b>
</div>
</c:if>
<div class="nav-header">
......@@ -36,7 +36,7 @@
<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="/c/${trait.crop.shortName}" />">${trait.crop.name}</a></td>
<td><a href="<c:url value="/c/${trait.crop.shortName}" />">${trait.crop.getName(pageContext.response.locale)}</a></td>
</tr>
</c:forEach>
</tbody>
......
......@@ -21,7 +21,7 @@
</sec:authorize>
<ul class="funny-list">
<c:forEach items="${cropList}" var="crop" varStatus="status">
<li><a class="show" href="/c/${crop.shortName}/data"><c:out value="${crop.name}" /></a></li>
<li><a class="show" href="/c/${crop.shortName}/data"><c:out value="${crop.getName(pageContext.response.locale)}" /></a></li>
</c:forEach>
</ul>
</c:if>
......
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