|
|
@ -1,5 +1,6 @@ |
|
|
|
package me.bearns.fias.helpers; |
|
|
|
package me.bearns.fias.helpers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import me.bearns.fias.domain.FiasVersion; |
|
|
|
import me.bearns.fias.domain.FiasVersion; |
|
|
|
import me.bearns.fias.exceptions.CommonException; |
|
|
|
import me.bearns.fias.exceptions.CommonException; |
|
|
|
import me.bearns.fias.exceptions.DownloadException; |
|
|
|
import me.bearns.fias.exceptions.DownloadException; |
|
|
@ -26,6 +27,7 @@ import java.util.zip.ZipEntry; |
|
|
|
import java.util.zip.ZipException; |
|
|
|
import java.util.zip.ZipException; |
|
|
|
import java.util.zip.ZipFile; |
|
|
|
import java.util.zip.ZipFile; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
|
@Component |
|
|
|
public class UpdaterImpl implements UpdateHelper { |
|
|
|
public class UpdaterImpl implements UpdateHelper { |
|
|
|
|
|
|
|
|
|
|
@ -47,24 +49,32 @@ public class UpdaterImpl implements UpdateHelper { |
|
|
|
public void processUpdates(List<FiasVersion> updates, boolean reloadFlag) throws CommonException { |
|
|
|
public void processUpdates(List<FiasVersion> updates, boolean reloadFlag) throws CommonException { |
|
|
|
|
|
|
|
|
|
|
|
if(reloadFlag) { |
|
|
|
if(reloadFlag) { |
|
|
|
|
|
|
|
log.info("Clear tables"); |
|
|
|
|
|
|
|
|
|
|
|
versions.deleteAll(); |
|
|
|
versions.deleteAll(); |
|
|
|
catalog.getAll().stream().map(Catalog.Item::getRepository).forEach(JpaRepository::deleteAll); |
|
|
|
catalog.getAll().stream().map(Catalog.Item::getRepository).forEach(JpaRepository::deleteAll); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Clear tables completed"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId())); |
|
|
|
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId())); |
|
|
|
|
|
|
|
|
|
|
|
Map<FiasVersion, Future<File>> map = new HashMap<>(); |
|
|
|
Map<FiasVersion, Future<File>> map = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Start downloading"); |
|
|
|
//start download
|
|
|
|
//start download
|
|
|
|
updates.forEach(u -> map.put(u, fileService.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()))); |
|
|
|
updates.forEach(u -> map.put(u, fileService.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()))); |
|
|
|
|
|
|
|
|
|
|
|
//strict order
|
|
|
|
//strict order
|
|
|
|
for (FiasVersion item : updates) { |
|
|
|
for (FiasVersion item : updates) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Apply version {}", item); |
|
|
|
|
|
|
|
|
|
|
|
//set region filter
|
|
|
|
//set region filter
|
|
|
|
final String regions = item.getRegions(); |
|
|
|
final String regions = item.getRegions(); |
|
|
|
RegionFilter filter = null; |
|
|
|
RegionFilter filter = null; |
|
|
|
if(regions != null) { |
|
|
|
if(regions != null) { |
|
|
|
|
|
|
|
log.debug("Use region filter ({})", regions); |
|
|
|
filter = new RegionFilter(regions); |
|
|
|
filter = new RegionFilter(regions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -72,19 +82,23 @@ public class UpdaterImpl implements UpdateHelper { |
|
|
|
try { |
|
|
|
try { |
|
|
|
//wait for downloading
|
|
|
|
//wait for downloading
|
|
|
|
file = map.get(item).get(); |
|
|
|
file = map.get(item).get(); |
|
|
|
|
|
|
|
log.debug("Download completed"); |
|
|
|
|
|
|
|
|
|
|
|
//process update
|
|
|
|
//process update
|
|
|
|
processArchive(file, filter); |
|
|
|
processArchive(file, filter); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Archive process Ok"); |
|
|
|
|
|
|
|
|
|
|
|
//apply this version
|
|
|
|
//apply this version
|
|
|
|
versions.save(item); |
|
|
|
versions.save(item); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Save version {}", item); |
|
|
|
|
|
|
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("InterruptedException while download file"); |
|
|
|
//todo log
|
|
|
|
|
|
|
|
throw new DownloadException(e); |
|
|
|
throw new DownloadException(e); |
|
|
|
} catch (ExecutionException e) { |
|
|
|
} catch (ExecutionException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("ExecutionException while download file"); |
|
|
|
//todo log
|
|
|
|
|
|
|
|
throw new DownloadException(e); |
|
|
|
throw new DownloadException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -94,24 +108,36 @@ public class UpdaterImpl implements UpdateHelper { |
|
|
|
|
|
|
|
|
|
|
|
try(final ZipFile zipFile = new ZipFile(file)){ |
|
|
|
try(final ZipFile zipFile = new ZipFile(file)){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Process Zip file"); |
|
|
|
|
|
|
|
|
|
|
|
final Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
|
|
|
final Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
|
|
|
|
|
|
|
|
|
|
|
while (entries.hasMoreElements()){ |
|
|
|
while (entries.hasMoreElements()){ |
|
|
|
|
|
|
|
|
|
|
|
final ZipEntry entry = entries.nextElement(); |
|
|
|
final ZipEntry entry = entries.nextElement(); |
|
|
|
|
|
|
|
final String name = entry.getName(); |
|
|
|
|
|
|
|
|
|
|
|
final UnmarshallerParameters config = catalog.getByPrefix(entry.getName()); |
|
|
|
log.debug("Find '{}' in zip", name); |
|
|
|
|
|
|
|
|
|
|
|
if(config!=null) { //todo
|
|
|
|
final UnmarshallerParameters config = catalog.getByPrefix(name); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(config!=null) { |
|
|
|
try (InputStream is = zipFile.getInputStream(entry)) { |
|
|
|
try (InputStream is = zipFile.getInputStream(entry)) { |
|
|
|
|
|
|
|
log.debug("Process '{}' in zip", name); |
|
|
|
unmarshaller.process(is, config, filter); |
|
|
|
unmarshaller.process(is, config, filter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
log.debug("Skip '{}'", name); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("Completed Zip file"); |
|
|
|
|
|
|
|
|
|
|
|
} catch (ZipException e) { |
|
|
|
} catch (ZipException e) { |
|
|
|
|
|
|
|
log.error("Common Zip exception"); |
|
|
|
throw new UnzipException(e); |
|
|
|
throw new UnzipException(e); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
log.error("IO Unzip Exception"); |
|
|
|
throw new UnzipException(e); |
|
|
|
throw new UnzipException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|