Skip to content

Commit

Permalink
can't update or delete IH descriptor groups
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Aug 14, 2024
1 parent fc7f2bc commit a957a24
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
import java.util.*;
import lombok.SneakyThrows;
import org.gbif.api.model.collections.Collection;
import org.gbif.api.model.collections.MasterSourceMetadata;
import org.gbif.api.model.collections.descriptors.Descriptor;
import org.gbif.api.model.collections.descriptors.DescriptorGroup;
import org.gbif.api.model.collections.request.DescriptorGroupSearchRequest;
import org.gbif.api.model.collections.request.DescriptorSearchRequest;
import org.gbif.api.model.common.export.ExportFormat;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.MachineTag;
import org.gbif.api.service.collections.CollectionService;
import org.gbif.api.service.collections.DescriptorsService;
import org.gbif.api.util.GrSciCollUtils;
import org.gbif.api.vocabulary.collections.Source;
import org.gbif.registry.database.TestCaseDatabaseInitializer;
import org.gbif.registry.test.mocks.NubResourceClientMock;
import org.gbif.registry.ws.it.collections.service.BaseServiceIT;
Expand Down Expand Up @@ -142,4 +147,62 @@ public void descriptorsTest() {
collection.getKey(), DescriptorGroupSearchRequest.builder().build())
.getCount());
}

@Test
@SneakyThrows
public void ihDescriptorsTest() {
Collection collection = new Collection();
collection.setCode("c1");
collection.setName("n1");
collectionService.create(collection);

collectionService.addMasterSourceMetadata(
collection.getKey(), new MasterSourceMetadata(Source.IH_IRN, "dsfgds"));

String title = "My descriptor set";
String description = "description";

Resource descriptorsFile = new ClassPathResource("collections/descriptors.csv");
long descriptorGroupKey =
descriptorsService.createDescriptorGroup(
StreamUtils.copyToByteArray(descriptorsFile.getInputStream()),
ExportFormat.TSV,
title,
description,
collection.getKey());
assertTrue(descriptorGroupKey > 0);

assertEquals(
1,
descriptorsService
.listDescriptorGroups(
collection.getKey(), DescriptorGroupSearchRequest.builder().build())
.getResults()
.size());

PagingResponse<Descriptor> descriptors =
descriptorsService.listDescriptors(DescriptorSearchRequest.builder().build());
assertEquals(5, descriptors.getResults().size());

collectionService.addMachineTag(
collection.getKey(),
new MachineTag(
GrSciCollUtils.IH_NS,
GrSciCollUtils.COLL_SUMMARY_MT,
String.valueOf(descriptorGroupKey)));

descriptorsService.updateDescriptorGroup(
descriptorGroupKey,
StreamUtils.copyToByteArray(descriptorsFile.getInputStream()),
ExportFormat.TSV,
"foo",
"foo");

DescriptorGroup updated = descriptorsService.getDescriptorGroup(descriptorGroupKey);
assertEquals(title, updated.getTitle());
assertEquals(description, updated.getDescription());

descriptorsService.deleteDescriptorGroup(descriptorGroupKey);
assertNull(descriptorsService.getDescriptorGroup(descriptorGroupKey).getDeleted());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gbif.registry.service.collections.descriptors;

import static org.gbif.api.util.GrSciCollUtils.*;
import static org.gbif.registry.service.collections.utils.ParamUtils.parseIntegerRangeParameter;

import com.google.common.base.CharMatcher;
Expand Down Expand Up @@ -28,8 +29,10 @@
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.model.common.paging.PagingRequest;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.service.collections.CollectionService;
import org.gbif.api.service.collections.DescriptorsService;
import org.gbif.api.vocabulary.Country;
import org.gbif.api.vocabulary.collections.MasterSourceType;
import org.gbif.checklistbank.ws.client.NubResourceClient;
import org.gbif.dwc.terms.DwcTerm;
import org.gbif.registry.events.EventManager;
Expand All @@ -55,15 +58,18 @@ public class DefaultDescriptorService implements DescriptorsService {
private final NubResourceClient nubResourceClient;
private final DescriptorsMapper descriptorsMapper;
private final EventManager eventManager;
private final CollectionService collectionService;

@Autowired
public DefaultDescriptorService(
NubResourceClient nubResourceClient,
DescriptorsMapper descriptorsMapper,
EventManager eventManager) {
EventManager eventManager,
CollectionService collectionService) {
this.nubResourceClient = nubResourceClient;
this.descriptorsMapper = descriptorsMapper;
this.eventManager = eventManager;
this.collectionService = collectionService;
}

@SneakyThrows
Expand Down Expand Up @@ -222,6 +228,12 @@ public void deleteDescriptorGroup(@NotNull long key) {
DescriptorGroup descriptorGroup = descriptorsMapper.getDescriptorGroup(key);
Preconditions.checkArgument(
descriptorGroup != null, "Descriptor group not found for key " + key);

if (isIHDescriptorGroup(key, descriptorGroup.getCollectionKey())) {
// can't delete a descriptor group that comes from IH
return;
}

descriptorsMapper.deleteDescriptorGroup(key);

eventManager.post(
Expand Down Expand Up @@ -255,6 +267,12 @@ public void updateDescriptorGroup(
final String username = authentication.getName();

DescriptorGroup descriptorGroup = descriptorsMapper.getDescriptorGroup(descriptorGroupKey);

if (isIHDescriptorGroup(descriptorGroupKey, descriptorGroup.getCollectionKey())) {
// can't update a descriptor group that comes from IH
return;
}

descriptorGroup.setTitle(title);
descriptorGroup.setDescription(description);
descriptorGroup.setModifiedBy(username);
Expand All @@ -267,12 +285,29 @@ public void updateDescriptorGroup(
importDescriptorsFile(descriptorGroupFile, format, descriptorGroup.getKey());

eventManager.post(
SubEntityCollectionEvent.newInstance(
descriptorGroup.getCollectionKey(),
Collection.class,
DescriptorGroup.class,
descriptorGroupKey,
EventType.UPDATE));
SubEntityCollectionEvent.newInstance(
descriptorGroup.getCollectionKey(),
Collection.class,
DescriptorGroup.class,
descriptorGroupKey,
EventType.UPDATE));
}

private boolean isIHDescriptorGroup(long descriptorGroupKey, UUID collectionKey) {
Collection collection = collectionService.get(collectionKey);
List<Long> ihDescriptorGroups =
collection.getMachineTags().stream()
.filter(
mt ->
mt.getNamespace().equals(IH_NS)
&& (mt.getName().equals(COLL_SUMMARY_MT)
|| mt.getName().equals(COLLECTORS_MT))
&& mt.getValue() != null)
.map(mt -> Long.parseLong(mt.getValue()))
.collect(Collectors.toList());

return collection.getMasterSource().equals(MasterSourceType.IH)
&& ihDescriptorGroups.contains(descriptorGroupKey);
}

@Override
Expand Down

0 comments on commit a957a24

Please sign in to comment.