Commit 15b0e5a0 authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-408 extract dimensions link

parent 1e0bc170
......@@ -18,11 +18,15 @@ public class ExcelConfig {
private String type;
private String realLevels;
private String separator;
private Path locationPath;
private String location;
private Path locationPath;
private String widgetLocation;
private Path widgetLocationPath;
private String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
if (location != null)
......
......@@ -14,4 +14,5 @@ public class StatConfig {
private Integer urlLength;
private String linkMetaUrl;
private String dimensionMetaUrl;
}
package ee.stat.dashboard.service.statdata;
import ee.stat.dashboard.config.props.StatConfig;
import ee.stat.dashboard.controller.publics.dto.Language;
import ee.stat.dashboard.service.statdata.xml.AllowedDimensions;
import ee.stat.dashboard.service.statdata.xml.XmlDimension;
import ee.stat.dashboard.service.statdata.xml.XmlValue;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
......@@ -16,13 +18,14 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8;
@Slf4j
@Service
@AllArgsConstructor
......@@ -30,18 +33,18 @@ public class StatDimensionsService {
public static final String CODELISTS = "/message:Structure/message:CodeLists/*[name()='CodeList']";
public static final String KEY_FAMILY = "/message:Structure/message:KeyFamilies/*[name()='KeyFamily']/*[name()='Components']";
//todo make property
public static final String META_URL = "http://andmebaas.stat.ee/restsdmx/sdmx.ashx/GetDataStructure/{0}";
private RestTemplate template;
private StatConfig statConfig;
@Cacheable(value = "StatDimensionsService_getDimensions", key = "#code")
public List<XmlDimension> getDimensions(String code) {
checkUrl(statConfig.getDimensionMetaUrl());
try {
ResponseEntity<String> exchange = template.getForEntity(MessageFormat.format(META_URL, code), String.class);
ResponseEntity<String> exchange = template.getForEntity(MessageFormat.format(statConfig.getDimensionMetaUrl(), code), String.class);
SAXReader reader = new SAXReader();
String response = exchange.getBody();
if (response == null) throw new IllegalStateException("response is null, code: " + code);
Document read = reader.read(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
Document read = reader.read(new ByteArrayInputStream(response.getBytes(UTF_8)));
DefaultElement keyFamilyInfo = (DefaultElement) read.selectSingleNode(KEY_FAMILY);
AllowedDimensions allowedDimensions = allowedDimensions(keyFamilyInfo);
......@@ -104,4 +107,11 @@ public class StatDimensionsService {
private String getLang(Node node) {
return ((DefaultElement) node).attribute("lang").getValue();
}
private void checkUrl(String metaUrl) {
if (StringUtils.isBlank(metaUrl)) {
throw new IllegalStateException("Link meta url is not configured");
}
}
}
......@@ -165,25 +165,10 @@ public class ExcelToCoordinates {
}
private Workbook getSheets(Excel excel, Language lang) {
//todo if no location then error and that is it
if (excel.getExcelType().isDataOnly()) {
String result = excel.getLocation() != null ? excel.getLocation() : excelConfig.getLocation() + excel.getFilename();
log.info("looking for excel: {} {}", lang.name(), result);
return commonExcelMapper.tryToCreateWorkbook(new File(result));
} else {
if (excel.getLocation() != null) {
String result = excel.getLocation();
log.info("looking for excel: {} {}", lang.name(), result);
return commonExcelMapper.tryToCreateWorkbook(new File(result));
} else if (excelConfig.getWidgetLocation() != null) {
String file = excelConfig.getWidgetLocation() + excel.getFilename();
log.info("looking for excel: {} {}", lang.name(), file);
Workbook sheets = commonExcelMapper.tryToCreateWorkbook(new File(file));
if (sheets != null) {
return sheets;
}
}
return null;
if (excel.getLocation() == null) {
throw new RuntimeException("Excel has no location, filename: " + excel.getFilename());
}
log.info("looking for excel: {} {}", lang.name(), excel.getLocation());
return commonExcelMapper.tryToCreateWorkbook(new File(excel.getLocation()));
}
}
......@@ -84,6 +84,7 @@ app:
stat:
urlLength: 375
linkMetaUrl: http://andmebaas.stat.ee/OECDStat_Metadata/ShowMetadata.ashx?Dataset={0}&Lang={1}&isembededmetadata=true
dimensionMetaUrl: http://andmebaas.stat.ee/restsdmx/sdmx.ashx/GetDataStructure/{0}
cron:
statDbUpdate: 0 0 * * * *
statDbEnabled: true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment