List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
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); }// www. j a v a 2s . c om 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);/*from ww w . j a v a 2 s . co 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);//from w w w. j a va 2 s . co 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); }//www. j ava 2 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 www.j av a 2s. c om Set<Integer> ids = new HashSet<>(); ids.add(otherId); map.put(mainId, ids); } } return map; }
From source file:org.eclipse.sw360.importer.ComponentCSVRecordBuilder.java
ComponentCSVRecordBuilder(CSVRecord record) { int i = 0;/*from w w w . java 2s . c om*/ //String componentName = record.get(i++); componentDescription = record.get(i++); componentCreatedOn = record.get(i++); componentType = record.get(i++); componentCreatedBy = record.get(i++); componentSubscribers = record.get(i++); categories = record.get(i++); softwarePlatforms = record.get(i++); componentHomePage = record.get(i++); componentMailingList = record.get(i++); componentWiki = record.get(i++); componentBlog = record.get(i++); componentWikipedia = record.get(i++); componentOpenHub = record.get(i++); releaseName = record.get(i++); releaseVersion = record.get(i++); releaseDate = record.get(i++); CPEId = record.get(i++); releaseCreatedOn = record.get(i++); releaseCreatedBy = record.get(i++); releaseRepostitoryURL = record.get(i++); releaseRepostitoryType = record.get(i++); releaseMainlineState = record.get(i++); releaseClearingState = record.get(i++); releaseContributors = record.get(i++); releaseModerators = record.get(i++); releaseSubscribers = record.get(i++); releaseLanguages = record.get(i++); releaseOperatingSystems = record.get(i++); releaseMainLicenseNames = record.get(i++); releaseDownloadURL = record.get(i++); vendorName = record.get(i++); vendorShortname = record.get(i++); vendorUrl = record.get(i++); cIExternalSupplierID = record.get(i++); cIAdditionalInfo = record.get(i++); cIEvaluated = record.get(i++); cIProcStart = record.get(i++); cIRequestId = record.get(i++); cIScanned = record.get(i++); cIClearingStandard = record.get(i++); cIComment = record.get(i++); cIExternalUrl = record.get(i++); // Booleans cIBinariesOriginalFromCommunity = getBoolOrNull(record.get(i++)); cIBinariesSelfMade = getBoolOrNull(record.get(i++)); cIComponentLicenseInformation = getBoolOrNull(record.get(i++)); cISourceCodeDelivery = getBoolOrNull(record.get(i++)); cISourceCodeOriginalFromCommunity = getBoolOrNull(record.get(i++)); cISourceCodeToolMade = getBoolOrNull(record.get(i++)); cISourceCodeSelfMade = getBoolOrNull(record.get(i++)); cIScreenshotOfWebSite = getBoolOrNull(record.get(i++)); cIFinalizedLicenseScanReport = getBoolOrNull(record.get(i++)); cILicenseScanReportResult = getBoolOrNull(record.get(i++)); cILegalEvaluation = getBoolOrNull(record.get(i++)); cILicenseAgreement = getBoolOrNull(record.get(i++)); cIComponentClearingReport = getBoolOrNull(record.get(i++)); // Int cICountOfSecurityVn = getIntegerOrNull(record.get(i++)); eccStatus = record.get(i++); eccAL = record.get(i++); eccECCN = record.get(i++); eccMaterialIndexNumber = record.get(i++); eccComment = record.get(i++); eccAssessorContactPerson = record.get(i++); eccAssessorDepartment = record.get(i++); eccAssessmentDate = record.get(i); // Default copies: releaseCreatedBy = alternative(releaseCreatedBy, componentCreatedBy); componentCreatedBy = alternative(componentCreatedBy, releaseCreatedBy); releaseCreatedOn = alternative(releaseCreatedOn, componentCreatedOn); componentCreatedOn = alternative(componentCreatedOn, releaseCreatedOn); }
From source file:org.etudes.mneme.tool.ImportOfflineView.java
/** * {@inheritDoc}//from www . j a v a 2 s. c o 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.hawkular.datamining.forecast.CSVTimeSeriesReader.java
public static List<DataPoint> getData(String fileName) throws IOException { fileName = TestDirectory.pathPrefix + fileName; File fileToRead = new File(fileName); Reader in = new FileReader(fileToRead); Iterable<CSVRecord> records = CSVFormat.DEFAULT.withAllowMissingColumnNames(true).withHeader("").parse(in); List<DataPoint> dataPoints = new ArrayList<>(); long counter = 0; for (CSVRecord record : records) { String value = record.get(0); Double doubleValue = null; try {//from ww w . j a va2s. co m doubleValue = Double.parseDouble(value); } catch (NumberFormatException ex) { continue; } DataPoint dataPoint = new DataPoint(doubleValue, counter++); dataPoints.add(dataPoint); } return dataPoints; }
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 {//from ww w.ja v a 2 s. c o m 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); } }
From source file:org.hoteia.qalingo.translation.LoaderTranslationUtil.java
public static final void buildMessagesProperties(List<CSVRecord> records, String fileFullPath, String prefixKey, String languageCode, int languagePosition, String outputEncoding, boolean isDefault) { try {/*from ww w. j a v a 2 s . c om*/ File file = new File(fileFullPath); if (!file.exists()) { file.createNewFile(); } DataOutputStream writer = new DataOutputStream(new FileOutputStream(file)); int linePosition = 1; for (Iterator<CSVRecord> iterator = records.iterator(); iterator.hasNext();) { CSVRecord line = (CSVRecord) iterator.next(); String firstCell = line.get(0); if (firstCell.contains("Prefix") || firstCell.contains("Filename")) { // THIS IS THE LINE TITLE - DO NOTHING } else if (firstCell.contains("##")) { // THIS IS A COMMENT String value = firstCell; if (value.contains("XXLANGUAGE_CODEXX")) { if (isDefault) { value = value.replace("XXLANGUAGE_CODEXX", "Default Locale: " + languageCode); } else { value = value.replace("XXLANGUAGE_CODEXX", "Locale: " + languageCode); } } processLineWithComment(writer, languageCode, value, outputEncoding); linePosition++; } else { processLineWithValue(writer, prefixKey, line, languagePosition, linePosition, outputEncoding); linePosition++; } } writer.flush(); writer.close(); } catch (Exception e) { LOG.info("Exception", e); } }