Example usage for org.apache.commons.csv CSVStrategy CSVStrategy

List of usage examples for org.apache.commons.csv CSVStrategy CSVStrategy

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVStrategy CSVStrategy.

Prototype

public CSVStrategy(char delimiter, char encapsulator, char commentStart) 

Source Link

Usage

From source file:GIST.IzbirkomExtractor.IzbirkomExtractor.java

/**
 * @param args//from  ww w .  j a  v  a  2  s . c  om
 */
public static void main(String[] args) {

    // process command-line options
    Options options = new Options();
    options.addOption("n", "noaddr", false, "do not do any address matching (for testing)");
    options.addOption("i", "info", false, "create and populate address information table");
    options.addOption("h", "help", false, "this message");

    // database connection
    options.addOption("s", "server", true, "database server to connect to");
    options.addOption("d", "database", true, "OSM database name");
    options.addOption("u", "user", true, "OSM database user name");
    options.addOption("p", "pass", true, "OSM database password");

    // logging options
    options.addOption("l", "logdir", true, "log file directory (default './logs')");
    options.addOption("e", "loglevel", true, "log level (default 'FINEST')");

    // automatically generate the help statement
    HelpFormatter help_formatter = new HelpFormatter();

    // database URI for connection
    String dburi = null;

    // Information message for help screen
    String info_msg = "IzbirkomExtractor [options] <html_directory>";

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption('h') || cmd.getArgs().length != 1) {
            help_formatter.printHelp(info_msg, options);
            System.exit(1);
        }

        /* prohibit n and i together */
        if (cmd.hasOption('n') && cmd.hasOption('i')) {
            System.err.println("Options 'n' and 'i' cannot be used together.");
            System.exit(1);
        }

        /* require database arguments without -n */
        if (cmd.hasOption('n')
                && (cmd.hasOption('s') || cmd.hasOption('d') || cmd.hasOption('u') || cmd.hasOption('p'))) {
            System.err.println("Options 'n' and does not need any databse parameters.");
            System.exit(1);
        }

        /* require all 4 database options to be used together */
        if (!cmd.hasOption('n')
                && !(cmd.hasOption('s') && cmd.hasOption('d') && cmd.hasOption('u') && cmd.hasOption('p'))) {
            System.err.println(
                    "For database access all of the following arguments have to be specified: server, database, user, pass");
            System.exit(1);
        }

        /* useful variables */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm");
        String dateString = formatter.format(new Date());

        /* setup logging */
        File logdir = new File(cmd.hasOption('l') ? cmd.getOptionValue('l') : "logs");
        FileUtils.forceMkdir(logdir);
        File log_file_name = new File(
                logdir + "/" + IzbirkomExtractor.class.getName() + "-" + formatter.format(new Date()) + ".log");
        FileHandler log_file = new FileHandler(log_file_name.getPath());

        /* create "latest" link to currently created log file */
        Path latest_log_link = Paths.get(logdir + "/latest");
        Files.deleteIfExists(latest_log_link);
        Files.createSymbolicLink(latest_log_link, Paths.get(log_file_name.getName()));

        log_file.setFormatter(new SimpleFormatter());
        LogManager.getLogManager().reset(); // prevents logging to console
        logger.addHandler(log_file);
        logger.setLevel(cmd.hasOption('e') ? Level.parse(cmd.getOptionValue('e')) : Level.FINEST);

        // open directory with HTML files and create file list
        File dir = new File(cmd.getArgs()[0]);
        if (!dir.isDirectory()) {
            System.err.println("Unable to find directory '" + cmd.getArgs()[0] + "', exiting");
            System.exit(1);
        }
        PathMatcher pmatcher = FileSystems.getDefault()
                .getPathMatcher("glob:?  * ?*.html");
        ArrayList<File> html_files = new ArrayList<>();
        for (Path file : Files.newDirectoryStream(dir.toPath()))
            if (pmatcher.matches(file.getFileName()))
                html_files.add(file.toFile());
        if (html_files.size() == 0) {
            System.err.println("No matching HTML files found in '" + dir.getAbsolutePath() + "', exiting");
            System.exit(1);
        }

        // create csvResultSink
        FileOutputStream csvout_file = new FileOutputStream("parsed_addresses-" + dateString + ".csv");
        OutputStreamWriter csvout = new OutputStreamWriter(csvout_file, "UTF-8");
        ResultSink csvResultSink = new CSVResultSink(csvout, new CSVStrategy('|', '"', '#'));

        // Connect to DB and osmAddressMatcher
        AddressMatcher osmAddressMatcher;
        DBSink dbSink = null;
        DBInfoSink dbInfoSink = null;
        if (cmd.hasOption('n')) {
            osmAddressMatcher = new DummyAddressMatcher();
        } else {
            dburi = "jdbc:postgresql://" + cmd.getOptionValue('s') + "/" + cmd.getOptionValue('d');
            Connection con = DriverManager.getConnection(dburi, cmd.getOptionValue('u'),
                    cmd.getOptionValue('p'));
            osmAddressMatcher = new OsmAddressMatcher(con);
            dbSink = new DBSink(con);
            if (cmd.hasOption('i'))
                dbInfoSink = new DBInfoSink(con);
        }

        /* create resultsinks */
        SinkMultiplexor sm = SinkMultiplexor.newSinkMultiplexor();
        sm.addResultSink(csvResultSink);
        if (dbSink != null) {
            sm.addResultSink(dbSink);
            if (dbInfoSink != null)
                sm.addResultSink(dbInfoSink);
        }

        // create tableExtractor
        TableExtractor te = new TableExtractor(osmAddressMatcher, sm);

        // TODO: printout summary of options: processing date/time, host, directory of HTML files, jdbc uri, command line with parameters

        // iterate through files
        logger.info("Start processing " + html_files.size() + " files in " + dir);
        for (int i = 0; i < html_files.size(); i++) {
            System.err.println("Parsing #" + i + ": " + html_files.get(i));
            te.processHTMLfile(html_files.get(i));
        }

        System.err.println("Processed " + html_files.size() + " HTML files");
        logger.info("Finished processing " + html_files.size() + " files in " + dir);

    } catch (ParseException e1) {
        System.err.println("Failed to parse CLI: " + e1.getMessage());
        help_formatter.printHelp(info_msg, options);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("I/O Exception: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    } catch (SQLException e) {
        System.err.println("Database '" + dburi + "': " + e.getMessage());
        System.exit(1);
    } catch (ResultSinkException e) {
        System.err.println("Failed to initialize ResultSink: " + e.getMessage());
        System.exit(1);
    } catch (TableExtractorException e) {
        System.err.println("Failed to initialize Table Extractor: " + e.getMessage());
        System.exit(1);
    } catch (CloneNotSupportedException | IllegalAccessException | InstantiationException e) {
        System.err.println("Something really odd happened: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.px100systems.util.CsvParser.java

/**
 * Custom constructor for non-standard CSV files
 * @param csvSeparator separator (,)//from   ww w  .j av a 2 s  .c o  m
 * @param csvQuote quote (")
 * @param csvComment comment (#)
 */
public CsvParser(char csvSeparator, char csvQuote, char csvComment) {
    csvStrategy = new CSVStrategy(csvSeparator, csvQuote, csvComment);
}

From source file:com.cloudera.science.ml.parallel.normalize.StringSplitFn.java

@Override
public void initialize() {
    this.csvStrategy = new CSVStrategy(delim, quote, comment);
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.BorgSelectionReader.java

/**
 * Constructor setting SSL certificates or bypassing them. Bypassing is easier since OPs 
 * sometimes change their certificates requiring you to re-download and store them
 * @param databasetype// ww w . j a  va 2  s . c om
 */
public BorgSelectionReader(DatabaseType databasetype) {
    this.databasetype = databasetype;
    this.strategy = new CSVStrategy(',', '"', '#');
    System.out.println(System.getProperty("java.home"));

    SSLReaderUtilities.bypassSSLAuthentication();

    //System.setProperty("javax.net.ssl.trustStore", Extractor.chooseCACertsPath()+"cacerts");
    //System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

    //System.out.println("javax.net.ssl.trustStore = "+System.getProperty("javax.net.ssl.trustStore"));
}

From source file:ispyb.client.common.shipping.CreateShippingFileAction.java

/**
 * uploadCsvFile//from   ww w  .  ja v a2s  . co  m
 * 
 * @param mapping
 * @param actForm
 * @param request
 * @param in_reponse
 * @return
 */
@SuppressWarnings({ "deprecation", "unchecked" })
public ActionForward uploadCsvFile(ActionMapping mapping, ActionForm actForm, HttpServletRequest request,
        HttpServletResponse in_reponse) {
    ActionMessages errors = new ActionMessages();
    ActionMessages messages = new ActionMessages();
    try {
        CreateShippingFileForm form = (CreateShippingFileForm) actForm; // Parameters submitted by form
        String currentProposalCode = (String) request.getSession().getAttribute(Constants.PROPOSAL_CODE);
        String currentProposalNumber = (String) request.getSession().getAttribute(Constants.PROPOSAL_NUMBER);
        Integer proposalId = (Integer) request.getSession().getAttribute(Constants.PROPOSAL_ID);
        boolean errorFile = false;
        Proposal3VO proposalVO = proposalService.findByPk(proposalId);
        List<LabContact3VO> listLabContacts = labContactService.findFiltered(proposalId, null);
        LabContact3VO shippingLabContactVO = null;
        if (listLabContacts != null && listLabContacts.size() > 0) {
            shippingLabContactVO = listLabContacts.get(0);
        } else {
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("error.user.upload", "No labConctact found"));
            errorFile = true;
        }
        List<String> allowedSpaceGroups = (List<String>) request.getSession()
                .getAttribute(Constants.ISPYB_ALLOWED_SPACEGROUPS_LIST);
        String fieldSeparator = form.getFieldSeparator();
        String textSeparator = form.getTextSeparator();
        char fieldSep = fieldSeparator.charAt(0);
        char textSep = ' ';
        if (textSeparator.length() > 0)
            textSep = textSeparator.charAt(0);

        String uploadedFileName;
        String realCSVPath;

        List<Shipping3VO> listShippingCreated = new ArrayList<Shipping3VO>();
        List<Dewar3VO> listDewarCreated = new ArrayList<Dewar3VO>();
        List<Container3VO> listContainerCreated = new ArrayList<Container3VO>();
        // List<Crystal3VO> listCrystalCreated = new ArrayList<Crystal3VO>();
        List<Crystal3VO> listCrystalCreated = crystalService.findByProposalId(proposalId);
        List<DiffractionPlan3VO> listDifPlanCreated = new ArrayList<DiffractionPlan3VO>();
        List<BLSample3VO> listSampleCreated = new ArrayList<BLSample3VO>();

        if (request != null) {
            uploadedFileName = form.getRequestFile().getFileName();
            if (uploadedFileName.equals("")) {
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("error.user.shipping.upload.file.empty"));
                errorFile = true;
            } else if (!uploadedFileName.endsWith(".csv")) {
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("error.user.shipping.upload.file.format"));
                errorFile = true;
            }

            if (!errorFile) {
                realCSVPath = request.getRealPath("/") + "/tmp/" + uploadedFileName;
                // Write the received file to tmp directory
                FormFile f = form.getRequestFile();
                InputStream in = f.getInputStream();
                File outputFile = new File(realCSVPath);
                if (outputFile.exists())
                    outputFile.delete();
                FileOutputStream out = new FileOutputStream(outputFile);
                while (in.available() != 0) {
                    out.write(in.read());
                    out.flush();
                }
                out.flush();
                out.close();

                // received file
                Reader inFile = new FileReader(realCSVPath);
                LOG.info(" ---[uploadFile] Upload Shipment csv ");
                CSVStrategy csvStrategy = new CSVStrategy(fieldSep, textSep, CSVStrategy.COMMENTS_DISABLED);
                CSVParser parser = new CSVParser(inFile, csvStrategy);
                String[][] values = parser.getAllValues();
                int nbRows = values.length;
                boolean isError = false;
                for (int i = 0; i < nbRows; i++) {
                    int nbCol = values[i].length;
                    if (nbCol != NB_COL) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The number of columns is incorrect (" + nbCol + " instead of " + NB_COL
                                                + ")"));
                        isError = true;
                        break;
                    }
                    int j = 0;
                    // proposalCode & proposalNumber
                    String proposalCode = values[i][j++];
                    String proposalNumber = values[i][j++];
                    if (proposalCode == null || proposalNumber == null
                            || !(proposalCode.equalsIgnoreCase(currentProposalCode))
                            || !(proposalNumber.equals(currentProposalNumber))) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The proposal is incorrect line " + (i + 1)));
                        isError = true;
                        break;
                    }
                    // visitNumber
                    j++;
                    // shippingName
                    String shippingName = values[i][j++];
                    if (shippingName == null || shippingName.trim().length() == 0
                            || shippingName.length() > MAX_SHIPPING_NAME) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The shipping name is incorrect line " + (i + 1) + " (required and < "
                                                + MAX_SHIPPING_NAME + " characters)"));
                        isError = true;
                        break;
                    }
                    // dewarCode
                    String dewarCode = values[i][j++];
                    if (dewarCode == null || dewarCode.trim().length() == 0
                            || dewarCode.length() > MAX_DEWAR_CODE) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The dewar code is incorrect line " + (i + 1) + " (required and < "
                                                + MAX_DEWAR_CODE + " characters)"));
                        isError = true;
                        break;
                    }
                    // containerCode
                    String containerCode = values[i][j++];
                    if (containerCode == null || containerCode.trim().length() == 0
                            || containerCode.length() > MAX_CONTAINER_CODE) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The container code is incorrect line " + (i + 1) + " (required and < "
                                                + MAX_CONTAINER_CODE + " characters)"));
                        isError = true;
                        break;
                    }
                    // preObsResolution
                    String preObsResolutionS = values[i][j++];
                    Double preObsResolution = null;
                    if (preObsResolutionS == null || preObsResolutionS.trim().length() != 0) {
                        try {
                            preObsResolution = Double.parseDouble(preObsResolutionS);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for preObsResolution line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // neededResolution
                    String neededResolutionS = values[i][j++];
                    Double neededResolution = null;
                    if (neededResolutionS == null || neededResolutionS.trim().length() != 0) {
                        try {
                            neededResolution = Double.parseDouble(neededResolutionS);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for neededResolution line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // oscillationRange
                    String oscillationRangeS = values[i][j++];
                    Double oscillationRange = null;
                    if (oscillationRangeS == null || oscillationRangeS.trim().length() != 0) {
                        try {
                            oscillationRange = Double.parseDouble(oscillationRangeS);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for oscillationRange line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // proteinAcronym
                    String proteinAcronym = values[i][j++];
                    Protein3VO protein = null;
                    if (proteinAcronym == null || proteinAcronym.trim().length() == 0) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The protein Acronym is required line " + (i + 1)));
                        isError = true;
                        break;
                    }
                    List<Protein3VO> proteinTab = proteinService.findByAcronymAndProposalId(proposalId,
                            proteinAcronym);
                    if (proteinTab == null || proteinTab.size() == 0) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                                "error.user.shipping.upload.file.data",
                                "Protein " + proteinAcronym + " can not be found in ISPyB, line " + (i + 1)));
                        isError = true;
                        break;
                    } else {
                        protein = proteinTab.get(0);
                    }
                    // proteinName
                    String proteinName = values[i][j++];
                    if (proteinName == null || proteinName.trim().length() == 0
                            || !protein.getName().equalsIgnoreCase(proteinName)) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                                "error.user.shipping.upload.file.data",
                                "The protein name is incorrect line " + (i + 1)
                                        + " (required and must correspond to the proteinAcronym). The expected proteinName is "
                                        + protein.getName()));
                        isError = true;
                        break;
                    }
                    // spaceGroup
                    String spaceGroup = values[i][j++];
                    if (spaceGroup != null && !spaceGroup.equals("")
                            && !allowedSpaceGroups.contains(spaceGroup.toUpperCase())) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The space group can not be found in ISPyB line " + (i + 1)));
                        isError = true;
                        break;
                    }
                    // sampleBarcode
                    String sampleBarcode = values[i][j++];
                    if (sampleBarcode != null && sampleBarcode.length() > MAX_SAMPLE_BARCODE) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The sample barcode is incorrect line " + (i + 1) + " (< "
                                                + MAX_SAMPLE_BARCODE + " characters)"));
                        isError = true;
                        break;
                    }
                    // sampleName
                    String sampleName = values[i][j++];
                    if (sampleName == null || sampleName.trim().length() == 0
                            || sampleName.length() > MAX_SAMPLE_NAME || !sampleName
                                    .matches(Constants.MASK_BASIC_CHARACTERS_WITH_DASH_UNDERSCORE_NO_SPACE)) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                                "error.user.shipping.upload.file.data",
                                "The sample name is incorrect line " + (i + 1) + " (required and  < "
                                        + MAX_SAMPLE_NAME
                                        + " characters, unique name for the protein acronym, must contain only a-z, A-Z or 0-9 or - or _ characters.)"));
                        isError = true;
                        break;
                    }
                    // samplePosition
                    String samplePos = values[i][j++];
                    int samplePosition = 0;
                    try {
                        samplePosition = Integer.parseInt(samplePos);
                    } catch (NumberFormatException e) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The sample position is incorrect: " + samplePos + ", line " + (i + 1)
                                                + " (required number between 1 and 10)"));
                        isError = true;
                        break;
                    }
                    if (samplePosition < 1 || samplePosition > Constants.BASKET_SAMPLE_CAPACITY) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The sample position is incorrect: " + samplePos + ", line " + (i + 1)
                                                + " (required number between 1 and 10)"));
                        isError = true;
                        break;
                    }
                    // sample comments
                    String sampleComments = values[i][j++];
                    if (sampleComments != null && sampleComments.length() > MAX_SAMPLE_COMMENTS) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The sample comments is incorrect line " + (i + 1) + " ( < "
                                                + MAX_SAMPLE_COMMENTS + " characters)"));
                        isError = true;
                        break;
                    }
                    // cell_a
                    String cellA = values[i][j++];
                    Double cell_a = null;
                    if (cellA == null || cellA.trim().length() != 0) {
                        try {
                            cell_a = Double.parseDouble(cellA);
                        } catch (NumberFormatException e) {
                            isError = true;
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for cell_a line " + (i + 1)));
                            break;
                        }
                    }
                    // cell_b
                    String cellB = values[i][j++];
                    Double cell_b = null;
                    if (cellB == null || cellB.trim().length() != 0) {
                        try {
                            cell_b = Double.parseDouble(cellB);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for cell_b line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // cell_c
                    String cellC = values[i][j++];
                    Double cell_c = null;
                    if (cellC == null || cellC.trim().length() != 0) {
                        try {
                            cell_c = Double.parseDouble(cellC);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for cell_c line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // cell_alpha
                    String cellAlpha = values[i][j++];
                    Double cell_alpha = null;
                    if (cellAlpha == null || cellAlpha.trim().length() != 0) {
                        try {
                            cell_alpha = Double.parseDouble(cellAlpha);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for cell_alpha line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // cell_beta
                    String cellBeta = values[i][j++];
                    Double cell_beta = null;
                    if (cellBeta == null || cellBeta.trim().length() != 0) {
                        try {
                            cell_beta = Double.parseDouble(cellBeta);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for cell_beta line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // cell_gamma
                    String cellGamma = values[i][j++];
                    Double cell_gamma = null;
                    if (cellGamma == null || cellGamma.trim().length() != 0) {
                        try {
                            cell_gamma = Double.parseDouble(cellGamma);
                        } catch (NumberFormatException e) {
                            errors.add(ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage("error.user.shipping.upload.file.data",
                                            "Wrong fromat for cell_gamma line " + (i + 1)));
                            isError = true;
                            break;
                        }
                    }
                    // creation of the objects
                    // Shipping
                    Shipping3VO shippingVO = new Shipping3VO();
                    shippingVO.setProposalVO(proposalVO);
                    shippingVO.setShippingName(shippingName);
                    shippingVO.setCreationDate(new Date());
                    shippingVO.setShippingStatus(Constants.SHIPPING_STATUS_OPENED);
                    shippingVO.setTimeStamp(StringUtils.getCurrentTimeStamp());
                    shippingVO.setShippingType(Constants.DEWAR_TRACKING_SHIPPING_TYPE);
                    shippingVO.setSendingLabContactVO(shippingLabContactVO);
                    shippingVO.setReturnLabContactVO(shippingLabContactVO);
                    Shipping3VO shipment = getShipment(listShippingCreated, shippingVO);
                    if (shipment == null) {
                        shippingVO = shippingService.create(shippingVO);
                        listShippingCreated.add(shippingVO);
                    } else {
                        shippingVO = shipment;
                    }
                    // Dewar
                    Dewar3VO dewarVO = new Dewar3VO();
                    dewarVO.setShippingVO(shippingVO);
                    dewarVO.setCode(dewarCode);
                    dewarVO.setType(Constants.PARCEL_DEWAR_TYPE);
                    dewarVO.setDewarStatus(Constants.SHIPPING_STATUS_OPENED);
                    dewarVO.setTimeStamp(StringUtils.getCurrentTimeStamp());
                    dewarVO.setCustomsValue(shippingLabContactVO.getDewarAvgCustomsValue());
                    dewarVO.setTransportValue(shippingLabContactVO.getDewarAvgTransportValue());
                    Dewar3VO dewar = getDewar(listDewarCreated, dewarVO);
                    if (dewar == null) {
                        dewarVO = dewarService.create(dewarVO);
                        listDewarCreated.add(dewarVO);
                    } else {
                        dewarVO = dewar;
                    }
                    // Container
                    Container3VO containerVO = new Container3VO();
                    containerVO.setContainerType("Puck");
                    containerVO.setCode(containerCode);
                    containerVO.setCapacity(Constants.BASKET_SAMPLE_CAPACITY);
                    containerVO.setTimeStamp(StringUtils.getCurrentTimeStamp());
                    containerVO.setDewarVO(dewarVO);
                    Container3VO container = getContainer(listContainerCreated, containerVO);
                    if (container == null) {
                        containerVO = containerService.create(containerVO);
                        listContainerCreated.add(containerVO);
                    } else {
                        containerVO = container;
                    }
                    if (isLocationOccInContainer(samplePosition, listSampleCreated, containerVO)) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                                "error.user.shipping.upload.file.data",
                                "The sample position is incorrect: " + samplePos + ", line " + (i + 1)));
                        isError = true;
                        break;
                    }
                    // DiffractionPlan
                    DiffractionPlan3VO difPlan = new DiffractionPlan3VO();
                    difPlan.setObservedResolution(preObsResolution);
                    difPlan.setRequiredResolution(neededResolution);
                    difPlan.setExposureTime((double) 0);
                    difPlan.setOscillationRange(oscillationRange);
                    difPlan.setExperimentKind(DEFAULT_EXPERIMENT_TYPE);
                    difPlan = difPlanService.create(difPlan);
                    listDifPlanCreated.add(difPlan);
                    // Crystal
                    Crystal3VO crystalVO = new Crystal3VO();
                    String crystalID = UUID.randomUUID().toString();
                    crystalVO.setProteinVO(protein);
                    crystalVO.setCrystalUUID(crystalID);
                    crystalVO.setSpaceGroup(spaceGroup);
                    crystalVO.setName(crystalID);
                    crystalVO.setCellA(cell_a);
                    crystalVO.setCellB(cell_b);
                    crystalVO.setCellC(cell_c);
                    crystalVO.setCellAlpha(cell_alpha);
                    crystalVO.setCellBeta(cell_beta);
                    crystalVO.setCellGamma(cell_gamma);
                    crystalVO.setDiffractionPlanVO(difPlan);
                    if ((crystalVO.getSpaceGroup() == null) || (crystalVO.getSpaceGroup().equals(""))) {
                        crystalVO.setSpaceGroup(DEFAULT_SPACE_GROUP);
                    }
                    Crystal3VO crystal = getCrystal(listCrystalCreated, crystalVO);
                    if (crystal == null) {
                        crystalVO = crystalService.create(crystalVO);
                        listCrystalCreated.add(crystalVO);

                    } else {
                        crystalVO = crystal;
                    }

                    if (!crystalVO.hasCellInfo()) {
                        messages.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("message.free",
                                        "Warning: the unit cell parameters are not filled for the spaceGroup "
                                                + crystalVO.getSpaceGroup() + "!"));
                    }
                    // BLSample
                    BLSample3VO sample = new BLSample3VO();
                    sample.setCrystalVO(crystalVO);
                    sample.setDiffractionPlanVO(difPlan);
                    sample.setName(sampleName);
                    sample.setCode(sampleBarcode);
                    sample.setLocation("" + samplePosition);
                    sample.setHolderLength(DEFAULT_HOLDER_LENGTH);
                    sample.setLoopLength(DEFAULT_LOOP_LENGTH);
                    sample.setLoopType(DEFAULT_LOOP_TYPE);
                    sample.setWireWidth(DEFAULT_WIRE_WIDTH);
                    sample.setComments(sampleComments);
                    sample.setContainerVO(containerVO);
                    if (isSampleNameAlreadyExist(sampleName, protein, listSampleCreated)) {
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("error.user.shipping.upload.file.data",
                                        "The sample name already exists: " + sampleName + ", line " + (i + 1)));
                        isError = true;
                        break;
                    }
                    sample = sampleService.create(sample);
                    listSampleCreated.add(sample);
                }
                if (isError) {
                    // remove created in db
                    for (Iterator<Shipping3VO> ship = listShippingCreated.iterator(); ship.hasNext();) {
                        Shipping3VO shipVO = ship.next();
                        shippingService.delete(shipVO);
                    }
                } else {
                    messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.upload.shipping"));
                }
            }
        }
    } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.detail", e.toString()));
        LOG.error(e.toString());
        saveErrors(request, errors);
        return mapping.findForward("error");
    }
    if (!messages.isEmpty())
        saveMessages(request, messages);
    if (!errors.isEmpty())
        saveErrors(request, errors);

    return display(mapping, actForm, request, in_reponse);
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.BorgBlockReader.java

/** 
 * Constructor, sets up SSL certs.//  w  w  w . j  a  v  a  2s . com
 * @param databasetype
 */
public BorgBlockReader(DatabaseType databasetype) {
    this.databasetype = databasetype;
    this.strategy = new CSVStrategy(',', '"', '#');

    System.setProperty("javax.net.ssl.trustStore", Extractor.chooseCACertsPath() + "cacerts");
    System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

    System.out.println("javax.net.ssl.trustStore = " + System.getProperty("javax.net.ssl.trustStore"));
}

From source file:org.alfresco.repo.web.scripts.DeclarativeSpreadsheetWebScript.java

/**
 * Generates the spreadsheet, based on the properties in the header
 *  and a callback for the body.//from www .  j  ava 2 s . c o m
 */
public void generateSpreadsheet(Object resource, String format, WebScriptRequest req, Status status,
        Map<String, Object> model) throws IOException {
    Pattern qnameMunger = Pattern.compile("([A-Z][a-z]+)([A-Z].*)");
    String delimiterParam = req.getParameter(PARAM_REQ_DELIMITER);
    CSVStrategy reqCSVstrategy = null;
    if (delimiterParam != null && !delimiterParam.isEmpty()) {
        reqCSVstrategy = new CSVStrategy(delimiterParam.charAt(0), '"', CSVStrategy.COMMENTS_DISABLED);
    }
    // Build up the details of the header
    List<Pair<QName, Boolean>> propertyDetails = buildPropertiesForHeader(resource, format, req);
    String[] headings = new String[propertyDetails.size()];
    String[] descriptions = new String[propertyDetails.size()];
    boolean[] required = new boolean[propertyDetails.size()];
    for (int i = 0; i < headings.length; i++) {
        Pair<QName, Boolean> property = propertyDetails.get(i);
        if (property == null || property.getFirst() == null) {
            headings[i] = "";
            required[i] = false;
        } else {
            QName column = property.getFirst();
            required[i] = property.getSecond();

            // Ask the dictionary service nicely for the details
            PropertyDefinition pd = dictionaryService.getProperty(column);
            if (pd != null && pd.getTitle(dictionaryService) != null) {
                // Use the friendly titles, which may even be localised!
                headings[i] = pd.getTitle(dictionaryService);
                descriptions[i] = pd.getDescription(dictionaryService);
            } else {
                // Nothing friendly found, try to munge the raw qname into
                //  something we can show to a user...
                String raw = column.getLocalName();
                raw = raw.substring(0, 1).toUpperCase() + raw.substring(1);

                Matcher m = qnameMunger.matcher(raw);
                if (m.matches()) {
                    headings[i] = m.group(1) + " " + m.group(2);
                } else {
                    headings[i] = raw;
                }
            }
        }
    }

    // Build a list of just the properties
    List<QName> properties = new ArrayList<QName>(propertyDetails.size());
    for (Pair<QName, Boolean> p : propertyDetails) {
        QName qn = null;
        if (p != null) {
            qn = p.getFirst();
        }
        properties.add(qn);
    }

    // Output
    if ("csv".equals(format)) {
        StringWriter sw = new StringWriter();
        CSVPrinter csv = new CSVPrinter(sw, reqCSVstrategy != null ? reqCSVstrategy : getCsvStrategy());
        csv.println(headings);

        populateBody(resource, csv, properties);

        model.put(MODEL_CSV, sw.toString());
    } else {
        Workbook wb;
        if ("xlsx".equals(format)) {
            wb = new XSSFWorkbook();
            // TODO Properties
        } else {
            wb = new HSSFWorkbook();
            // TODO Properties
        }

        // Add our header row
        Sheet sheet = wb.createSheet("Export");
        Row hr = sheet.createRow(0);
        sheet.createFreezePane(0, 1);

        Font fb = wb.createFont();
        fb.setBoldweight(Font.BOLDWEIGHT_BOLD);
        Font fi = wb.createFont();
        fi.setBoldweight(Font.BOLDWEIGHT_BOLD);
        fi.setItalic(true);

        CellStyle csReq = wb.createCellStyle();
        csReq.setFont(fb);
        CellStyle csOpt = wb.createCellStyle();
        csOpt.setFont(fi);

        // Populate the header
        Drawing draw = null;
        for (int i = 0; i < headings.length; i++) {
            Cell c = hr.createCell(i);
            c.setCellValue(headings[i]);

            if (required[i]) {
                c.setCellStyle(csReq);
            } else {
                c.setCellStyle(csOpt);
            }

            if (headings[i].length() == 0) {
                sheet.setColumnWidth(i, 3 * 250);
            } else {
                sheet.setColumnWidth(i, 18 * 250);
            }

            if (descriptions[i] != null && descriptions[i].length() > 0) {
                // Add a description for it too
                if (draw == null) {
                    draw = sheet.createDrawingPatriarch();
                }
                ClientAnchor ca = wb.getCreationHelper().createClientAnchor();
                ca.setCol1(c.getColumnIndex());
                ca.setCol2(c.getColumnIndex() + 1);
                ca.setRow1(hr.getRowNum());
                ca.setRow2(hr.getRowNum() + 2);

                Comment cmt = draw.createCellComment(ca);
                cmt.setAuthor("");
                cmt.setString(wb.getCreationHelper().createRichTextString(descriptions[i]));
                cmt.setVisible(false);
                c.setCellComment(cmt);
            }
        }

        // Have the contents populated
        populateBody(resource, wb, sheet, properties);

        // Save it for the template
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        wb.write(baos);
        model.put(MODEL_EXCEL, baos.toByteArray());
    }
}

From source file:org.apache.any23.extractor.csv.CSVReaderBuilder.java

private static CSVStrategy getCsvStrategy(char delimiter, char comment) {
    return new CSVStrategy(delimiter, '\'', comment);
}

From source file:org.apache.any23.extractor.csv.CSVReaderBuilder.java

private static CSVStrategy getCSVStrategyFromConfiguration() {
    char fieldDelimiter = getCharValueFromConfiguration("any23.extraction.csv.field", DEFAULT_FIELD_DELIMITER);
    char commentDelimiter = getCharValueFromConfiguration("any23.extraction.csv.comment",
            DEFAULT_COMMENT_DELIMITER);//ww w. j av  a2  s  .c o  m
    return new CSVStrategy(fieldDelimiter, '\'', commentDelimiter);
}

From source file:uk.co.droidinactu.common.file.DelimitedFile.java

public ArrayList<String> convertLineToFields(String line) {
    // Log.w("DelimitedFile", "convertLineToFields converting line [" + line +
    // "] to fields");
    ArrayList<String> alFields = new ArrayList<String>();

    CSVStrategy csvStrat = new CSVStrategy(this.m_sDelimiter.charAt(0), '\"', '#');
    csvStrat.setIgnoreLeadingWhitespaces(true);
    CSVParser csvParser = new CSVParser(new StringReader(line), csvStrat);
    String[][] st;/*from w w  w. j ava2 s  .  c  o  m*/
    try {
        st = csvParser.getAllValues();
        for (String[] element : st) {
            for (int tokenNbr = 0; tokenNbr < element.length; tokenNbr++) {
                alFields.add(element[tokenNbr]);
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return alFields;
}