Commit cf9c063a authored by Matija Obreza's avatar Matija Obreza
Browse files

Networks: Update PGRFA network with AccessionFilter

parent 211adb58
...@@ -95,7 +95,7 @@ public class OrganizationController extends ApiBaseController { ...@@ -95,7 +95,7 @@ public class OrganizationController extends ApiBaseController {
if (network == null) { if (network == null) {
network = networkService.create(organizationJson.getSlug(), organizationJson.getTitle()); 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 { ...@@ -91,7 +91,7 @@ public class NetworkController extends ApiBaseController {
if (!network.getId().equals(organizationJson.getId())) { if (!network.getId().equals(organizationJson.getId())) {
throw new InvalidApiUsageException("Cannot overwrite existing record"); 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());
} }
/** /**
......
...@@ -40,7 +40,7 @@ public interface PGRFANetworkService { ...@@ -40,7 +40,7 @@ public interface PGRFANetworkService {
Article updateAbout(@Valid PGRFANetwork network, String body, String summary, Locale locale) throws CRMException; 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); PGRFANetwork update(@Valid PGRFANetwork network);
......
...@@ -24,6 +24,8 @@ import java.util.Map; ...@@ -24,6 +24,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.exception.InvalidApiUsageException; import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement; import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
...@@ -113,11 +115,12 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService { ...@@ -113,11 +115,12 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService {
@Override @Override
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false) @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(); final PGRFANetwork network = new PGRFANetwork();
network.setId(id); network.setId(id);
network.setSlug(newSlug); network.setSlug(newSlug);
network.setTitle(title); network.setTitle(title);
network.setAccessionFilter(accessionFilter);
return update(network); return update(network);
} }
...@@ -132,6 +135,7 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService { ...@@ -132,6 +135,7 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService {
toUpdate.setSlug(network.getSlug()); toUpdate.setSlug(network.getSlug());
toUpdate.setTitle(network.getTitle()); toUpdate.setTitle(network.getTitle());
toUpdate.setAccessionFilter(network.getAccessionFilter());
return lazyLoad(networkRepository.save(toUpdate)); return lazyLoad(networkRepository.save(toUpdate));
} }
...@@ -163,12 +167,16 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService { ...@@ -163,12 +167,16 @@ public class PGRFANetworkServiceImpl implements PGRFANetworkService {
public Map<String, ElasticsearchService.TermResult> overview(String slug) throws SearchException { public Map<String, ElasticsearchService.TermResult> overview(String slug) throws SearchException {
var network = networkRepository.findBySlug(slug); var network = networkRepository.findBySlug(slug);
var accessionFilter = new AccessionFilter(); var accessionFilter = new AccessionFilter();
try {
accessionFilter = AccessionFilter.fromJson(network.getAccessionFilter()); if (StringUtils.isNotBlank(network.getAccessionFilter())) {
} catch (JsonProcessingException e) { try {
LOG.error("Exception in parsing json filter: {}", network.getAccessionFilter(), e); 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); accessionFilter.networks = Sets.newHashSet(slug);
if (elasticsearchService != null) { if (elasticsearchService != null) {
......
...@@ -136,6 +136,23 @@ public class NetworkControllerTest extends AbstractNetworkControllerTest { ...@@ -136,6 +136,23 @@ public class NetworkControllerTest extends AbstractNetworkControllerTest {
/*@formatter:on*/ /*@formatter:on*/
assertEquals(2, organizationRepository.findAll().size()); 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 @Test
......
...@@ -68,11 +68,15 @@ public class PGRFANetworkServiceTest extends AbstractServicesTest { ...@@ -68,11 +68,15 @@ public class PGRFANetworkServiceTest extends AbstractServicesTest {
PGRFANetwork toUpdate = setupOrganization(SLUG_1, TITLE_1); PGRFANetwork toUpdate = setupOrganization(SLUG_1, TITLE_1);
final String newTitle = "NEW_TITLE"; 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); PGRFANetwork updated = networkService.getNetwork(SLUG_1);
assertEquals(updated.getId(), toUpdate.getId()); assertEquals(updated.getId(), toUpdate.getId());
assertEquals(updated.getTitle(), newTitle); 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) @Test(expected = ConstraintViolationException.class)
......
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