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

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

Introduction

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

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

From source file:apiconnector.TestDataFunctionality.java

@Ignore
@Test/*w  w w.  j av a  2  s .c o m*/
public void testGetDataAsCsv() throws Exception {
    //client_read.setVerboseLevel(1);
    Random random = new Random();

    Map<String, String> filters = new TreeMap<String, String>();
    filters.put("tag", "study_14");

    DataSet[] all = client_read.dataList(filters).getData();

    for (int i = 0; i < 5;) {
        DataSet current = all[random.nextInt(all.length)];

        String numInst = current.getQualityMap().get("NumberOfInstances");

        if (current.getFileId() == null || !current.getFormat().toLowerCase().equals("arff")) {
            continue;
        }

        String fullUrl = url + "data/get_csv/" + current.getFileId() + "/" + current.getName() + ".csv";
        System.out.println(fullUrl);
        final URL url = new URL(fullUrl);
        final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
        final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
        try {
            if (numInst != null) {
                int numberOfInstances = (int) Double.parseDouble(numInst);
                assertEquals(parser.getRecords().size(), numberOfInstances);
            }
        } finally {
            parser.close();
            reader.close();
        }

        // important
        i += 1;
    }

}

From source file:com.wx3.galacdecks.Bootstrap.java

private void importCards(GameDatastore datastore, String path) throws IOException {
    Reader reader = new FileReader(path);
    CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
    int count = 0;
    for (CSVRecord record : parser) {
        String id = record.get("id");
        String name = record.get("name");
        String description = record.get("description");
        String flavor = record.get("flavor");
        String unitPrefab = record.get("unitPrefab");
        String summonEffect = record.get("summonEffect");
        String projectile = record.get("projectile");
        String portrait = record.get("portrait");
        Set<EntityTag> tags = new HashSet<EntityTag>();
        for (EntityTag tag : EntityTag.values()) {
            if (record.isSet(tag.toString())) {
                if (record.get(tag).equals("Y")) {
                    tags.add(tag);// w ww . jav a  2s .c om
                }
            }
        }

        String[] types = record.get("types").split(",");
        for (String type : types) {
            if (type.length() > 0) {
                EntityTag tag = EntityTag.valueOf(type);
                tags.add(tag);
            }
        }

        Map<EntityStat, Integer> stats = new HashMap<EntityStat, Integer>();
        for (EntityStat stat : EntityStat.values()) {
            String statName = stat.toString();
            if (record.isSet(statName)) {
                stats.put(stat, parseIntOrZero(record.get(statName)));
            }
        }

        List<EntityRule> rules = new ArrayList<EntityRule>();
        String ruleField = record.get("rules");
        String[] ruleIds = ruleField.split(",");
        for (String ruleId : ruleIds) {
            if (ruleId.length() > 0) {
                if (!ruleCache.containsKey(ruleId)) {
                    throw new RuntimeException("Unable to find rule '" + ruleId + "'");
                }
                EntityRule rule = ruleCache.get(ruleId);
                rules.add(rule);
            }
        }

        ValidatorScript playValidator = null;
        String playValidatorId = record.get("playValidator");
        if (playValidatorId != null && !playValidatorId.isEmpty()) {
            if (!playValidatorCache.containsKey(playValidatorId)) {
                throw new RuntimeException("Unknown validator '" + playValidatorId + "'");
            }
            playValidator = playValidatorCache.get(playValidatorId);
        }

        ValidatorScript discardValidator = null;
        String discardValidatorId = record.get("discardValidator");
        if (discardValidatorId != null && !discardValidatorId.isEmpty()) {
            if (!discardValidatorCache.containsKey(discardValidatorId)) {
                throw new RuntimeException("Unknown validator '" + discardValidatorId + "'");
            }
            discardValidator = discardValidatorCache.get(discardValidatorId);
        }

        Set<AiHint> aiHints = new HashSet<>();
        String hintField = record.get("aiHints");
        String[] hintIds = hintField.split(",");
        for (String hintId : hintIds) {
            if (hintId.length() > 0) {
                if (!aiHintCache.containsKey(hintId)) {
                    throw new RuntimeException("Unable to find AI Hint '" + hintId + "'");
                }
                AiHint hint = aiHintCache.get(hintId);
                aiHints.add(hint);
            }
        }

        EntityPrototype card = EntityPrototype.createPrototype(id, name, description, flavor, unitPrefab,
                summonEffect, projectile, portrait, tags, rules, playValidator, discardValidator, stats,
                aiHints);
        datastore.createPrototype(card);
        if (cardCache.containsKey(card.getId())) {
            throw new RuntimeException("Duplicate card id: " + card.getId());
        }
        cardCache.put(card.getId(), card);
        ++count;
    }
    logger.info("Imported " + count + " cards");
    parser.close();
}

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

/**
 * uploadCsvFile/*from   ww w  .  j av a  2  s.c o 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:net.sourceforge.ganttproject.io.GanttCSVOpen.java

/**
 * Create tasks from file./*from   ww  w . j  av  a 2  s  .c  o m*/
 *
 * @throws IOException
 *           on parse error or input read-failure
 */
public boolean load() throws IOException {
    CSVParser parser = new CSVParser(myInputSupplier.get(),
            CSVFormat.DEFAULT.withEmptyLinesIgnored(false).withSurroundingSpacesIgnored(true));
    int numGroup = 0;
    RecordGroup currentGroup = null;
    boolean searchHeader = true;
    List<CSVRecord> records = parser.getRecords();
    for (CSVRecord record : records) {
        if (record.size() == 0) {
            // If line is empty then current record group is probably finished.
            // Let's search for the next group header.
            searchHeader = true;
            continue;
        }
        if (searchHeader) {
            // Record is not empty and we're searching for header.
            if (numGroup < myRecordGroups.size() && myRecordGroups.get(numGroup).isHeader(record)) {
                // If next group acknowledges the header, then we give it the turn,
                // otherwise it was just an empty line in the current group
                searchHeader = false;
                currentGroup = myRecordGroups.get(numGroup);
                parser.readHeader(record);
                currentGroup.setHeader(Lists.newArrayList(record.iterator()));
                numGroup++;
                continue;
            }
            searchHeader = false;
        }
        assert currentGroup != null;
        currentGroup.process(record);
    }
    for (RecordGroup group : myRecordGroups) {
        group.postProcess();
    }
    // Succeeded
    return true;
}

From source file:com.wx3.galacdecks.Bootstrap.java

private void importSystems(GameDatastore datastore, String path) throws IOException {
    Reader reader = new FileReader(path);
    CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
    int count = 0;
    for (CSVRecord record : parser) {
        String id = record.get("id");
        String name = record.get("name");
        String description = record.get("description");
        String pvp = record.get("pvp");
        boolean usePlayerDecks = true;
        if (record.get("usePlayerDecks").toLowerCase().equals("n")) {
            usePlayerDecks = false;// w  w  w.  ja v a  2s.co  m
        }

        String ruleField = record.get("rootRules");
        String[] ruleIds = ruleField.split(",");
        GameSystem system = new GameSystem();
        system.id = id;
        system.name = name;
        system.description = description;
        system.usePlayerDecks = usePlayerDecks;
        system.rootRules = new ArrayList<>(Arrays.asList(ruleIds));
        if (pvp.toUpperCase().equals("Y")) {
            system.pvp = true;
        } else {
            system.pvp = false;
        }
        datastore.createSystem(system);

        ++count;
    }
    logger.info("Imported " + count + " systems");
    parser.close();
}

From source file:com.archimatetool.csv.importer.CSVImporter.java

/**
 * Get all records for a CSV file.//from   www  .j av  a2  s .c o  m
 * This is a brute-force approach to try with a comma delimiter first. If that fails then
 * try a semicolon, and if that fails, a tab.
 * 
 * @param file The file to open
 * @return Records, which may be empty but never null
 * @throws IOException
 */
List<CSVRecord> getRecords(File file) throws IOException {
    List<CSVRecord> records = new ArrayList<CSVRecord>();
    CSVParser parser = null;

    String errorMessage = "invalid char between encapsulated token and delimiter"; //$NON-NLS-1$

    try {
        parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT);
        records = parser.getRecords();
    } catch (IOException ex) {
        if (parser != null) {
            parser.close();
        }
        if (ex.getMessage() != null && ex.getMessage().contains(errorMessage)) {
            try {
                parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT.withDelimiter(';'));
                records = parser.getRecords();
            } catch (IOException ex2) {
                if (parser != null) {
                    parser.close();
                }
                if (ex2.getMessage() != null && ex2.getMessage().contains(errorMessage)) {
                    parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT.withDelimiter('\t'));
                    records = parser.getRecords();
                } else {
                    throw ex2;
                }
            }
        } else {
            throw ex;
        }
    } finally {
        if (parser != null) {
            parser.close();
        }
    }

    return records;
}

From source file:com.webtide.jetty.load.generator.jenkins.LoadGeneratorBuilder.java

protected void parseTimeValues(FilePath workspace, Path responseTimeResultFilePath,
        List<Resource.NodeListener> nodeListeners) throws Exception {
    Path responseTimeResultFile = Files.createTempFile("loadgenerator_result_responsetime", ".csv");

    workspace.child(responseTimeResultFilePath.toString())
            .copyTo(Files.newOutputStream(responseTimeResultFile));

    CSVParser csvParser = new CSVParser(Files.newBufferedReader(responseTimeResultFile),
            CSVFormat.newFormat('|'));

    csvParser.forEach(strings -> {//  www  . j av  a 2s.co m
        Values values = new Values() //
                .eventTimestamp(Long.parseLong(strings.get(0))) //
                .method(strings.get(1)) //
                .path(strings.get(2)) //
                .status(Integer.parseInt(strings.get(3))) //
                .size(Long.parseLong(strings.get(4))) //
                .responseTime(Long.parseLong(strings.get(5))) //
                .latencyTime(Long.parseLong(strings.get(6)));

        for (Resource.NodeListener listener : nodeListeners) {
            listener.onResourceNode(values.getInfo());
        }
    });

    Files.deleteIfExists(responseTimeResultFile);
}

From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java

/**
 * String Parsing //from  ww  w. ja v a 2 s  .c om
 */

public static String[] splitStr(String val, Integer len) throws IOException {

    String[] input;

    try {
        CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT);
        CSVRecord record = parser.getRecords().get(0);
        input = new String[len];
        Iterator<String> valuesIt = record.iterator();
        int i = 0;
        while (valuesIt.hasNext()) {
            input[i] = valuesIt.next().trim();
            i++;
        }
        parser.close();
    } catch (ArrayIndexOutOfBoundsException e) {
        input = val.split(",", len);
        for (int i = 0; i < input.length; i++)
            input[i] = input[i].trim();
    }

    return input;
}

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

@Override
public void readAll() {
    ArrayList<ColumnStructure> colstruct = new ArrayList<ColumnStructure>();
    colstruct.add(ColumnStructure.VC);//from   ww w . ja v a2  s.com
    colstruct.add(ColumnStructure.TS);
    colstruct.add(ColumnStructure.FL);
    for (RncAp r : EnumSet.allOf(RncAp.class)) {
        for (AppProc a : EnumSet.allOf(AppProc.class)) {
            ArrayList<ArrayList<String>> mapmap = new ArrayList<ArrayList<String>>();
            try {
                URL borg = new URL(BORG + r.getFile(a));
                URLConnection conn = borg.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                CSVParser parser = new CSVParser(in, strategy);
                //if header
                String[] header = parser.getLine();
                //and body
                String[] line = null;

                while ((line = parser.getLine()) != null) {

                    Calendar cal = Calendar.getInstance();
                    cal.setTime(BORG_DF.parse(line[0]));
                    String datestr = ALUDBUtilities.ALUDB_DF.format(cal.getTime());
                    //System.out.println(r.toString()+"/"+a.toString()+"//"+line[0]+"///"+datestr);
                    for (int i = 2; i < line.length; i++) {
                        ArrayList<String> map = new ArrayList<String>();
                        map.add(0, idConvert(r.toString(), a.toString(), header[i]));
                        map.add(1, datestr);
                        map.add(2, line[i]);
                        mapmap.add(map);
                    }

                }
                in.close();
            } catch (MalformedURLException mrue) {
                System.err.println("Borg Path incorrect " + mrue);
            } catch (IOException ioe) {
                System.err.println("Cannot read Borg file " + ioe);
            } catch (ParseException pe) {
                System.err.println("Cannot parse Date field " + pe);
            }

            ALUDBUtilities.insert(databasetype, TABLE, colstruct, mapmap);

        }
    }

}

From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java

public static String[] splitStr(String val) throws IOException {

    CSVParser parser = new CSVParser(new StringReader(val), CSVFormat.DEFAULT);
    CSVRecord record = parser.getRecords().get(0);
    Iterator<String> valuesIt = record.iterator();
    String[] input = new String[record.size()];
    int i = 0;/*from   w w w  .j av  a 2 s .  co m*/
    while (valuesIt.hasNext()) {
        input[i] = valuesIt.next();
        i++;
    }
    parser.close();
    return input;
}