Resolve some TODOs, change null return based logic to exception based
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package me.bearns.fias.config;
|
package me.bearns.fias.config;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -21,6 +22,7 @@ import javax.sql.DataSource;
|
|||||||
@EnableJpaRepositories("me.bearns.fias.repository")
|
@EnableJpaRepositories("me.bearns.fias.repository")
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
//@PropertySource(value = "application-{profile}.properties", ignoreResourceNotFound = true)
|
//@PropertySource(value = "application-{profile}.properties", ignoreResourceNotFound = true)
|
||||||
|
@Slf4j
|
||||||
class ApplicationConfig {
|
class ApplicationConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -43,7 +45,10 @@ class ApplicationConfig {
|
|||||||
|
|
||||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
vendorAdapter.setGenerateDdl(false); //use liquibase
|
vendorAdapter.setGenerateDdl(false); //use liquibase
|
||||||
vendorAdapter.setShowSql(true); //TODO Debug
|
if(log.isDebugEnabled()) {
|
||||||
|
log.debug("Enable SQL echo.");
|
||||||
|
vendorAdapter.setShowSql(true);
|
||||||
|
}
|
||||||
|
|
||||||
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
|
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
|
||||||
factory.setJpaVendorAdapter(vendorAdapter);
|
factory.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package me.bearns.fias.exceptions;
|
||||||
|
|
||||||
|
public class RemoteAPIException extends CommonException{
|
||||||
|
public RemoteAPIException(Exception e) {
|
||||||
|
super(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteAPIException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -105,7 +105,6 @@ public class UpdaterImpl implements UpdateHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processArchive(File file, Predicate filter) throws CommonException {
|
private void processArchive(File file, Predicate filter) throws CommonException {
|
||||||
//TODO
|
|
||||||
|
|
||||||
try(final ZipFile zipFile = new ZipFile(file)){
|
try(final ZipFile zipFile = new ZipFile(file)){
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package me.bearns.fias.service;
|
package me.bearns.fias.service;
|
||||||
|
|
||||||
import me.bearns.fias.domain.FiasVersion;
|
import me.bearns.fias.domain.FiasVersion;
|
||||||
|
import me.bearns.fias.exceptions.RemoteAPIException;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface OnlineVersion {
|
public interface OnlineVersion {
|
||||||
|
|
||||||
long getLastVersionId();
|
long getLastVersionId() throws RemoteAPIException;
|
||||||
|
|
||||||
List<FiasVersion> getVersionsAfter(long version);
|
@lombok.NonNull
|
||||||
|
List<FiasVersion> getVersionsAfter(long version) throws RemoteAPIException;
|
||||||
|
|
||||||
List<FiasVersion> getLastVersion();
|
@lombok.NonNull
|
||||||
|
List<FiasVersion> getLastVersion() throws RemoteAPIException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package me.bearns.fias.service;
|
|||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import me.bearns.fias.domain.FiasVersion;
|
import me.bearns.fias.domain.FiasVersion;
|
||||||
|
import me.bearns.fias.exceptions.RemoteAPIException;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -20,33 +22,45 @@ public class OnlineVersionImpl implements OnlineVersion{
|
|||||||
RestTemplate client = new RestTemplate();
|
RestTemplate client = new RestTemplate();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getLastVersionId() {
|
public long getLastVersionId() throws RemoteAPIException {
|
||||||
|
|
||||||
final FiasVersion version = client.getForObject(GET_LAST_FILE_INFO, FiasVersion.class);
|
final FiasVersion version = request(GET_LAST_FILE_INFO, FiasVersion.class);
|
||||||
|
|
||||||
if(version != null) return version.getVersionId();
|
return version.getVersionId();
|
||||||
|
|
||||||
return 0; //todo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FiasVersion> getVersionsAfter(long version) {
|
public List<FiasVersion> getVersionsAfter(long version) throws RemoteAPIException {
|
||||||
|
|
||||||
final FiasVersion[] versions = client.getForObject(GET_ALL_FILE_INFO, FiasVersion[].class);
|
final FiasVersion[] versions = request(GET_ALL_FILE_INFO, FiasVersion[].class);
|
||||||
|
|
||||||
if(versions != null) {
|
|
||||||
return Arrays.stream(versions).filter(v -> v.getVersionId() > version).collect(Collectors.toList());
|
return Arrays.stream(versions).filter(v -> v.getVersionId() > version).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null; //todo
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FiasVersion> getLastVersion() {
|
public List<FiasVersion> getLastVersion() throws RemoteAPIException {
|
||||||
|
|
||||||
List<FiasVersion> versions = new ArrayList<>();
|
List<FiasVersion> versions = new ArrayList<>();
|
||||||
|
|
||||||
versions.add(client.getForObject(GET_LAST_FILE_INFO, FiasVersion.class));
|
final FiasVersion lastFile = request(GET_LAST_FILE_INFO, FiasVersion.class);
|
||||||
|
|
||||||
return versions; //todo
|
versions.add(lastFile);
|
||||||
|
|
||||||
|
return versions;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T request(final String url, Class<T> responseType) throws RemoteAPIException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
final T result = client.getForObject(url, responseType);
|
||||||
|
|
||||||
|
if (result == null) throw new RemoteAPIException("Request to " + url + " return no data.");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
} catch (RestClientException e){
|
||||||
|
throw new RemoteAPIException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user