org.supercsv.cellprocessor.constraint.NotNull Java Examples

The following examples show how to use org.supercsv.cellprocessor.constraint.NotNull. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: javakiteconnect   Author: zerodhatech   File: KiteConnect.java    License: MIT License 6 votes vote down vote up
/** This method returns array of cellprocessor for parsing csv.
 * @return CellProcessor[] array
 * */
private CellProcessor[] getProcessors(){
    CellProcessor[] processors = new CellProcessor[]{
            new NotNull(new ParseLong()),   //instrument_token
            new NotNull(new ParseLong()),   //exchange_token
            new NotNull(),                  //trading_symbol
            new org.supercsv.cellprocessor.Optional(),                 //company name
            new NotNull(new ParseDouble()), //last_price
            new org.supercsv.cellprocessor.Optional(new ParseDate("yyyy-MM-dd")),                 //expiry
            new org.supercsv.cellprocessor.Optional(),                 //strike
            new NotNull(new ParseDouble()), //tick_size
            new NotNull(new ParseInt()),    //lot_size
            new NotNull(),                  //instrument_type
            new NotNull(),                  //segment
            new NotNull()                   //exchange
    };
    return processors;
}
 
Example #2
Source Project: geode-demo-application   Author: Pivotal-Open-Source-Hub   File: CacheLoader.java    License: Apache License 2.0 6 votes vote down vote up
public void loadData() {
	System.out.println("Loading the Data");
	// load the current transactions
	String[] nameMapping = new String[] { "customerId", "productId",
			"quantity", "retailPrice", "id", "markUp", "orderStatus" };
	CellProcessor[] processors = new CellProcessor[] { new NotNull(),// customerId
			new NotNull(),// productId
			new ParseInt(),// quantity
			new ParseDouble(),// retailsPrice
			new NotNull(),// transactionId
			new ParseDouble(),// markUp
			new NotNull() // order status
	};
	loadCurrentTransactions("current_transactions.csv", nameMapping,
			processors);
	System.out
			.println("*************************************************************");
	System.out
			.println("********************PLAYING TRANSACTIONS*********************");
	System.out
			.println("*************************************************************");
	start();
}
 
Example #3
Source Project: super-csv   Author: super-csv   File: Reading.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty
 * columns are read as null (hence the NotNull() for mandatory columns).
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {
	
	final String emailRegex = "[a-z0-9\\._][email protected][a-z0-9\\.]+"; // just an example, not very robust!
	StrRegEx.registerMessage(emailRegex, "must be a valid email address");
	
	final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
		new NotNull(), // firstName
		new NotNull(), // lastName
		new ParseDate("dd/MM/yyyy"), // birthDate
		new ParseSqlTime("HH:mm:ss"),
		new NotNull(), // mailingAddress
		new Optional(new ParseBool()), // married
		new Optional(new ParseInt()), // numberOfKids
		new NotNull(), // favouriteQuote
		new StrRegEx(emailRegex), // email
		new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	
	return processors;
}
 
Example #4
Source Project: super-csv   Author: super-csv   File: Writing.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. All values
 * are converted to Strings before writing (there's no need to convert them), and null values will be written as
 * empty columns (no need to convert them to "").
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {
	
	final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
		new NotNull(), // firstName
		new NotNull(), // lastName
		new FmtDate("dd/MM/yyyy"), // birthDate
		new NotNull(), // mailingAddress
		new Optional(new FmtBool("Y", "N")), // married
		new Optional(), // numberOfKids
		new NotNull(), // favouriteQuote
		new NotNull(), // email
		new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	
	return processors;
}
 
Example #5
Source Project: super-csv   Author: super-csv   File: SuperCsvBOMTest.java    License: Apache License 2.0 6 votes vote down vote up
public void ReadTestCSVFile(Reader reader) throws IOException {
	ICsvBeanReader beanReader = new CsvBeanReader(reader, CsvPreference.STANDARD_PREFERENCE);
	final String[] header = beanReader.getHeader(true);
	assertEquals("customerNo", header[0]);
	CustomerBean customer = null;
	final String emailRegex = "[a-z0-9\\._][email protected][a-z0-9\\.]+"; // just an example, not very robust!
	StrRegEx.registerMessage(emailRegex, "must be a valid email address");
	final CellProcessor[] processors = new CellProcessor[]{new UniqueHashCode(), // customerNo (must be unique)
			new NotNull(), // firstName
			new NotNull(), // lastName
			new ParseDate("dd/MM/yyyy"), // birthDate
			new ParseSqlTime("HH:mm:ss"), // birthTime
			new NotNull(), // mailingAddress
			new Optional(new ParseBool()), // married
			new Optional(new ParseInt()), // numberOfKids
			new NotNull(), // favouriteQuote
			new StrRegEx(emailRegex), // email
			new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	customer = beanReader.read(CustomerBean.class, header, processors);
	assertEquals("1", customer.getCustomerNo());
	assertEquals("John", customer.getFirstName());
	assertEquals("[email protected]", customer.getEmail());
	assertEquals(0, customer.getLoyaltyPoints());
	beanReader.close();
}
 
Example #6
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertsToBasicObjects() throws Exception {
	String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
		new ParseBigDecimal(decimalFormatSymbols) };
	
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
	Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
	Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
}
 
Example #7
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConverterSupport() throws Exception {
	String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
		new ParseBigDecimal(decimalFormatSymbols) };
	
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
	Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
	Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
}
 
Example #8
Source Project: haxademic   Author: cacheflowe   File: CsvTest.java    License: MIT License 6 votes vote down vote up
/**
 * Sets up the CSV processors for loading and saving data
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {

	final String emailRegex = "[a-z0-9\\._][email protected][a-z0-9\\.]+"; // just an example, not very robust!
	StrRegEx.registerMessage(emailRegex, "must be a valid email address");

	final CellProcessor[] processors = new CellProcessor[] { 
			new UniqueHashCode(), 	// score id (must be unique)
			new NotNull(), 			// initials
			new NotNull(), 			// emails
			new ParseInt(), 		// completeTime
			new ParseInt() 			// regionId
	};

	return processors;
}
 
Example #9
Source Project: AIDR   Author: qcri-social   File: ReadWriteCSV.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private static CellProcessor[] getProcessors4ClassifiedCCSV() {

		final CellProcessor[] processors = new CellProcessor[]{
				//new UniqueHashCode(), // tweetID (must be unique)
				//new NotNull(),	// tweetID (must be unique)
				new Optional(),	// tweetID - data shows that sometimes tweetID CAN be null!
				new Optional(), // message
				new Optional(), // userID
				new Optional(), // userName
				new Optional(), // userURL
				new Optional(), // createdAt
				new NotNull(),	// crisis name
				new Optional(),	// attribute name
				new Optional(),	// attribute code
				new Optional(), // label name
				new Optional(), // label description
				new Optional(), // label code
				new Optional(), // confidence
				new Optional() // humanLabeled
		};
		return processors;
	}
 
Example #10
Source Project: AIDR   Author: qcri-social   File: ReadWriteCSV.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private static CellProcessor[] getProcessors4ClassifiedTweetIDSCCSV() {

		final CellProcessor[] processors = new CellProcessor[]{
				//new UniqueHashCode(), // tweetID (must be unique)
				new NotNull(),	// tweetID (must be unique): sometimes CAN be null!
				new NotNull(), 	// crisis name
				new Optional(),	// attribute name
				new Optional(), // attribute code
				new Optional(), // label name
				new Optional(), // label description
				new Optional(),	// label code
				new Optional(), // confidence
				new Optional(), // humanLabeled
		};
		return processors;
	}
 
Example #11
Source Project: ipst   Author: itesla   File: DtaParser.java    License: Mozilla Public License 2.0 5 votes vote down vote up
public static Map<String, String> readWithCsvMapReader(Path dicoFile) throws Exception {

        Map<String, String> retMap = new HashMap<>();
        try (ICsvMapReader mapReader = new CsvMapReader(Files.newBufferedReader(dicoFile, StandardCharsets.UTF_8), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE)) {
            final String[] header = mapReader.getHeader(true);
            LOGGER.debug(" cvsheader length: " + header.length);
            final CellProcessor[] rowProcessors = new CellProcessor[header.length];
            for (int i = 0; i < rowProcessors.length; i++) {
                if (i == 0) {
                    rowProcessors[i] = new NotNull();
                } else {
                    rowProcessors[i] = null;
                }
            }

            Map<String, Object> componentMap;
            while ((componentMap = mapReader.read(header, rowProcessors)) != null) {
                //System.out.println(String.format("lineNo=%s, rowNo=%s, mapping=%s", mapReader.getLineNumber(), mapReader.getRowNumber(), customerMap));
                String eurostagId = (String) componentMap.get(header[1]);
                String cimId = (String) componentMap.get(header[0]);
                if (eurostagId == null) {
                    LOGGER.warn("eurostagId=" + eurostagId + ", cimId=" + cimId);
                } else {
                    if (retMap.containsKey(eurostagId)) {
                        LOGGER.warn("eurostagId=" + eurostagId + " already in the map");
                    }
                    retMap.put(eurostagId, cimId);
                }
            }
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("ids map: " + retMap);
        }
        return retMap;

    }
 
Example #12
Source Project: journaldev   Author: journaldev   File: SuperCSVParserExample.java    License: MIT License 5 votes vote down vote up
private static CellProcessor[] getProcessors() {
	
	final CellProcessor[] processors = new CellProcessor[] { 
               new UniqueHashCode(), // ID (must be unique)
               new NotNull(), // Name
               new Optional(), // Role
               new NotNull() // Salary
       };
	return processors;
}
 
Example #13
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSeparator() throws IOException {
	List<String> data = Arrays.asList("John", "Connor");
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	char customSeparator = '+';
	CsvPreference customPreference = new Builder('"', customSeparator, "").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("John+Connor", result);
}
 
Example #14
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomQuote() throws IOException {
	List<String> data = Collections.singletonList("John \n Connor");
	CellProcessor[] processors = { new NotNull() };
	
	char customQuote = '|';
	CsvPreference customPreference = new Builder(customQuote, ',', "").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("|John  Connor|", result);
}
 
Example #15
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomEOL() throws IOException {
	List<String> data = Collections.singletonList("John Connor");
	CellProcessor[] processors = { new NotNull() };
	
	String customEndOfLine = ">\r\n";
	CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("John Connor>\r\n", result);
}
 
Example #16
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineInDelimitedField() throws IOException {
	List<String> data = Arrays.asList("Jo\nhn", "Con\nnor");
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "\n").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("\"Jo\nhn\",\"Con\nnor\"\n", result);
}
 
Example #17
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedQuoteInQuotedField() throws IOException {
	List<String> data = Arrays.asList("Joh\"n", "Con\"nor");
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("\"Joh\"\"n\",\"Con\"\"nor\"", result);
}
 
Example #18
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertsToBasicObjects() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setSavings(new BigDecimal(6.65));
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
		new FmtNumber(formatter) };
	
	StringWriter writer = new StringWriter();
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
}
 
Example #19
Source Project: super-csv   Author: super-csv   File: WritingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testConverterSupport() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setSavings(new BigDecimal(6.65));
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
		new FmtNumber(formatter) };
	
	StringWriter writer = new StringWriter();
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
}
 
Example #20
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSeparator() throws IOException {
	String csv = "John+Connor";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	char customSeparator = '+';
	CsvPreference customPreference = new Builder('"', customSeparator, "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example #21
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomQuote() throws IOException {
	String csv = "|John  Connor|";
	CellProcessor[] processors = { new NotNull() };
	
	char customQuote = '|';
	CsvPreference customPreference = new Builder(customQuote, ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(1, result.size());
	Assert.assertEquals("John  Connor", result.get(0));
}
 
Example #22
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomEOL() throws IOException {
	String csv = "John,Connor\r>\n";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	String customEndOfLine = "\r>\n";
	CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example #23
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineInDelimitedField() throws IOException {
	String csv = "\"Jo\nhn\",\"Con\nnor\"\n";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "\n").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("Jo\nhn", result.get(0));
	Assert.assertEquals("Con\nnor", result.get(1));
}
 
Example #24
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedQuoteInQuotedField() throws IOException {
	String csv = "\"Joh\"\"n\",\"Con\"\"nor\"";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("Joh\"n", result.get(0));
	Assert.assertEquals("Con\"nor", result.get(1));
}
 
Example #25
Source Project: super-csv   Author: super-csv   File: ReadingFeaturesTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertsToPrimitives() throws IOException {
	String csv = "Connor,John,16\r\n";
	String[] mapping = { "lastName", "firstName", "age" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt() };
	
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), STANDARD_PREFERENCE);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
}
 
Example #26
Source Project: super-csv-annotation   Author: mygreen   File: ProcesssorBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CellProcessor> buildForReading(final Class<Integer> type, final FieldAccessor field,
        final Configuration config, final Class<?>[] groups) {
    
    CellProcessor processor = new NotNull(new Trim(new ParseInt()));
    return Optional.of(processor);
}
 
Example #27
Source Project: super-csv-annotation   Author: mygreen   File: ProcesssorBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CellProcessor> buildForWriting(final Class<Integer> type, final FieldAccessor field,
        final Configuration config, final Class<?>[] groups) {
    
    CellProcessor processor = new NotNull(new FmtNumber(new DecimalFormat("#,##0")));
    return Optional.of(processor);
}
 
Example #28
Source Project: ipst   Author: itesla   File: DdbDyrLoader.java    License: Mozilla Public License 2.0 4 votes vote down vote up
private Map<String, String> readWithCsvMapReader(Path mappingFile) throws IOException {
    Map<String, String> retMap = new HashMap<>();
    try (ICsvMapReader mapReader = new CsvMapReader(Files.newBufferedReader(mappingFile, StandardCharsets.UTF_8), CsvPreference.STANDARD_PREFERENCE)) {
        final String[] header = mapReader.getHeader(true);
        log.info(" cvsheader length: " + header.length);
        final CellProcessor[] rowProcessors = new CellProcessor[header.length];
        for (int i = 0; i < rowProcessors.length; i++) {
            if (i == 0) {
                rowProcessors[i] = new NotNull();
            } else {
                rowProcessors[i] = new NotNull();
            }
        }

        Map<String, Object> componentMap;
        while ((componentMap = mapReader.read(header, rowProcessors)) != null) {
            String psseId = (String) componentMap.get(header[0]);
            String rdfId = (String) componentMap.get(header[1]);
            if (psseId == null) {
                log.warn("psseId=" + psseId + ", rdfId=" + rdfId);
            } else {
                if (retMap.containsKey(psseId)) {
                    log.warn("psseId=" + psseId + " already in the map");
                }
                retMap.put(psseId, rdfId);
            }
        }
    }

    if (log.isTraceEnabled()) {
        log.trace("ids map: " + retMap);
    }
    log.info("ids map: " + retMap);
    return retMap;
}
 
Example #29
Source Project: geode-demo-application   Author: Pivotal-Open-Source-Hub   File: CacheLoader.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Call this to load the Data, it uses the CSV files in its classpath
 */
public void loadData() {
	
	startTime = new Date().getTime();
	
	//load the customers
	String[] nameMapping = new String[]{"city","birthday","id","name", "emailAddress"};
	CellProcessor[] processors = new CellProcessor[] { 
       		new NotNull(), //city
       		new ParseDate("dd-MM-yyyy"), //birthday
               new NotNull(), //id
               new NotNull(), //name
               new NotNull() //email
       };
       loadCustomers("customer.csv",nameMapping,processors);
       
       //load the products
       nameMapping = new String[]{"stockOnHand","wholeSalePrice","brand","type", "color", "size", "gender", "id"};
       processors = new CellProcessor[] { 
       		new ParseInt(),//stockOnHand
       		new ParseDouble(),//wholeSalePrice
       		new NotNull(),//brand
       		new NotNull(),//type
       		new NotNull(),//color
       		new ParseDouble(),//size
       		new NotNull(),//gender
               new NotNull()//productId
       };
       loadProducts("products.csv",nameMapping,processors);
       
       //load the historic transactions - these are just randomly generated and do not respect stock quantity
       nameMapping = new String[]{"customerId","transactionDate","productId","quantity", "retailPrice", "id", "markUp", "orderStatus"};
       processors = new CellProcessor[] { 
       		new NotNull(),//customerId
       		new ParseDate("dd-MM-yyyy"),//transactionDate
       		new NotNull(),//productId
       		new ParseInt(),//quantity
       		new ParseDouble(),//retailsPrice
       		new NotNull(),//transactionId
       		new ParseDouble(),//markUp
       		new NotNull()//order status
       };
       loadTransactions("transactions.csv",nameMapping,processors);
       
       //load the mark ups
       nameMapping = new String[]{"id", "rate","levelName","qualifyingTransactionCountFloor","qualifyingTransactionCountCeiling"};
       processors = new CellProcessor[] {
       		new NotNull(),//id
       		new ParseDouble(),//rate
       		new NotNull(),//levelName
       		new ParseInt(),//qualifyingTransactionCountFloor
       		new ParseInt()//qualifyingTransactionCountCeiling
       };
       loadMarkUps("markUps.csv",nameMapping,processors);
       
       //clean out the alerts
       for (Alert alert : alertRepository.findAll()) {
       	alertRepository.delete(alert);
       }
       long endTime = new Date().getTime();
	long timeToLoad = endTime - startTime;
	activityLog.add("Total Loading Time: " + timeToLoad/1000 + " seconds");
       closeBeanReader();
	writeOutLogs();
}
 
Example #30
Source Project: super-csv   Author: super-csv   File: Writing.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * An example of writing using CsvDozerBeanWriter.
 */
private static void writeWithDozerCsvBeanWriter() throws Exception {
	
	final CellProcessor[] processors = new CellProcessor[] { 
		new Token(0, null),     // age
		new FmtBool("Y", "N"),  // consent
		new NotNull(),          // questionNo 1
		new Optional(),         // answer 1
		new NotNull(),          // questionNo 2
		new Optional(),         // answer 2
		new NotNull(),          // questionNo 3
		new Optional() };       // answer 3
	
	// create the survey responses to write
	SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
		"Albert Einstein"), new Answer(3, "Big Bang Theory")));
	SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
		"Nikola Tesla"), new Answer(3, "Stargate")));
	SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
		"Carl Sagan"), new Answer(3, "Star Wars")));
	final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
	
	ICsvDozerBeanWriter beanWriter = null;
	try {
		beanWriter = new CsvDozerBeanWriter(new FileWriter("target/writeWithCsvDozerBeanWriter.csv"),
			CsvPreference.STANDARD_PREFERENCE);
		
		// configure the mapping from the fields to the CSV columns
		beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
		
		// write the header
		beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
			"questionNo3", "answer3");
		
		// write the beans
		for( final SurveyResponse surveyResponse : surveyResponses ) {
			beanWriter.write(surveyResponse, processors);
		}
		
	}
	finally {
		if( beanWriter != null ) {
			beanWriter.close();
		}
	}
}