From 57a7c58a3ce09e2b17a02846d0bc5e5c8ca3bbb5 Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Tue, 5 Mar 2024 16:43:39 +0100 Subject: [PATCH] some code refactorings --- .../definitions/EntityRecordFields.java | 2 -- .../web/xml/model/XmlEdmCountry.java | 24 +++++++++---------- .../web/xml/model/XmlOrganizationImpl.java | 2 +- .../repository/EntityRecordRepository.java | 2 +- .../entitymanagement/web/EMController.java | 15 +----------- .../web/service/EntityRecordService.java | 12 ++++++++-- 6 files changed, 25 insertions(+), 32 deletions(-) diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/EntityRecordFields.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/EntityRecordFields.java index 6d4cc136..46edc52d 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/EntityRecordFields.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/EntityRecordFields.java @@ -16,6 +16,4 @@ private EntityRecordFields() { public static final String ENTITY_SAME_AS = "entity.sameAs"; public static final String ENTITY_EXACT_MATCH = "entity.exactMatch"; public static final String ENTITY_TYPE = "entity.type"; - public static final String ENTITY_ENTITY_ID = "entity.entityId"; - public static final String ENTITY_PREF_LABEL = "entity.prefLabel"; } diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlEdmCountry.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlEdmCountry.java index 4ed2ffe3..5bf2d717 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlEdmCountry.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlEdmCountry.java @@ -1,9 +1,9 @@ package eu.europeana.entitymanagement.web.xml.model; -import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.ABOUT; import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.NAMESPACE_EDM; import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.NAMESPACE_RDF; import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.PREF_LABEL; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.RESOURCE; import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.XML_PLACE; import java.util.ArrayList; import java.util.List; @@ -19,33 +19,25 @@ @XmlAccessorType(XmlAccessType.FIELD) @XmlType( propOrder = { - ABOUT, + RESOURCE, PREF_LABEL }) public class XmlEdmCountry { @XmlAttribute(namespace = NAMESPACE_RDF, name = XmlConstants.RESOURCE) - private String about; + private String resource; @XmlElement(namespace = XmlConstants.NAMESPACE_SKOS, name = PREF_LABEL) private List prefLabel = new ArrayList<>(); public XmlEdmCountry(Place place) { - this.about=place.getAbout(); + this.resource=place.getAbout(); this.prefLabel= RdfXmlUtils.convertMapToXmlMultilingualString(place.getPrefLabel()); } public XmlEdmCountry() { } - public String getAbout() { - return this.about; - } - - public void setAbout(String about) { - this.about = about; - } - public List getPrefLabel() { return this.prefLabel; } @@ -53,4 +45,12 @@ public List getPrefLabel() { public void setPrefLabel(List prefLabel) { this.prefLabel=prefLabel; } + + public String getResource() { + return resource; + } + + public void setResource(String resource) { + this.resource = resource; + } } diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlOrganizationImpl.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlOrganizationImpl.java index 5a3e67a9..1fac4b4d 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlOrganizationImpl.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlOrganizationImpl.java @@ -160,7 +160,7 @@ public Organization toEntityModel() throws EntityModelCreationException { //set country id (external dereferencers deliver only the ids, not transitive data) if(getCountry() != null) { //we need to extract the countryID as well (xml about holds the entityId) - entity.setCountryId(getCountry().getAbout()); + entity.setCountryId(getCountry().getResource()); } if (getHomepage() != null) { diff --git a/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java b/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java index fc2260d7..0bb32e5a 100644 --- a/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java +++ b/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java @@ -110,7 +110,7 @@ public EntityRecord findEntityRecord(String entityId, String... fields) { return query.first(); } } - + public List getEntityIds( List entityIds, boolean excludeDisabled) { List entityRecords = findByEntityIds(entityIds, excludeDisabled, false); diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java index cc7150bd..bd062c3f 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java @@ -643,25 +643,12 @@ private ResponseEntity createResponseMultipleEntities(List entit throw new EntityNotFoundException(entityIds.toString()); } - // LinkedHashMap iterates keys() and values() in order of insertion. Using a map - // improves sort performance significantly - Map sortedEntityRecordMap = new LinkedHashMap<>(entityIds.size()); - for (String id : entityIds) { - Optional recordIdMatched= entityRecords.stream().filter(er -> id.equals(er.getEntityId()) || er.getEntity().getSameReferenceLinks().contains(id)).findFirst(); - if(recordIdMatched.isPresent()) { - sortedEntityRecordMap.put(id, recordIdMatched.get()); - } - } - // create response headers String contentType = HttpHeaders.CONTENT_TYPE_JSONLD_UTF8; org.springframework.http.HttpHeaders headers = createAllowHeader(request); headers.add(HttpHeaders.CONTENT_TYPE, contentType); - // remove null values in response - List responseBody = sortedEntityRecordMap.values().stream() - .filter(Objects::nonNull).collect(Collectors.toList()); - String body = serialize(responseBody); + String body = serialize(entityRecords); return ResponseEntity.status(HttpStatus.OK).headers(headers).body(body); } diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java index 77f873ec..29c00c59 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java @@ -96,7 +96,16 @@ public List retrieveMultipleByEntityIds(List entityIds, } public List retrieveMultipleByEntityIdsOrCoreference(List entityIds) { - return entityRecordRepository.findByEntityIdsOrCoreference(entityIds); + List records=entityRecordRepository.findByEntityIdsOrCoreference(entityIds); + //sorting the list in order of the input ids + List recordsSorted=new ArrayList<>(); + for (String id : entityIds) { + Optional recordIdMatched= records.stream().filter(er -> id.equals(er.getEntityId()) || er.getEntity().getSameReferenceLinks().contains(id)).findFirst(); + if(recordIdMatched.isPresent()) { + recordsSorted.add(recordIdMatched.get()); + } + } + return recordsSorted; } public EntityRecord retrieveEntityRecord(EntityTypes type, String identifier, String profiles, @@ -138,7 +147,6 @@ private void dereferenceLinkedEntities(Organization org) { if (org.getCountryId() != null) { EntityRecord countryRecord = entityRecordRepository.findEntityRecord(org.getCountryId(), EntityRecordFields.ENTITY); if (countryRecord != null) { - // fill in only the chosen fields Place country = new Place(); country.setEntityId(countryRecord.getEntity().getEntityId()); country.setPrefLabel(countryRecord.getEntity().getPrefLabel());