Skip to content

Commit

Permalink
some code refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
SrdjanStevanetic committed Mar 5, 2024
1 parent 9e5dec6 commit 57a7c58
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,38 +19,38 @@
@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<LabelledResource> 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<LabelledResource> getPrefLabel() {
return this.prefLabel;
}

public void setPrefLabel(List<LabelledResource> prefLabel) {
this.prefLabel=prefLabel;
}

public String getResource() {
return resource;
}

public void setResource(String resource) {
this.resource = resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public EntityRecord findEntityRecord(String entityId, String... fields) {
return query.first();
}
}

public List<EntityIdDisabledStatus> getEntityIds(
List<String> entityIds, boolean excludeDisabled) {
List<EntityRecord> entityRecords = findByEntityIds(entityIds, excludeDisabled, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,25 +643,12 @@ private ResponseEntity<String> createResponseMultipleEntities(List<String> entit
throw new EntityNotFoundException(entityIds.toString());
}

// LinkedHashMap iterates keys() and values() in order of insertion. Using a map
// improves sort performance significantly
Map<String, EntityRecord> sortedEntityRecordMap = new LinkedHashMap<>(entityIds.size());
for (String id : entityIds) {
Optional<EntityRecord> 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<EntityRecord> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ public List<EntityRecord> retrieveMultipleByEntityIds(List<String> entityIds,
}

public List<EntityRecord> retrieveMultipleByEntityIdsOrCoreference(List<String> entityIds) {
return entityRecordRepository.findByEntityIdsOrCoreference(entityIds);
List<EntityRecord> records=entityRecordRepository.findByEntityIdsOrCoreference(entityIds);
//sorting the list in order of the input ids
List<EntityRecord> recordsSorted=new ArrayList<>();
for (String id : entityIds) {
Optional<EntityRecord> 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,
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 57a7c58

Please sign in to comment.