Commit c6d005a8 authored by Matija Obreza's avatar Matija Obreza Committed by Maxym Borodenko
Browse files

Drop CropRule

parent 4fd19bc1
......@@ -21,9 +21,7 @@ import java.util.List;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.ModelValidationException;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -119,39 +117,4 @@ public class CropsController extends ApiBaseController {
LOG.info("Getting crop {}", shortName);
return cropService.delete(cropService.getCrop(shortName));
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
* @return
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/rules", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CropRule> getCropRules(@PathVariable("shortName") String shortName) throws AuthorizationException {
LOG.info("Getting crop rules {}", shortName);
final Crop crop = cropService.getCrop(shortName);
final List<CropRule> cropRules = cropService.getCropRules(crop);
return cropRules;
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
*
* @return
*
* @return
* @throws AuthorizationException
*/
@RequestMapping(value = "/{shortName}/rules", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody List<CropRule> updateCropRules(@PathVariable("shortName") String shortName, @RequestBody List<CropRule> rules) throws AuthorizationException {
LOG.info("Updating crop rules for {}", shortName);
final Crop crop = cropService.getCrop(shortName);
if (crop == null)
throw new NotFoundElement("No crop " + shortName);
cropService.setCropRules(crop, rules);
return cropService.getCropRules(crop);
}
}
......@@ -28,7 +28,6 @@ import org.genesys2.server.api.ModelValidationException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.CropService.CropDetails;
......@@ -204,37 +203,6 @@ public class CropsController extends ApiBaseController {
return cropService.delete(cropService.getCrop(shortName));
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
*
* @return
*/
@RequestMapping(value = "/{shortName}/rules", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CropRule> getCropRules(@PathVariable("shortName") String shortName) {
LOG.info("Getting crop rules {}", shortName);
final Crop crop = cropService.getCrop(shortName);
final List<CropRule> cropRules = cropService.getCropRules(crop);
return cropRules;
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
*
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/{shortName}/rules", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CropRule> updateCropRules(@PathVariable("shortName") String shortName, @RequestBody List<CropRule> rules) {
LOG.info("Updating crop rules for {}", shortName);
final Crop crop = cropService.getCrop(shortName);
if (crop == null)
throw new NotFoundElement("No crop " + shortName);
cropService.setCropRules(crop, rules);
return cropService.getCropRules(crop);
}
/**
* Link accession#cropName with crop
*
......
......@@ -29,7 +29,6 @@ import org.genesys2.server.exception.DetailedConstraintViolationException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.impl.SearchException;
......@@ -171,35 +170,6 @@ public class CropsController extends ApiBaseController {
}).collect(Collectors.toList());
}
/**
* Get crop taxonomy rules /crops/{shortName}/rules
*
* @return
*/
@RequestMapping(value = "/{shortName}/rules", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CropRule> getCropRules(@PathVariable("shortName") String shortName) {
LOG.info("Getting crop rules {}", shortName);
final Crop crop = cropService.getCrop(shortName);
return cropService.getCropRules(crop);
}
/**
* Update crop taxonomy rules /crops/{shortName}/rules
*
* @return operation response
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "/{shortName}/rules", method = { RequestMethod.PUT, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CropRule> updateCropRules(@PathVariable("shortName") String shortName, @RequestBody List<CropRule> rules) {
LOG.info("Updating crop rules for {}", shortName);
final Crop crop = cropService.getCrop(shortName);
if (crop == null) {
throw new NotFoundElement("Crop not found by shortName=" + shortName);
}
cropService.setCropRules(crop, rules);
return cropService.getCropRules(crop);
}
/**
* Link accession#cropName with crop
*
......
......@@ -25,7 +25,6 @@ import java.util.Locale;
import java.util.Map;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
......@@ -33,7 +32,6 @@ import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
......@@ -108,13 +106,6 @@ public class Crop extends GlobalVersionedAuditedModel implements SelfCleaning, A
@IgnoreField
private String i18n;
/**
* Rules
*/
@JsonIgnore
@OneToMany(mappedBy = "crop", cascade = { CascadeType.REMOVE }, orphanRemoval = true)
private List<CropRule> cropRules;
@Transient
@JsonIgnore
private transient JsonNode i18nJ;
......@@ -160,14 +151,6 @@ public class Crop extends GlobalVersionedAuditedModel implements SelfCleaning, A
this.description = description;
}
public List<CropRule> getCropRules() {
return cropRules;
}
public void setCropRules(List<CropRule> cropRules) {
this.cropRules = cropRules;
}
public String getI18n() {
return i18n;
}
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.model.impl;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.genesys.blocks.model.BasicModel;
/**
* Crop rules establish which taxonomies are part of a {@link Crop}.
*
* @author mobreza
*/
@Entity
@Table(name = "croprule", uniqueConstraints = { @UniqueConstraint(name = "UK_croprule", columnNames = { "cropId", "genus", "species", "subtaxa" }) })
public class CropRule extends BasicModel {
private static final long serialVersionUID = -2336100072991067193L;
@Column
private boolean included;
@Column(nullable = false, length = 100)
private String genus;
@Column(nullable = true, length = 100)
private String species;
@Column(nullable = true, length = 100)
private String subtaxa;
@JsonIgnore
@ManyToOne(cascade = {}, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "cropId")
private Crop crop;
public boolean isIncluded() {
return included;
}
public void setIncluded(boolean included) {
this.included = included;
}
public String getGenus() {
return genus;
}
public void setGenus(String genus) {
this.genus = genus;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getSubtaxa() {
return subtaxa;
}
public void setSubtaxa(String subtaxa) {
this.subtaxa = subtaxa;
}
public Crop getCrop() {
return crop;
}
public void setCrop(Crop crop) {
this.crop = crop;
}
@Override
public String toString() {
return "CropRule included=" + included + " genus=" + genus + " species=" + species + " crop=" + crop.getShortName();
}
public boolean matches(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
if (this.genus != null && !this.genus.equalsIgnoreCase(genus))
return false;
if (this.species != null && !this.species.equalsIgnoreCase(species))
return false;
if (this.subtaxa != null && !this.subtaxa.equalsIgnoreCase(subtaxa))
return false;
return true;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.persistence;
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);
}
......@@ -34,7 +34,6 @@ import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.model.impl.DiversityTree;
import org.genesys2.server.model.impl.Subset;
import org.springframework.data.domain.Page;
......@@ -85,18 +84,6 @@ public interface CropService {
Crop updateCrop(@Valid Crop crop);
CropRule addCropRule(Crop crop, String genus, String species, boolean included);
List<CropRule> getCropRules(Crop crop);
/**
* Replace crop taxonomic rules
*
* @param crop
* @param cropRules
*/
void setCropRules(Crop crop, List<CropRule> cropRules);
/**
* Remove crop
*
......
......@@ -46,12 +46,10 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.model.impl.DiversityTree;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.CropRepository;
import org.genesys2.server.persistence.CropRuleRepository;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
......@@ -107,9 +105,6 @@ public class CropServiceImpl implements CropService {
@Autowired
CropRepository cropRepository;
@Autowired
CropRuleRepository cropRuleRepository;
@Autowired
private HtmlSanitizer htmlSanitizer;
......@@ -433,40 +428,6 @@ public class CropServiceImpl implements CropService {
return cropRepository.save(crop);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#crop, 'ADMINISTRATION')")
@Transactional(readOnly = false)
public CropRule addCropRule(Crop crop, String genus, String species, boolean included) {
if (crop == null || StringUtils.isBlank(genus)) {
LOG.debug("Ignoring blank rule");
return null;
}
final CropRule rule = new CropRule();
rule.setCrop(crop);
rule.setGenus(genus);
rule.setSpecies(StringUtils.defaultIfBlank(species, null));
rule.setIncluded(included);
cropRuleRepository.save(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.saveAll(cropRules);
}
@Override
public List<CropRule> getCropRules(Crop crop) {
return crop == null ? null : cropRuleRepository.findByCrop(crop);
}
@Override
public void unlinkAccessionsForCrop(Crop crop) {
final BooleanBuilder predicate = new BooleanBuilder();
......@@ -550,8 +511,5 @@ public class CropServiceImpl implements CropService {
if (source.getOtherNames() != null) {
target.setOtherNames(source.getOtherNames().stream().distinct().map(otherName -> StringUtils.trim(otherName)).filter(otherName -> StringUtils.isNotBlank(otherName)).sorted().collect(Collectors.toList()));
}
if (source.getCropRules() != null) {
target.getCropRules().addAll(source.getCropRules());
}
}
}
......@@ -7312,3 +7312,14 @@ databaseChangeLog:
- dropTable:
cascadeConstraints: true
tableName: croptaxonomy
- changeSet:
id: 1598429119000-1
author: mobreza
comment: Drop CropRule
changes:
- dropTable:
cascadeConstraints: true
tableName: croprule
......@@ -15,18 +15,18 @@
*/
package org.genesys.test.server.api;
import java.util.Locale;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.ArticleRepository;
import org.genesys2.server.persistence.CropRepository;
import org.genesys2.server.persistence.CropRuleRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.service.CRMException;
......@@ -38,8 +38,6 @@ import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.Locale;
/**
* @author Maxym Borodenko
*/
......@@ -48,8 +46,6 @@ public abstract class AbstractCropsControllerTest extends AbstractApiTest {
@Autowired
protected CropRepository cropRepository;
@Autowired
protected CropRuleRepository cropRuleRepository;
@Autowired
protected CropService cropService;
@Autowired
protected AccessionRepository accessionRepository;
......@@ -65,7 +61,6 @@ public abstract class AbstractCropsControllerTest extends AbstractApiTest {
protected ContentService contentService;
protected Crop crop;
protected CropRule cropRule;
@Transactional
@Before
......@@ -74,8 +69,6 @@ public abstract class AbstractCropsControllerTest extends AbstractApiTest {
super.beforeTest();
crop = cropService.addCrop("maize", "Maize", "Crop description in EN", null);
cropRule = cropService.addCropRule(crop, "Zea", "mays", true);
crop = cropRule.getCrop();
}
@Transactional
......@@ -87,7 +80,6 @@ public abstract class AbstractCropsControllerTest extends AbstractApiTest {
articleRepository.deleteAll();
taxonomyRepository.deleteAll();
instituteRepository.deleteAll();
cropRuleRepository.deleteAll();
cropRepository.deleteAll();
super.cleanup();
}
......
......@@ -22,19 +22,13 @@ import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.ArrayList;
import java.util.List;
import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v0.CropsController;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropRule;
import org.genesys2.server.persistence.CropRepository;
import org.genesys2.server.persistence.CropRuleRepository;
import org.genesys2.server.service.CropService;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
......@@ -50,7 +44,6 @@ import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Transactional(transactionManager = "transactionManager")
......@@ -70,12 +63,9 @@ public class CropsControllerTest extends AbstractApiTest {
@Autowired
private CropRepository cropRepository;
@Autowired
private CropRuleRepository cropRuleRepository;
@Autowired
private CropService cropService;
private Crop crop;
private CropRule cropRule;
static {
......@@ -96,15 +86,12 @@ public class CropsControllerTest extends AbstractApiTest {
.withPort(443)).build();
crop = cropService.addCrop("maize", "Maize", "Crop description in EN", null);
cropRule = cropService.addCropRule(crop, "Zea", "mays", true);
crop = cropRule.getCrop();
}
@Transactional
@After
@Override
public void cleanup() throws Exception {
cropRuleRepository.deleteAll();
cropRepository.deleteAll();
super.cleanup();
}
......@@ -239,124 +226,4 @@ public class CropsControllerTest extends AbstractApiTest {
)));
/*@formatter:on*/
}
@Test
public void getCropRulesTest() throws Exception {
LOG.info("Start test-method getCropRulesTest");
/*@formatter:off*/
mockMvc.perform(get("/api/v0/crops/{shortName}/rules", crop.getShortName())
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].id", is(notNullValue())))
.andExpect(jsonPath("$[0].included", is(true)))
.andExpect(jsonPath("$[0].genus", is("Zea")))
.andExpect(jsonPath("$[0].species", is("mays")))
.andExpect(jsonPath("$[0].subtaxa", is(nullValue())))
.andDo(document("crop-rules-list", pathParameters(
parameterWithName("shortName").description("Crop short name or code (e.g. maize)")
)));