Commit 90f566ea authored by Matija Obreza's avatar Matija Obreza

Make entity Crop cacheable

parent 8eb6aa43
......@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.persistence.Cacheable;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
......@@ -48,6 +49,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Cacheable
@Entity
@Table(name = "crop")
public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
......@@ -87,8 +89,18 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
private List<CropRule> cropRules;
@Transient
private JsonNode i18nJ;
@JsonIgnore
private transient JsonNode i18nJ;
@Transient
@JsonIgnore
private transient JsonNode i18nJO;
@Transient
@JsonIgnore
private transient JsonNode i18nJU;
public String getName() {
return name;
}
......@@ -136,11 +148,6 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
}
}
@Transient
private JsonNode i18nJO;
@Transient
private JsonNode i18nJU;
/*
* This is meant to be a complex language tag merge operation... is there an
......@@ -182,8 +189,9 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
.textValue() : null;
}
@JsonIgnore
@Transient
private final Map<String, Map<String, String>> i18nMap = new HashMap<String, Map<String, String>>();
private transient final Map<String, Map<String, String>> i18nMap = new HashMap<String, Map<String, String>>();
private synchronized Map<String, String> buildVernacularMap(String field) {
getI18nJson();
......
......@@ -49,6 +49,7 @@ import java.util.*;
public class CropServiceImpl implements CropService {
private static final String CACHE_CROP_TAXONOMYCROPS = "hibernate.org.genesys2.server.model.impl.Crop.taxonomyCrops";
private static final String CACHE_CROPS = "hibernate.org.genesys2.server.model.impl.Crop.cache";
public static final Log LOG = LogFactory.getLog(CropServiceImpl.class);
......@@ -83,6 +84,7 @@ public class CropServiceImpl implements CropService {
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Override
@Transactional
@CacheEvict(allEntries=true, value=CACHE_CROPS)
public Crop delete(Crop crop) {
cropRepository.delete(crop);
crop.setId(null);
......@@ -97,18 +99,24 @@ public class CropServiceImpl implements CropService {
}
@Override
@Cacheable(value = CACHE_CROPS, key = "'findAll'")
public List<Crop> listCrops() {
List<Crop> crops = cropRepository.findAll();
// Fetch otherNames list
crops.stream().forEach(c -> { c.getOtherNames().size(); });
crops.stream().forEach(c -> {
c.getOtherNames().size();
});
return crops;
}
@Override
@Cacheable(value = CACHE_CROPS, key = "'findAll-'.concat(#locale)")
public List<Crop> list(final Locale locale) {
final List<Crop> crops = cropRepository.findAll();
// Fetch otherNames list
crops.stream().forEach(c -> { c.getOtherNames().size(); });
crops.stream().forEach(c -> {
c.getOtherNames().size();
});
Collections.sort(crops, new Comparator<Crop>() {
@Override
public int compare(Crop o1, Crop o2) {
......@@ -252,6 +260,7 @@ public class CropServiceImpl implements CropService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
@CacheEvict(allEntries=true, value=CACHE_CROPS)
public Crop addCrop(String shortName, String name, String description, String i18n) {
LOG.info("Adding crop " + shortName);
final Crop crop = new Crop();
......@@ -272,6 +281,7 @@ public class CropServiceImpl implements CropService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#crop, 'ADMINISTRATION')")
@Transactional(readOnly = false)
@CacheEvict(allEntries=true, value=CACHE_CROPS)
public Crop updateCrop(Crop crop, String name, String description, String i18n) {
LOG.info("Updating crop " + crop);
......
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