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; ...@@ -21,8 +21,14 @@ import java.util.List;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule; import org.genesys2.server.model.impl.CropRule;
import org.springframework.data.jpa.repository.JpaRepository; 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> { public interface CropRuleRepository extends JpaRepository<CropRule, Long> {
List<CropRule> findByCrop(Crop crop); List<CropRule> findByCrop(Crop crop);
@Modifying
@Query("delete from CropRule cr where cr.crop = ?1")
void deleteByCrop(Crop crop);
} }
...@@ -42,7 +42,7 @@ public interface CropService { ...@@ -42,7 +42,7 @@ public interface CropService {
/** /**
* Add a {@link Crop} to the system * Add a {@link Crop} to the system
* *
* @param shortName * @param shortName
* @param name * @param name
* @param description * @param description
...@@ -53,7 +53,7 @@ public interface CropService { ...@@ -53,7 +53,7 @@ public interface CropService {
/** /**
* Updates a crop record * Updates a crop record
* *
* @param shortName * @param shortName
* @param name * @param name
* @param description * @param description
...@@ -66,4 +66,12 @@ public interface CropService { ...@@ -66,4 +66,12 @@ public interface CropService {
List<CropRule> getCropRules(Crop crop); 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 { ...@@ -280,6 +280,18 @@ public class CropServiceImpl implements CropService {
return rule; 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 @Override
public List<CropRule> getCropRules(Crop crop) { public List<CropRule> getCropRules(Crop crop) {
return crop == null ? null : cropRuleRepository.findByCrop(crop); return crop == null ? null : cropRuleRepository.findByCrop(crop);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.genesys2.server.servlet.controller.rest; package org.genesys2.server.servlet.controller.rest;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.ValidationException; import javax.xml.bind.ValidationException;
...@@ -24,6 +25,7 @@ import net.sf.oval.ConstraintViolation; ...@@ -24,6 +25,7 @@ import net.sf.oval.ConstraintViolation;
import net.sf.oval.Validator; import net.sf.oval.Validator;
import net.sf.oval.constraint.NotBlank; import net.sf.oval.constraint.NotBlank;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.exception.AuthorizationException; import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.genesys.Parameter; import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.impl.Crop; import org.genesys2.server.model.impl.Crop;
...@@ -32,6 +34,7 @@ import org.genesys2.server.model.impl.CropTaxonomy; ...@@ -32,6 +34,7 @@ import org.genesys2.server.model.impl.CropTaxonomy;
import org.genesys2.server.service.CropService; import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService; import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.TraitService; import org.genesys2.server.service.TraitService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
...@@ -61,7 +64,7 @@ public class CropsController extends RestController { ...@@ -61,7 +64,7 @@ public class CropsController extends RestController {
/** /**
* List all crops * List all crops
* *
* @return * @return
* @throws AuthorizationException * @throws AuthorizationException
*/ */
...@@ -75,7 +78,7 @@ public class CropsController extends RestController { ...@@ -75,7 +78,7 @@ public class CropsController extends RestController {
/** /**
* Add a crop * Add a crop
* *
* @return * @return
* @throws ValidationException * @throws ValidationException
*/ */
...@@ -98,19 +101,21 @@ public class CropsController extends RestController { ...@@ -98,19 +101,21 @@ public class CropsController extends RestController {
crop = cropService.updateCrop(crop, cropJson.name, cropJson.description, cropJson.i18n); crop = cropService.updateCrop(crop, cropJson.name, cropJson.description, cropJson.i18n);
} }
for (final CropTaxonomyJson ctj : cropJson.taxonomies) { // TODO Remove
final CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included); if (cropJson.rules != null)
if (cropRule != null) { for (final CropRuleJson ctj : cropJson.rules) {
LOG.info("Added rule: " + cropRule); final CropRule cropRule = cropService.addCropRule(crop, ctj.genus, ctj.species, ctj.included);
if (cropRule != null) {
LOG.info("Added rule: " + cropRule);
}
} }
}
return crop; return crop;
} }
/** /**
* Get crop details /crops/{shortName} * Get crop details /crops/{shortName}
* *
* @return * @return
* @throws AuthorizationException * @throws AuthorizationException
*/ */
...@@ -123,7 +128,7 @@ public class CropsController extends RestController { ...@@ -123,7 +128,7 @@ public class CropsController extends RestController {
/** /**
* Get crop descriptors /crops/{shortName}/descriptors * Get crop descriptors /crops/{shortName}/descriptors
* *
* @return * @return
* @throws AuthorizationException * @throws AuthorizationException
*/ */
...@@ -138,7 +143,7 @@ public class CropsController extends RestController { ...@@ -138,7 +143,7 @@ public class CropsController extends RestController {
/** /**
* Get crop taxonomy rules /crops/{shortName}/rules * Get crop taxonomy rules /crops/{shortName}/rules
* *
* @return * @return
* @throws AuthorizationException * @throws AuthorizationException
*/ */
...@@ -151,9 +156,38 @@ public class CropsController extends RestController { ...@@ -151,9 +156,38 @@ public class CropsController extends RestController {
return OAuth2Cleanup.clean(cropRules); 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 * Get crop taxonomies /crops/{shortName}/taxa
* *
* @return * @return
* @throws AuthorizationException * @throws AuthorizationException
*/ */
...@@ -172,13 +206,13 @@ public class CropsController extends RestController { ...@@ -172,13 +206,13 @@ public class CropsController extends RestController {
public String name; public String name;
public String description; public String description;
public String i18n; public String i18n;
public List<CropTaxonomyJson> taxonomies; public List<CropRuleJson> rules;
} }
public static class CropTaxonomyJson { public static class CropRuleJson {
public boolean included;
@NotBlank @NotBlank
public String genus; public String genus;
public String species; public String species;
public boolean included;
} }
} }
...@@ -51,6 +51,7 @@ public class OAuth2Cleanup { ...@@ -51,6 +51,7 @@ public class OAuth2Cleanup {
if (t instanceof CropRule) { if (t instanceof CropRule) {
final CropRule cropRule = (CropRule) t; final CropRule cropRule = (CropRule) t;
cropRule.setCrop(null); cropRule.setCrop(null);
cropRule.setId(null);
} }
if (t instanceof CropTaxonomy) { if (t instanceof CropTaxonomy) {
final CropTaxonomy cropTaxa = (CropTaxonomy) t; final CropTaxonomy cropTaxa = (CropTaxonomy) t;
......
...@@ -33,7 +33,7 @@ public class CropRuleSerializer extends JsonSerializer<CropRule> { ...@@ -33,7 +33,7 @@ public class CropRuleSerializer extends JsonSerializer<CropRule> {
jgen.writeNull(); jgen.writeNull();
} else { } else {
jgen.writeStartObject(); jgen.writeStartObject();
jgen.writeObjectField("id", cropRule.getId()); jgen.writeBooleanField("included", cropRule.isIncluded());
jgen.writeObjectField("genus", cropRule.getGenus()); jgen.writeObjectField("genus", cropRule.getGenus());
if (cropRule.getSpecies() != null) { if (cropRule.getSpecies() != null) {
jgen.writeObjectField("species", cropRule.getSpecies()); jgen.writeObjectField("species", cropRule.getSpecies());
......
...@@ -37,6 +37,7 @@ public class CropSerializer extends JsonSerializer<Crop> { ...@@ -37,6 +37,7 @@ public class CropSerializer extends JsonSerializer<Crop> {
} else { } else {
jgen.writeStartObject(); jgen.writeStartObject();
jgen.writeObjectField("shortName", crop.getShortName()); jgen.writeObjectField("shortName", crop.getShortName());
jgen.writeObjectField("name", crop.getName());
if (StringUtils.isNotBlank(crop.getDescription())) { if (StringUtils.isNotBlank(crop.getDescription())) {
jgen.writeObjectField("description", 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