Skip to content

Commit

Permalink
added countryLabel to solr index and consolidation, code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GordeaS authored and GordeaS committed Apr 2, 2024
1 parent 0a9e6da commit eaaac39
Show file tree
Hide file tree
Showing 35 changed files with 515 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class FailedTask {

private Instant modified;

private FailedTask() {
// default constructor
FailedTask() {
// default constructor used by morphia
}

public FailedTask(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,8 @@ public Place getCountry() {
if(country == null && getCountryId() != null) {
//set country if not dereferenced during retrieval from database
country = new Place(getCountryId());
}else if(country != null) {
//reset context to remove it from serialization
country.setContext(null);
}
}

return country;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ int compareClass(Object e1, Object e2) {
return 0;
}

@SuppressWarnings("unchecked")
int compareContent(Entity e1, Entity e2) throws IllegalAccessException {

List<Field> allObjectFieldsE1 = EntityUtils.getAllFields(e1.getClass());
Expand All @@ -74,6 +73,7 @@ int compareContent(Entity e1, Entity e2) throws IllegalAccessException {
return 0;
}

@SuppressWarnings("unchecked")
private int compareEntityField(Entity e1, Entity e2, Field field) throws IllegalAccessException {
Class<?> fieldType = field.getType();

Expand Down Expand Up @@ -148,7 +148,6 @@ private int compareLists(List<Object> l1, List<Object> l2) {
return 0;
}

@SuppressWarnings("unchecked")
private int compareMaps(Map<Object, Object> m1, Map<Object, Object> m2) {
if (MapUtils.isEmpty(m1) && MapUtils.isEmpty(m2)) {
// if both null or empty
Expand All @@ -166,6 +165,7 @@ private int compareMaps(Map<Object, Object> m1, Map<Object, Object> m2) {
return getMapResults(m1, m2);
}

@SuppressWarnings("unchecked")
private int getMapResults(Map<Object, Object> m1, Map<Object, Object> m2) {
for (Map.Entry<Object, Object> m1Elem : m1.entrySet()) {
if (m2.containsKey(m1Elem.getKey())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package eu.europeana.entitymanagement.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import eu.europeana.corelib.edm.model.schemaorg.ContextualEntity;
import eu.europeana.corelib.edm.model.schemaorg.EdmOrganization;
import eu.europeana.corelib.edm.model.schemaorg.GeoCoordinates;
Expand All @@ -17,30 +25,13 @@
import eu.europeana.entitymanagement.definitions.model.Organization;
import eu.europeana.entitymanagement.definitions.model.Place;
import eu.europeana.entitymanagement.definitions.model.TimeSpan;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class SchemaOrgUtils {

private static final Logger LOG = LogManager.getLogger(SchemaOrgUtils.class);

private static final String URL_PREFIX = "http://data.europeana.eu";
private static final String PLACE_PREFIX = "http://data.europeana.eu/place";
private static final String TIMESPAN_PREFIX = "http://semium.org";
private static final String UNIT_CODE_E37 = "E37";

private static DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
private static DateTimeFormatter dateFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);


private SchemaOrgUtils() {
// empty constructor prevent initialization
}
Expand Down Expand Up @@ -649,6 +640,7 @@ private static void addProperty(
* @param propertyName name of property
* @param referenceClass class of reference that should be used for Reference object
*/
@SuppressWarnings("rawtypes")
private static void addReference(
Thing object, String id, String propertyName, Class<? extends Thing> referenceClass) {
Reference reference = new Reference(referenceClass);
Expand Down Expand Up @@ -711,8 +703,8 @@ private static Thing instantiateResourceObject(Class<? extends Thing> referenceC
if (referenceClass == null) return new Thing();
Thing resource = null;
try {
resource = referenceClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
resource = referenceClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
resource = new Thing();
LOG.debug(
"Cannot instantiate object of class {} . Instance of Thing is used instead!",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package eu.europeana.entitymanagement.vocabulary;

/*
* TODO: define the constant for the fields countryMap and address
/**
* Constants for solr field names
*/
public interface OrganizationSolrFields extends EntitySolrFields {

public static final String EXTENSION_ALL = DYNAMIC_FIELD_SEPARATOR + "*";
public static final String DC_DESCRIPTION = "dc_description";
public static final String DC_DESCRIPTION_ALL = "dc_description" + EXTENSION_ALL;
public static final String EDM_ACRONYM = "edm_acronym";
public static final String EDM_ACRONYM_ALL = EDM_ACRONYM + EXTENSION_ALL;
public static final String FOAF_LOGO = "foaf_logo";
public static final String FOAF_HOMEPAGE = "foaf_homepage";
public static final String FOAF_PHONE = "foaf_phone";
public static final String FOAF_MBOX = "foaf_mbox";
public static final String EUROPEANA_ROLE = "europeanaRole";
public static final String COUNTRY = "country";
public static final String VCARD_HAS_ADDRESS = "vcard_hasAddress.1";
public static final String VCARD_STREET_ADDRESS = "vcard_streetAddress.1";
public static final String VCARD_LOCALITY = "vcard_locality.1";
public static final String VCARD_REGION = "vcard_region.1";
public static final String VCARD_POSTAL_CODE = "vcard_postalCode.1";
public static final String VCARD_COUNTRYNAME = "vcard_countryName.1";
public static final String VCARD_POST_OFFICE_BOX = "vcard_postOfficeBox.1";
public static final String VCARD_HAS_GEO = "hasGeo";
String EXTENSION_ALL = DYNAMIC_FIELD_SEPARATOR + "*";
String DC_DESCRIPTION = "dc_description";
String DC_DESCRIPTION_ALL = "dc_description" + EXTENSION_ALL;
String EDM_ACRONYM = "edm_acronym";
String EDM_ACRONYM_ALL = EDM_ACRONYM + EXTENSION_ALL;
String FOAF_LOGO = "foaf_logo";
String FOAF_HOMEPAGE = "foaf_homepage";
String FOAF_PHONE = "foaf_phone";
String FOAF_MBOX = "foaf_mbox";
String EUROPEANA_ROLE = "europeanaRole";
String COUNTRY = "country";
String COUNTRY_LABEL = "countryLabel";
String COUNTRY_LABEL_ALL = "countryLabel" + EXTENSION_ALL;
String VCARD_HAS_ADDRESS = "vcard_hasAddress.1";
String VCARD_STREET_ADDRESS = "vcard_streetAddress.1";
String VCARD_LOCALITY = "vcard_locality.1";
String VCARD_REGION = "vcard_region.1";
String VCARD_POSTAL_CODE = "vcard_postalCode.1";
String VCARD_COUNTRYNAME = "vcard_countryName.1";
String VCARD_POST_OFFICE_BOX = "vcard_postOfficeBox.1";
String VCARD_HAS_GEO = "hasGeo";
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public List<EntityRecord> saveBulk(List<EntityRecord> entityRecords) {
* @param filters Query filters
* @return List with results
*/
public List<EntityRecord> findWithCount(int start, int count, Filter[] filters) {
public List<EntityRecord> find(int start, int count, Filter[] filters) {
return getDataStore().find(EntityRecord.class)
.filter(filters)
.iterator(new FindOptions().skip(start).sort(ascending(ENTITY_MODIFIED)).limit(count))
Expand Down
3 changes: 1 addition & 2 deletions entity-management-solr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solrj.version}</version>
<artifactId>solr-solrj</artifactId>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package eu.europeana.entitymanagement.solr;

import eu.europeana.entitymanagement.definitions.model.Entity;
import eu.europeana.entitymanagement.solr.exception.InvalidSearchQueryException;
import eu.europeana.entitymanagement.solr.exception.SolrServiceException;
import eu.europeana.entitymanagement.solr.model.SolrEntity;
import eu.europeana.entitymanagement.vocabulary.EntitySolrFields;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -17,14 +12,19 @@
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CursorMarkParams;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import eu.europeana.entitymanagement.definitions.model.Entity;
import eu.europeana.entitymanagement.solr.exception.InvalidSearchQueryException;
import eu.europeana.entitymanagement.solr.exception.SolrServiceException;
import eu.europeana.entitymanagement.solr.model.SolrEntity;
import eu.europeana.entitymanagement.vocabulary.EntitySolrFields;

/**
* Fetches documents from Solr using a cursor. See
Expand Down Expand Up @@ -69,7 +69,7 @@ public <T extends Entity> List<SolrEntity<T>> next() throws SolrServiceException

try {
response = client.query(solrQuery);
} catch (HttpSolrClient.RemoteSolrException e) {
} catch (BaseHttpSolrClient.RemoteSolrException e) {
throw handleRemoteSolrException(solrQuery, e);
} catch (SolrServerException | IOException ex) {
throw new SolrServiceException(
Expand Down Expand Up @@ -137,7 +137,7 @@ private void ensureSortClause(SolrQuery solrQuery) {
}

private SolrServiceException handleRemoteSolrException(
SolrQuery searchQuery, HttpSolrClient.RemoteSolrException e) {
SolrQuery searchQuery, BaseHttpSolrClient.RemoteSolrException e) {
String remoteMessage = e.getMessage();

if (remoteMessage.contains(UNDEFINED_FIELD)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import eu.europeana.entitymanagement.solr.SolrUtils;
import eu.europeana.entitymanagement.utils.EntityUtils;
import eu.europeana.entitymanagement.vocabulary.EntitySolrFields;
import eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields;
import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.*;

public class SolrOrganization extends SolrEntity<Organization> {

Expand All @@ -21,53 +21,55 @@ public class SolrOrganization extends SolrEntity<Organization> {
@Field(EntitySolrFields.AGGREGATED_VIA)
private List<String> aggregatedVia;

@Field(OrganizationSolrFields.DC_DESCRIPTION_ALL)
@Field(DC_DESCRIPTION_ALL)
private Map<String, String> description;

@Field(OrganizationSolrFields.EDM_ACRONYM_ALL)
@Field(EDM_ACRONYM_ALL)
private Map<String, List<String>> acronym;

@Field(OrganizationSolrFields.FOAF_LOGO)
@Field(FOAF_LOGO)
private String logo;

@Field(OrganizationSolrFields.FOAF_HOMEPAGE)
@Field(FOAF_HOMEPAGE)
private String homepage;

@Field(OrganizationSolrFields.FOAF_PHONE)
@Field(FOAF_PHONE)
private List<String> phone;

@Field(OrganizationSolrFields.FOAF_MBOX)
@Field(FOAF_MBOX)
private List<String> mbox;

//MAPPING TO BE ENABLED BACK IN THE NEXT VERSION - requires schema and ENtity API UPDATE
@Field(OrganizationSolrFields.EUROPEANA_ROLE)
@Field(EUROPEANA_ROLE)
private List<String> europeanaRole;

@Field(OrganizationSolrFields.COUNTRY)
@Field(COUNTRY)
private List<String> country;

@Field(COUNTRY_LABEL_ALL)
private Map<String, String> countryLabel;

@Field(OrganizationSolrFields.VCARD_HAS_ADDRESS)
@Field(VCARD_HAS_ADDRESS)
private String hasAddress;

@Field(OrganizationSolrFields.VCARD_STREET_ADDRESS)
@Field(VCARD_STREET_ADDRESS)
private String streetAddress;

@Field(OrganizationSolrFields.VCARD_LOCALITY)
@Field(VCARD_LOCALITY)
private String locality;

@Field(OrganizationSolrFields.VCARD_REGION)
@Field(VCARD_REGION)
private String region;

@Field(OrganizationSolrFields.VCARD_POSTAL_CODE)
@Field(VCARD_POSTAL_CODE)
private String postalCode;

@Field(OrganizationSolrFields.VCARD_COUNTRYNAME)
@Field(VCARD_COUNTRYNAME)
private String countryName;

@Field(OrganizationSolrFields.VCARD_POST_OFFICE_BOX)
@Field(VCARD_POST_OFFICE_BOX)
private String postBox;

@Field(OrganizationSolrFields.VCARD_HAS_GEO)
@Field(VCARD_HAS_GEO)
private String hasGeo;

public SolrOrganization() {
Expand Down Expand Up @@ -97,6 +99,10 @@ public SolrOrganization(Organization organization) {
if(orgCoutryISO!=null) {
this.country.add(orgCoutryISO);
}
if(organization.getCountry() != null) {
this.setCountryLabel(organization.getCountry().getPrefLabel());
}


if (organization.getSameReferenceLinks() != null) {
this.sameAs = new ArrayList<>(organization.getSameReferenceLinks());
Expand All @@ -121,7 +127,7 @@ private void setDescription(Map<String, String> dcDescription) {
this.description =
new HashMap<>(
SolrUtils.normalizeStringMapByAddingPrefix(
OrganizationSolrFields.DC_DESCRIPTION + EntitySolrFields.DYNAMIC_FIELD_SEPARATOR,
DC_DESCRIPTION + EntitySolrFields.DYNAMIC_FIELD_SEPARATOR,
dcDescription));
}
}
Expand All @@ -131,7 +137,7 @@ private void setAcronym(Map<String, List<String>> acronym) {
this.acronym =
new HashMap<>(
SolrUtils.normalizeStringListMapByAddingPrefix(
OrganizationSolrFields.EDM_ACRONYM + EntitySolrFields.DYNAMIC_FIELD_SEPARATOR,
EDM_ACRONYM + EntitySolrFields.DYNAMIC_FIELD_SEPARATOR,
acronym));
}
}
Expand Down Expand Up @@ -208,4 +214,18 @@ public List<String> getAggregatedVia() {
public List<String> getCountry() {
return country;
}

public Map<String, String> getCountryLabel() {
return countryLabel;
}

public void setCountryLabel(Map<String, String> countryLabel) {
if (MapUtils.isNotEmpty(countryLabel)) {
this.countryLabel =
new HashMap<>(
SolrUtils.normalizeStringMapByAddingPrefix(
COUNTRY_LABEL + EntitySolrFields.DYNAMIC_FIELD_SEPARATOR,
countryLabel));
}
}
}
Loading

0 comments on commit eaaac39

Please sign in to comment.