|
|
|
@ -4,10 +4,11 @@ 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.helpers.UpdateHelper; |
|
|
|
|
import me.bearns.fias.repository.FiasVersionRepository; |
|
|
|
|
import me.bearns.fias.util.Catalog; |
|
|
|
|
import me.bearns.fias.util.RegionFilter; |
|
|
|
|
import me.bearns.fias.util.UnmarshallerParameters; |
|
|
|
|
import me.bearns.fias.helpers.Catalog; |
|
|
|
|
import me.bearns.fias.helpers.RegionFilter; |
|
|
|
|
import me.bearns.fias.helpers.UnmarshallerParameters; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
@ -27,17 +28,11 @@ public class FiasUpdater implements Updater { |
|
|
|
|
@Autowired |
|
|
|
|
private static FiasVersionRepository versions; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private static StreamSaver unmarshaller; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private static Downloader fileService; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private static OnlineVersion clientStub; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
private static Catalog catalog; |
|
|
|
|
private static UpdateHelper helper; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void update() throws CommonException { |
|
|
|
@ -61,7 +56,7 @@ public class FiasUpdater implements Updater { |
|
|
|
|
if(regions != null) updates.forEach(u -> u.setRegions(regions)); |
|
|
|
|
|
|
|
|
|
if(updates != null && !updates.isEmpty()) { |
|
|
|
|
processUpdates(updates, false); |
|
|
|
|
helper.processUpdates(updates, false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -79,83 +74,10 @@ public class FiasUpdater implements Updater { |
|
|
|
|
//set region filter
|
|
|
|
|
lastVersion.get(0).setRegions(regions); |
|
|
|
|
|
|
|
|
|
processUpdates(lastVersion, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//transaction from here
|
|
|
|
|
@Transactional |
|
|
|
|
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag) throws CommonException { |
|
|
|
|
|
|
|
|
|
if(reloadFlag) { |
|
|
|
|
//todo reload flag impl
|
|
|
|
|
versions.deleteAll(); |
|
|
|
|
helper.processUpdates(lastVersion, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId())); |
|
|
|
|
|
|
|
|
|
Map<FiasVersion, Future<File>> map = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
//start download
|
|
|
|
|
updates.forEach(u -> map.put(u, fileService.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()))); |
|
|
|
|
|
|
|
|
|
//strict order
|
|
|
|
|
for (FiasVersion item : updates) { |
|
|
|
|
|
|
|
|
|
//set region filter
|
|
|
|
|
final String regions = item.getRegions(); |
|
|
|
|
RegionFilter filter = null; |
|
|
|
|
if(regions != null) { |
|
|
|
|
filter = new RegionFilter(regions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final File file; |
|
|
|
|
try { |
|
|
|
|
//wait for downloading
|
|
|
|
|
file = map.get(item).get(); |
|
|
|
|
|
|
|
|
|
//process update
|
|
|
|
|
processArchive(file, filter); |
|
|
|
|
//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 processArchive(File file, Predicate filter) throws CommonException { |
|
|
|
|
|
|
|
|
|
try(final ZipFile zipFile = new ZipFile(file)){ |
|
|
|
|
|
|
|
|
|
final Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
|
|
|
|
|
|
|
|
|
while (entries.hasMoreElements()){ |
|
|
|
|
|
|
|
|
|
final ZipEntry entry = entries.nextElement(); |
|
|
|
|
|
|
|
|
|
final UnmarshallerParameters config = catalog.getByPrefix(entry.getName()); |
|
|
|
|
|
|
|
|
|
if(config!=null) { //todo
|
|
|
|
|
try (InputStream is = zipFile.getInputStream(entry)) { |
|
|
|
|
unmarshaller.process(is, config, filter); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (ZipException e) { |
|
|
|
|
throw new UnzipException(e); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
throw new UnzipException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |