|
|
|
@ -1,16 +1,16 @@ |
|
|
|
|
package me.bearns.fias.service; |
|
|
|
|
|
|
|
|
|
import me.bearns.fias.domain.FiasVersion; |
|
|
|
|
import me.bearns.fias.exceptions.DownloadException; |
|
|
|
|
import me.bearns.fias.repository.FiasVersionRepository; |
|
|
|
|
import me.bearns.fias.util.DownlodableUpdate; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.util.AbstractMap; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
import java.util.concurrent.Future; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
@ -22,11 +22,14 @@ public class FiasUpdater implements Updater { |
|
|
|
|
@Autowired |
|
|
|
|
FiasVersionRepository versions; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private static Downloader service; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
OnlineVersion clientStub; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void update() { |
|
|
|
|
public void update() throws DownloadException { |
|
|
|
|
|
|
|
|
|
//Max value
|
|
|
|
|
FiasVersion dbVersionObj = versions.findTopByOrderByVersionIdDesc(); |
|
|
|
@ -52,7 +55,7 @@ public class FiasUpdater implements Updater { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void reload(Long... regions) { |
|
|
|
|
public void reload(Long... regions) throws DownloadException { |
|
|
|
|
|
|
|
|
|
final List<FiasVersion> lastVersion = clientStub.getLastVersion(); |
|
|
|
|
if(lastVersion != null) { |
|
|
|
@ -62,29 +65,46 @@ public class FiasUpdater implements Updater { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//transaction from here
|
|
|
|
|
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag, Long ... regions) { |
|
|
|
|
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag, Long ... regions) throws DownloadException { |
|
|
|
|
|
|
|
|
|
//todo reload flag impl
|
|
|
|
|
if(reloadFlag) { |
|
|
|
|
//todo reload flag impl
|
|
|
|
|
versions.deleteAll(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId())); |
|
|
|
|
|
|
|
|
|
final Stream<DownlodableUpdate> stream = reloadFlag ? updates.stream().map(DownlodableUpdate::processReload) : updates.stream().map(DownlodableUpdate::processUpdate); |
|
|
|
|
Map<FiasVersion, Future<File>> map = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
for (DownlodableUpdate item : stream.collect(Collectors.toList())) { |
|
|
|
|
//start download
|
|
|
|
|
updates.forEach(u -> map.put(u, service.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()))); |
|
|
|
|
|
|
|
|
|
//strict order
|
|
|
|
|
for (FiasVersion item : updates) { |
|
|
|
|
|
|
|
|
|
final File file; |
|
|
|
|
try { |
|
|
|
|
//todo process
|
|
|
|
|
processFile(item.getFile(), regions); |
|
|
|
|
versions.save(item.getVersion()); |
|
|
|
|
|
|
|
|
|
//todo save updates
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
//todo log
|
|
|
|
|
break; |
|
|
|
|
//wait for downloading
|
|
|
|
|
file = map.get(item).get(); |
|
|
|
|
|
|
|
|
|
//process update
|
|
|
|
|
processArchive(file, regions); |
|
|
|
|
//apply this version
|
|
|
|
|
versions.save(item); |
|
|
|
|
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
//todo log
|
|
|
|
|
throw new DownloadException(e); |
|
|
|
|
} catch (ExecutionException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
//todo log
|
|
|
|
|
throw new DownloadException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void processFile(File file, Long ... regions) { |
|
|
|
|
private void processArchive(File file, Long ... regions) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |