Compare commits
4 Commits
a8b29ac936
...
6197956d4f
Author | SHA1 | Date |
---|---|---|
![]() |
6197956d4f | 5 years ago |
![]() |
0ab13dd395 | 5 years ago |
![]() |
1344b50a62 | 5 years ago |
![]() |
8a8ed24cfa | 5 years ago |
@ -1,4 +1,4 @@ |
||||
package me.bearns.fias.util; |
||||
package me.bearns.fias.helpers; |
||||
|
||||
import me.bearns.fias.domain.Addrobj; |
||||
import me.bearns.fias.domain.House; |
@ -1,4 +1,4 @@ |
||||
package me.bearns.fias.util; |
||||
package me.bearns.fias.helpers; |
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
@ -0,0 +1,10 @@ |
||||
package me.bearns.fias.helpers; |
||||
|
||||
import me.bearns.fias.domain.FiasVersion; |
||||
import me.bearns.fias.exceptions.CommonException; |
||||
|
||||
import java.util.List; |
||||
|
||||
public interface UpdateHelper { |
||||
public void processUpdates(List<FiasVersion> updates, boolean reloadFlag) throws CommonException; |
||||
} |
@ -0,0 +1,145 @@ |
||||
package me.bearns.fias.helpers; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import me.bearns.fias.domain.FiasVersion; |
||||
import me.bearns.fias.exceptions.CommonException; |
||||
import me.bearns.fias.exceptions.DownloadException; |
||||
import me.bearns.fias.exceptions.UnzipException; |
||||
import me.bearns.fias.repository.FiasVersionRepository; |
||||
import me.bearns.fias.service.Downloader; |
||||
import me.bearns.fias.service.StreamSaver; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.Enumeration; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ExecutionException; |
||||
import java.util.concurrent.Future; |
||||
import java.util.function.Predicate; |
||||
import java.util.zip.ZipEntry; |
||||
import java.util.zip.ZipException; |
||||
import java.util.zip.ZipFile; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class UpdaterImpl implements UpdateHelper { |
||||
|
||||
@Autowired |
||||
private static Downloader fileService; |
||||
|
||||
@Autowired |
||||
private static FiasVersionRepository versions; |
||||
|
||||
@Autowired |
||||
private static Catalog catalog; |
||||
|
||||
@Autowired |
||||
private static StreamSaver unmarshaller; |
||||
|
||||
|
||||
//transaction from here
|
||||
@Transactional(rollbackFor = Exception.class) |
||||
public void processUpdates(List<FiasVersion> updates, boolean reloadFlag) throws CommonException { |
||||
|
||||
if(reloadFlag) { |
||||
log.info("Clear tables"); |
||||
|
||||
versions.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())); |
||||
|
||||
Map<FiasVersion, Future<File>> map = new HashMap<>(); |
||||
|
||||
log.debug("Start downloading"); |
||||
//start download
|
||||
updates.forEach(u -> map.put(u, fileService.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()))); |
||||
|
||||
//strict order
|
||||
for (FiasVersion item : updates) { |
||||
|
||||
log.debug("Apply version {}", item); |
||||
|
||||
//set region filter
|
||||
final String regions = item.getRegions(); |
||||
RegionFilter filter = null; |
||||
if(regions != null) { |
||||
log.debug("Use region filter ({})", regions); |
||||
filter = new RegionFilter(regions); |
||||
} |
||||
|
||||
final File file; |
||||
try { |
||||
//wait for downloading
|
||||
file = map.get(item).get(); |
||||
log.debug("Download completed"); |
||||
|
||||
//process update
|
||||
processArchive(file, filter); |
||||
|
||||
log.debug("Archive process Ok"); |
||||
|
||||
//apply this version
|
||||
versions.save(item); |
||||
|
||||
log.debug("Save version {}", item); |
||||
|
||||
} catch (InterruptedException e) { |
||||
log.error("InterruptedException while download file"); |
||||
throw new DownloadException(e); |
||||
} catch (ExecutionException e) { |
||||
log.error("ExecutionException while download file"); |
||||
throw new DownloadException(e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void processArchive(File file, Predicate filter) throws CommonException { |
||||
|
||||
try(final ZipFile zipFile = new ZipFile(file)){ |
||||
|
||||
log.debug("Process Zip file"); |
||||
|
||||
final Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
||||
|
||||
while (entries.hasMoreElements()){ |
||||
|
||||
final ZipEntry entry = entries.nextElement(); |
||||
final String name = entry.getName(); |
||||
|
||||
log.debug("Find '{}' in zip", name); |
||||
|
||||
final UnmarshallerParameters config = catalog.getByPrefix(name); |
||||
|
||||
if(config!=null) { |
||||
try (InputStream is = zipFile.getInputStream(entry)) { |
||||
log.debug("Process '{}' in zip", name); |
||||
unmarshaller.process(is, config, filter); |
||||
} |
||||
} else { |
||||
log.debug("Skip '{}'", name); |
||||
} |
||||
} |
||||
|
||||
log.debug("Completed Zip file"); |
||||
|
||||
} catch (ZipException e) { |
||||
log.error("Common Zip exception"); |
||||
throw new UnzipException(e); |
||||
} catch (IOException e) { |
||||
log.error("IO Unzip Exception"); |
||||
throw new UnzipException(e); |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package me.bearns.fias.helper; |
||||
|
||||
import me.bearns.fias.domain.FiasVersion; |
||||
import me.bearns.fias.exceptions.CommonException; |
||||
import me.bearns.fias.exceptions.UnmarshallingException; |
||||
import me.bearns.fias.repository.FiasVersionRepository; |
||||
import org.springframework.boot.test.context.TestComponent; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
@TestComponent |
||||
public class TransactionalSaveHelper { |
||||
|
||||
private FiasVersionRepository repository; |
||||
|
||||
public static final long FIRST = 1L; |
||||
public static final long NEXT = 2L; |
||||
|
||||
public void setRepository(FiasVersionRepository repository) { |
||||
this.repository = repository; |
||||
} |
||||
|
||||
@Transactional(rollbackFor = Exception.class) |
||||
public void addNext() throws CommonException { |
||||
final FiasVersion nextVersion = new FiasVersion(NEXT, null, null, null); |
||||
repository.save(nextVersion); |
||||
throw new UnmarshallingException("test"); |
||||
} |
||||
|
||||
public void addFirst() { |
||||
final FiasVersion version = new FiasVersion(FIRST, null, null, null); |
||||
repository.save(version); |
||||
} |
||||
} |
Loading…
Reference in new issue