use of org.opendatakit.briefcase.reused.transfer.AggregateServer in project briefcase by opendatakit.
the class PullFromAggregateIntegrationTest method setUp.
@Before
public void setUp() throws IOException {
Files.createDirectories(briefcaseDir);
prefs = new BriefcasePreferences(InMemoryPreferences.empty());
prefs.setStorageDir(tmpDir);
server = httpServer(serverPort);
tracker = new PullFromAggregateTracker(form, e -> {
});
formMetadataPort = new InMemoryFormMetadataAdapter();
pullOp = new PullFromAggregate(CommonsHttp.of(1), aggregateServer, briefcaseDir, true, e -> {
}, formMetadataPort);
runnerStatus = new TestRunnerStatus(false);
}
use of org.opendatakit.briefcase.reused.transfer.AggregateServer in project briefcase by opendatakit.
the class ExportPanel method export.
private void export() {
Stream<Job<?>> allJobs = forms.getSelectedForms().stream().map(form -> {
FormKey key = FormKey.from(form);
FormMetadata formMetadata = formMetadataPort.fetch(key).orElseThrow(BriefcaseException::new);
form.setStatusString("Starting to export form");
String formId = form.getFormDefinition().getFormId();
ExportConfiguration configuration = forms.getConfiguration(formId);
FormDefinition formDef = FormDefinition.from((BriefcaseFormDefinition) form.getFormDefinition());
// TODO Abstract away the subtype of RemoteServer. This should say Optional<RemoteServer>
Optional<AggregateServer> savedPullSource = RemoteServer.readFromPrefs(appPreferences, pullPanelPrefs, form);
Job<Void> pullJob = configuration.resolvePullBefore() && savedPullSource.isPresent() ? new PullFromAggregate(http, savedPullSource.get(), appPreferences.getBriefcaseDir().orElseThrow(BriefcaseException::new), false, EventBus::publish, formMetadataPort).pull(form, appPreferences.resolveStartFromLast() ? Optional.of(formMetadata.getCursor()) : Optional.empty()) : Job.noOpSupplier();
Job<Void> exportJob = Job.run(runnerStatus -> ExportToCsv.export(formMetadataPort, formMetadata, form, formDef, appPreferences.getBriefcaseDir().orElseThrow(BriefcaseException::new), configuration, analytics));
Job<Void> exportGeoJsonJob = configuration.resolveIncludeGeoJsonExport() ? Job.run(runnerStatus -> ExportToGeoJson.export(formMetadata, formDef, configuration, analytics)) : Job.noOp;
return Job.run(runnerStatus -> form.clearStatusHistory()).thenRun(pullJob).thenRun(exportJob).thenRun(exportGeoJsonJob);
});
JobsRunner.launchAsync(allJobs).onComplete(form::unsetExporting).waitForCompletion();
}
use of org.opendatakit.briefcase.reused.transfer.AggregateServer in project briefcase by opendatakit.
the class PullFormFromAggregate method pullFormFromAggregate.
public static void pullFormFromAggregate(Path storageDir, Optional<String> formId, String username, String password, URL server, boolean resumeLastPull, Optional<LocalDate> startFromDate, boolean includeIncomplete, Optional<Integer> maybeMaxHttpConnections) {
CliEventsCompanion.attach(log);
Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir);
FormCache formCache = FormCache.from(briefcaseDir);
formCache.update();
BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
FormMetadataPort formMetadataPort = FileSystemFormMetadataAdapter.at(briefcaseDir);
int maxHttpConnections = Optionals.race(maybeMaxHttpConnections, appPreferences.getMaxHttpConnections()).orElse(DEFAULT_HTTP_CONNECTIONS);
Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
AggregateServer aggregateServer = AggregateServer.authenticated(server, new Credentials(username, password));
Response<List<RemoteFormDefinition>> response = http.execute(aggregateServer.getFormListRequest());
if (!response.isSuccess()) {
System.err.println(response.isRedirection() ? "Error connecting to Aggregate: Redirection detected" : response.isUnauthorized() ? "Error connecting to Aggregate: Wrong credentials" : response.isNotFound() ? "Error connecting to Aggregate: Aggregate not found" : "Error connecting to Aggregate");
return;
}
List<FormStatus> filteredForms = response.orElseThrow(BriefcaseException::new).stream().filter(f -> formId.map(id -> f.getFormId().equals(id)).orElse(true)).map(FormStatus::new).collect(toList());
if (formId.isPresent() && filteredForms.isEmpty())
throw new BriefcaseException("Form " + formId.get() + " not found");
TransferForms forms = TransferForms.empty();
forms.load(filteredForms);
forms.selectAll();
PullFromAggregate pullOp = new PullFromAggregate(http, aggregateServer, briefcaseDir, includeIncomplete, PullFormFromAggregate::onEvent, formMetadataPort);
JobsRunner.launchAsync(forms.map(form -> pullOp.pull(form, resolveCursor(resumeLastPull, startFromDate, form, formMetadataPort))), PullFormFromAggregate::onError).waitForCompletion();
System.out.println();
System.out.println("All operations completed");
System.out.println();
}
use of org.opendatakit.briefcase.reused.transfer.AggregateServer in project briefcase by opendatakit.
the class PushFormToAggregate method pushFormToAggregate.
private static void pushFormToAggregate(Path storageDir, Optional<String> formid, String username, String password, URL server, boolean forceSendBlank, Optional<Integer> maybeMaxConnections) {
CliEventsCompanion.attach(log);
Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir);
FormCache formCache = FormCache.from(briefcaseDir);
formCache.update();
BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
int maxHttpConnections = Optionals.race(maybeMaxConnections, appPreferences.getMaxHttpConnections()).orElse(DEFAULT_HTTP_CONNECTIONS);
Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
AggregateServer aggregateServer = AggregateServer.authenticated(server, new Credentials(username, password));
Response response = http.execute(aggregateServer.getPushFormPreflightRequest());
if (!response.isSuccess()) {
System.err.println(response.isRedirection() ? "Error connecting to Aggregate: Redirection detected" : response.isUnauthorized() ? "Error connecting to Aggregate: Wrong credentials" : response.isNotFound() ? "Error connecting to Aggregate: Aggregate not found" : "Error connecting to Aggregate");
return;
}
List<FormStatus> statuses;
if (formid.isPresent()) {
String requestedFormId = formid.get();
FormStatus status = formCache.getForms().stream().filter(form -> form.getFormId().equals(requestedFormId)).map(FormStatus::new).findFirst().orElseThrow(() -> new BriefcaseException("Form " + requestedFormId + " not found"));
statuses = Arrays.asList(status);
} else {
statuses = formCache.getForms().stream().map(FormStatus::new).collect(Collectors.toList());
}
TransferForms forms = TransferForms.of(statuses);
forms.selectAll();
PushToAggregate pushOp = new PushToAggregate(http, aggregateServer, briefcaseDir, forceSendBlank, PushFormToAggregate::onEvent);
JobsRunner.launchAsync(forms.map(pushOp::push), PushFormToAggregate::onError).waitForCompletion();
System.out.println();
System.out.println("All operations completed");
System.out.println();
}
use of org.opendatakit.briefcase.reused.transfer.AggregateServer in project briefcase by opendatakit.
the class Export method export.
public static void export(Path storageDir, String formid, Path exportDir, String baseFilename, boolean exportMedia, boolean overwriteFiles, boolean pullBefore, Optional<LocalDate> startDate, Optional<LocalDate> endDate, Optional<Path> maybePemFile, boolean splitSelectMultiples, boolean includeGeoJsonExport, boolean removeGroupNames, boolean smartAppend) {
CliEventsCompanion.attach(log);
Path briefcaseDir = Common.getOrCreateBriefcaseDir(storageDir);
FormCache formCache = FormCache.from(briefcaseDir);
formCache.update();
BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
BriefcasePreferences exportPrefs = BriefcasePreferences.forClass(ExportPanel.class);
BriefcasePreferences pullPrefs = BriefcasePreferences.forClass(ExportPanel.class);
FormMetadataPort formMetadataPort = FileSystemFormMetadataAdapter.at(briefcaseDir);
int maxHttpConnections = appPreferences.getMaxHttpConnections().orElse(DEFAULT_HTTP_CONNECTIONS);
Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
Optional<BriefcaseFormDefinition> maybeFormDefinition = formCache.getForms().stream().filter(form -> form.getFormId().equals(formid)).findFirst();
createDirectories(exportDir);
BriefcaseFormDefinition formDefinition = maybeFormDefinition.orElseThrow(() -> new BriefcaseException("Form " + formid + " not found"));
FormDefinition formDef = FormDefinition.from(formDefinition);
System.out.println("Exporting form " + formDefinition.getFormName() + " (" + formDefinition.getFormId() + ") to: " + exportDir);
DateRange dateRange = new DateRange(startDate, endDate);
ExportConfiguration configuration = ExportConfiguration.Builder.empty().setExportFilename(baseFilename).setExportDir(exportDir).setPemFile(maybePemFile).setDateRange(dateRange).setPullBefore(pullBefore).setOverwriteFiles(overwriteFiles).setExportMedia(exportMedia).setSplitSelectMultiples(splitSelectMultiples).setIncludeGeoJsonExport(includeGeoJsonExport).setRemoveGroupNames(removeGroupNames).setSmartAppend(smartAppend).build();
FormStatus formStatus = new FormStatus(formDefinition);
FormKey key = FormKey.from(formStatus);
FormMetadata formMetadata = formMetadataPort.fetch(key).orElseThrow(BriefcaseException::new);
Job<Void> pullJob = Job.noOpSupplier();
if (configuration.resolvePullBefore()) {
Optional<AggregateServer> server = AggregateServer.readFromPrefs(appPreferences, pullPrefs, formStatus);
if (server.isPresent()) {
Optional<Cursor> lastCursor = appPreferences.resolveStartFromLast() ? formMetadataPort.query(lastCursorOf(key)) : Optional.empty();
pullJob = new PullFromAggregate(http, server.get(), briefcaseDir, false, Export::onEvent, formMetadataPort).pull(formStatus, lastCursor);
}
}
Job<Void> exportJob = Job.run(runnerStatus -> ExportToCsv.export(formMetadataPort, formMetadata, formStatus, formDef, briefcaseDir, configuration));
Job<Void> exportGeoJsonJob = configuration.resolveIncludeGeoJsonExport() ? Job.run(runnerStatus -> ExportToGeoJson.export(formMetadata, formDef, configuration)) : Job.noOp;
Job<Void> job = pullJob.thenRun(exportJob).thenRun(exportGeoJsonJob).thenRun(__ -> exportPrefs.put(buildExportDateTimePrefix(formDefinition.getFormId()), LocalDateTime.now().format(ISO_DATE_TIME)));
JobsRunner.launchAsync(job, Export::onError).waitForCompletion();
System.out.println();
System.out.println("All operations completed");
System.out.println();
}
Aggregations