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

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of values in this record.

Usage

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static List<RiskCategory> convertRiskCategories(List<CSVRecord> records) {
    ArrayList<RiskCategory> list = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        int id = Integer.parseInt(record.get(0));
        String text = record.get(1);

        RiskCategory category = new RiskCategory().setRiskCategoryId(id).setText(text);
        list.add(category);/*from  w w w.j  a  v  a  2  s .co m*/
    }

    return list;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static List<Risk> convertRisks(List<CSVRecord> records, Map<Integer, RiskCategory> categories) {
    List<Risk> list = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 3)
            break;
        int id = Integer.parseInt(record.get(0));
        int catId = Integer.parseInt(record.get(1));
        String text = record.get(2);

        Risk risk = new Risk().setRiskId(id).setText(text);
        risk.setCategory(categories.get(catId));

        list.add(risk);//from   ww  w  . j av  a  2s.  c o  m
    }

    return list;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

private static boolean isValidPropertyRecord(CSVRecord record) {
    if (record.size() < 3 || isNullEmptyOrWhitespace(record.get(1))) {
        return false;
    }/*from   w w  w  .  j  a v a2s.c  om*/
    try {
        Integer.parseInt(record.get(0));
    } catch (NumberFormatException nfe) {
        return false;
    }
    return true;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static List<Todo> convertTodos(List<CSVRecord> records) {
    List<Todo> list = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        String id = record.get(0);
        String text = record.get(1);

        Todo todo = new Todo().setTodoId(Integer.parseInt(id)).setText(text);

        // Parse boolean values

        String developmentString = record.get(2);
        if (!"NULL".equals(developmentString)) {
            boolean development = parseBoolean(developmentString);
            todo.setDevelopment(development);
        }/*from  ww  w  . ja v a 2  s  . com*/

        String distributionString = record.get(3);
        if (!"NULL".equals(distributionString)) {
            boolean distribution = parseBoolean(distributionString);
            todo.setDistribution(distribution);
        }
        list.add(todo);
    }
    return list;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static List<Obligation> convertObligation(List<CSVRecord> records) {
    List<Obligation> list = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        String id = record.get(0);
        String name = record.get(1);

        Obligation obligation = new Obligation().setObligationId(Integer.parseInt(id)).setName(name);
        list.add(obligation);//ww  w. j av a 2s. c  o m
    }

    return list;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static List<LicenseType> convertLicenseTypes(List<CSVRecord> records) {
    List<LicenseType> list = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        int id = Integer.parseInt(record.get(0));
        String text = record.get(1);

        LicenseType type = new LicenseType().setLicenseTypeId(id).setLicenseType(text);
        list.add(type);// w ww  . j  a  va  2s  .  c  o m
    }

    return list;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static List<License> fillLicenses(List<CSVRecord> records, Map<Integer, LicenseType> licenseTypeMap,
        Map<Integer, Todo> todoMap, Map<Integer, Risk> riskMap, Map<String, Set<Integer>> licenseTodo,
        Map<String, Set<Integer>> licenseRisk) {
    List<License> licenses = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 7)
            break;
        String identifier = record.get(0);
        String fullname = record.get(1);

        License license = new License().setId(identifier).setFullname(fullname);

        String typeString = record.get(2);
        if (!Strings.isNullOrEmpty(typeString) && !"NULL".equals(typeString)) {
            Integer typeId = Integer.parseInt(typeString);
            LicenseType licenseType = licenseTypeMap.get(typeId);
            license.setLicenseType(licenseType);
        }//from w  ww .j ava2  s .c  om

        String gplv2CompatString = record.get(3);
        if (!Strings.isNullOrEmpty(gplv2CompatString) && !"NULL".equals(gplv2CompatString)) {
            Ternary gplv2Compat = ThriftEnumUtils.stringToEnum(gplv2CompatString, Ternary.class);
            license.setGPLv2Compat(gplv2Compat);
        }

        String gplv3CompatString = record.get(4);
        if (!Strings.isNullOrEmpty(gplv3CompatString) && !"NULL".equals(gplv3CompatString)) {
            Ternary gplv3Compat = ThriftEnumUtils.stringToEnum(gplv3CompatString, Ternary.class);
            license.setGPLv3Compat(gplv3Compat);
        }

        String reviewdate = record.get(5);
        license.setReviewdate(ConvertUtil.parseDate(reviewdate));

        String text = record.get(6);
        license.setText(text);

        if (record.size() > 7) {
            String externalLink = record.get(7);
            license.setExternalLicenseLink(externalLink);
        }

        // Add all risks
        Set<Integer> riskIds = licenseRisk.get(identifier);
        if (riskIds != null) {
            for (int riskId : riskIds) {
                Risk risk = riskMap.get(riskId);
                if (risk != null) {
                    license.addToRiskDatabaseIds(risk.getId());
                }
            }
        }

        // Add all todos
        Set<Integer> todoIds = licenseTodo.get(identifier);
        if (todoIds != null) {
            for (int todoId : todoIds) {
                Todo todo = todoMap.get(todoId);
                if (todo != null) {
                    license.addToTodoDatabaseIds(todo.getId());
                }
            }
        }

        licenses.add(license);
    }

    return licenses;
}

From source file:org.eclipse.sw360.commonIO.ConvertRecord.java

public static Map<String, Set<Integer>> convertRelationalTable(List<CSVRecord> records) {
    Map<String, Set<Integer>> map = new HashMap<>(records.size());

    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        String mainId = record.get(0);
        int otherId = Integer.parseInt(record.get(1));

        if (map.containsKey(mainId)) {
            map.get(mainId).add(otherId);
        } else {/*from   w  w  w.  ja  v a2  s.c  om*/
            Set<Integer> ids = new HashSet<>();
            ids.add(otherId);
            map.put(mainId, ids);
        }
    }

    return map;
}

From source file:org.etudes.mneme.tool.ImportOfflineView.java

/**
 * {@inheritDoc}// w  ww  . ja  v  a  2 s  . co  m
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    String returnUrl = (params.length > 3) ? params[2] : "";
    String sort = (params.length > 3) ? params[3] : "0A";

    String siteId = toolManager.getCurrentPlacement().getContext();
    if (!this.assessmentService.allowManageAssessments(siteId)) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    Site site = null;
    try {
        site = this.siteService.getSite(siteId);
    } catch (IdUnusedException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    String iidCode = this.rosterService.findInstitutionCode(site.getTitle());

    // a CSV uploader for the CSV file
    UploadCsv upload = new UploadCsv();
    context.put("upload", upload);

    // read the form
    String destination = uiService.decode(req, context);

    // import the offlines
    if ("UPLOAD".equals(destination)) {
        List<GradeImportSet> importSets = new ArrayList<GradeImportSet>(1);

        // get the Tool session, where we will be storing this until confirmed
        ToolSession toolSession = sessionManager.getCurrentToolSession();
        toolSession.setAttribute(GradeImportSet.ATTR_NAME, importSets);

        // col 0, the student id col 1 .. n, the title of the offline assessment, in the first record, and the score, in the rest
        List<CSVRecord> records = upload.getRecords();

        if ((records != null) && (records.size() > 1)) {
            // the header record
            CSVRecord header = records.get(0);

            // find the "Student ID" column
            int idCol = -1;
            for (int c = 0; c < header.size(); c++) {
                // get the assessment title, extras
                String title = StringUtil.trimToZero(header.get(c));
                if ("Student ID".equalsIgnoreCase(title)) {
                    idCol = c;
                    break;
                }
            }

            if (idCol != -1) {
                // try all other columns
                for (int i = 0; i < header.size(); i++) {
                    // we use this column for the Student ID
                    if (i == idCol)
                        continue;

                    // we will keep this ONLY if we see at least one valid grade entry
                    GradeImportSet set = new GradeImportSet();
                    boolean hasValidEntries = false;

                    // get the assessment title, points
                    String title = StringUtil.trimToZero(header.get(i));
                    Float points = null;
                    int extraPos = title.indexOf("{{");
                    if (extraPos != -1) {
                        String extra = StringUtil.trimToZero(title.substring(extraPos + 2, title.length() - 2));
                        title = StringUtil.trimToZero(title.substring(0, extraPos));
                        try {
                            points = Float.valueOf(extra);
                        } catch (NumberFormatException e) {
                        }
                    }

                    // skip blank header columns
                    if (title.length() == 0)
                        continue;

                    // these two titles we also skip, as created by UCB Gradebook's export
                    if ("Section ID".equals(title))
                        continue;
                    if ("Cumulative".equals(title))
                        continue;

                    // new assessment or existing
                    Assessment existing = assessmentService.assessmentExists(siteId, title);
                    if (existing != null) {
                        // just ignore if it does not take points
                        if (!existing.getHasPoints()) {
                            continue;
                        }
                        set.assessment = existing;
                    } else {
                        set.assessmentTitle = title;
                        set.points = points;
                    }

                    for (CSVRecord r : records) {
                        // skip the header
                        if (r.getRecordNumber() == 1)
                            continue;

                        GradeImport x = new GradeImport();
                        set.rows.add(x);

                        if (r.size() >= 1) {
                            x.studentId = r.get(idCol);
                        }
                        if (r.size() > i) {
                            x.scoreGiven = r.get(i);
                        }

                        if (x.scoreGiven != null) {
                            try {
                                x.score = Float.parseFloat(x.scoreGiven);
                            } catch (NumberFormatException e) {
                            }
                        }

                        if ((x.score != null) & (x.studentId != null)) {
                            // use value in paren, if there
                            String id = x.studentId;
                            int parenPos = id.indexOf("(");
                            if (parenPos != -1) {
                                id = StringUtil.trimToZero(id.substring(parenPos + 1, id.length() - 1));
                            }

                            User user = findIdentifiedStudentInSite(id, siteId, iidCode);
                            if (user != null) {
                                // check for duplicate records
                                boolean duplicate = false;
                                for (GradeImport gi : set.rows) {
                                    if (gi == x)
                                        continue;
                                    if (gi.userId == null)
                                        continue;
                                    if (gi.userId.equals(user.getId())) {
                                        duplicate = true;
                                        x.duplicate = Boolean.TRUE;
                                        break;
                                    }
                                }

                                if (!duplicate) {
                                    x.userId = user.getId();
                                    x.name = user.getDisplayName();
                                    hasValidEntries = true;
                                }
                            }
                        }
                    }

                    // keep this ONLY if we see at least one valid grade entry
                    if (hasValidEntries) {
                        importSets.add(set);
                    }
                }
            }
        }

        destination = "/confirm_grades_import/" + returnUrl + "/" + sort;
    }

    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
}

From source file:org.hoteia.qalingo.translation.LoaderTranslationUtil.java

public static final void buildMessagesProperties(String currentPath, String project, String filePath,
        List<String> activedLanguages, String defaultLanguage, String inputEncoding, String outputEncoding) {
    try {/*  w ww . j  a  v  a  2 s  . c  om*/
        String newPath = currentPath + project + LoaderTranslation.PROPERTIES_PATH;
        File folderProject = new File(newPath);
        if (!folderProject.exists()) {
            folderProject.mkdirs();
        }

        InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(filePath)),
                inputEncoding);
        LOG.info("File CSV encoding: " + inputEncoding);
        LOG.info("File properties encoding: " + outputEncoding);

        CSVFormat csvFormat = CSVFormat.DEFAULT;

        CSVParser readerCSV = new CSVParser(reader, csvFormat);
        List<CSVRecord> records = null;
        try {
            records = readerCSV.getRecords();
        } catch (Exception e) {
            LOG.error("Failed to load: " + filePath, e);
        }

        String prefixFileName = "";
        CSVRecord firstLineRecord = records.get(0);
        String[] prefixFileNameTemp = firstLineRecord.get(0).split("## Filename :");
        if (prefixFileNameTemp.length > 1) {
            prefixFileName = prefixFileNameTemp[1].trim();
        }

        String prefixKey = "";
        CSVRecord secondLineRecord = records.get(1);
        String[] prefixKeyTemp = secondLineRecord.get(0).split("## PrefixKey :");
        if (prefixKeyTemp.length > 1) {
            prefixKey = prefixKeyTemp[1].trim();
        }

        Map<String, Integer> availableLanguages = new HashMap<String, Integer>();
        for (CSVRecord record : records) {
            String firstCell = record.get(0);
            if (firstCell.contains("Prefix") && record.size() > 1) {
                String secondCell = record.get(1);
                if (secondCell.contains("Key")) {
                    for (int i = 2; i < record.size(); i++) {
                        String languageCode = record.get(i);
                        availableLanguages.put(languageCode, new Integer(i));
                    }
                    break;
                }
            }
        }

        // BUILD DEFAULT FILE
        String fileFullPath = newPath + "/" + prefixFileName + ".properties";
        Integer defaultLanguagePosition = availableLanguages.get(defaultLanguage);
        buildMessagesProperties(records, fileFullPath, prefixKey, defaultLanguage, defaultLanguagePosition,
                outputEncoding, true);

        for (Iterator<String> iterator = availableLanguages.keySet().iterator(); iterator.hasNext();) {
            String languageCode = (String) iterator.next();
            if (activedLanguages.contains(languageCode)) {
                Integer languagePosition = availableLanguages.get(languageCode);
                String languegFileFullPath = newPath + "/" + prefixFileName + "_" + languageCode
                        + ".properties";
                buildMessagesProperties(records, languegFileFullPath, prefixKey, languageCode,
                        languagePosition.intValue(), outputEncoding, false);
            }
        }
        LOG.info(newPath + "/" + prefixFileName + ".properties");

    } catch (Exception e) {
        LOG.info("Exception", e);
    }
}