diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Aggregator.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Aggregator.java index b5650110..ed5c6040 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Aggregator.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Aggregator.java @@ -1,33 +1,6 @@ package eu.europeana.entitymanagement.definitions.model; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.ACRONYM; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.AGGREGATED_VIA; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.AGGREGATES_FROM; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.ALT_LABEL; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.CONTEXT; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.COUNTRY; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.DEPICTION; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.DESCRIPTION; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.EUROPEANA_ROLE; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.FOAF_HOMEPAGE; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.FOAF_LOGO; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.FOAF_MBOX; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.FOAF_PHONE; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.GEOGRAPHIC_SCOPE; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.HAS_ADDRESS; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.HERITAGE_DOMAIN; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.HIDDEN_LABEL; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.ID; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.IDENTIFIER; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.IS_AGGREGATED_BY; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.LANGUAGE; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.PREF_LABEL; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.PROVIDES_AUDIENCE_ENGAGEMENT_ACTIVITY; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.PROVIDES_SUPPORT_FOR_BUILDING_ACTIVITY; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.PROVIDES_SUPPORT_FOR_DATA_ACTIVITY; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.PROVIDES_SUPPORT_FOR_MEDIA_TYPE; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.SAME_AS; -import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.TYPE; +import static eu.europeana.entitymanagement.vocabulary.WebEntityFields.*; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonGetter; @@ -67,6 +40,9 @@ PROVIDES_SUPPORT_FOR_BUILDING_ACTIVITY, PROVIDES_AUDIENCE_ENGAGEMENT_ACTIVITY }) +/** + * Class implementing aggregator extension for organizations + */ public class Aggregator extends Organization { protected String mbox; protected String geographicScope; @@ -75,12 +51,19 @@ public class Aggregator extends Organization { protected List providesSupportForDataActivity; protected List providesCapacityBuildingActivity; protected List providesAudienceEngagementActivity; - + + /** + * Public constructor + */ public Aggregator() { super(); type=EntityTypes.Aggregator.getEntityType(); } + /** + * Public constructor creating a copy of the provided aggregator + * @param copy + */ public Aggregator(Aggregator copy) { super(copy); this.type=copy.getType(); diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/utils/SolrGeneralUtils.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/utils/SolrGeneralUtils.java index 1e0f80a2..9978d5de 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/utils/SolrGeneralUtils.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/utils/SolrGeneralUtils.java @@ -3,12 +3,21 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; +/** + * Utils for converting the representation of data to solr fields + */ public class SolrGeneralUtils { + /** + * Hide default constructor + */ + private SolrGeneralUtils() { + + } + /** * This method adds prefixes to the fields in format Map> languageMap e.g. * "skos_prefLabel" @@ -23,13 +32,11 @@ public static Map> normalizeStringListMapByAddingPrefix( return new HashMap<>(); } Map> res; - if (!languageMap.keySet().iterator().next().contains(fieldNamePrefix)) { - res = - languageMap.entrySet().stream() - .collect( - Collectors.toMap(entry -> fieldNamePrefix + entry.getKey(), Map.Entry::getValue)); - } else { + if (languageMap.keySet().iterator().next().contains(fieldNamePrefix)) { res = languageMap; + } else { + res = languageMap.entrySet().stream().collect( + Collectors.toMap(entry -> fieldNamePrefix + entry.getKey(), Map.Entry::getValue)); } return res; } @@ -42,21 +49,19 @@ public static Map> normalizeStringListMapByAddingPrefix( * @param languageMap e.g. prefLabel * @return normalized content in format Map */ - public static Map normalizeStringMapByAddingPrefix( - String fieldNamePrefix, Map languageMap) { - + public static Map normalizeStringMapByAddingPrefix(String fieldNamePrefix, + Map languageMap) { + if (MapUtils.isEmpty(languageMap)) { return new HashMap<>(); } - + Map res; - if (!languageMap.keySet().iterator().next().contains(fieldNamePrefix)) { - res = - languageMap.entrySet().stream() - .collect( - Collectors.toMap(entry -> fieldNamePrefix + entry.getKey(), Map.Entry::getValue)); - } else { + if (languageMap.keySet().iterator().next().contains(fieldNamePrefix)) { res = languageMap; + } else { + res = languageMap.entrySet().stream().collect( + Collectors.toMap(entry -> fieldNamePrefix + entry.getKey(), Map.Entry::getValue)); } return res; } diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/AggregatorSolrFields.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/AggregatorSolrFields.java index ae77b993..fa2e6e02 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/AggregatorSolrFields.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/AggregatorSolrFields.java @@ -3,14 +3,14 @@ /** * Constants for solr field names */ -public interface AggregatorSolrFields { +public abstract class AggregatorSolrFields { - String MBOX = "mbox"; - String GEOGRAPHIC_SCOPE = "geographicScope"; - String HERITAGE_DOMAIN = "heritageDomain"; - String PROVIDES_SUPPORT_FOR_MEDIA_TYPE = "providesSupportForMediaType"; - String PROVIDES_SUPPORT_FOR_DATA_ACTIVITY = "providesSupportForDataActivity"; - String PROVIDES_CAPACITY_BUILDING_ACTIVITY = "providesCapacityBuildingActivity"; - String AUDIENCE_ENGAGEMENT_ACTIVITY = "providesAudienceEngagementActivity"; - String PARENT_TYPE = "parentType"; + public static final String MBOX = "mbox"; + public static final String GEOGRAPHIC_SCOPE = "geographicScope"; + public static final String HERITAGE_DOMAIN = "heritageDomain"; + public static final String PROVIDES_SUPPORT_FOR_MEDIA_TYPE = "providesSupportForMediaType"; + public static final String PROVIDES_SUPPORT_FOR_DATA_ACTIVITY = "providesSupportForDataActivity"; + public static final String PROVIDES_CAPACITY_BUILDING_ACTIVITY = "providesCapacityBuildingActivity"; + public static final String AUDIENCE_ENGAGEMENT_ACTIVITY = "providesAudienceEngagementActivity"; + public static final String PARENT_TYPE = "parentType"; } diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityFieldsTypes.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityFieldsTypes.java index b0b7445d..316e610b 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityFieldsTypes.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityFieldsTypes.java @@ -7,6 +7,7 @@ * * @author StevaneticS */ +@SuppressWarnings("java:S115") public enum EntityFieldsTypes { // General fields diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityTypes.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityTypes.java index c967d00f..1e7ea12f 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityTypes.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/EntityTypes.java @@ -20,14 +20,6 @@ public enum EntityTypes implements EntityKeyword { private String httpUri; private String parentType; - public String getEntityType() { - return entityType; - } - - public String getUrlPath() { - return urlPath; - } - EntityTypes(String entityType, String urlPath, String uri, String parentType) { this.entityType = entityType; this.urlPath = urlPath; @@ -35,7 +27,13 @@ public String getUrlPath() { this.parentType = parentType; } + public String getEntityType() { + return entityType; + } + public String getUrlPath() { + return urlPath; + } /** * Check if an array of EntityTypes contains an Entity type * diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityConstants.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityConstants.java index d57285fd..219cf1ef 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityConstants.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityConstants.java @@ -1,6 +1,6 @@ package eu.europeana.entitymanagement.vocabulary; -public interface WebEntityConstants extends WebEntityFields { +public abstract class WebEntityConstants extends WebEntityFields { public static final String PATH_PARAM_TYPE = "type"; public static final String PATH_PARAM_NAMESPACE = "namespace"; diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityFields.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityFields.java index f5277e4e..7ad96878 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityFields.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/vocabulary/WebEntityFields.java @@ -1,6 +1,6 @@ package eu.europeana.entitymanagement.vocabulary; -public interface WebEntityFields { +public abstract class WebEntityFields { // LD fields public static final String CONTEXT = "@context"; diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregationImpl.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregationImpl.java index 338ea72d..408af5b0 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregationImpl.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregationImpl.java @@ -1,14 +1,22 @@ package eu.europeana.entitymanagement.web.xml.model; -import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.*; - -import eu.europeana.entitymanagement.definitions.model.Aggregation; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.ABOUT; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.NAMESPACE_DC_TERMS; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.NAMESPACE_ORE; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.NAMESPACE_RDF; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.XML_AGGREGATES; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.XML_CREATED; +import static eu.europeana.entitymanagement.web.xml.model.XmlConstants.XML_MODIFIED; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; +import eu.europeana.entitymanagement.definitions.model.Aggregation; +/** + * class for xml serialization of aggregations + */ public class XmlAggregationImpl { @XmlAttribute(namespace = NAMESPACE_RDF, name = ABOUT) @@ -23,6 +31,10 @@ public class XmlAggregationImpl { @XmlElement(namespace = NAMESPACE_ORE, name = XML_AGGREGATES) private List aggregates; + /** + * Build object for xml serialization from POJO + * @param aggregation the aggregator object to serialize + */ public XmlAggregationImpl(Aggregation aggregation) { this.id = aggregation.getId(); this.created = aggregation.getCreated(); diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregatorImpl.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregatorImpl.java index 507e58c8..f53d781b 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregatorImpl.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/web/xml/model/XmlAggregatorImpl.java @@ -22,6 +22,10 @@ @XmlRootElement(namespace = NAMESPACE_EDM, name = XML_AGGREGATOR) @XmlAccessorType(XmlAccessType.FIELD) +/** + * class for xml serialization of Aggregators + */ +@SuppressWarnings("java:S2384") public class XmlAggregatorImpl extends XmlOrganizationImpl { @XmlElement(namespace = NAMESPACE_FOAF, name = XML_MBOX) @@ -45,27 +49,35 @@ public class XmlAggregatorImpl extends XmlOrganizationImpl { @XmlElement(namespace = NAMESPACE_EDM, name = XML_PROVIDES_AUDIENCE_ENGAGEMENT_ACTIVITY) private List providesAudienceEngagementActivity; + /** + * Constructor to convert the POJO for XML serialization + * @param aggregator + */ public XmlAggregatorImpl(Aggregator aggregator) { super(aggregator); this.mbox=aggregator.getMbox(); this.geographicScope=aggregator.getGeographicScope(); if (aggregator.getHeritageDomain() != null) { - this.heritageDomain = new ArrayList(aggregator.getHeritageDomain()); + this.heritageDomain = new ArrayList<>(aggregator.getHeritageDomain()); } if (aggregator.getProvidesSupportForMediaType() != null) { - this.providesSupportForMediaType = new ArrayList(aggregator.getProvidesSupportForMediaType()); + this.providesSupportForMediaType = new ArrayList<>(aggregator.getProvidesSupportForMediaType()); } if (aggregator.getProvidesSupportForDataActivity() != null) { - this.providesSupportForDataActivity = new ArrayList(aggregator.getProvidesSupportForDataActivity()); + this.providesSupportForDataActivity = new ArrayList<>(aggregator.getProvidesSupportForDataActivity()); } if (aggregator.getProvidesCapacityBuildingActivity() != null) { - this.providesCapacityBuildingActivity = new ArrayList(aggregator.getProvidesCapacityBuildingActivity()); + this.providesCapacityBuildingActivity = new ArrayList<>(aggregator.getProvidesCapacityBuildingActivity()); } if (aggregator.getProvidesAudienceEngagementActivity() != null) { - this.providesAudienceEngagementActivity = new ArrayList(aggregator.getProvidesAudienceEngagementActivity()); + this.providesAudienceEngagementActivity = new ArrayList<>(aggregator.getProvidesAudienceEngagementActivity()); } } + public XmlAggregatorImpl() { + // default constructor + } + @Override public Aggregator toEntityModel() throws EntityModelCreationException { super.toEntityModel(); @@ -77,10 +89,7 @@ public Aggregator toEntityModel() throws EntityModelCreationException { ((Aggregator)entity).setProvidesAudienceEngagementActivity(getProvidesAudienceEngagementActivity()); return ((Aggregator)entity); } - - public XmlAggregatorImpl() { - // default constructor - } + @Override protected EntityTypes getTypeEnum() { diff --git a/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/VocabularyRepository.java b/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/VocabularyRepository.java index 15983c96..d65935cb 100644 --- a/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/VocabularyRepository.java +++ b/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/VocabularyRepository.java @@ -16,13 +16,16 @@ public class VocabularyRepository { @Autowired - @Qualifier(AppConfigConstants.BEAN_EM_DATA_STORE) Datastore datastore; private static final String ID = "id"; private List europeanaRoles; + public VocabularyRepository(@Qualifier(AppConfigConstants.BEAN_EM_DATA_STORE) Datastore datastore) { + this.datastore = datastore; + } + public List getEuropeanaRoles() { synchronized(this) { if(europeanaRoles==null) { diff --git a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntitySuggesterFilterConfig.java b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntitySuggesterFilterConfig.java index 5819bad4..a4799551 100644 --- a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntitySuggesterFilterConfig.java +++ b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntitySuggesterFilterConfig.java @@ -1,11 +1,7 @@ package eu.europeana.entitymanagement.solr; import static eu.europeana.entitymanagement.common.vocabulary.AppConfigConstants.BEAN_SOLR_ENTITY_SUGGESTER_FILTER; -import static eu.europeana.entitymanagement.solr.SolrEntityUtils.SOLR_AGENT_SUGGESTER_FILTER; -import static eu.europeana.entitymanagement.solr.SolrEntityUtils.SOLR_CONCEPT_SUGGESTER_FILTER; -import static eu.europeana.entitymanagement.solr.SolrEntityUtils.SOLR_ORGANIZATION_SUGGESTER_FILTER; -import static eu.europeana.entitymanagement.solr.SolrEntityUtils.SOLR_PLACE_SUGGESTER_FILTER; -import static eu.europeana.entitymanagement.solr.SolrEntityUtils.SOLR_TIMESPAN_SUGGESTER_FILTER; +import static eu.europeana.entitymanagement.solr.SolrEntityUtils.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.databind.ser.FilterProvider; diff --git a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntityUtils.java b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntityUtils.java index 05bfd6f6..97a9285f 100644 --- a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntityUtils.java +++ b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/SolrEntityUtils.java @@ -1,10 +1,9 @@ package eu.europeana.entitymanagement.solr; import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import eu.europeana.entitymanagement.definitions.model.Agent; import eu.europeana.entitymanagement.definitions.model.Aggregation; import eu.europeana.entitymanagement.definitions.model.Aggregator; @@ -35,7 +34,13 @@ public class SolrEntityUtils { public static final String SOLR_TIMESPAN_SUGGESTER_FILTER = "solrTimeSpanFilter"; public static final String SOLR_PLACE_SUGGESTER_FILTER = "solrPlaceFilter"; public static final String SOLR_CONCEPT_SUGGESTER_FILTER = "solrConceptFilter"; - + public static final int MAX_FILTERS = 3; + + /** + * Hide default constructor + */ + private SolrEntityUtils(){} + /** * Gets the {@link SolrEntity} class for an entity type. * @@ -80,6 +85,11 @@ public static Class getSolrEntityClass( return solrEntityClass; } + /** + * Factory method to instantiate solr objects + * @param record the entity record + * @return the solr entity + */ public static SolrEntity createSolrEntity(EntityRecord record) { final Entity entity = record.getEntity(); SolrEntity solrEntity = null; @@ -170,7 +180,7 @@ private static List collectLabelEnrichGeneral(EntityRecord record) { private static Map> collectLabelEnrich(EntityRecord record) { // collect values from prefLabel, altLabel, hiddenLabel acronym Entity entity = record.getEntity(); - Map> values = new HashMap<>(); + Map> values = new ConcurrentHashMap<>(); if (entity == null) { return values; } @@ -230,13 +240,11 @@ private static void addLabels( } private static boolean isEnrichmentDisabled(EntityRecord record) { - if (record.getEntity() == null || record.getEntity().getIsAggregatedBy() == null) { - // entity is not consolidated, should not be index at this stage - return true; - } - + // entity is not consolidated, should not be index at this stage + boolean notConsolidated = record.getEntity() == null || record.getEntity().getIsAggregatedBy() == null; // check if flag is set to false - if (Boolean.FALSE.equals(record.getEntity().getIsAggregatedBy().getEnrich())) { + boolean noEnrichment = Boolean.FALSE.equals(record.getEntity().getIsAggregatedBy().getEnrich()); + if (notConsolidated || noEnrichment) { return true; } @@ -259,8 +267,7 @@ private static void setMetricsAndFilters( } EntityTypes entityType = EntityTypes.valueOf(solrEntity.getType()); - int initialCapacity = 3; - List filters = new ArrayList(initialCapacity); + List filters = new ArrayList<>(MAX_FILTERS); filters.add(entityType.getEntityType()); if(entityType.getParentType() != null) { //add organization as type for Aggregators diff --git a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrAggregator.java b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrAggregator.java index 0f3556e6..cc1e6d54 100644 --- a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrAggregator.java +++ b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrAggregator.java @@ -1,18 +1,10 @@ package eu.europeana.entitymanagement.solr.model; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.AUDIENCE_ENGAGEMENT_ACTIVITY; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.GEOGRAPHIC_SCOPE; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.HERITAGE_DOMAIN; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.MBOX; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.PROVIDES_CAPACITY_BUILDING_ACTIVITY; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.PROVIDES_SUPPORT_FOR_DATA_ACTIVITY; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.PROVIDES_SUPPORT_FOR_MEDIA_TYPE; -import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.PARENT_TYPE; +import static eu.europeana.entitymanagement.vocabulary.AggregatorSolrFields.*; import java.util.ArrayList; import java.util.List; import org.apache.solr.client.solrj.beans.Field; import eu.europeana.entitymanagement.definitions.model.Aggregator; -import eu.europeana.entitymanagement.vocabulary.EntityTypes; public class SolrAggregator extends SolrOrganization { @@ -23,26 +15,30 @@ public class SolrAggregator extends SolrOrganization { private String geographicScope; @Field(HERITAGE_DOMAIN) - List heritageDomain; + private List heritageDomain; @Field(PROVIDES_SUPPORT_FOR_MEDIA_TYPE) - List providesSupportForMediaType; + private List providesSupportForMediaType; @Field(PROVIDES_SUPPORT_FOR_DATA_ACTIVITY) - List providesSupportForDataActivity; + private List providesSupportForDataActivity; @Field(PROVIDES_CAPACITY_BUILDING_ACTIVITY) - List providesCapacityBuildingActivity; + private List providesCapacityBuildingActivity; @Field(AUDIENCE_ENGAGEMENT_ACTIVITY) - List providesAudienceEngagementActivity; + private List providesAudienceEngagementActivity; -// @Field(PARENT_TYPE) -// String parentType; - + /** + * public constructor + */ public SolrAggregator() { } + /** + * Constructor to convert aggregators to solr representation + * @param aggregator aggregator POJO + */ public SolrAggregator(Aggregator aggregator) { super(aggregator); this.mbox = aggregator.getMbox(); diff --git a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrOrganization.java b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrOrganization.java index eba336e8..abb0abd3 100644 --- a/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrOrganization.java +++ b/entity-management-solr/src/main/java/eu/europeana/entitymanagement/solr/model/SolrOrganization.java @@ -1,24 +1,6 @@ package eu.europeana.entitymanagement.solr.model; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.COUNTRY; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.COUNTRY_LABEL; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.COUNTRY_LABEL_ALL; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.DC_DESCRIPTION; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.DC_DESCRIPTION_ALL; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.EDM_ACRONYM; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.EDM_ACRONYM_ALL; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.EUROPEANA_ROLE; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.FOAF_HOMEPAGE; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.FOAF_LOGO; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.FOAF_PHONE; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_COUNTRYNAME; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_HAS_ADDRESS; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_HAS_GEO; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_LOCALITY; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_POSTAL_CODE; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_POST_OFFICE_BOX; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_REGION; -import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.VCARD_STREET_ADDRESS; +import static eu.europeana.entitymanagement.vocabulary.OrganizationSolrFields.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java index 82151b48..48c449f0 100644 --- a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java @@ -259,7 +259,7 @@ public class IntegrationTestUtils { Map.of(Record.class, new ZohoRecordTestDeserializer()))); /** Maps ZOHO organization URIs to mocked JSON responses */ - public static Map ZOHO_ORG_URL_RESPONSE_MAP = + public static final Map ZOHO_ORG_URL_RESPONSE_MAP = Map.of( ORGANIZATION_NATURALIS_URI_ZOHO, ORGANIZATION_NATURALIS_ZOHO_RESPONSE, @@ -277,19 +277,19 @@ public class IntegrationTestUtils { ORGANIZATION_EUSKARIANA_ZOHO_RESPONSE); /** Maps ZOHO organization names to mocked JSON responses */ - public static Map ZOHO_ORG_NAME_RESPONSE_MAP = new HashMap(); + public static final Map ZOHO_ORG_NAME_RESPONSE_MAP = new HashMap(); static { ZOHO_ORG_NAME_RESPONSE_MAP.put(ORGANIZATION_EUSKARIANA_NAME, ORGANIZATION_EUSKARIANA_ZOHO_RESPONSE); } /** Maps ZOHO organization urls to the aggregators mocked JSON responses */ - public static Map ZOHO_ORG_URL_AGGREGATOR_RESPONSE_MAP = + public static final Map ZOHO_ORG_URL_AGGREGATOR_RESPONSE_MAP = Map.of( ORGANIZATION_EUSKARIANA_URI_ZOHO, AGGREGATOR_EUSKARIANA_ZOHO_RESPONSE); /** Maps ZOHO organization:aggregator to the linking mocked JSON responses */ - public static Map ZOHO_ORG_AGGREG_LINKING_RESPONSE_MAP = + public static final Map ZOHO_ORG_AGGREG_LINKING_RESPONSE_MAP = Map.of( String.format("%s:%s", ORGANIZATION_ARMA_NAME, ORGANIZATION_EUSKARIANA_NAME), LINKING_ARMA_EUSKARIANA_ZOHO_RESPONSE); diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/web/EntityRegistrationIT.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/web/EntityRegistrationIT.java index b372fea2..62c5e924 100644 --- a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/web/EntityRegistrationIT.java +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/web/EntityRegistrationIT.java @@ -353,6 +353,7 @@ public void zohoCheckAggregatedViaOverZohoLinkingAndLocalDbSearch() throws Excep .content( loadFile(IntegrationTestUtils.ORGANIZATION_REGISTER_EUSKARIANA_JSON)) .contentType(MediaType.APPLICATION_JSON_VALUE)); + assertNotNull(response); /* * 2. remove the entry from the map that mocks the zoho response for the organization diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseEntityRecordService.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseEntityRecordService.java index 67305181..cbb67db9 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseEntityRecordService.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseEntityRecordService.java @@ -580,7 +580,7 @@ protected void fillAggregatedViaWithOrgIds(Organization org) throws EntityUpdate List aggregatorUrls = org.getAggregatedViaAggregatorUrls(); //search in the corefs List entities = entityRecordRepository.findEntitiesByCoreference(aggregatorUrls, null, false); - List entityIds = entities.stream().map(el -> el.getEntityId()).collect(Collectors.toList());; + List entityIds = entities.stream().map(el -> el.getEntityId()).collect(Collectors.toList()); if(entityIds.isEmpty()) { if (logger.isWarnEnabled()) { logger.warn( @@ -592,8 +592,7 @@ protected void fillAggregatedViaWithOrgIds(Organization org) throws EntityUpdate */ throw new EntityUpdateException("The organization's aggregatedVia field is still not set" + " and the update task need to be run again."); - } - else { + } else { org.setAggregatedVia(entityIds); } } diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseZohoAccess.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseZohoAccess.java index d92f547b..1a22f3bd 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseZohoAccess.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/BaseZohoAccess.java @@ -46,21 +46,21 @@ public class BaseZohoAccess { static final Logger logger = LogManager.getLogger(BaseZohoAccess.class); - final EntityRecordService entityRecordService; + protected final EntityRecordService entityRecordService; - final EntityUpdateService entityUpdateService; + protected final EntityUpdateService entityUpdateService; - final EntityManagementConfiguration emConfiguration; + protected final EntityManagementConfiguration emConfiguration; - final DataSources datasources; + protected final DataSources datasources; - final DataSource zohoDataSource; + protected final DataSource zohoDataSource; - final ZohoConfiguration zohoConfiguration; + protected final ZohoConfiguration zohoConfiguration; - final ZohoSyncRepository zohoSyncRepo; + protected final ZohoSyncRepository zohoSyncRepo; - final ZohoDereferenceService zohoDereferenceService; + protected final ZohoDereferenceService zohoDereferenceService; /** * Constructor for service initialization @@ -72,6 +72,7 @@ public class BaseZohoAccess { * @param zohoConfiguration zoho access configuration * @param solrService solr service * @param zohoSyncRepo repository for zoho sync logging + * @param zohoDereferenceService the service used to dereference zoho organizations */ public BaseZohoAccess(EntityRecordService entityRecordService, EntityUpdateService entityUpdateService, EntityManagementConfiguration emConfiguration, @@ -378,21 +379,16 @@ List performEntityRegistration(SortedSet createOperations, */ private Optional performEntityRegistration(Operation operation, ZohoSyncReport zohoSyncReport, List entitiesToUpdate) { - // Organization zohoOrganization=new Organization(); - // ZohoOrganizationConverter.fillOrganizationInfoFromZohoRecord(zohoOrganization, - // operation.getZohoRecord(), zohoConfiguration.getZohoBaseUrlOrganizations(), - // emConfiguration.getCountryMappings(), emConfiguration.getRoleMappings()); - // use dereference service to retrieve also aggregator info - Optional res = Optional.empty(); - // dereference organization + Optional res = Optional.empty(); Long zohoId = operation.getZohoRecord().getId(); Organization zohoOrganization = dereferenceFullOrganization(zohoId); if (zohoOrganization == null) { // should not happen, except for wrong configurations zohoSyncReport.addFailedOperation(zohoId.toString(), "Cannot dereference organization", "operation.getZohoRecord().getId() :" + zohoId, null); + return res; } // perform registration @@ -440,17 +436,14 @@ private Optional performEntityRegistration(Operation operation, Organization dereferenceFullOrganization(Long zohoId) { try { - Optional orgOptional = Optional.empty(); - orgOptional = zohoDereferenceService.dereferenceOrganizationByZohoRecordId(zohoId); + Optional orgOptional = zohoDereferenceService.dereferenceOrganizationByZohoRecordId(zohoId); if (orgOptional.isPresent()) { return (Organization) orgOptional.get(); - } else { - return null; - } + } } catch (Exception e) { logger.warn("Cannot dereference organization by zoho record id: {}", zohoId, e); - return null; } + return null; } List findDupplicateOrganization(Operation operation, diff --git a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java index 606a0b5d..e981cb0c 100644 --- a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java +++ b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java @@ -53,7 +53,7 @@ public class ZohoAccessClient { private static final Logger LOGGER = LogManager.getLogger(ZohoAccessClient.class); - + /** * Constructor with all parameters. * @@ -77,7 +77,7 @@ public class ZohoAccessClient { */ public ZohoAccessClient(TokenStore tokenStore, String zohoEmail, String clientId, String clientSecret, String refreshToken, String redirectUrl) throws ZohoException { - + try { UserSignature userSignature = new UserSignature(zohoEmail); Token token = @@ -111,110 +111,134 @@ public Optional getZohoOrganizationByUrl(String zohoUrl) throws ZohoExce String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, ZohoConstants.ID_FIELD, ZohoConstants.EQUALS_OPERATION, zohoId)); - APIResponse response = recordOperations.getRecord( - Long.valueOf(zohoId), ZohoConstants.ACCOUNTS_MODULE_API_NAME, null, null); + APIResponse response = recordOperations.getRecord(Long.valueOf(zohoId), + ZohoConstants.ACCOUNTS_MODULE_API_NAME, null, null); return getZohoRecords(response).stream().findFirst(); } catch (SDKException e) { - throw new ZohoException("Zoho search organization by organization id threw an exception", e); + throw convertToZohoException(e); } } - - public Optional searchZohoOrganizationByName(@NonNull String orgName) throws ZohoException { + + ZohoException convertToZohoException(SDKException e) { + return new ZohoException("Zoho search organization by organization id threw an exception", e); + } + + /** + * Search orgnaizations in Zoho by name + * + * @param orgName organization name + * @return zoho record as optional + * @throws ZohoException wrapping the original SDK exception + */ + public Optional searchZohoOrganizationByName(@NonNull String orgName) + throws ZohoException { try { RecordOperations recordOperations = new RecordOperations(); ParameterMap paramInstance = new ParameterMap(); paramInstance.add(SearchRecordsParam.CRITERIA, - String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, ZohoConstants.ACCOUNT_NAME_FIELD, - ZohoConstants.EQUALS_OPERATION, orgName)); + String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, + ZohoConstants.ACCOUNT_NAME_FIELD, ZohoConstants.EQUALS_OPERATION, orgName)); APIResponse response = recordOperations.searchRecords(ZohoConstants.ACCOUNTS_MODULE_API_NAME, paramInstance); List records = getZohoRecords(response); - for(Record rec : records) { - /* - * since the equals operator in zoho behaves like contains - * (https://www.zoho.com/crm/developer/docs/api/v7/search-records.html), - * we need to check the exact name + for (Record rec : records) { + /* + * since the equals operator in zoho behaves like contains + * (https://www.zoho.com/crm/developer/docs/api/v7/search-records.html), we need to check + * the exact name */ - String accountName = ZohoOrganizationConverter.getStringFieldValue(rec, ZohoConstants.ACCOUNT_NAME_FIELD); - if(orgName.equals(accountName)) { + String accountName = + ZohoOrganizationConverter.getStringFieldValue(rec, ZohoConstants.ACCOUNT_NAME_FIELD); + if (orgName.equals(accountName)) { return Optional.of(rec); } } } catch (SDKException e) { - throw new ZohoException("Zoho search organization by organization id threw an exception", e); + throw convertToZohoException(e); } return Optional.empty(); } /** * Zoho records can have additional information in the related records like e.g. products, notes, - * attachments, aggregators, etc. In this case we use this method to get the aggregator information. + * attachments, aggregators, etc. In this case we use this method to get the aggregator + * information. * - * @param zohoUrl - * @return - * @throws ZohoException + * @param zohoUrl the organization's URL in Zoho + * @return zoho records as optional + * @throws ZohoException wrapping the original SDK exception */ public Optional getZohoAggregatorByOrgUrl(String zohoUrl) throws ZohoException { String zohoId = EntityRecordUtils.getIdentifierFromUrl(zohoUrl); - try { - //Get instance of RelatedRecordsOperations class that takes relatedListAPIName moduleAPIName as parameter - RelatedRecordsOperations relatedRecordsOperations = new RelatedRecordsOperations(ZohoConstants.RELATED_RECORDS_MODULE_API_NAME, Long.valueOf(zohoId), ZohoConstants.ACCOUNTS_MODULE_API_NAME); - APIResponse response = relatedRecordsOperations.getRelatedRecords(null, null); + try { + // Get instance of RelatedRecordsOperations class that takes relatedListAPIName moduleAPIName + // as parameter + RelatedRecordsOperations relatedRecordsOperations = + new RelatedRecordsOperations(ZohoConstants.RELATED_RECORDS_MODULE_API_NAME, + Long.valueOf(zohoId), ZohoConstants.ACCOUNTS_MODULE_API_NAME); + APIResponse response = + relatedRecordsOperations.getRelatedRecords(null, null); return getZohoRecords(response).stream().findFirst(); } catch (SDKException e) { throw new ZohoException("Zoho get related records by organization id threw an exception", e); } } - + /** * Get the Linking record from the module for the aggregated_via/from (name: LinkingModule1). * - * @param zohoUrl - * @return - * @throws ZohoException + * @param orgName the name of the organization + * @param aggregatorName name of the aggragator + * @return zoho record as optional + * @throws ZohoException if zoho access fails */ - public Optional searchZohoAggregatedViaModule(@NonNull String orgName, @NonNull String aggregatorName) throws ZohoException { + public Optional searchZohoAggregatedViaModule(@NonNull String orgName, + @NonNull String aggregatorName) throws ZohoException { try { RecordOperations recordOperations = new RecordOperations(); ParameterMap paramInstance = new ParameterMap(); - String criteria = "("; - criteria += String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, ZohoConstants.AGGREGATING_FROM, - ZohoConstants.EQUALS_OPERATION, orgName); - criteria += ZohoConstants.AND; - criteria += String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, ZohoConstants.AGGREGATORS, - ZohoConstants.EQUALS_OPERATION, aggregatorName); - criteria += ")"; - paramInstance.add(SearchRecordsParam.CRITERIA, criteria); + StringBuilder criteria = new StringBuilder("(") + .append( + String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, ZohoConstants.AGGREGATING_FROM, ZohoConstants.EQUALS_OPERATION, orgName)) + .append(ZohoConstants.AND) + .append( + String.format(ZohoConstants.ZOHO_OPERATION_FORMAT_STRING, ZohoConstants.AGGREGATORS, ZohoConstants.EQUALS_OPERATION, aggregatorName)) + .append(")"); + paramInstance.add(SearchRecordsParam.CRITERIA, criteria.toString()); + + APIResponse response = recordOperations + .searchRecords(ZohoConstants.AGGREGATED_VIA_FROM_MODULE_API_NAME, paramInstance); - APIResponse response = - recordOperations.searchRecords(ZohoConstants.AGGREGATED_VIA_FROM_MODULE_API_NAME, paramInstance); - List records = getZohoRecords(response); - for(Record rec : records) { - /* - * since the equals operator in zoho behaves like contains - * (https://www.zoho.com/crm/developer/docs/api/v7/search-records.html), - * we need to check the exact values + for (Record rec : records) { + /* + * since the equals operator in zoho behaves like contains + * (https://www.zoho.com/crm/developer/docs/api/v7/search-records.html), we need to check + * the exact values */ - Record aggregatingFrom = ZohoOrganizationConverter.getSubRecord(rec, ZohoConstants.AGGREGATING_FROM); - String aggregatingFromName=null; - if(aggregatingFrom!=null) { - aggregatingFromName=ZohoOrganizationConverter.getStringFieldValue(aggregatingFrom, ZohoConstants.NAME_FIELD); + Record aggregatingFrom = + ZohoOrganizationConverter.getSubRecord(rec, ZohoConstants.AGGREGATING_FROM); + String aggregatingFromName = null; + if (aggregatingFrom != null) { + aggregatingFromName = ZohoOrganizationConverter.getStringFieldValue(aggregatingFrom, + ZohoConstants.NAME_FIELD); } - Record aggregatorsRecord = ZohoOrganizationConverter.getSubRecord(rec, ZohoConstants.AGGREGATORS); - String aggregatorsRecordName=null; - if(aggregatorsRecord!=null) { - aggregatorsRecordName=ZohoOrganizationConverter.getStringFieldValue(aggregatorsRecord, ZohoConstants.NAME_FIELD); + Record aggregatorsRecord = + ZohoOrganizationConverter.getSubRecord(rec, ZohoConstants.AGGREGATORS); + String aggregatorsRecordName = null; + if (aggregatorsRecord != null) { + aggregatorsRecordName = ZohoOrganizationConverter.getStringFieldValue(aggregatorsRecord, + ZohoConstants.NAME_FIELD); } - if(orgName.equals(aggregatingFromName) && aggregatorName.equals(aggregatorsRecordName)) { + if (orgName.equals(aggregatingFromName) && aggregatorName.equals(aggregatorsRecordName)) { return Optional.of(rec); } } } catch (SDKException e) { - throw new ZohoException("Zoho search organization by organization id threw an exception", e); + throw convertToZohoException(e); } return Optional.empty(); } @@ -230,7 +254,7 @@ public Optional searchZohoAggregatedViaModule(@NonNull String orgName, @ */ public boolean updateZohoRecordOrganizationStringField(String zohoUrl, String fieldName, String fieldValue) throws ZohoException { - + String zohoId = EntityRecordUtils.getIdentifierFromUrl(zohoUrl); try { RecordOperations recordOperations = new RecordOperations(); @@ -260,15 +284,15 @@ BodyWrapper buildUpdateRequest(String fieldName, String fieldValue) { /** * Source: https://www.zoho.com/crm/developer/docs/java-sdk/v2/record-samples.html - * - * @throws ZohoException + * @param response zoho response + * @throws ZohoException wrapping SDK exception */ private void validateZohoUpdateResponse(APIResponse response) throws ZohoException { if (response == null || !response.isExpected()) { // response is expected, if empty the update operation is not confirmed - throw new ZohoException( - "Unexpected response during updating a field in Zoho." + response.getStatusCode() + response.getObject()); + throw new ZohoException("Unexpected response during updating a field in Zoho." + + response.getStatusCode() + response.getObject()); } else { // Get object from response ActionHandler actionHandler = response.getObject(); @@ -277,7 +301,7 @@ private void validateZohoUpdateResponse(APIResponse response) throw new ZohoException(extractErrorMessage((APIException) actionHandler)); } else if (actionHandler instanceof ActionWrapper) { verifyZohoConfirmationResponse(actionHandler); - } + } } } @@ -302,9 +326,10 @@ else if (actionResponse instanceof APIException) { throw new ZohoException(message); } else { // - throw new ZohoException("Cannot process Zoho API Response, unknown response type: " + actionResponse); + throw new ZohoException( + "Cannot process Zoho API Response, unknown response type: " + actionResponse); } - + } } @@ -403,9 +428,10 @@ public List getZohoDeletedRecordOrganizations(OffsetDateTime modi paramInstance.add(GetDeletedRecordsParam.TYPE, "all"); // all, recycle, permanent paramInstance.add(GetDeletedRecordsParam.PAGE, 1); paramInstance.add(GetDeletedRecordsParam.PER_PAGE, pageSize); - Param scopeParam = new Param("scope", "com.zoho.crm.api.Record.GetDeletedRecordsParam"); + Param scopeParam = + new Param("scope", "com.zoho.crm.api.Record.GetDeletedRecordsParam"); paramInstance.add(scopeParam, "ZohoCRM.modules.ALL"); - + HeaderMap headersMap = new HeaderMap(); if (modifiedSince != null) { headersMap.add(GetRecordsHeader.IF_MODIFIED_SINCE, modifiedSince); @@ -439,9 +465,15 @@ private List getZohoDeletedRecords(APIResponse m) { return m == null || m.isEmpty(); } - - public static List getZohoRecords(APIResponse response) - throws ZohoException { + + /** + * Extract records form Zoho API Response + * @param the type of the record in API response + * @param response the zoho api response + * @return the list of extracted records + * @throws ZohoException wrapping SDK exception + */ + private static List getZohoRecords(APIResponse response) throws ZohoException { if (response == null) { return Collections.emptyList(); } @@ -459,17 +491,16 @@ public static List getZohoRecords(APIResponse response) // Get the object from response T responseHandler = response.getObject(); if (responseHandler instanceof com.zoho.crm.api.relatedrecords.ResponseWrapper) { - com.zoho.crm.api.relatedrecords.ResponseWrapper responseWrapper = + com.zoho.crm.api.relatedrecords.ResponseWrapper responseWrapper = (com.zoho.crm.api.relatedrecords.ResponseWrapper) responseHandler; return responseWrapper.getData(); - } - else if (responseHandler instanceof com.zoho.crm.api.record.ResponseWrapper) { - com.zoho.crm.api.record.ResponseWrapper responseWrapper = + } else if (responseHandler instanceof com.zoho.crm.api.record.ResponseWrapper) { + com.zoho.crm.api.record.ResponseWrapper responseWrapper = (com.zoho.crm.api.record.ResponseWrapper) responseHandler; return responseWrapper.getData(); } } return Collections.emptyList(); } - + } diff --git a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java index 2ac59f77..4f12671e 100644 --- a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java +++ b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java @@ -34,7 +34,13 @@ public ZohoDereferenceService(ZohoConfiguration zohoConfiguration, EntityManagem this.zohoConfiguration = zohoConfiguration; this.emConfig = emConfig; } - + /** + * Method to dereference organizations by zohoRecordId + * @see #dereferenceEntityById(String) + * @param zohoRecordId record id in zoho + * @return the dereferenced organization/aggregators as option + * @throws Exception if errors occur during dereferencing + */ public Optional dereferenceOrganizationByZohoRecordId(@NonNull Long zohoRecordId) throws Exception { String url = ZohoUtils.buildZohoRecordUrl(zohoConfiguration.getZohoBaseUrlOrganizations(), zohoRecordId); return dereferenceEntityById(url); @@ -51,18 +57,14 @@ public Optional dereferenceEntityById(@NonNull String url) throws Except Optional zohoAggregator = zohoConfiguration.getZohoAccessClient().getZohoAggregatorByOrgUrl(url); - //enable when you need to print the data for debuging purposes System.out.println(serialize(zohoOrganization.get())); - - Organization org = createOrganizationFromZohoRecords(zohoOrganization, zohoAggregator); + if(zohoOrganization.isEmpty()){ + return Optional.empty(); + } + //enable when you need to print the data for debuging purposes System.out.println(serialize(zohoOrganization.get())); + Organization org = createOrganizationFromZohoRecords(zohoOrganization.get(), zohoAggregator); fillAggregatedVia(org, zohoOrganization); - - if(org!=null) { - return Optional.of(org); - } - else { - return Optional.empty(); - } + return Optional.of(org); } public String serialize(Record zohoRecord) throws JsonProcessingException { @@ -77,22 +79,22 @@ public String serialize(Record zohoRecord) throws JsonProcessingException { return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(zohoRecord.getKeyValues()); } - private Organization createOrganizationFromZohoRecords(Optional zohoOrganization, + private Organization createOrganizationFromZohoRecords(Record zohoOrganization, Optional zohoAggregator) { - Organization org=null; - if(zohoOrganization.isPresent()) { - if(zohoAggregator.isPresent()) { + + if(zohoOrganization == null){ + return null; + } + boolean isAggregator = zohoAggregator.isPresent(); + Organization org= isAggregator? new Aggregator() : new Organization(); + if(isAggregator) { //fill aggregator properties - org = new Aggregator(); ZohoOrganizationConverter.fillAggregatorInfoFromZohoRecord((Aggregator)org, zohoAggregator.get(), zohoConfiguration.getZohoBaseUrlAggregators()); - } - else { - org=new Organization(); - } - //fill common organization properties - ZohoOrganizationConverter.fillOrganizationInfoFromZohoRecord(org, zohoOrganization.get(), - zohoConfiguration.getZohoBaseUrlOrganizations(), emConfig.getCountryMappings(), emConfig.getRoleMappings()); } + //fill common organization properties + ZohoOrganizationConverter.fillOrganizationInfoFromZohoRecord(org, zohoOrganization, + zohoConfiguration.getZohoBaseUrlOrganizations(), emConfig.getCountryMappings(), emConfig.getRoleMappings()); + return org; } @@ -100,47 +102,56 @@ private void fillAggregatedVia(Organization org, Optional zohoOrganizati if(org!=null && zohoOrganization.isPresent()) { String aggregName = ZohoOrganizationConverter.getStringFieldValue(zohoOrganization.get(), ZohoConstants.AGGREGATORS); String orgName = ZohoOrganizationConverter.getStringFieldValue(zohoOrganization.get(), ZohoConstants.ACCOUNT_NAME_FIELD); + if(StringUtils.isBlank(aggregName) || StringUtils.isBlank(orgName)) { + //organization has no aggregator, nothing to do + return; + } + //if the organization has aggregator - if(StringUtils.isNotBlank(orgName) && StringUtils.isNotBlank(aggregName)) { /* * search zoho organization that has the same name as the aggregator, * and get its europeana id, to store in the aggregatedVia */ - Optional zohoAggregatorOrg = - zohoConfiguration.getZohoAccessClient().searchZohoOrganizationByName(aggregName); - if(zohoAggregatorOrg.isPresent()) { - String europeanaId = ZohoOrganizationConverter.getStringFieldValue(zohoAggregatorOrg.get(), ZohoConstants.EUROPEANA_ID_FIELD); - if(europeanaId!=null) { - List aggregatedVia = new ArrayList(); - aggregatedVia.add(europeanaId); - org.setAggregatedVia(aggregatedVia); - } - } + fillAggregatedViaByAggregatorName(org, aggregName); //if the aggregatedVia field is still not set if(org.getAggregatedVia()==null) { - String aggregatorUrl=null; //search the aggregatedVia/From zoho module for the aggregator id - Optional zohoLinking = - zohoConfiguration.getZohoAccessClient().searchZohoAggregatedViaModule(orgName, aggregName); - if(zohoLinking.isPresent()) { - Record aggregRecord = ZohoOrganizationConverter.getSubRecord(zohoLinking.get(), ZohoConstants.AGGREGATORS); - if(aggregRecord != null) { - aggregatorUrl = ZohoUtils.buildZohoRecordUrl(zohoConfiguration.getZohoBaseUrlAggregators(), aggregRecord.getId()); - } - } - if(aggregatorUrl!=null) { - org.setAggregatedViaAggregatorUrls(List.of(aggregatorUrl)); - } - else { - /* - * in this case since the organization is aggregator and we cannot set the aggregatedVia field, - * we throw an exception in order to execute the org update task later on again - */ - throw new ZohoException("Could not set the aggregatedVia field for the Zoho organization which has its aggregator."); - } + fillAggregatedViaByZohoModule(org, aggregName, orgName); } - } } } + void fillAggregatedViaByZohoModule(Organization org, String aggregName, String orgName) + throws ZohoException { + String aggregatorUrl=null; + Optional zohoLinking = + zohoConfiguration.getZohoAccessClient().searchZohoAggregatedViaModule(orgName, aggregName); + if(zohoLinking.isPresent()) { + Record aggregRecord = ZohoOrganizationConverter.getSubRecord(zohoLinking.get(), ZohoConstants.AGGREGATORS); + if(aggregRecord != null) { + aggregatorUrl = ZohoUtils.buildZohoRecordUrl(zohoConfiguration.getZohoBaseUrlAggregators(), aggregRecord.getId()); + } + } + if(aggregatorUrl==null) { + /* + * in this case since the organization is aggregator and we cannot set the aggregatedVia field, + * we throw an exception in order to execute the org update task later on again + */ + throw new ZohoException("Could not set the aggregatedVia field for the Zoho organization which has its aggregator."); + } + org.setAggregatedViaAggregatorUrls(List.of(aggregatorUrl)); + } + + void fillAggregatedViaByAggregatorName(Organization org, String aggregName) throws ZohoException { + Optional zohoAggregatorOrg = + zohoConfiguration.getZohoAccessClient().searchZohoOrganizationByName(aggregName); + if(zohoAggregatorOrg.isPresent()) { + String europeanaId = ZohoOrganizationConverter.getStringFieldValue(zohoAggregatorOrg.get(), ZohoConstants.EUROPEANA_ID_FIELD); + if(europeanaId!=null) { + List aggregatedVia = new ArrayList(); + aggregatedVia.add(europeanaId); + org.setAggregatedVia(aggregatedVia); + } + } + } }