View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.supercsv.example.dozer;
17  
18  import java.io.FileWriter;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.supercsv.cellprocessor.ConvertNullTo;
23  import org.supercsv.cellprocessor.FmtBool;
24  import org.supercsv.cellprocessor.Optional;
25  import org.supercsv.cellprocessor.Token;
26  import org.supercsv.cellprocessor.constraint.NotNull;
27  import org.supercsv.cellprocessor.ift.CellProcessor;
28  import org.supercsv.io.dozer.CsvDozerBeanWriter;
29  import org.supercsv.io.dozer.ICsvDozerBeanWriter;
30  import org.supercsv.mock.dozer.Answer;
31  import org.supercsv.mock.dozer.SurveyResponse;
32  import org.supercsv.prefs.CsvPreference;
33  
34  /**
35   * Dozer writing examples.
36   */
37  public class Writing {
38  	
39  	private static final String[] FIELD_MAPPING = new String[] { 
40  		"age",                   // simple field mapping (like CsvBeanWriter)
41  		"consentGiven",          // as above
42  		"answers[0].questionNo", // indexed (first element) + deep mapping
43  		"answers[0].answer", 
44  		"answers[1].questionNo", // indexed (second element) + deep mapping
45  		"answers[1].answer", 
46  		"answers[2].questionNo", 
47  		"answers[2].answer" };
48  	
49  	public static void main(String[] args) throws Exception {
50  		writeWithDozerCsvBeanWriter();
51  		partialWriteWithCsvDozerBeanWriter();
52  	}
53  	
54  	/**
55  	 * An example of writing using CsvDozerBeanWriter.
56  	 */
57  	private static void writeWithDozerCsvBeanWriter() throws Exception {
58  		
59  		final CellProcessor[] processors = new CellProcessor[] { 
60  			new Token(0, null),     // age
61  			new FmtBool("Y", "N"),  // consent
62  			new NotNull(),          // questionNo 1
63  			new Optional(),         // answer 1
64  			new NotNull(),          // questionNo 2
65  			new Optional(),         // answer 2
66  			new NotNull(),          // questionNo 3
67  			new Optional() };       // answer 4
68  		
69  		// create the survey responses to write
70  		SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
71  			"Albert Einstein"), new Answer(3, "Big Bang Theory")));
72  		SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
73  			"Nikola Tesla"), new Answer(3, "Stargate")));
74  		SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
75  			"Carl Sagan"), new Answer(3, "Star Wars")));
76  		final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
77  		
78  		ICsvDozerBeanWriter beanWriter = null;
79  		try {
80  			beanWriter = new CsvDozerBeanWriter(new FileWriter("target/writeWithCsvDozerBeanWriter.csv"),
81  				CsvPreference.STANDARD_PREFERENCE);
82  			
83  			// configure the mapping from the fields to the CSV columns
84  			beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
85  			
86  			// write the header
87  			beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
88  				"questionNo3", "answer3");
89  			
90  			// write the beans
91  			for( final SurveyResponse surveyResponse : surveyResponses ) {
92  				beanWriter.write(surveyResponse, processors);
93  			}
94  			
95  		}
96  		finally {
97  			if( beanWriter != null ) {
98  				beanWriter.close();
99  			}
100 		}
101 	}
102 	
103 	/**
104 	 * An example of partial reading using CsvDozerBeanWriter.
105 	 */
106 	private static void partialWriteWithCsvDozerBeanWriter() throws Exception {
107 		
108 		// null ages and answers are converted to something more meaningful
109 		final CellProcessor[] partialProcessors = new CellProcessor[] { 
110 			new Token(0, "age not supplied"), // age
111 			new FmtBool("Y", "N"),                 // consent
112 			new NotNull(),                         // questionNo 1
113 			new ConvertNullTo("not answered"),     // answer 1
114 			new NotNull(),                         // questionNo 2
115 			new ConvertNullTo("not answered"),     // answer 2
116 			new NotNull(),                         // questionNo 3
117 			new ConvertNullTo("not answered")};    // answer 4
118 		
119 		// create the survey responses to write
120 		SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
121 			"Albert Einstein"), new Answer(3, "Big Bang Theory")));
122 		SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
123 			"Nikola Tesla"), new Answer(3, "Stargate")));
124 		SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
125 			"Carl Sagan"), new Answer(3, "Star Wars")));
126 		final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
127 		
128 		ICsvDozerBeanWriter beanWriter = null;
129 		try {
130 			beanWriter = new CsvDozerBeanWriter(new FileWriter("target/partialWriteWithCsvDozerBeanWriter.csv"),
131 				CsvPreference.STANDARD_PREFERENCE);
132 			
133 			// configure the mapping from the fields to the CSV columns
134 			beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
135 			
136 			// write the header
137 			beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
138 				"questionNo3", "answer3");
139 			
140 			// write the beans
141 			for( final SurveyResponse surveyResponse : surveyResponses ) {
142 				beanWriter.write(surveyResponse, partialProcessors);
143 			}
144 			
145 		}
146 		finally {
147 			if( beanWriter != null ) {
148 				beanWriter.close();
149 			}
150 		}
151 	}
152 }