class XESExportMain {

    public static void main(String[] args) {
        // create the Options
        Options options = getOptions();

        CommandLineParser parser = new DefaultParser();
        try (BasicDataSource ds = new BasicDataSource()) {
            // parse the command line arguments
            CommandLine line = parser.parse(options, args);

            ds.setDriverClassName(line.getOptionValue("driver"));
            ds.setUrl(line.getOptionValue("url"));
            ds.setUsername(line.getOptionValue("user"));
            ds.setPassword(line.getOptionValue("password"));
            ds.setDefaultReadOnly(true);
//          Test connection
            ds.getConnection().close();

            DataSetService dataSetService = new DataSetServiceImpl(() -> ds);
            XESExportServiceImpl service = new XESExportServiceImpl();
            service.setDataSetService(dataSetService);
            final XESProcessFilter.Builder filter = XESProcessFilter.builder();
            filter.withProcessId(line.getOptionValue("process"));
            if (line.hasOption("version")) {
                filter.withProcessVersion(line.getOptionValue("version"));
            }
            if (line.hasOption("status")) {
                List<Integer> status = new ArrayList<>();
                for (String statusLine : line.getOptionValues("status")) {
                    try {
                        final Integer pStatus = Integer.valueOf(statusLine);
                        //only add valid status
                        if (pStatus >= 0 && pStatus <= 4) {
                            status.add(pStatus);
                        }
                    } catch (NumberFormatException ex) {
                        System.err.println("Invalid process status number for input: " + statusLine + ", valid status are number between 0 and 4.");
                    }
                }
                if (status.isEmpty() == false) {
                    filter.withStatus(status);
                }
            }
            if (line.hasOption("logtype")) {
                filter.withNodeInstanceLogType(Integer.valueOf(line.getOptionValue("logtype")));
            }
            if (line.hasOption("nodetypes")) {
                filter.withAllNodeTypes();
            }

            final String xml = service.export(filter.build());
            if (line.hasOption("file")) {
                FileUtils.write(new File(line.getOptionValue("file")), xml, "UTF-8");
            } else {
                System.out.println(xml);
            }
        } catch (ParseException exp) {
            System.err.println("Parsing options failed. Reason: " + exp.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("xes", options, true);
            System.exit(-1);
        } catch (Exception ex) {
            System.err.println("Failed to execute export due to: " + ex.getMessage());
            ex.printStackTrace();
            System.exit(-1);
        }
    }

}