Commit 771ea2a2 authored by Matija Obreza's avatar Matija Obreza
Browse files

Crops API update: update croprules, crop description

parent a86bd449
......@@ -21,8 +21,14 @@ import java.util.List;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface CropRuleRepository extends JpaRepository<CropRule, Long> {
List<CropRule> findByCrop(Crop crop);
@Modifying
@Query("delete from CropRule cr where cr.crop = ?1")
void deleteByCrop(Crop crop);
}
......@@ -66,4 +66,12 @@ public interface CropService {
List<CropRule> getCropRules(Crop crop);
/**
* Replace crop taxonomic rules
*
* @param crop
* @param cropRules
*/
void setCropRules(Crop crop, List<CropRule> cropRules);
}
......@@ -280,6 +280,18 @@ public class CropServiceImpl implements CropService {
return rule;
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#crop, 'ADMINISTRATION')")
@Transactional(readOnly = false)
public void setCropRules(Crop crop, List<CropRule> cropRules) {
cropRuleRepository.deleteByCrop(crop);
for (CropRule cr : cropRules) {
cr.setId(null);
cr.setCrop(crop);
}
cropRuleRepository.save(cropRules);
}
@Override
public List<CropRule> getCropRules(Crop crop) {
return crop == null ? null : cropRuleRepository.findByCrop(crop);
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.servlet.controller.rest;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.ValidationException;
......@@ -24,6 +25,7 @@ import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator;
import net.sf.oval.constraint.NotBlank;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.impl.Crop;
......@@ -32,6 +34,7 @@ import org.genesys2.server.model.impl.CropTaxonomy;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.TraitService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
......@@ -98,7 +101,9 @@ public class CropsController extends RestController {
crop = cropService.updateCrop(crop, cropJson.name, cropJson.description, cropJson.i18n);
}
for (final CropTaxonomyJson ctj : cropJson.taxonomies) {
// TODO Remove
if (cropJson.rules != null)
for (final CropRuleJson ctj : cropJson.rules) {
final CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included);
if (cropRule != null) {
LOG.info("Added rule: " + cropRule);
......@@ -151,6 +156,35 @@ public class CropsController extends RestController {
return OAuth2Cleanup.clean(cropRules);
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/rules", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object updateCropRules(@PathVariable("shortName") String shortName, @RequestBody List<CropRuleJson> rules) throws AuthorizationException {
LOG.info("Updating crop rules for " + shortName);
final Crop crop = cropService.getCrop(shortName);
if (crop == null)
throw new ResourceNotFoundException("No crop " + shortName);
final List<CropRule> cropRules = new ArrayList<CropRule>();
for (CropRuleJson ctj : rules) {
CropRule cr = new CropRule();
cr.setIncluded(ctj.included);
cr.setGenus(StringUtils.defaultIfBlank(ctj.genus, null));
cr.setSpecies(StringUtils.defaultIfBlank(ctj.species, null));
cropRules.add(cr);
}
cropService.setCropRules(crop, cropRules);
final List<CropRule> cropRules2 = cropService.getCropRules(crop);
return OAuth2Cleanup.clean(cropRules2);
}
/**
* Get crop taxonomies /crops/{shortName}/taxa
*
......@@ -172,13 +206,13 @@ public class CropsController extends RestController {
public String name;
public String description;
public String i18n;
public List<CropTaxonomyJson> taxonomies;
public List<CropRuleJson> rules;
}
public static class CropTaxonomyJson {
public static class CropRuleJson {
public boolean included;
@NotBlank
public String genus;
public String species;
public boolean included;
}
}
......@@ -51,6 +51,7 @@ public class OAuth2Cleanup {
if (t instanceof CropRule) {
final CropRule cropRule = (CropRule) t;
cropRule.setCrop(null);
cropRule.setId(null);
}
if (t instanceof CropTaxonomy) {
final CropTaxonomy cropTaxa = (CropTaxonomy) t;
......
......@@ -33,7 +33,7 @@ public class CropRuleSerializer extends JsonSerializer<CropRule> {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("id", cropRule.getId());
jgen.writeBooleanField("included", cropRule.isIncluded());
jgen.writeObjectField("genus", cropRule.getGenus());
if (cropRule.getSpecies() != null) {
jgen.writeObjectField("species", cropRule.getSpecies());
......
......@@ -37,6 +37,7 @@ public class CropSerializer extends JsonSerializer<Crop> {
} else {
jgen.writeStartObject();
jgen.writeObjectField("shortName", crop.getShortName());
jgen.writeObjectField("name", crop.getName());
if (StringUtils.isNotBlank(crop.getDescription())) {
jgen.writeObjectField("description", crop.getDescription());
}
......
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