List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:entity.service.EntryFacadeREST.java
@POST @Path("/processcsv/{raceid}") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces("application/json; charset=UTF-8") public Response processCSV(@FormParam("filename") String fileName, @PathParam("raceid") Integer raceid) { parameters.initClubs();/* www. j ava 2 s . c o m*/ List<String> invalidLicences = new ArrayList<>(); invalidLicences.add("nev;rajtszam;licensz"); try (Reader in = new InputStreamReader( new FileInputStream(appParameters.getProperty("uploadFolder") + fileName), "UTF-8")) { Iterable<CSVRecord> records = CSVFormat.EXCEL.withSkipHeaderRecord().withDelimiter(';') .withHeader(CSV_HEADERS).withNullString("").parse(in); int count = 0; for (CSVRecord record : records) { EntryData ed = new EntryData(); ed.setPreentry(true); ed.setStatus("PRE"); ed.setName(record.get("name")); ed.setRacenum(record.get("racenum")); ed.setGender(record.get("gender")); ed.setBirthYear(Short.valueOf(record.get("birthyear"))); ed.setCategory(record.get("category")); ed.setFromTown(record.get("fromtown")); ed.setClubName(record.get("club")); ed.setAgegroup(record.get("agegroup")); ed.setPaid(record.get("paid").equals("IGEN")); ed.setRemainingpayment( record.get("paid").matches("[1-9]\\d{0,10}") ? Integer.parseInt(record.get("paid")) : 0); ed.setLicencenum(record.get("licencenum")); if (ed.getLicencenum() != null && !ed.getLicencenum().isEmpty()) { if (!licenceFacade.exists(ed.getLicencenum())) { invalidLicences.add(ed.getName() + ";" + ed.getRacenum() + ";" + ed.getLicencenum()); } } insertEntry(ed, raceid); ++count; } String invalidLicencesFileName = "invalid_licences_" + fileName; Files.write(Paths.get(appParameters.getProperty("uploadFolder"), invalidLicencesFileName), invalidLicences, Charset.forName("UTF-8")); HashMap<String, Object> params = new HashMap<>(); params.put("invalidLicences", invalidLicencesFileName); JsonObject jsonMsg = JsonBuilder.getJsonMsg(count + " db elnevezs beolvasva!", JsonBuilder.MsgType.INFO, params); return Response.ok(String.valueOf(jsonMsg)).build(); } catch (FileNotFoundException ex) { HashMap<String, Object> params = new HashMap<>(); params.put("fileName", fileName); JsonObject jsonMsg = JsonBuilder.getJsonMsg("A megadott fjl nem tallhat!", JsonBuilder.MsgType.ERROR, params); return Response.status(500).entity(jsonMsg).build(); } catch (IOException ex) { HashMap<String, Object> params = new HashMap<>(); params.put("fileName", fileName); JsonObject jsonMsg = JsonBuilder.getJsonMsg("Hiba a CSV fjl beolvassa kzben!", JsonBuilder.MsgType.ERROR, params); return Response.status(500).entity(jsonMsg).build(); } }
From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java
/** * 1. Gets the file header or creates one. * 2. Sets the number of columns/*from www.j a va 2 s. com*/ * 3. Checks the type of data stored in each variable using the first rowsToScan rows. * Variables are integers by default. This method will change the data type to either * double or string. * */ private void setDataTypes() { CSVParser parser = null; Reader reader = null; try { reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); //Get column names from variable attributes colNames = new String[variableAttributeMap.size()]; int index = 0; Iterator<VariableName> iter = variableAttributeMap.keySet().iterator(); VariableName tempName = null; while (iter.hasNext()) { colNames[index++] = iter.next().toString(); } //Create a parser with variable names from the variable attributes if (hasHeader) { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true).withCommentMarker('#')); } else { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withCommentMarker('#')); } //Check data types in each column. String value = ""; Iterator<CSVRecord> csvIter = parser.iterator(); CSVRecord csvRecord = null; double testValue = 0; nrow = 0; while (csvIter.hasNext()) { csvRecord = csvIter.next(); iter = variableAttributeMap.keySet().iterator(); while (iter.hasNext()) { tempName = iter.next(); value = csvRecord.get(tempName.toString()).trim(); //Check that string can be converted to double. If not, Change variable type. //Ignore missing data and other special codes try { if (!"".equals(value) && !specialDataCodes.isMissing(value)) { testValue = Double.parseDouble(value); if (testValue != Math.floor(testValue)) { //if any value is a double, the variable is a double variableAttributeMap.get(tempName).setDataType(DataType.DOUBLE); } } } catch (NumberFormatException ex) { //if any value is a String, the variable is a String variableAttributeMap.get(tempName).setDataType(DataType.STRING); } } nrow++; } } catch (IOException ex) { theException = ex; } finally { try { if (parser != null) parser.close(); if (reader != null) reader.close(); } catch (IOException ex) { theException = ex; logger.fatal(ex); } } }
From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java
private void convertFile() { CSVParser parser = null;/*from w w w . j a va 2 s . c o m*/ Reader reader = null; CSVPrinter printer = null; Writer writer = null; try { if (outputFile.exists()) { if (!overwrite) { theException = new IOException("File already exists and overwrite==false"); return; } } else { outputFile.createNewFile(); } //For debugging // System.out.println("CREATED: " + outputFile.getAbsolutePath()); //Writer header to file writer = new OutputStreamWriter(new FileOutputStream(outputFile)); printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#')); printer.printComment("VERSION"); printer.printRecord(new String[] { "jmetrik1" }); printer.printComment("METADATA"); printer.printRecord(new String[] { Integer.valueOf(nrow).toString() }); printer.printComment("ATTRIBUTES"); for (VariableName v : variableAttributeMap.keySet()) { printer.printRecord(variableAttributeMap.get(v).getAttributeArray()); } printer.printComment("DATA"); //Write data to file reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, dataFileFormat); if (hasHeader) { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true)); } else { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames)); } Iterator<CSVRecord> iter = parser.iterator(); CSVRecord csvRecord = null; VariableAttributes variableAttributes = null; DataType dataType = null; String temp = ""; while (iter.hasNext()) { csvRecord = iter.next(); for (VariableName v : variableAttributeMap.keySet()) { temp = csvRecord.get(v.toString()); variableAttributes = variableAttributeMap.get(v); dataType = variableAttributes.getDataType(); if (!variableAttributes.isMissing(temp)) { if (DataType.INTEGER == dataType) { printer.print(Double.valueOf(Double.parseDouble(temp)).intValue()); } else if (DataType.DOUBLE == dataType) { printer.print(Double.parseDouble(temp)); } else { printer.print(temp); } } else { printer.print(temp); } } printer.println(); } } catch (IOException ex) { theException = ex; } finally { try { if (parser != null) parser.close(); if (reader != null) reader.close(); if (printer != null) printer.close(); if (writer != null) writer.close(); } catch (IOException ex) { theException = ex; logger.fatal(ex); } } }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * @return True if csvRecord is a model record *//* w w w. j ava 2s .c o m*/ private boolean isModelRecord(CSVRecord csvRecord) { return ARCHIMATE_MODEL_TYPE.equals(csvRecord.get(1)); }
From source file:ca.nrc.cadc.tap.db.AsciiTableData.java
/** * @return The list of formatted objects representing a row of data. *//* w ww.j a va 2 s. com*/ @Override public List<Object> next() { if (!hasNext()) { throw new IllegalStateException("No more data to read."); } CSVRecord rec = rowIterator.next(); if (rec.size() != columnNames.size()) { throw new IllegalArgumentException( "wrong number of columns (" + rec.size() + ") expected " + columnNames.size()); } try { List<Object> row = new ArrayList<Object>(columnNames.size()); String cell = null; Object value = null; Format format = null; for (int i = 0; i < rec.size(); i++) { cell = rec.get(i); format = columnFormats.get(i); value = format.parse(cell); row.add(value); } return row; } catch (NumberFormatException ex) { throw new IllegalArgumentException("invalid number: " + ex.getMessage()); } }
From source file:ca.twoducks.vor.ossindex.report.Assistant.java
/** Convert a CSV file back to a JSON config file. * /*from w w w. j ava 2 s . co m*/ * @param file * @return * @throws IOException */ private Configuration loadCsv(File file) throws IOException { Configuration config = new Configuration(); Reader in = new FileReader(file); Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in); for (CSVRecord record : records) { String path = record.get("Path"); String state = record.get("State"); if (!"UNASSIGNED".equals(state)) { String projectName = record.get("Project Name"); // There could be 3 "projectUri" fields. In order: // SCM,Project,Home // // SCM should always have a value, others *may* String[] projectUris = parseList(record.get("Project URI")); String scmUri = projectUris[0]; String projectUri = null; String homeUri = null; if (projectUris.length > 1 && !projectUris[1].trim().isEmpty()) projectUri = projectUris[1].trim(); if (projectUris.length > 2 && !projectUris[2].trim().isEmpty()) homeUri = projectUris[2].trim(); String version = record.get("Version"); String[] cpes = parseList(record.get("CPEs")); String[] projectLicenses = parseList(record.get("Project Licenses")); String fileLicense = record.get("File License"); String projectDescription = record.get("Project Description"); String digest = record.get("Digest"); String comment = record.get("Comment"); String overrideName = null; String overrideLicense = null; try { overrideName = record.get("Override Name"); } catch (IllegalArgumentException e) { } try { overrideLicense = record.get("Override License"); } catch (IllegalArgumentException e) { } // Override applicable fields if (overrideName != null && !overrideName.trim().isEmpty()) { projectName = overrideName; scmUri = null; projectUri = null; homeUri = null; version = null; cpes = new String[0]; projectLicenses = new String[0]; projectDescription = null; if (overrideLicense != null) { projectLicenses = new String[] { overrideLicense }; } } // Add the checksum to the file list FileConfig fileConfig = config.addFile(digest); if (path != null && !path.isEmpty()) { File aFile = new File(path); File parent = aFile.getParentFile(); if (parent == null) fileConfig.setName(path); else fileConfig.setPath(path); } if (fileLicense != null && !fileLicense.isEmpty()) fileConfig.setLicense(fileLicense); if (comment != null && !comment.isEmpty()) fileConfig.setComment(comment); if (state != null && !state.isEmpty()) fileConfig.setState(state); ProjectGroup group = config.getGroup(projectName); ProjectConfig project = group.getProject(scmUri, version); // If the project has not been defined yet then set its values if (project.getName() == null) { project.setName(projectName); if (projectUri != null) project.setProjectUri(projectUri); if (homeUri != null) project.setHomeUri(homeUri); if (cpes != null) { for (String cpe : cpes) { project.addCpe(cpe); } } if (projectLicenses != null) { for (String license : projectLicenses) { project.addLicense(license); } } if (projectDescription != null && !projectDescription.isEmpty()) project.setDescription(projectDescription); } // Add the file to the project project.addFile(fileConfig); } } return config; }
From source file:com.siemens.sw360.importer.ComponentCSVRecordBuilder.java
ComponentCSVRecordBuilder(CSVRecord record) { int i = 0;/* w w w . j av a2 s . c o m*/ //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++); releaseContacts = 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++); cIAL = record.get(i++); cIECCN = record.get(i++); cIExternalSupplierID = record.get(i++); cIAssessorContactPerson = record.get(i++); cIAssessorDepartment = 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)); // Default copies: releaseCreatedBy = alternative(releaseCreatedBy, componentCreatedBy); componentCreatedBy = alternative(componentCreatedBy, releaseCreatedBy); releaseCreatedOn = alternative(releaseCreatedOn, componentCreatedOn); componentCreatedOn = alternative(componentCreatedOn, releaseCreatedOn); }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * Create an Archimate Element from a given CSVRecord *///ww w . j a va2s .co m private void createElementFromRecord(CSVRecord csvRecord) throws CSVParseException { // ID String id = csvRecord.get(0); if (!StringUtils.isSet(id)) { id = generateID(); } else { checkIDForInvalidCharacters(id); } // Class type String type = csvRecord.get(1); EClass eClass = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(type); // Can only be Archimate element type if (!isArchimateElementEClass(eClass)) { throw new CSVParseException(Messages.CSVImporter_3); } String name = normalise(csvRecord.get(2)); String documentation = csvRecord.get(3); // Is the element already in the model? IArchimateElement element = (IArchimateElement) findArchimateComponentInModel(id, eClass); // Yes if (element != null) { updatedComponents.put(element, new String[] { name, documentation }); } // No, create a new one else { element = (IArchimateElement) IArchimateFactory.eINSTANCE.create(eClass); element.setId(id); element.setName(name); element.setDocumentation(documentation); newComponents.put(id, element); } }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * Create a Property from a given CSVRecord *///from w w w . j a v a2 s . c o m private void createPropertyFromRecord(CSVRecord csvRecord) throws CSVParseException { // ID String id = csvRecord.get(0); if (!StringUtils.isSet(id)) { throw new CSVParseException(Messages.CSVImporter_6); } else { checkIDForInvalidCharacters(id); } // Find referenced element in newly created list IProperties propertiesObject = newComponents.get(id); // Not found, check if it's referencing an existing element in the model if (propertiesObject == null) { EObject eObject = ArchimateModelUtils.getObjectByID(fModel, id); if (eObject instanceof IProperties) { propertiesObject = (IProperties) eObject; } } // Not found, check if it's referencing the model if (propertiesObject == null && id.equals(modelID)) { propertiesObject = fModel; } // Not found at all if (propertiesObject == null) { throw new CSVParseException(Messages.CSVImporter_7 + id); } String key = normalise(csvRecord.get(1)); String value = normalise(csvRecord.get(2)); // Is there already a property with this key? IProperty property = getProperty(propertiesObject, key); if (property != null) { updatedProperties.put(property, value); } // No, create new one else { property = IArchimateFactory.eINSTANCE.createProperty(); property.setKey(key); property.setValue(value); newProperties.put(property, propertiesObject); } }
From source file:com.archimatetool.csv.importer.CSVImporter.java
/** * Create an Archimate relationship from a given CSVRecord *///from ww w .java2s . c o m private void createRelationFromRecord(CSVRecord csvRecord) throws CSVParseException { // ID String id = csvRecord.get(0); if (!StringUtils.isSet(id)) { id = generateID(); } else { checkIDForInvalidCharacters(id); } // Type String type = csvRecord.get(1); EClass eClass = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(type); if (!isRelationshipEClass(eClass)) { throw new CSVParseException(Messages.CSVImporter_4 + id); } String name = normalise(csvRecord.get(2)); String documentation = csvRecord.get(3); // Is the relation already in the model? IRelationship relation = (IRelationship) findArchimateComponentInModel(id, eClass); // Yes if (relation != null) { updatedComponents.put(relation, new String[] { name, documentation }); } // No, create a new one else { relation = (IRelationship) IArchimateFactory.eINSTANCE.create(eClass); // Find source and target elements String sourceID = csvRecord.get(4); IArchimateElement source = findReferencedElement(sourceID); String targetID = csvRecord.get(5); IArchimateElement target = findReferencedElement(targetID); // Is it a valid relationship? if (!ArchimateModelUtils.isValidRelationship(source.eClass(), target.eClass(), eClass)) { throw new CSVParseException(Messages.CSVImporter_5 + id); } relation.setSource(source); relation.setTarget(target); relation.setId(id); relation.setName(name); relation.setDocumentation(documentation); newComponents.put(id, relation); } }