List of usage examples for io.vertx.core.file FileSystem readDir
@Fluent FileSystem readDir(String path, String filter, Handler<AsyncResult<List<String>>> handler);
From source file:org.entcore.feeder.csv.CsvImportsLauncher.java
License:Open Source License
@Override public void handle(Long event) { final FileSystem fs = vertx.fileSystem(); fs.readDir(path, ".*.zip", new Handler<AsyncResult<List<String>>>() { @Override//from w ww .j av a 2 s . c o m public void handle(final AsyncResult<List<String>> event) { if (event.succeeded()) { Collections.sort(event.result()); final Handler[] handlers = new Handler[event.result().size() + 1]; handlers[handlers.length - 1] = new Handler<Void>() { @Override public void handle(Void v) { postImport.execute(); } }; for (int i = event.result().size() - 1; i >= 0; i--) { final int j = i; handlers[i] = new Handler<Void>() { @Override public void handle(Void v) { final String file = event.result().get(j); log.info("Importing file : " + file); Matcher matcher; Matcher nameMatcher; if (file != null && (matcher = UAI_PATTERN.matcher(file)).find() && (nameMatcher = namePattern.matcher(file)).find()) { final String uai = matcher.group(1); final String structureName = nameMatcher.group(1); TransactionManager.getNeo4jHelper().execute( "MATCH (s:Structure {UAI:{uai}}) return s.externalId as externalId", new JsonObject().put("uai", uai), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { final String structureExternalId; JsonArray res = event.body().getJsonArray("result"); if ("ok".equals(event.body().getString("status")) && res.size() > 0) { structureExternalId = res.getJsonObject(0) .getString("externalId"); } else { structureExternalId = null; } final String parentDir = path + File.separator + UUID.randomUUID().toString(); final String dirName = parentDir + File.separator + structureName + (structureExternalId != null ? "@" + structureExternalId : "") + "_" + uai; fs.mkdirs(dirName, new Handler<AsyncResult<Void>>() { @Override public void handle(AsyncResult<Void> event) { try { FileUtils.unzip(file.replaceAll("\\s", "%20"), dirName); moveCsvFiles(structureExternalId, fs, dirName, parentDir, handlers, j); } catch (IOException e) { fs.deleteRecursive(parentDir, true, null); log.error("Error unzip : " + file, e); handlers[j + 1].handle(null); } } }); } }); } else { log.error("UAI not found in filename : " + file); handlers[j + 1].handle(null); } } }; } handlers[0].handle(null); } else { log.error("Error reading directory."); } } }); }
From source file:org.entcore.feeder.csv.CsvImportsLauncher.java
License:Open Source License
private void moveCsvFiles(final String structureExternalId, final FileSystem fs, final String dirName, final String parentDir, final Handler<Void>[] handlers, final int j) { fs.readDir(dirName, ".*.csv", new Handler<AsyncResult<List<String>>>() { @Override/* w w w .ja v a2s . c o m*/ public void handle(final AsyncResult<List<String>> l) { if (l.succeeded()) { final int size = l.result().size(); if (!(size > 0)) { emptyDirectory(fs, parentDir, handlers, j); return; } final AtomicInteger validFilesCount = new AtomicInteger(size); final AtomicInteger count = new AtomicInteger(size); for (final String f : l.result()) { String profile = null; for (String profilePattern : profiles.fieldNames()) { if (f.contains(profilePattern)) { profile = profiles.getString(profilePattern); break; } } if (profile == null) { validFilesCount.decrementAndGet(); fs.delete(f, new Handler<AsyncResult<Void>>() { @Override public void handle(AsyncResult<Void> event) { if (count.decrementAndGet() == 0) { importFiles(validFilesCount, parentDir, structureExternalId, handlers, j, fs); } } }); continue; } fs.move(f, dirName + File.separator + profile + ".csv", new Handler<AsyncResult<Void>>() { @Override public void handle(AsyncResult<Void> event2) { if (event2.succeeded()) { if (count.decrementAndGet() == 0) { importFiles(validFilesCount, parentDir, structureExternalId, handlers, j, fs); } } else { fs.deleteRecursive(parentDir, true, null); log.error("Error mv csv file : " + f, l.cause()); handlers[j + 1].handle(null); } } }); } } else { fs.deleteRecursive(parentDir, true, null); log.error("Error listing csv in directory : " + dirName, l.cause()); handlers[j + 1].handle(null); } } }); }