Search in sources :

Example 1 with BriefcaseException

use of org.opendatakit.briefcase.reused.BriefcaseException in project briefcase by opendatakit.

the class Cli method run.

/**
 * Runs the command line program
 *
 * @param args command line arguments
 * @see <a href="https://blog.idrsolutions.com/2015/03/java-8-consumer-supplier-explained-in-5-minutes/">Java 8 consumer supplier explained in 5 minutes</a>
 */
public void run(String[] args) {
    Set<Param> allParams = getAllParams();
    CommandLine cli = getCli(args, allParams);
    try {
        requiredOperations.forEach(operation -> {
            checkForMissingParams(cli, operation.requiredParams);
            operation.argsConsumer.accept(Args.from(cli, operation.requiredParams));
        });
        operations.forEach(operation -> {
            if (cli.hasOption(operation.param.shortCode)) {
                checkForMissingParams(cli, operation.requiredParams);
                operation.argsConsumer.accept(Args.from(cli, operation.getAllParams()));
                executedOperations.add(operation);
            }
        });
        if (executedOperations.isEmpty())
            otherwiseRunnables.forEach(Runnable::run);
    } catch (BriefcaseException e) {
        System.err.println("Error: " + e.getMessage());
        log.error("Error", e);
        System.exit(1);
    } catch (Throwable t) {
        System.err.println("Briefcase unexpected error. Please review the logs and contact maintainers on the following URLs:");
        System.err.println("\thttps://opendatakit.slack.com/messages/C374LNDK9/");
        System.err.println("\thttps://forum.opendatakit.org/c/support");
        log.error("Error", t);
        System.exit(1);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException)

Example 2 with BriefcaseException

use of org.opendatakit.briefcase.reused.BriefcaseException in project briefcase by opendatakit.

the class Export method export.

public static void export(String storageDir, String formid, Path exportPath, String baseFilename, boolean includeMediaFiles, boolean overwriteFiles, Optional<LocalDate> startDate, Optional<LocalDate> endDate, Optional<Path> maybePemFile) {
    CliEventsCompanion.attach(log);
    bootCache(storageDir);
    Optional<BriefcaseFormDefinition> maybeFormDefinition = FileSystemUtils.getBriefcaseFormList().stream().filter(form -> form.getFormId().equals(formid)).findFirst();
    BriefcaseFormDefinition formDefinition = maybeFormDefinition.orElseThrow(() -> new FormNotFoundException(formid));
    if (formDefinition.isFileEncryptedForm() || formDefinition.isFieldEncryptedForm()) {
        Path pemFile = maybePemFile.filter(Files::exists).orElseThrow(() -> new BriefcaseException("Missing pem file configuration"));
        try (PEMReader rdr = new PEMReader(new BufferedReader(new InputStreamReader(Files.newInputStream(pemFile), "UTF-8")))) {
            Object o = Optional.ofNullable(rdr.readObject()).orElseThrow(() -> new BriefcaseException("Can't parse Pem file"));
            Optional<PrivateKey> privKey;
            if (o instanceof KeyPair)
                privKey = Optional.of(((KeyPair) o).getPrivate());
            else if (o instanceof PrivateKey)
                privKey = Optional.of((PrivateKey) o);
            else
                privKey = Optional.empty();
            formDefinition.setPrivateKey(privKey.orElseThrow(() -> new BriefcaseException("No private key found on Pem file")));
            EventBus.publish(new ExportProgressEvent("Successfully parsed Pem file", formDefinition));
        } catch (IOException e) {
            throw new BriefcaseException("Can't parse Pem file");
        }
    }
    System.out.println("Exporting form " + formDefinition.getFormName() + " (" + formDefinition.getFormId() + ") to: " + exportPath);
    ExportToCsv.export(exportPath, formDefinition, baseFilename, includeMediaFiles, overwriteFiles, startDate, endDate);
    BriefcasePreferences.forClass(ExportPanel.class).put(buildExportDateTimePrefix(formDefinition.getFormId()), LocalDateTime.now().format(ISO_DATE_TIME));
}
Also used : Common.bootCache(org.opendatakit.briefcase.operations.Common.bootCache) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) ExportProgressEvent(org.opendatakit.briefcase.model.ExportProgressEvent) LocalDateTime(java.time.LocalDateTime) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) LoggerFactory(org.slf4j.LoggerFactory) BriefcaseFormDefinition(org.opendatakit.briefcase.model.BriefcaseFormDefinition) FORM_ID(org.opendatakit.briefcase.operations.Common.FORM_ID) Operation(org.opendatakit.common.cli.Operation) ExportPanel(org.opendatakit.briefcase.ui.export.ExportPanel) EventBus(org.bushe.swing.event.EventBus) ISO_DATE_TIME(java.time.format.DateTimeFormatter.ISO_DATE_TIME) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) Path(java.nio.file.Path) FileSystemUtils(org.opendatakit.briefcase.util.FileSystemUtils) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Param(org.opendatakit.common.cli.Param) STORAGE_DIR(org.opendatakit.briefcase.operations.Common.STORAGE_DIR) PEMReader(org.bouncycastle.openssl.PEMReader) IOException(java.io.IOException) ExportToCsv(org.opendatakit.briefcase.util.ExportToCsv) InputStreamReader(java.io.InputStreamReader) ExportForms.buildExportDateTimePrefix(org.opendatakit.briefcase.export.ExportForms.buildExportDateTimePrefix) Paths(java.nio.file.Paths) PrivateKey(java.security.PrivateKey) LocalDate(java.time.LocalDate) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) Path(java.nio.file.Path) ExportPanel(org.opendatakit.briefcase.ui.export.ExportPanel) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) InputStreamReader(java.io.InputStreamReader) PEMReader(org.bouncycastle.openssl.PEMReader) IOException(java.io.IOException) ExportProgressEvent(org.opendatakit.briefcase.model.ExportProgressEvent) BriefcaseException(org.opendatakit.briefcase.reused.BriefcaseException) BufferedReader(java.io.BufferedReader) BriefcaseFormDefinition(org.opendatakit.briefcase.model.BriefcaseFormDefinition)

Aggregations

BriefcaseException (org.opendatakit.briefcase.reused.BriefcaseException)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ISO_DATE_TIME (java.time.format.DateTimeFormatter.ISO_DATE_TIME)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 CommandLine (org.apache.commons.cli.CommandLine)1 PEMReader (org.bouncycastle.openssl.PEMReader)1 EventBus (org.bushe.swing.event.EventBus)1 ExportForms.buildExportDateTimePrefix (org.opendatakit.briefcase.export.ExportForms.buildExportDateTimePrefix)1 BriefcaseFormDefinition (org.opendatakit.briefcase.model.BriefcaseFormDefinition)1 BriefcasePreferences (org.opendatakit.briefcase.model.BriefcasePreferences)1