2019-07-31 16:57:34 +02:00
|
|
|
package eu.eudat.configurations.dynamicgrant;
|
2018-03-28 15:24:47 +02:00
|
|
|
|
2019-07-31 16:57:34 +02:00
|
|
|
import eu.eudat.configurations.dynamicgrant.entities.Configuration;
|
|
|
|
import eu.eudat.configurations.dynamicgrant.entities.Property;
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.models.data.dynamicfields.Dependency;
|
|
|
|
import eu.eudat.models.data.dynamicfields.DynamicField;
|
2018-03-28 15:24:47 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-05-14 08:44:35 +02:00
|
|
|
import org.springframework.context.annotation.Profile;
|
2018-03-28 15:24:47 +02:00
|
|
|
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;
|
2018-07-11 15:47:36 +02:00
|
|
|
import java.nio.file.Paths;
|
2018-03-28 15:24:47 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 3/23/2018.
|
|
|
|
*/
|
2019-07-31 16:57:34 +02:00
|
|
|
@Service("dynamicGrantConfiguration")
|
2018-11-27 15:13:56 +01:00
|
|
|
@Profile({ "production", "staging" })
|
2019-07-31 16:57:34 +02:00
|
|
|
public class DynamicGrantConfigurationProdImpl implements DynamicGrantConfiguration {
|
2018-03-28 15:24:47 +02:00
|
|
|
|
|
|
|
private Configuration configuration;
|
|
|
|
|
|
|
|
private List<DynamicField> fields;
|
|
|
|
|
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
@Autowired
|
2019-07-31 16:57:34 +02:00
|
|
|
public DynamicGrantConfigurationProdImpl(Environment environment) {
|
2018-03-28 15:24:47 +02:00
|
|
|
this.environment = environment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Configuration getConfiguration() {
|
|
|
|
if (this.configuration != null) return this.configuration;
|
2019-07-31 16:57:34 +02:00
|
|
|
String fileUrl = this.environment.getProperty("configuration.dynamicGrantUrl");
|
2018-03-28 15:24:47 +02:00
|
|
|
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();
|
2018-07-11 15:47:36 +02:00
|
|
|
is = new URL(Paths.get(fileUrl).toUri().toURL().toString()).openStream();
|
2018-03-28 15:24:47 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|