Adds Project external dynamic configuration properties.

This commit is contained in:
gkolokythas 2019-08-02 18:25:39 +03:00
parent 5dcf8397df
commit beb7744e43
8 changed files with 463 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package eu.eudat.configurations.dynamicproject;
import eu.eudat.configurations.dynamicgrant.entities.Configuration;
import eu.eudat.models.data.dynamicfields.DynamicField;
import java.util.List;
public interface DynamicProjectConfiguration {
Configuration getConfiguration();
List<DynamicField> getFields();
}

View File

@ -0,0 +1,85 @@
package eu.eudat.configurations.dynamicproject;
import eu.eudat.configurations.dynamicgrant.entities.Configuration;
import eu.eudat.configurations.dynamicgrant.entities.Property;
import eu.eudat.models.data.dynamicfields.Dependency;
import eu.eudat.models.data.dynamicfields.DynamicField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
@Service("dynamicProjectConfiguration")
@Profile("devel")
public class DynamicProjectConfigurationDevelImpl implements DynamicProjectConfiguration{
private Configuration configuration;
private List<DynamicField> fields;
private Environment environment;
@Autowired
public DynamicProjectConfigurationDevelImpl(Environment environment) {
this.environment = environment;
}
@Override
public Configuration getConfiguration() {
if (this.configuration != null) return this.configuration;
String fileUrl = this.environment.getProperty("configuration.dynamicProjectUrl");
System.out.println("Loaded also config file: " + fileUrl);
String current = null;
InputStream is = null;
try {
current = new java.io.File(".").getCanonicalPath();
JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
is = new URL("file:///"+current + "/web/src/main/resources/ProjectConfiguration.xml").openStream();
this.configuration = (Configuration) jaxbUnmarshaller.unmarshal(is);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Cannot find in folder" + current);
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl);
}
}
return this.configuration;
}
@Override
public List<DynamicField> getFields() {
if (this.fields != null) return this.fields;
Configuration configuration = this.getConfiguration();
List<DynamicField> fields = new LinkedList<>();
List<Property> properties = configuration.getConfigurationProperties();
properties.stream().forEach(item -> {
DynamicField dynamicField = new DynamicField();
dynamicField.setId(item.getId());
dynamicField.setName(item.getName());
dynamicField.setQueryProperty(item.getQueryProperty());
dynamicField.setRequired(item.getRequired());
List<Dependency> dependencies = new LinkedList<>();
item.getDependencies().stream().forEach(dependency -> {
Dependency modelDependency = new Dependency();
modelDependency.setId(dependency.getId());
modelDependency.setQueryProperty(dependency.getQueryProperty());
dependencies.add(modelDependency);
});
dynamicField.setDependencies(dependencies);
fields.add(dynamicField);
});
this.fields = fields;
return fields;
}
}

View File

@ -0,0 +1,88 @@
package eu.eudat.configurations.dynamicproject;
import eu.eudat.configurations.dynamicgrant.entities.Configuration;
import eu.eudat.configurations.dynamicgrant.entities.Property;
import eu.eudat.models.data.dynamicfields.Dependency;
import eu.eudat.models.data.dynamicfields.DynamicField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
@Service("dynamicProjectConfiguration")
@Profile({ "production", "staging" })
public class DynamicProjectConfigurationProdImpl implements DynamicProjectConfiguration{
private Configuration configuration;
private List<DynamicField> fields;
private Environment environment;
@Autowired
public DynamicProjectConfigurationProdImpl(Environment environment) {
this.environment = environment;
}
@Override
public Configuration getConfiguration() {
if (this.configuration != null) return this.configuration;
String fileUrl = this.environment.getProperty("configuration.dynamicProjectUrl");
System.out.println("Loaded also config file: " + fileUrl);
String current = null;
InputStream is = null;
try {
current = new java.io.File(".").getCanonicalPath();
JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
is = new URL(Paths.get(fileUrl).toUri().toURL().toString()).openStream();
this.configuration = (Configuration) jaxbUnmarshaller.unmarshal(is);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Cannot find in folder" + current);
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl);
}
}
return this.configuration;
}
@Override
public List<DynamicField> getFields() {
if (this.fields != null) return this.fields;
Configuration configuration = this.getConfiguration();
List<DynamicField> fields = new LinkedList<>();
List<Property> properties = configuration.getConfigurationProperties();
properties.stream().forEach(item -> {
DynamicField dynamicField = new DynamicField();
dynamicField.setId(item.getId());
dynamicField.setName(item.getName());
dynamicField.setQueryProperty(item.getQueryProperty());
dynamicField.setRequired(item.getRequired());
List<Dependency> dependencies = new LinkedList<>();
item.getDependencies().stream().forEach(dependency -> {
Dependency modelDependency = new Dependency();
modelDependency.setId(dependency.getId());
modelDependency.setQueryProperty(dependency.getQueryProperty());
dependencies.add(modelDependency);
});
dynamicField.setDependencies(dependencies);
fields.add(dynamicField);
});
this.fields = fields;
return fields;
}
}

View File

@ -0,0 +1,32 @@
package eu.eudat.configurations.dynamicproject.entities;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement
public class Configuration {
private List<Property> configurationProperties;
private MainProperty mainExternalField;
public MainProperty getMainExternalField() {
return mainExternalField;
}
@XmlElement(name = "mainExternalField")
public void setMainExternalField(MainProperty mainExternalField) {
this.mainExternalField = mainExternalField;
}
public List<Property> getConfigurationProperties() {
return configurationProperties;
}
@XmlElementWrapper
@XmlElement(name = "property")
public void setConfigurationProperties(List<Property> configurationProperties) {
this.configurationProperties = configurationProperties;
}
}

View File

@ -0,0 +1,26 @@
package eu.eudat.configurations.dynamicproject.entities;
import javax.xml.bind.annotation.XmlElement;
public class Dependency {
private String id;
private String queryProperty;
public String getId() {
return id;
}
public String getQueryProperty() {
return queryProperty;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "queryProperty")
public void setQueryProperty(String queryProperty) {
this.queryProperty = queryProperty;
}
}

View File

@ -0,0 +1,26 @@
package eu.eudat.configurations.dynamicproject.entities;
import javax.xml.bind.annotation.XmlElement;
public class Language {
private String key;
private String languageKey;
public String getKey() {
return key;
}
@XmlElement(name = "key")
public void setKey(String key) {
this.key = key;
}
public String getLanguageKey() {
return languageKey;
}
@XmlElement(name = "languageKey")
public void setLanguageKey(String languageKey) {
this.languageKey = languageKey;
}
}

View File

@ -0,0 +1,103 @@
package eu.eudat.configurations.dynamicproject.entities;
import eu.eudat.logic.proxy.config.UrlConfiguration;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import java.util.List;
public class MainProperty {
private String id;
private String name;
private String queryProperty;
private String externalFieldId;
private UrlConfiguration urlConfig;
private String externalFieldLabel;
private List<Dependency> dependencies;
private Boolean required;
private List<Language> language;
public String getId() {
return id;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement(name = "name")
public void setName(String name) {
this.name = name;
}
public String getExternalFieldId() {
return externalFieldId;
}
@XmlElement(name = "externalFieldId")
public void setExternalFieldId(String externalFieldId) {
this.externalFieldId = externalFieldId;
}
public String getExternalFieldLabel() {
return externalFieldLabel;
}
@XmlElement(name = "externalFieldLabel")
public void setExternalFieldLabel(String externalFieldLabel) {
this.externalFieldLabel = externalFieldLabel;
}
public List<Dependency> getDependencies() {
return dependencies;
}
@XmlElementWrapper
@XmlElement(name = "dependency")
public void setDependencies(List<Dependency> dependencies) {
this.dependencies = dependencies;
}
public Boolean getRequired() {
return required;
}
@XmlElement(name = "required")
public void setRequired(Boolean required) {
this.required = required;
}
public String getQueryProperty() {
return queryProperty;
}
@XmlElement(name = "queryProperty")
public void setQueryProperty(String queryProperty) {
this.queryProperty = queryProperty;
}
public UrlConfiguration getUrlConfig() {
return urlConfig;
}
@XmlElement(name = "urlConfig")
public void setUrlConfig(UrlConfiguration urlConfig) {
this.urlConfig = urlConfig;
}
public List<Language> getLanguage() {
return language;
}
@XmlElementWrapper
@XmlElement(name = "languageProperty")
public void setLanguage(List<Language> language) {
this.language = language;
}
}

View File

@ -0,0 +1,91 @@
package eu.eudat.configurations.dynamicproject.entities;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import java.util.List;
public class Property {
private String id;
private String name;
private String sourceUrl;
private String queryProperty;
private String externalFieldId;
private String externalFieldLabel;
private List<Dependency> dependencies;
private Boolean required;
public String getId() {
return id;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement(name = "name")
public void setName(String name) {
this.name = name;
}
public String getSourceUrl() {
return sourceUrl;
}
@XmlElement(name = "sourceUrl")
public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}
public String getExternalFieldId() {
return externalFieldId;
}
@XmlElement(name = "externalFieldId")
public void setExternalFieldId(String externalFieldId) {
this.externalFieldId = externalFieldId;
}
public String getExternalFieldLabel() {
return externalFieldLabel;
}
@XmlElement(name = "externalFieldLabel")
public void setExternalFieldLabel(String externalFieldLabel) {
this.externalFieldLabel = externalFieldLabel;
}
public List<Dependency> getDependencies() {
return dependencies;
}
@XmlElementWrapper
@XmlElement(name = "dependency")
public void setDependencies(List<Dependency> dependencies) {
this.dependencies = dependencies;
}
public Boolean getRequired() {
return required;
}
@XmlElement(name = "required")
public void setRequired(Boolean required) {
this.required = required;
}
public String getQueryProperty() {
return queryProperty;
}
@XmlElement(name = "queryProperty")
public void setQueryProperty(String queryProperty) {
this.queryProperty = queryProperty;
}
}