diff --git a/geonetwork-core/src/main/java/au/org/aodn/geonetwork4/controller/Api.java b/geonetwork-core/src/main/java/au/org/aodn/geonetwork4/controller/Api.java index ead8e0a..0180444 100644 --- a/geonetwork-core/src/main/java/au/org/aodn/geonetwork4/controller/Api.java +++ b/geonetwork-core/src/main/java/au/org/aodn/geonetwork4/controller/Api.java @@ -11,13 +11,18 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.fao.geonet.domain.Group; import org.fao.geonet.domain.Metadata; import org.fao.geonet.kernel.harvest.HarvestManagerImpl; +import org.fao.geonet.kernel.harvest.harvester.AbstractHarvester; +import org.fao.geonet.kernel.harvest.harvester.AbstractParams; +import org.fao.geonet.kernel.harvest.harvester.HarvestResult; import org.fao.geonet.kernel.harvest.harvester.csw.CswHarvester; import org.fao.geonet.kernel.harvest.harvester.geonet.GeonetHarvester; import org.fao.geonet.kernel.harvest.harvester.geonet20.Geonet20Harvester; import org.fao.geonet.kernel.harvest.harvester.oaipmh.OaiPmhHarvester; import org.fao.geonet.kernel.harvest.harvester.ogcwxs.OgcWxSHarvester; +import org.fao.geonet.repository.GroupRepository; import org.fao.geonet.repository.MetadataRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -47,16 +52,22 @@ public class Api { protected Setup setup; protected MetadataRepository repository; protected HarvestManagerImpl harvestManager; + protected GroupRepository groupRepository; protected ObjectMapper objectMapper; @Autowired @Qualifier("remoteSources") protected Map remoteConfigMap; - public Api(Setup setup, MetadataRepository metadataRepository, HarvestManagerImpl harvestManager, ObjectMapper objectMapper) { + public Api(Setup setup, + MetadataRepository metadataRepository, + HarvestManagerImpl harvestManager, + GroupRepository groupRepository, + ObjectMapper objectMapper) { this.harvestManager = harvestManager; this.repository = metadataRepository; this.setup = setup; + this.groupRepository = groupRepository; this.objectMapper = objectMapper; } @@ -69,10 +80,8 @@ protected RemoteConfig getRemoteConfig(String type) { * metadata object itself and then expose the additional values, this object contains the sourceId * which is the uuid of the record from the source system being harvested, it different from the * UUID use in this geonetwork, because harvested record get assign a new UUID locally. - * * This source id can be useful because the geonetwork may download the logo, it all depends on which harvester * you use, for GeonetHarvester, it will download but others don't, therefore the logo list will be different - * * TODO: We should add suggestion based on group logo. * * @param uuid - UUID of the record use by this geonetwork @@ -82,8 +91,9 @@ protected RemoteConfig getRemoteConfig(String type) { * "schemaid": "iso19115-3.2018", * "harvesterUri": "https://catalogue-imos.aodn.org.au/geonetwork", * "suggest_logos": [ - * "http://localhost:8080/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png", - * "https://catalogue-imos.aodn.org.au/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png" + * "http://localhost:8080/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png", <-- likely the icon store locally + * "https://catalogue-imos.aodn.org.au/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png" <-- the icon that store from metadata source server + * "https://localhost:8080/geonetwork/images/harvesting/... " <-- the icon use by the group and this metadata belongs to this group * ], * "isHarvested": true, * "harvesterType": "GeonetHarvester" @@ -92,7 +102,9 @@ protected RemoteConfig getRemoteConfig(String type) { @GetMapping("/records/{uuid}/info") public ResponseEntity> getRecordExtraInfo(@PathVariable("uuid") String uuid) { Map info = new HashMap<>(); - info.put(SUGGEST_LOGOS, new ArrayList()); + // The insert order is important as this is the order of suggested icon + List logos = new ArrayList<>(); + info.put(SUGGEST_LOGOS, logos); Metadata metadata = repository.findOneByUuid(uuid); if(metadata != null) { @@ -101,49 +113,67 @@ public ResponseEntity> getRecordExtraInfo(@PathVariable("uui // which is extract logo from this host info.put("sourceId", metadata.getSourceInfo().getSourceId()); - if(info.get(SUGGEST_LOGOS) instanceof ArrayList) { - String host = setup.getSiteSetting(SiteHelper.HOST); - String port = setup.getSiteSetting(SiteHelper.PORT); - String protocol = setup.getSiteSetting(SiteHelper.PROTOCOL); - ((ArrayList) info.get(SUGGEST_LOGOS)) - .add(String.format("%s://%s:%s/geonetwork/images/logos/%s.png", protocol, host, port, info.get("sourceId"))); - } + String host = setup.getSiteSetting(SiteHelper.HOST); + String port = setup.getSiteSetting(SiteHelper.PORT); + String protocol = setup.getSiteSetting(SiteHelper.PROTOCOL); + // Default logo location of record + logos.add(String.format("%s://%s:%s/geonetwork/images/logos/%s.png", protocol, host, port, info.get("sourceId"))); } // We can also get the harvester uuid, from there we can get the harvester url if(metadata.getHarvestInfo() != null) { - info.put("isHarvested", metadata.getHarvestInfo().isHarvested()); Object harvester = harvestManager.getHarvester(metadata.getHarvestInfo().getUuid()); if(harvester != null) { // Set the harvester class info.put("harvesterType", harvester.getClass()); - if (harvester instanceof GeonetHarvester) { - info.put("harvesterUri", StringUtils.removeEnd(((GeonetHarvester) harvester).getParams().host, "/")); + GeonetHarvester h = (GeonetHarvester) harvester; + info.put("harvesterName", h.getParams().getName()); + info.put("harvesterUri", StringUtils.removeEnd(h.getParams().host, "/")); // The geonetwork store logo under this dir with the uuid name, we provide a list of suggestion // on where to find the logo - if (info.get(SUGGEST_LOGOS) instanceof ArrayList) { - ((ArrayList) info.get(SUGGEST_LOGOS)) - .add(String.format("%s/images/logos/%s.png", info.get("harvesterUri"), info.get("sourceId"))); - } - } else if (harvester instanceof OaiPmhHarvester) { - // Will have remote link to logo + logos.add(String.format("%s/images/logos/%s.png", info.get("harvesterUri"), info.get("sourceId"))); + } + else if (harvester instanceof OaiPmhHarvester) { + // If non GN harvester e.g. OAI then logo from harvester info.put("harvesterUri", StringUtils.removeEnd(((OaiPmhHarvester) harvester).getParams().url, "/")); - } else if (harvester instanceof CswHarvester) { + } + else if (harvester instanceof CswHarvester) { // Will have remote link to logo info.put("harvesterUri", StringUtils.removeEnd(((CswHarvester) harvester).getParams().capabUrl, "/")); - } else if (harvester instanceof OgcWxSHarvester) { + } + else if (harvester instanceof OgcWxSHarvester) { // Will have remote link to logo info.put("harvesterUri", StringUtils.removeEnd(((OgcWxSHarvester) harvester).getParams().url, "/")); - } else if (harvester instanceof Geonet20Harvester) { + } + else if (harvester instanceof Geonet20Harvester) { // Will have remote link to logo info.put("harvesterUri", StringUtils.removeEnd(((Geonet20Harvester) harvester).getParams().host, "/")); } else { logger.error("Unknown instanceof type for harvester {}", harvester.getClass()); } + + // Get owner group and get the icon + @SuppressWarnings("unchecked") + AbstractHarvester h = (AbstractHarvester) harvester; + if(h.getParams().getOwnerIdGroup() != null) { + try { + Optional group = groupRepository.findById(Integer.parseInt(h.getParams().getOwnerIdGroup())); + group.ifPresent(g -> { + String host = setup.getSiteSetting(SiteHelper.HOST); + String port = setup.getSiteSetting(SiteHelper.PORT); + String protocol = setup.getSiteSetting(SiteHelper.PROTOCOL); + logos.add(String.format("%s://%s:%s/geonetwork/images/harvesting/%s", protocol, host, port, g.getLogo())); + }); + } + catch(Exception nfe) { + // If the group is not a number then ignore it. + } + } + } } diff --git a/geonetwork-core/src/test/java/au/org/aodn/geonetwork4/controller/ApiTest.java b/geonetwork-core/src/test/java/au/org/aodn/geonetwork4/controller/ApiTest.java index 12f2266..69bd2fe 100644 --- a/geonetwork-core/src/test/java/au/org/aodn/geonetwork4/controller/ApiTest.java +++ b/geonetwork-core/src/test/java/au/org/aodn/geonetwork4/controller/ApiTest.java @@ -3,6 +3,7 @@ import au.org.aodn.geonetwork4.Setup; import au.org.aodn.geonetwork_api.openapi.api.helper.SiteHelper; import com.fasterxml.jackson.databind.ObjectMapper; +import org.fao.geonet.domain.Group; import org.fao.geonet.domain.Metadata; import org.fao.geonet.domain.MetadataHarvestInfo; import org.fao.geonet.domain.MetadataSourceInfo; @@ -11,6 +12,7 @@ import org.fao.geonet.kernel.harvest.harvester.geonet.GeonetParams; import org.fao.geonet.kernel.harvest.harvester.oaipmh.OaiPmhHarvester; import org.fao.geonet.kernel.harvest.harvester.oaipmh.OaiPmhParams; +import org.fao.geonet.repository.GroupRepository; import org.fao.geonet.repository.MetadataRepository; import org.junit.Test; import org.mockito.Mockito; @@ -18,8 +20,10 @@ import java.util.List; import java.util.Map; +import java.util.Optional; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -61,11 +65,22 @@ public void verifyRecordExtraInfoWorks() { params.host = "https://catalogue-imos.aodn.org.au/geonetwork"; harvester.setParams(params); + when(params.getOwnerIdGroup()) + .thenReturn("100"); + HarvestManagerImpl harvestManager = Mockito.mock(HarvestManagerImpl.class); when(harvestManager.getHarvester(eq(harvesterUuid))) .thenReturn(harvester); - Api api = new Api(setup, metadataRepository, harvestManager, new ObjectMapper()); + Group group = new Group(); + group.setLogo("logo.gif"); + + GroupRepository groupRepository = Mockito.mock(GroupRepository.class); + when(groupRepository.findById(anyInt())) + .thenReturn(Optional.empty()) + .thenReturn(Optional.of(group)); + + Api api = new Api(setup, metadataRepository, harvestManager, groupRepository, new ObjectMapper()); ResponseEntity> v = api.getRecordExtraInfo(uuid); @@ -83,6 +98,9 @@ public void verifyRecordExtraInfoWorks() { OaiPmhParams pmhParams = Mockito.mock(OaiPmhParams.class); pmhParams.url = oaiHarvesterUrl; + when(pmhParams.getOwnerIdGroup()) + .thenReturn("100"); + when(oaiPmhHarvester.getParams()) .thenReturn(pmhParams); @@ -95,10 +113,12 @@ public void verifyRecordExtraInfoWorks() { v = api.getRecordExtraInfo(uuid); // Only one link this time and suggestion is localhost - assertEquals("Logo have two suggestions", 1, ((List)v.getBody().get(Api.SUGGEST_LOGOS)).size()); + assertEquals("Logo have two suggestions", 2, ((List)v.getBody().get(Api.SUGGEST_LOGOS)).size()); assertEquals("Logo link 1", "http://localhost:8080/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png", ((List)v.getBody().get(Api.SUGGEST_LOGOS)).get(0)); - + assertEquals("Logo link 2", + "http://localhost:8080/geonetwork/images/harvesting/logo.gif", + ((List)v.getBody().get(Api.SUGGEST_LOGOS)).get(1)); } }