Search in sources :

Example 1 with SERVER_URL

use of org.opendatakit.briefcase.operations.Common.SERVER_URL in project briefcase by opendatakit.

the class PullFormFromCentral method pullFromCentral.

private static void pullFromCentral(Args args) {
    CliEventsCompanion.attach(log);
    Path briefcaseDir = Common.getOrCreateBriefcaseDir(args.get(STORAGE_DIR));
    FormCache formCache = FormCache.from(briefcaseDir);
    formCache.update();
    BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
    FormMetadataPort formMetadataPort = FileSystemFormMetadataAdapter.at(briefcaseDir);
    int maxHttpConnections = Optionals.race(args.getOptional(MAX_HTTP_CONNECTIONS), appPreferences.getMaxHttpConnections()).orElse(DEFAULT_HTTP_CONNECTIONS);
    Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
    CentralServer server = CentralServer.of(args.get(SERVER_URL), args.get(PROJECT_ID), new Credentials(args.get(CREDENTIALS_EMAIL), args.get(CREDENTIALS_PASSWORD)));
    String token = http.execute(server.getSessionTokenRequest()).orElseThrow(() -> new BriefcaseException("Can't authenticate with ODK Central"));
    Response<List<RemoteFormDefinition>> response = http.execute(server.getFormsListRequest(token));
    if (!response.isSuccess()) {
        System.err.println(response.isRedirection() ? "Error connecting to Central: Redirection detected" : response.isUnauthorized() ? "Error connecting to Central: Wrong credentials" : response.isNotFound() ? "Error connecting to Central: Central not found" : "Error connecting to Central");
        return;
    }
    Optional<String> formId = args.getOptional(FORM_ID);
    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();
    PullFromCentral pullOp = new PullFromCentral(http, server, briefcaseDir, token, PullFormFromCentral::onEvent, formMetadataPort);
    JobsRunner.launchAsync(forms.map(pullOp::pull), PullFormFromCentral::onError).waitForCompletion();
    System.out.println();
    System.out.println("All operations completed");
    System.out.println();
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) FileSystemFormMetadataAdapter(org.opendatakit.briefcase.model.form.FileSystemFormMetadataAdapter) Credentials(org.opendatakit.briefcase.reused.http.Credentials) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) Optionals(org.opendatakit.briefcase.reused.Optionals) Response(org.opendatakit.briefcase.reused.http.response.Response) LoggerFactory(org.slf4j.LoggerFactory) DEFAULT_HTTP_CONNECTIONS(org.opendatakit.briefcase.reused.http.Http.DEFAULT_HTTP_CONNECTIONS) MAX_HTTP_CONNECTIONS(org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS) FORM_ID(org.opendatakit.briefcase.operations.Common.FORM_ID) Operation(org.opendatakit.common.cli.Operation) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) SERVER_URL(org.opendatakit.briefcase.operations.Common.SERVER_URL) CREDENTIALS_PASSWORD(org.opendatakit.briefcase.operations.Common.CREDENTIALS_PASSWORD) FormCache(org.opendatakit.briefcase.util.FormCache) Path(java.nio.file.Path) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) RemoteFormDefinition(org.opendatakit.briefcase.model.RemoteFormDefinition) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Logger(org.slf4j.Logger) Param(org.opendatakit.common.cli.Param) STORAGE_DIR(org.opendatakit.briefcase.operations.Common.STORAGE_DIR) JobsRunner(org.opendatakit.briefcase.reused.job.JobsRunner) Http(org.opendatakit.briefcase.reused.http.Http) PullFromCentral(org.opendatakit.briefcase.pull.central.PullFromCentral) Collectors.toList(java.util.stream.Collectors.toList) PROJECT_ID(org.opendatakit.briefcase.operations.Common.PROJECT_ID) List(java.util.List) CREDENTIALS_EMAIL(org.opendatakit.briefcase.operations.Common.CREDENTIALS_EMAIL) Optional(java.util.Optional) Args(org.opendatakit.common.cli.Args) FormCache(org.opendatakit.briefcase.util.FormCache) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) PullFromCentral(org.opendatakit.briefcase.pull.central.PullFromCentral) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) Http(org.opendatakit.briefcase.reused.http.Http) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) FormStatus(org.opendatakit.briefcase.model.FormStatus) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Credentials(org.opendatakit.briefcase.reused.http.Credentials)

Example 2 with SERVER_URL

use of org.opendatakit.briefcase.operations.Common.SERVER_URL in project briefcase by opendatakit.

the class Launcher method main.

public static void main(String[] args) {
    BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
    if (!appPreferences.hasKey(BRIEFCASE_TRACKING_CONSENT_PROPERTY))
        appPreferences.put(BRIEFCASE_TRACKING_CONSENT_PROPERTY, TRUE.toString());
    Optional<SentryClient> sentry = SENTRY_ENABLED ? Optional.of(initSentryClient(appPreferences)) : Optional.empty();
    new Cli().deprecate(DEPRECATED_PULL_AGGREGATE, PULL_AGGREGATE).deprecate(DEPRECATED_PULL_IN_PARALLEL, MAX_HTTP_CONNECTIONS).deprecate(DEPRECATED_AGGREGATE_SERVER, SERVER_URL).register(PULL_FORM_FROM_AGGREGATE).register(PullFormFromCentral.OPERATION).register(PUSH_FORM_TO_AGGREGATE).register(PushFormToCentral.OPERATION).register(IMPORT_FROM_ODK).register(EXPORT_FORM).register(CLEAR_PREFS).otherwise((cli, commandLine) -> {
        if (args.length == 0)
            launchGUI();
        else
            runLegacyCli(commandLine, cli::printHelp);
    }).onError(throwable -> {
        System.err.println(throwable instanceof BriefcaseException ? "Error: " + throwable.getMessage() : "Unexpected error in Briefcase. Please review briefcase.log for more information. For help, post to https://forum.getodk.org/c/support");
        log.error("Error", throwable);
        sentry.ifPresent(client -> client.sendException(throwable));
        System.exit(1);
    }).run(args);
}
Also used : EXPORT_FORM(org.opendatakit.briefcase.operations.Export.EXPORT_FORM) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) LoggerFactory(org.slf4j.LoggerFactory) DEPRECATED_PULL_AGGREGATE(org.opendatakit.briefcase.operations.PullFormFromAggregate.DEPRECATED_PULL_AGGREGATE) PULL_AGGREGATE(org.opendatakit.briefcase.operations.PullFormFromAggregate.PULL_AGGREGATE) BRIEFCASE_TRACKING_CONSENT_PROPERTY(org.opendatakit.briefcase.model.BriefcasePreferences.BRIEFCASE_TRACKING_CONSENT_PROPERTY) PushFormToCentral(org.opendatakit.briefcase.operations.PushFormToCentral) MAX_HTTP_CONNECTIONS(org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS) MainBriefcaseWindow.launchGUI(org.opendatakit.briefcase.ui.MainBriefcaseWindow.launchGUI) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) SERVER_URL(org.opendatakit.briefcase.operations.Common.SERVER_URL) BriefcaseCLI.runLegacyCli(org.opendatakit.briefcase.ui.BriefcaseCLI.runLegacyCli) Sentry(io.sentry.Sentry) CLEAR_PREFS(org.opendatakit.briefcase.operations.ClearPreferences.CLEAR_PREFS) IMPORT_FROM_ODK(org.opendatakit.briefcase.operations.ImportFromODK.IMPORT_FROM_ODK) Logger(org.slf4j.Logger) PullFormFromCentral(org.opendatakit.briefcase.operations.PullFormFromCentral) SentryClient(io.sentry.SentryClient) PUSH_FORM_TO_AGGREGATE(org.opendatakit.briefcase.operations.PushFormToAggregate.PUSH_FORM_TO_AGGREGATE) Host.getOsName(org.opendatakit.briefcase.util.Host.getOsName) VERSION(org.opendatakit.briefcase.buildconfig.BuildConfig.VERSION) Cli(org.opendatakit.common.cli.Cli) SENTRY_DSN(org.opendatakit.briefcase.buildconfig.BuildConfig.SENTRY_DSN) SENTRY_ENABLED(org.opendatakit.briefcase.buildconfig.BuildConfig.SENTRY_ENABLED) DEPRECATED_AGGREGATE_SERVER(org.opendatakit.briefcase.operations.Common.DEPRECATED_AGGREGATE_SERVER) Optional(java.util.Optional) DEPRECATED_PULL_IN_PARALLEL(org.opendatakit.briefcase.operations.PullFormFromAggregate.DEPRECATED_PULL_IN_PARALLEL) TRUE(java.lang.Boolean.TRUE) PULL_FORM_FROM_AGGREGATE(org.opendatakit.briefcase.operations.PullFormFromAggregate.PULL_FORM_FROM_AGGREGATE) BriefcaseCLI.runLegacyCli(org.opendatakit.briefcase.ui.BriefcaseCLI.runLegacyCli) Cli(org.opendatakit.common.cli.Cli) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) SentryClient(io.sentry.SentryClient)

Example 3 with SERVER_URL

use of org.opendatakit.briefcase.operations.Common.SERVER_URL in project briefcase by opendatakit.

the class PushFormToCentral method pushToCentral.

private static void pushToCentral(Args args) {
    CliEventsCompanion.attach(log);
    Path briefcaseDir = Common.getOrCreateBriefcaseDir(args.get(STORAGE_DIR));
    FormCache formCache = FormCache.from(briefcaseDir);
    formCache.update();
    BriefcasePreferences appPreferences = BriefcasePreferences.appScoped();
    int maxHttpConnections = Optionals.race(args.getOptional(MAX_HTTP_CONNECTIONS), appPreferences.getMaxHttpConnections()).orElse(DEFAULT_HTTP_CONNECTIONS);
    Http http = appPreferences.getHttpProxy().map(host -> CommonsHttp.of(maxHttpConnections, host)).orElseGet(() -> CommonsHttp.of(maxHttpConnections));
    CentralServer server = CentralServer.of(args.get(SERVER_URL), args.get(PROJECT_ID), new Credentials(args.get(CREDENTIALS_EMAIL), args.get(CREDENTIALS_PASSWORD)));
    String token = http.execute(server.getSessionTokenRequest()).orElseThrow(() -> new BriefcaseException("Can't authenticate with ODK Central"));
    List<FormStatus> statuses;
    if (args.getOptional(FORM_ID).isPresent()) {
        String requestedFormId = args.getOptional(FORM_ID).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();
    PushToCentral pushOp = new PushToCentral(http, server, briefcaseDir, token, PushFormToCentral::onEvent);
    JobsRunner.launchAsync(forms.map(pushOp::push), PushFormToCentral::onError).waitForCompletion();
    System.out.println();
    System.out.println("All operations completed");
    System.out.println();
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) Credentials(org.opendatakit.briefcase.reused.http.Credentials) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) PushToCentral(org.opendatakit.briefcase.push.central.PushToCentral) Optionals(org.opendatakit.briefcase.reused.Optionals) LoggerFactory(org.slf4j.LoggerFactory) DEFAULT_HTTP_CONNECTIONS(org.opendatakit.briefcase.reused.http.Http.DEFAULT_HTTP_CONNECTIONS) MAX_HTTP_CONNECTIONS(org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS) FORM_ID(org.opendatakit.briefcase.operations.Common.FORM_ID) Operation(org.opendatakit.common.cli.Operation) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) SERVER_URL(org.opendatakit.briefcase.operations.Common.SERVER_URL) CREDENTIALS_PASSWORD(org.opendatakit.briefcase.operations.Common.CREDENTIALS_PASSWORD) FormCache(org.opendatakit.briefcase.util.FormCache) Path(java.nio.file.Path) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Logger(org.slf4j.Logger) Param(org.opendatakit.common.cli.Param) STORAGE_DIR(org.opendatakit.briefcase.operations.Common.STORAGE_DIR) JobsRunner(org.opendatakit.briefcase.reused.job.JobsRunner) Collectors(java.util.stream.Collectors) Http(org.opendatakit.briefcase.reused.http.Http) PROJECT_ID(org.opendatakit.briefcase.operations.Common.PROJECT_ID) List(java.util.List) CREDENTIALS_EMAIL(org.opendatakit.briefcase.operations.Common.CREDENTIALS_EMAIL) Args(org.opendatakit.common.cli.Args) FormCache(org.opendatakit.briefcase.util.FormCache) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) Http(org.opendatakit.briefcase.reused.http.Http) PushToCentral(org.opendatakit.briefcase.push.central.PushToCentral) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) TransferForms(org.opendatakit.briefcase.transfer.TransferForms) FormStatus(org.opendatakit.briefcase.model.FormStatus) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Credentials(org.opendatakit.briefcase.reused.http.Credentials)

Aggregations

BriefcasePreferences (org.opendatakit.briefcase.model.BriefcasePreferences)3 MAX_HTTP_CONNECTIONS (org.opendatakit.briefcase.operations.Common.MAX_HTTP_CONNECTIONS)3 SERVER_URL (org.opendatakit.briefcase.operations.Common.SERVER_URL)3 BriefcaseException (org.opendatakit.briefcase.reused.BriefcaseException)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Path (java.nio.file.Path)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 FormStatus (org.opendatakit.briefcase.model.FormStatus)2 FormStatusEvent (org.opendatakit.briefcase.model.FormStatusEvent)2 CREDENTIALS_EMAIL (org.opendatakit.briefcase.operations.Common.CREDENTIALS_EMAIL)2 CREDENTIALS_PASSWORD (org.opendatakit.briefcase.operations.Common.CREDENTIALS_PASSWORD)2 FORM_ID (org.opendatakit.briefcase.operations.Common.FORM_ID)2 PROJECT_ID (org.opendatakit.briefcase.operations.Common.PROJECT_ID)2 STORAGE_DIR (org.opendatakit.briefcase.operations.Common.STORAGE_DIR)2 Optionals (org.opendatakit.briefcase.reused.Optionals)2 CommonsHttp (org.opendatakit.briefcase.reused.http.CommonsHttp)2 Credentials (org.opendatakit.briefcase.reused.http.Credentials)2