Commit 86a87b27 authored by Artem Hrybeniuk's avatar Artem Hrybeniuk
Browse files

Merge branch '635-networks-with-accession-filter' into 'main'

Resolve "Networks with accession filter"

Closes #635

See merge request genesys-pgr/genesys-server!695
parents e75cd782 cf9c063a
......@@ -95,7 +95,7 @@ public class OrganizationController extends ApiBaseController {
if (network == null) {
network = networkService.create(organizationJson.getSlug(), organizationJson.getTitle());
}
return networkService.update(network.getId(), network.getSlug(), organizationJson.getTitle());
return networkService.update(network.getId(), network.getSlug(), organizationJson.getTitle(), organizationJson.getAccessionFilter());
}
/**
......
......@@ -91,7 +91,7 @@ public class NetworkController extends ApiBaseController {
if (!network.getId().equals(organizationJson.getId())) {
throw new InvalidApiUsageException("Cannot overwrite existing record");
}
return networkService.update(network.getId(), network.getSlug(), organizationJson.getTitle());
return networkService.update(network.getId(), network.getSlug(), organizationJson.getTitle(), organizationJson.getAccessionFilter());
}
/**
......
......@@ -57,6 +57,9 @@ public class PGRFANetwork extends AuditedVersionedModel {
@OrderBy("code")
private List<FaoInstitute> members = new ArrayList<FaoInstitute>();
@Column(name = "accessionFilter")
private String accessionFilter;
public String getSlug() {
return slug;
}
......@@ -80,4 +83,12 @@ public class PGRFANetwork extends AuditedVersionedModel {
public void setMembers(List<FaoInstitute> members) {
this.members = members;
}
public String getAccessionFilter() {
return accessionFilter;
}
public void setAccessionFilter(String accessionFilter) {
this.accessionFilter = accessionFilter;
}
}
......@@ -40,7 +40,7 @@ public interface PGRFANetworkService {
Article updateAbout(@Valid PGRFANetwork network, String body, String summary, Locale locale) throws CRMException;
PGRFANetwork update(long id, String newSlug, String title);
PGRFANetwork update(long id, String newSlug, String title, String accessionFilter);
PGRFANetwork update(@Valid PGRFANetwork network);
......
......@@ -22,6 +22,8 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -607,4 +609,12 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
public Map<String, String> remappedProperties() {
return REMAPPED_PROPERTIES;
}
public static AccessionFilter fromJson(String json) throws JsonProcessingException {
if (StringUtils.isBlank(json)) {
return new AccessionFilter();
}
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, AccessionFilter.class);
}
}
......@@ -23,6 +23,9 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
......@@ -112,11 +115,12 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
public PGRFANetwork update(long id, String newSlug, String title) {
public PGRFANetwork update(long id, String newSlug, String title, String accessionFilter) {
final PGRFANetwork network = new PGRFANetwork();
network.setId(id);
network.setSlug(newSlug);
network.setTitle(title);
network.setAccessionFilter(accessionFilter);
return update(network);
}
......@@ -131,6 +135,7 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService {
toUpdate.setSlug(network.getSlug());
toUpdate.setTitle(network.getTitle());
toUpdate.setAccessionFilter(network.getAccessionFilter());
return lazyLoad(networkRepository.save(toUpdate));
}
......@@ -160,11 +165,22 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService {
@Override
public Map<String, ElasticsearchService.TermResult> overview(String slug) throws SearchException {
AccessionFilter filterWithNetwork = new AccessionFilter();
filterWithNetwork.networks = Sets.newHashSet(slug);
var network = networkRepository.findBySlug(slug);
var accessionFilter = new AccessionFilter();
if (StringUtils.isNotBlank(network.getAccessionFilter())) {
try {
accessionFilter = AccessionFilter.fromJson(network.getAccessionFilter());
} catch (JsonProcessingException e) {
LOG.error("Exception in parsing JSON filter: {}", network.getAccessionFilter()); //, e);
}
}
accessionFilter.historic = false; // Force active accessions
accessionFilter.networks = Sets.newHashSet(slug);
if (elasticsearchService != null) {
return elasticsearchService.termStatisticsAuto(Accession.class, filterWithNetwork, 10, terms.toArray(new String[] {}));
return elasticsearchService.termStatisticsAuto(Accession.class, accessionFilter, 10, terms.toArray(new String[] {}));
}
return Map.of();
}
......
......@@ -7734,4 +7734,15 @@ databaseChangeLog:
sql: update descriptorlist set firstPublishedDate = lastModifiedDate where state = 1
- sql:
comment: Sets firstPublishedDate = lastModifiedDate to descriptors
sql: update descriptor set firstPublishedDate = lastModifiedDate where state = 1
\ No newline at end of file
sql: update descriptor set firstPublishedDate = lastModifiedDate where state = 1
- changeSet:
id: 1648043998325-1
author: ahrybeniuk
changes:
- addColumn:
columns:
- column:
name: accessionFilter
type: varchar(500)
tableName: organization
\ No newline at end of file
......@@ -136,6 +136,23 @@ public class NetworkControllerTest extends AbstractNetworkControllerTest {
/*@formatter:on*/
assertEquals(2, organizationRepository.findAll().size());
organization1.setAccessionFilter("{\"genus\": [ \"Cocos\" ]}");
/*@formatter:off*/
mockMvc
.perform(post(NetworkController.CONTROLLER_URL)
.content(verboseMapper.writeValueAsString(organization1))
.contentType(MediaType.APPLICATION_JSON_VALUE)
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$.slug", is(SLUG_1)))
.andExpect(jsonPath("$.title", is(newTitle)))
.andExpect(jsonPath("$.accessionFilter", is("{\"genus\": [ \"Cocos\" ]}")))
;
/*@formatter:on*/
}
@Test
......
......@@ -68,11 +68,15 @@ public class PGRFANetworkServiceTest extends AbstractServicesTest {
PGRFANetwork toUpdate = setupOrganization(SLUG_1, TITLE_1);
final String newTitle = "NEW_TITLE";
networkService.update(toUpdate.getId(), SLUG_1, newTitle);
networkService.update(toUpdate.getId(), SLUG_1, newTitle, null);
PGRFANetwork updated = networkService.getNetwork(SLUG_1);
assertEquals(updated.getId(), toUpdate.getId());
assertEquals(updated.getTitle(), newTitle);
updated.setAccessionFilter("{}");
var updated2 = networkService.update(updated.getId(), SLUG_1, newTitle + 2, updated.getAccessionFilter());
assertEquals(updated2.getAccessionFilter(), updated.getAccessionFilter());
}
@Test(expected = ConstraintViolationException.class)
......
......@@ -5,7 +5,9 @@ import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import java.io.IOException;
import java.util.Set;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Sets;
import org.genesys2.server.component.elastic.ElasticQueryBuilder;
import org.genesys2.server.service.filter.AccessionFilter;
......@@ -110,4 +112,19 @@ public class AccessionFilterTest {
assertThat("historic value should stay null", afCopy2.historic, nullValue());
assertThat(afCopy2.crop, hasItems("apple"));
}
@Test
public void testAccessionFilterFromJson() throws JsonProcessingException {
String json = "{ \"crop\": [ \"banana\" ] }";
AccessionFilter af = AccessionFilter.fromJson(json);
assertThat(af.historic, is(false));
assertThat(af.crop, hasItem("banana"));
json = "{\"taxonomy\":{\"genus\":[\"Musa\"]}}";
af = AccessionFilter.fromJson(json);
assertThat(af.taxonomy, is(notNullValue()));
assertThat(af.taxonomy.genus, hasItem("Musa"));
}
}
Supports Markdown
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