code clean up

This commit is contained in:
Efstratios Giannopoulos 2024-04-10 15:16:15 +03:00
parent 5a6ab32768
commit a5160d3ecd
11 changed files with 0 additions and 361 deletions

View File

@ -1,27 +0,0 @@
package eu.eudat.controllers.old;
import eu.eudat.logic.managers.LocalFetchManager;
import eu.eudat.models.old.local.LocalFetchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping(value = "api/currency")
public class CurrencyController {
private LocalFetchManager localFetchManager;
@Autowired
public CurrencyController(LocalFetchManager localFetchManager) {
this.localFetchManager = localFetchManager;
}
@RequestMapping(method = RequestMethod.GET)
public List<LocalFetchModel> getCurrencies( @RequestParam(value = "query", required = false) String query) throws Exception {
List<LocalFetchModel> currencies = localFetchManager.getCurrency(query);
return currencies;
}
}

View File

@ -1,28 +0,0 @@
package eu.eudat.logic.managers;
import eu.eudat.logic.proxy.fetching.LocalFetcher;
import eu.eudat.utilities.helpers.StreamDistinctBy;
import eu.eudat.models.old.local.LocalFetchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class LocalFetchManager {
private LocalFetcher localFetcher;
@Autowired
public LocalFetchManager(LocalFetcher localFetcher) {
this.localFetcher = localFetcher;
}
public List<LocalFetchModel> getCurrency(String query) throws Exception {
List<Map<String, String>> data = localFetcher.retrieveCurrency();
List<LocalFetchModel> result = data.stream().map(entry -> new LocalFetchModel(entry.get("name"), entry.get("value"))).collect(Collectors.toList());
result = result.stream().filter(localFetchModel -> localFetchModel.getValue() != null).filter(StreamDistinctBy.distinctByKey(LocalFetchModel::getValue)).filter(localFetchModel -> localFetchModel.getName().toLowerCase().contains(query.toLowerCase())).collect(Collectors.toList());
return result;
}
}

View File

@ -1,100 +0,0 @@
package eu.eudat.logic.proxy.fetching;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.logic.proxy.fetching.entities.Config;
import eu.eudat.logic.proxy.fetching.entities.ConfigSingle;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.*;
@Component
public class LocalFetcher {
@Cacheable("currencies")
public List<Map<String, String>> retrieveCurrency() throws Exception {
InputStream is = getClass().getClassLoader().getResource("internal/fetchConfig.xml").openStream();
JAXBContext context = JAXBContext.newInstance(Config.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Config config = (Config) unmarshaller.unmarshal(is);
is.close();
ConfigSingle currencyConfig = config.getConfigs().stream().filter(configSingle -> configSingle.getType().equals("currency")).findFirst().get();
return retrieveData(currencyConfig);
}
public List<Map<String, String>> retrieveData(ConfigSingle configSingle) throws Exception {
List<Map<String, String>> result = new ArrayList<>();
InputStream is = getClass().getClassLoader().getResource(configSingle.getFilePath()).openStream();
FileType type = FileType.fromName(configSingle.getFileType());
switch(type) {
case XML:
{
Class<?> aClass = Class.forName(configSingle.getParseClass());
JAXBContext context = JAXBContext.newInstance(aClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object object = unmarshaller.unmarshal(is);
is.close();
Method reader = null;
if (configSingle.getParseField() != null && !configSingle.getParseField().isEmpty()) {
String camelCaseGetter =configSingle.getParseField() != null && !configSingle.getParseField().isEmpty() ? "get" + configSingle.getParseField().substring(0, 1).toUpperCase() + configSingle.getParseField().substring(1) : "";
reader = aClass.getMethod(camelCaseGetter);
}
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, String>> values = new ArrayList<>();
int max = 1;
if (reader != null) {
Object invokedField = reader.invoke(object);
if (invokedField instanceof Collection) {
max = ((Collection) invokedField).size();
}
}
for (int i = 0; i< max; i++) {
Object value;
if (reader != null) {
Object invokedField = reader.invoke(object);
if (invokedField instanceof Collection) {
value = ((Collection) invokedField).toArray()[i];
} else {
value = invokedField;
}
} else {
value = object;
}
Map<String, String> map = objectMapper.convertValue(value, Map.class);
result.add(new HashMap<>());
result.get(result.size() - 1).put("name", map.get(configSingle.getName()));
result.get(result.size() - 1).put("value", map.get(configSingle.getValue()));
}
}
}
return result;
}
public enum FileType {
XML("xml"), JSON("json");
private String name;
FileType(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static FileType fromName(String name) {
for (FileType type: FileType.values()) {
if (name.equals(type.getName())) {
return type;
}
}
throw new NoSuchElementException("File Type [" + name + "] is not supported");
}
}
}

View File

@ -1,21 +0,0 @@
package eu.eudat.logic.proxy.fetching.entities;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "fetchConfig")
public class Config {
private List<ConfigSingle> configs;
@XmlElementWrapper
@XmlElement(name = "config")
public List<ConfigSingle> getConfigs() {
return configs;
}
public void setConfigs(List<ConfigSingle> configs) {
this.configs = configs;
}
}

View File

@ -1,71 +0,0 @@
package eu.eudat.logic.proxy.fetching.entities;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "config")
public class ConfigSingle {
private String type;
private String fileType;
private String filePath;
private String parseClass;
private String parseField;
private String name;
private String value;
@XmlElement(name = "type")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@XmlElement(name = "fileType")
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
@XmlElement(name = "filePath")
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
@XmlElement(name = "parseClass")
public String getParseClass() {
return parseClass;
}
public void setParseClass(String parseClass) {
this.parseClass = parseClass;
}
@XmlElement(name = "parseField")
public String getParseField() {
return parseField;
}
public void setParseField(String parseField) {
this.parseField = parseField;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "value")
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -1,21 +0,0 @@
package eu.eudat.logic.proxy.fetching.entities;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElementWrapper;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "ISO_4217")
public class CurrencyModel {
private List<CurrencySingleModel> currencies;
@XmlElementWrapper(name = "CcyTbl")
@XmlElement(name = "CcyNtry")
public List<CurrencySingleModel> getCurrencies() {
return currencies;
}
public void setCurrencies(List<CurrencySingleModel> currencies) {
this.currencies = currencies;
}
}

View File

@ -1,53 +0,0 @@
package eu.eudat.logic.proxy.fetching.entities;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "CcyNtry")
public class CurrencySingleModel {
private String country;
private String currency;
private String code;
private Integer numericCode;
private Integer unit;
@XmlElement(name = "CtryNm")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@XmlElement(name = "CcyNm")
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
@XmlElement(name = "Ccy")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@XmlElement(name = "CcyNbr")
public Integer getNumericCode() {
return numericCode;
}
public void setNumericCode(Integer numericCode) {
this.numericCode = numericCode;
}
@XmlElement(name = "CcyMnrUnts")
public Integer getUnit() {
return unit;
}
public void setUnit(Integer unit) {
this.unit = unit;
}
}

View File

@ -1,27 +0,0 @@
package eu.eudat.models.old.local;
public class LocalFetchModel {
private String name;
private String value;
public LocalFetchModel(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -1,13 +0,0 @@
<fetchConfig>
<configs>
<config>
<type>currency</type>
<fileType>xml</fileType>
<filePath>internal/iso-4217.xml</filePath>
<parseClass>eu.eudat.logic.proxy.fetching.entities.CurrencyModel</parseClass>
<parseField>currencies</parseField>
<name>currency</name>
<value>code</value>
</config>
</configs>
</fetchConfig>