Skip to content

Commit

Permalink
Merge pull request #357 from edmcouncil/348-refactoring-the-configura…
Browse files Browse the repository at this point in the history
…tion-loading-code-in-onto-viewer

348 Refactoring the configuration loading code in onto-viewer
  • Loading branch information
mereolog authored Oct 12, 2023
2 parents 0b21778 + 8604a98 commit 1c9d08c
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 227 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,7 @@ onto-viewer-web-app/config/*
onto-viewer-web-app/ontologies/*

# Mac
.DS_Store
.DS_Store

#VS CODE
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.edmcouncil.spec.ontoviewer.configloader.configuration.model.exception;

public class UnableToLoadConfigurationException extends Exception {

public UnableToLoadConfigurationException(String message) {
super(message);
}

public UnableToLoadConfigurationException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public class AppProperties {
private String defaultHomePath;
private String viewerConfigFileName;
private String defaultOntologyFileName;
private String configPath;
private String configDownloadPath;
private Map<String, Object> search;
private String fallbackUrl;

public String getConfigPath() {
return configPath;
public String getConfigDownloadPath() {
return configDownloadPath;
}

public void setConfigPath(String configPath) {
this.configPath = configPath;
public void setConfigDownloadPath(String configPath) {
this.configDownloadPath = configPath;
}

public String getDefaultHomePath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.*;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.ApplicationConfig;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.GroupsConfig;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.Integration;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.IntegrationsConfig;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.LabelConfig;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.OntologiesConfig;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData.SearchConfig;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationKey;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.FindProperty;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.LabelPriority;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.MissingLanguageAction;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.Pair;
import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.UserDefaultName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
Expand All @@ -29,7 +36,8 @@

public abstract class AbstractYamlConfigurationService implements ApplicationConfigurationService {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractYamlConfigurationService.class);
private static final Logger LOGGER =
LoggerFactory.getLogger(AbstractYamlConfigurationService.class);
private static final int FUZZY_DISTANCE_DEFAULT = 3;
private static final boolean REINDEX_ON_START_DEFAULT = false;
private static final boolean DISPLAY_LABEL_DEFAULT = true;
Expand All @@ -43,21 +51,18 @@ public abstract class AbstractYamlConfigurationService implements ApplicationCon
private static final boolean DISPLAY_QNAME_DEFAULT = true;

@Override
public void init() {
public void init() throws IOException {
// Default empty implementation
}

@Override
public void reloadConfiguration() {

}
public void reloadConfiguration() throws IOException {}

protected ConfigurationData readDefaultConfiguration() {
String defaultConfigContent = readDefaultConfigContent();

var yaml = new Yaml();
Map<String, Object> defaultConfiguration = yaml.load(defaultConfigContent);

var configuration = new ConfigurationData();

for (Entry<String, Object> entry : defaultConfiguration.entrySet()) {
Expand Down Expand Up @@ -90,7 +95,8 @@ protected ConfigurationData readDefaultConfiguration() {
break;
case ONTOLOGIES:
@SuppressWarnings("unchecked")
var ontologiesConfig = handleOntologyConfig((Map<String, Object>) entry.getValue(), new OntologiesConfig());
var ontologiesConfig =
handleOntologyConfig((Map<String, Object>) entry.getValue(), new OntologiesConfig());
configuration.setOntologiesConfig(ontologiesConfig);
break;
case INTEGRATIONS:
Expand All @@ -117,8 +123,7 @@ private String readDefaultConfigContent() {
if (resource == null) {
continue;
}
sb.append(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8))
.append("\n");
sb.append(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8)).append("\n");
}
} catch (IOException ex) {
throw new IllegalStateException("Exception thrown while reading default configuration", ex);
Expand All @@ -127,6 +132,29 @@ private String readDefaultConfigContent() {
return sb.toString();
}

protected String readDefaultConfigContent(String fileName) {
StringBuilder sb = new StringBuilder();

try {
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = patternResolver.getResources("classpath*:/default_*_config.yaml");
String defaultConfigName = "default_" + fileName;
for (Resource resource : resources) {
if (resource == null) {
continue;
}
if (resource.getFilename().equalsIgnoreCase(defaultConfigName)) {
sb.append(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8))
.append("\n");
}
}
return sb.toString();
} catch (IOException ex) {
throw new IllegalStateException("Exception thrown while reading default configuration", ex);
}

}

protected GroupsConfig handleGroupsConfig(Map<String, Object> groupsConfig) {
Map<String, List<String>> groups = new HashMap<>();

Expand Down Expand Up @@ -154,25 +182,30 @@ protected ApplicationConfig handleApplicationConfig(Map<String, Object> applicat
boolean displayLicense = getBooleanFromObject(displayLicenseObject, DISPLAY_LICENSE_DEFAULT);

var displayCopyrightObject = applicationConfig.get(DISPLAY_COPYRIGHT.getLabel());
boolean displayCopyright = getBooleanFromObject(displayCopyrightObject, DISPLAY_COPYRIGHT_DEFAULT);
boolean displayCopyright =
getBooleanFromObject(displayCopyrightObject, DISPLAY_COPYRIGHT_DEFAULT);

var displayQNameObject = applicationConfig.get(DISPLAY_QNAME.getLabel());
boolean displayQName = getBooleanFromObject(displayQNameObject, DISPLAY_QNAME_DEFAULT);

return new ApplicationConfig(license, copyright, displayLicense, displayCopyright, displayQName);
return new ApplicationConfig(
license, copyright, displayLicense, displayCopyright, displayQName);
}

protected LabelConfig handleLabelConfig(Map<String, Object> labelConfig) {
var displayLabelObject = labelConfig.get(DISPLAY_LABEL.getLabel());
boolean displayLabel = getBooleanFromObject(displayLabelObject, DISPLAY_LABEL_DEFAULT);

var labelPriorityObject = labelConfig.get(LABEL_PRIORITY.getLabel());
String labelPriorityString = labelPriorityObject != null ? labelPriorityObject.toString() : LABEL_PRIORITY_DEFAULT;
String labelPriorityString =
labelPriorityObject != null ? labelPriorityObject.toString() : LABEL_PRIORITY_DEFAULT;
LabelPriority labelPriority = LabelPriority.USER_DEFINED;
try {
labelPriority = LabelPriority.valueOf(labelPriorityString);
} catch (IllegalArgumentException ex) {
LOGGER.warn("'{}' string is not a valid value for label priority. Using the default value: USER_DEFINED",
LOGGER.warn(
"'{}' string is not a valid value for label priority. Using the default value:"
+ " USER_DEFINED",
labelPriorityString);
}

Expand All @@ -184,12 +217,16 @@ protected LabelConfig handleLabelConfig(Map<String, Object> labelConfig) {

var missingLanguageActionObject = labelConfig.get(MISSING_LANGUAGE_ACTION.getLabel());
String missingLanguageActionString =
missingLanguageActionObject != null ? missingLanguageActionObject.toString() : MISSING_LANGUAGE_ACTION_DEFAULT;
missingLanguageActionObject != null
? missingLanguageActionObject.toString()
: MISSING_LANGUAGE_ACTION_DEFAULT;
MissingLanguageAction missingLanguageAction = MissingLanguageAction.FIRST;
try {
missingLanguageAction = MissingLanguageAction.valueOf(missingLanguageActionString);
} catch (IllegalArgumentException ex) {
LOGGER.warn("'{}' string is not a valid value for missing language action. Using the default value: FIRST",
LOGGER.warn(
"'{}' string is not a valid value for missing language action. Using the default value:"
+ " FIRST",
missingLanguageActionString);
}

Expand Down Expand Up @@ -221,7 +258,8 @@ protected SearchConfig handleSearchConfig(Map<String, Object> searchConfig) {
return new SearchConfig(searchDescriptions, fuzzyDistance, reindexOnStart, findProperties);
}

protected OntologiesConfig handleOntologyConfig(Map<String, Object> ontologies, OntologiesConfig ontologiesConfig) {
protected OntologiesConfig handleOntologyConfig(
Map<String, Object> ontologies, OntologiesConfig ontologiesConfig) {
List<String> urls = new ArrayList<>();
List<String> paths = new ArrayList<>();
List<String> zips = new ArrayList<>();
Expand All @@ -240,34 +278,52 @@ protected OntologiesConfig handleOntologyConfig(Map<String, Object> ontologies,
} else if (sourceEntryKey.equals(ConfigurationKey.ZIP.getLabel())) {
zips.add(sourceEntry.getValue());
} else {
LOGGER.warn("Unknown key '{}' with value '{}' in the ontologies source configuration.",
sourceEntry.getKey(), sourceEntry.getValue());
LOGGER.warn(
"Unknown key '{}' with value '{}' in the ontologies source configuration.",
sourceEntry.getKey(),
sourceEntry.getValue());
}
}
}
}

List<String> catalogPaths = getListOfStringsFromObject(ontologies.get(CATALOG_PATH.getLabel()));
List<String> moduleIgnorePatterns = getListOfStringsFromObject(ontologies.get(MODULE_IGNORE_PATTERN.getLabel()));
List<String> moduleToIgnore = getListOfStringsFromObject(ontologies.get(MODULE_TO_IGNORE.getLabel()));
List<String> moduleIgnorePatterns =
getListOfStringsFromObject(ontologies.get(MODULE_IGNORE_PATTERN.getLabel()));
List<String> moduleToIgnore =
getListOfStringsFromObject(ontologies.get(MODULE_TO_IGNORE.getLabel()));
boolean automaticCreationOfModules =
getBooleanFromObject(ontologies.get(AUTOMATIC_CREATION_OF_MODULES.getLabel()),
getBooleanFromObject(
ontologies.get(AUTOMATIC_CREATION_OF_MODULES.getLabel()),
AUTOMATIC_CREATION_OF_MODULES_DEFULT);
List<String> downloadDirectory = getListOfStringsFromObject(ontologies.get(DOWNLOAD_DIRECTORY.getLabel()));
String moduleClassIri = getStringsFromObject(
ontologies.get(MODULE_CLASS_IRI.getLabel()),
ontologiesConfig.getModuleClassIri());
List<Pair> maturityLevelDefinition = getMaturityLevelDefinitionNameList(
ontologies.get(MATURITY_LEVEL_DEFINITION.getLabel()));
String maturityLevelProperty = getStringsFromObject(
ontologies.get(MATURITY_LEVEL_PROPERTY.getLabel()),
ontologiesConfig.getMaturityLevelProperty());

return new OntologiesConfig(urls, paths, catalogPaths, downloadDirectory, zips, moduleIgnorePatterns,
moduleToIgnore, maturityLevelDefinition, automaticCreationOfModules, moduleClassIri, maturityLevelProperty);
List<String> downloadDirectory =
getListOfStringsFromObject(ontologies.get(DOWNLOAD_DIRECTORY.getLabel()));
String moduleClassIri =
getStringsFromObject(
ontologies.get(MODULE_CLASS_IRI.getLabel()), ontologiesConfig.getModuleClassIri());
List<Pair> maturityLevelDefinition =
getMaturityLevelDefinitionNameList(ontologies.get(MATURITY_LEVEL_DEFINITION.getLabel()));
String maturityLevelProperty =
getStringsFromObject(
ontologies.get(MATURITY_LEVEL_PROPERTY.getLabel()),
ontologiesConfig.getMaturityLevelProperty());

return new OntologiesConfig(
urls,
paths,
catalogPaths,
downloadDirectory,
zips,
moduleIgnorePatterns,
moduleToIgnore,
maturityLevelDefinition,
automaticCreationOfModules,
moduleClassIri,
maturityLevelProperty);
}

protected IntegrationsConfig handleIntegrationsConfig(List<Map<String, Object>> integrationsList) {
protected IntegrationsConfig handleIntegrationsConfig(
List<Map<String, Object>> integrationsList) {
Map<String, Integration> integrations = new HashMap<>();
if (integrationsList == null) {
return new IntegrationsConfig(integrations);
Expand All @@ -279,20 +335,23 @@ protected IntegrationsConfig handleIntegrationsConfig(List<Map<String, Object>>
var integrationAccessToken = integrationMap.get(INTEGRATION_ACCESS_TOKEN.getLabel());

if (integrationId == null || integrationUrl == null) {
LOGGER.error("Missing data while reading integrations config: "
+ "integrationId={}, integrationUrl={}",
integrationId, integrationUrl);
LOGGER.error(
"Missing data while reading integrations config: "
+ "integrationId={}, integrationUrl={}",
integrationId,
integrationUrl);
continue;
}

if (integrationAccessToken == null) {
integrationAccessToken = "";
}

var integration = new Integration(
integrationId.toString(),
integrationUrl.toString(),
integrationAccessToken.toString());
var integration =
new Integration(
integrationId.toString(),
integrationUrl.toString(),
integrationAccessToken.toString());
integrations.put(integrationId.toString(), integration);
}

Expand Down Expand Up @@ -329,14 +388,15 @@ protected Map<String, List<String>> mapToMapOfList(List<?> rawGroupsList) {
var groupItems = groupItem.getOrDefault(ITEMS.getLabel(), new ArrayList<>());
if (groupItems instanceof List) {
var rawGroupItemsList = (List<?>) groupItems;
var groupItemsList = rawGroupItemsList.stream()
.map(Object::toString)
.collect(Collectors.toList());
var groupItemsList =
rawGroupItemsList.stream().map(Object::toString).collect(Collectors.toList());
groupsMap.put(groupName.toString(), groupItemsList);
}
} else {
LOGGER.warn("For a group list item the expected key '{}' was not present. Item details: {}",
NAME.getLabel(), groupItem);
LOGGER.warn(
"For a group list item the expected key '{}' was not present. Item details: {}",
NAME.getLabel(),
groupItem);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.edmcouncil.spec.ontoviewer.configloader.configuration.service;

import java.io.IOException;

import org.edmcouncil.spec.ontoviewer.configloader.configuration.model.ConfigurationData;

public interface ApplicationConfigurationService {

void init();
void init() throws IOException;

ConfigurationData getConfigurationData();

boolean hasConfiguredGroups();

void reloadConfiguration();
void reloadConfiguration() throws IOException;
}
Loading

0 comments on commit 1c9d08c

Please sign in to comment.