Example usage for org.apache.commons.csv CSVRecord isSet

List of usage examples for org.apache.commons.csv CSVRecord isSet

Introduction

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

Prototype

public boolean isSet(final String name) 

Source Link

Document

Checks whether a given columns is mapped and has a value.

Usage

From source file:biz.ganttproject.impex.csv.RecordGroup.java

protected boolean hasMandatoryFields(CSVRecord record) {
    for (String s : myMandatoryFields) {
        if (!record.isSet(s)) {
            return false;
        }//from  ww w . jav a2 s .com
        if (Strings.isNullOrEmpty(record.get(s))) {
            return false;
        }
    }
    return true;
}

From source file:edu.isi.misd.scanner.network.modules.worker.processors.ptr.PrepToResearchProcessor.java

private void validateCSVRecord(CSVRecord csvRecord) throws Exception {
    String columnNotFound = "Unable to locate required column: ";

    if (!csvRecord.isSet(ExpectedColumnName.OMOP_CONCEPT_ID.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.OMOP_CONCEPT_ID.toString());
    }/*from  w  w  w . j av  a  2 s  .  co m*/
    if (!csvRecord.isSet(ExpectedColumnName.OMOP_CONCEPT_NAME.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.OMOP_CONCEPT_NAME.toString());
    }
    if (!csvRecord.isSet(ExpectedColumnName.CATEGORY.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.CATEGORY.toString());
    }
    if (!csvRecord.isSet(ExpectedColumnName.CATEGORY_VALUE.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.CATEGORY_VALUE.toString());
    }
    if (!csvRecord.isSet(ExpectedColumnName.COUNT_FEMALES.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.COUNT_FEMALES.toString());
    }
    if (!csvRecord.isSet(ExpectedColumnName.COUNT_MALES.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.COUNT_MALES.toString());
    }
    if (!csvRecord.isSet(ExpectedColumnName.COUNT_TOTAL.toString())) {
        throw new Exception(columnNotFound + ExpectedColumnName.COUNT_TOTAL.toString());
    }
}

From source file:biz.ganttproject.impex.csv.TaskRecords.java

@Override
protected boolean doProcess(CSVRecord record) {
    if (!super.doProcess(record)) {
        return false;
    }/*w  w  w.  ja  v  a  2s .  com*/
    if (!hasMandatoryFields(record)) {
        return false;
    }
    Date startDate = parseDateOrError(getOrNull(record, TaskFields.BEGIN_DATE.toString()));
    // Create the task
    TaskManager.TaskBuilder builder = taskManager.newTaskBuilder()
            .withName(getOrNull(record, TaskFields.NAME.toString())).withStartDate(startDate)
            .withWebLink(getOrNull(record, TaskFields.WEB_LINK.toString()))
            .withNotes(getOrNull(record, TaskFields.NOTES.toString()));
    if (record.isSet(TaskDefaultColumn.DURATION.getName())) {
        builder = builder
                .withDuration(taskManager.createLength(record.get(TaskDefaultColumn.DURATION.getName())));
    }
    if (record.isSet(TaskFields.END_DATE.toString())) {
        if (record.isSet(TaskDefaultColumn.DURATION.getName())) {
            if (Objects.equal(record.get(TaskFields.BEGIN_DATE.toString()),
                    record.get(TaskFields.END_DATE.toString()))
                    && "0".equals(record.get(TaskDefaultColumn.DURATION.getName()))) {
                builder = builder.withLegacyMilestone();
            }
        } else {
            Date endDate = parseDateOrError(getOrNull(record, TaskFields.END_DATE.toString()));
            if (endDate != null) {
                builder = builder.withEndDate(myTimeUnitStack.getDefaultTimeUnit().adjustRight(endDate));
            }
        }
    }
    if (record.isSet(TaskFields.COMPLETION.toString())) {
        String completion = record.get(TaskFields.COMPLETION.toString());
        if (!Strings.isNullOrEmpty(completion)) {
            builder = builder.withCompletion(Integer.parseInt(completion));
        }
    }
    if (record.isSet(TaskDefaultColumn.COST.getName())) {
        try {
            String cost = record.get(TaskDefaultColumn.COST.getName());
            if (!Strings.isNullOrEmpty(cost)) {
                builder = builder.withCost(new BigDecimal(cost));
            }
        } catch (NumberFormatException e) {
            GPLogger.logToLogger(e);
            GPLogger.log(String.format("Failed to parse %s as cost value",
                    record.get(TaskDefaultColumn.COST.getName())));
        }
    }
    Task task = builder.build();

    if (record.isSet(TaskDefaultColumn.ID.getName())) {
        myTaskIdMap.put(record.get(TaskDefaultColumn.ID.getName()), task);
    }
    myAssignmentMap.put(task, getOrNull(record, TaskFields.RESOURCES.toString()));
    myPredecessorMap.put(task, getOrNull(record, TaskDefaultColumn.PREDECESSORS.getName()));
    String outlineNumber = getOrNull(record, TaskDefaultColumn.OUTLINE_NUMBER.getName());
    if (outlineNumber != null) {
        myWbsMap.put(outlineNumber, task);
    }
    for (String customField : getCustomFields()) {
        String value = getOrNull(record, customField);
        if (value == null) {
            continue;
        }
        CustomPropertyDefinition def = taskManager.getCustomPropertyManager()
                .getCustomPropertyDefinition(customField);
        if (def == null) {
            GPLogger.logToLogger("Can't find custom field with name=" + customField + " value=" + value);
            continue;
        }
        task.getCustomValues().addCustomProperty(def, value);
    }
    return true;
}

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 w  w  .  ja  v  a2 s  . c o  m*/
                }
            }
        }

        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:org.nuxeo.ecm.csv.core.CSVImporterWork.java

/**
 * Import a line from the CSV file./*from  w w w  . j  av  a2 s .c o m*/
 *
 * @return {@code true} if a document has been created or updated, {@code false} otherwise.
 * @since 6.0
 */
protected boolean importRecord(CSVRecord record, Map<String, Integer> header) {
    String name = record.get(CSV_NAME_COL);
    if (StringUtils.isBlank(name)) {
        log.debug("record.isSet=" + record.isSet(CSV_NAME_COL));
        logError(getLineNumber(record), "Missing 'name' value", LABEL_CSV_IMPORTER_MISSING_NAME_VALUE);
        return false;
    }

    Path targetPath = new Path(parentPath).append(name);
    name = targetPath.lastSegment();
    String newParentPath = targetPath.removeLastSegments(1).toString();
    boolean exists = options.getCSVImporterDocumentFactory().exists(session, newParentPath, name, null);

    DocumentRef docRef = null;
    String type = null;
    if (exists) {
        docRef = new PathRef(targetPath.toString());
        type = session.getDocument(docRef).getType();
    } else {
        if (hasTypeColumn) {
            type = record.get(CSV_TYPE_COL);
        }
        if (StringUtils.isBlank(type)) {
            log.debug("record.isSet=" + record.isSet(CSV_TYPE_COL));
            logError(getLineNumber(record), "Missing 'type' value", LABEL_CSV_IMPORTER_MISSING_TYPE_VALUE);
            return false;
        }
    }

    DocumentType docType = Framework.getService(SchemaManager.class).getDocumentType(type);
    if (docType == null) {
        logError(getLineNumber(record), "The type '%s' does not exist", LABEL_CSV_IMPORTER_NOT_EXISTING_TYPE,
                type);
        return false;
    }
    Map<String, Serializable> properties = computePropertiesMap(record, docType, header);
    if (properties == null) {
        // skip this line
        return false;
    }

    long lineNumber = getLineNumber(record);
    if (exists) {
        return updateDocument(lineNumber, docRef, properties);
    } else {
        return createDocument(lineNumber, newParentPath, name, type, properties);
    }
}

From source file:org.transitime.custom.sfmta.delayTimes.Intersection.java

private static String getValue(CSVRecord record, String name) {
    if (!record.isSet(name)) {
        logger.error("Column {} not defined", name);
        return null;
    }//from w  ww. j av  a  2s.co  m

    // Get the value. First trim whitespace so that
    // value will be consistent. 
    String value = record.get(name).trim();
    return value;
}

From source file:org.transitime.utils.csv.CsvBase.java

/**
 * For reading values from a CSVRecord. If the column was not defined then
 * CSVRecord.get() throws an exception. Therefore for optional CSV columns
 * better to use this function so don't get exception. This way can continue
 * processing and all errors for the data will be logged. Better than just
 * logging first error and then quitting.
 * <p>//from  w  ww.ja va  2 s. co m
 * Also, if the value is empty string then it is converted to null for
 * consistency. Also trims the resulting string since some agencies leave in
 * spaces.
 * 
 * @param record
 *            The data for the row in the CSV file
 * @param name
 *            The name of the column in the CSV file
 * @param required
 *            Whether this value is required. If required and the value is
 *            not set then an error is logged and null is returned.
 * @return The value, or null if it was not defined
 */
private String getValue(CSVRecord record, String name, boolean required) {
    // If the column is not defined in the file then return null.
    // After all, the item is optional so it is fine for it to
    // not be in the file.
    if (!record.isSet(name)) {
        if (required) {
            logger.error("Column {} not defined in file \"{}\" yet it is required", name, getFileName());
        }
        return null;
    }

    // Get the value. First trim whitespace so that
    // value will be consistent. Sometimes agencies will mistakenly have
    // some whitespace in the columns.
    String value = record.get(name).trim();

    // Return the value. But if the value is empty string
    // convert to null for consistency.
    if (value.isEmpty()) {
        if (required) {
            logger.error(
                    "For file \"{}\" line number {} for column {} value was not set " + "yet it is required",
                    getFileName(), lineNumber, name);
        }
        return null;
    } else {
        // Successfully got value so return intern() version of it. Using 
        // intern() because many strings are duplicates such as headsign
        // or shape info. By sharing the strings we can save a huge amount
        // of memory.
        return value.intern();
    }
}

From source file:permafrost.tundra.data.IDataCSVParser.java

/**
 * Returns an IData representation of the CSV data in the given input stream.
 *
 * @param inputStream The input stream to be decoded.
 * @param charset     The character set to use.
 * @return An IData representation of the given input stream data.
 * @throws IOException If there is a problem reading from the stream.
 *///from ww w .  j  a va  2 s.c  o  m
@Override
public IData decode(InputStream inputStream, Charset charset) throws IOException {
    if (inputStream == null)
        return null;

    Reader reader = new InputStreamReader(inputStream, CharsetHelper.normalize(charset));
    CSVFormat format = CSVFormat.DEFAULT.withHeader().withDelimiter(delimiter).withNullString("");
    CSVParser parser = format.parse(reader);

    Set<String> keys = parser.getHeaderMap().keySet();
    List<IData> list = new ArrayList<IData>();

    for (CSVRecord record : parser) {
        IData document = IDataFactory.create();
        IDataCursor cursor = document.getCursor();
        for (String key : keys) {
            if (record.isSet(key)) {
                String value = record.get(key);
                if (value != null)
                    IDataUtil.put(cursor, key, value);
            }
        }
        cursor.destroy();
        list.add(document);
    }

    IData output = IDataFactory.create();
    IDataCursor cursor = output.getCursor();
    IDataUtil.put(cursor, "recordWithNoID", list.toArray(new IData[list.size()]));

    return output;
}