001    package nl.cwi.sen1.visplugin.csvexport;
002    
003    import java.io.IOException;
004    import java.nio.charset.Charset;
005    
006    import nl.cwi.sen1.relationstores.types.RElem;
007    import nl.cwi.sen1.relationstores.types.RElemElements;
008    import nl.cwi.sen1.relationstores.types.RTuple;
009    import nl.cwi.sen1.relationstores.types.RType;
010    import nl.cwi.sen1.relationstores.types.RTypeColumnTypes;
011    
012    import com.csvreader.CsvWriter;
013    
014    /**
015     * CSV export functionality for RTuples
016     * @author Chris Woolderink
017     * @author Antoine Savelkoul
018     */
019    public class Exporter {
020    
021            /**
022             * Enumeration used to indicate which part of a RTuple should be exported to CSV
023             * @author Antoine Savelkoul
024             * @author Chris Woolderink
025             * @date 14-03-2007
026             */
027            public enum ExportSelection {
028                    DATA_ONLY,
029                    DATA_WITH_HEADERS
030            }
031            
032            /**
033             * Writes the content of a RTuple to an CSV formatted file 
034             * @param rTuple the RTuple to be exported
035             * @param fileName the name of the output file
036             * @param selection indicates which information has to be exported
037             * @author Antoine Savelkoul
038             * @author Chris Woolderink
039             * @date 13-03-2007
040             */
041            public static void exportToFile(RTuple rTuple, String fileName, ExportSelection selection) throws IOException {
042                    CsvWriter csvWriter = new CsvWriter(fileName,',',Charset.forName("UTF-8"));
043                    export(rTuple, csvWriter, selection);
044                    csvWriter.close();
045            }
046            
047            /**
048             * Writes the content of a RTupe to a CSV writer 
049             * @param rTuple the RTuple to be exported
050             * @param csvWriter the CSV writer to produce the output
051             * @param selection indicates which information has to be exported
052             * @author Antoine Savelkoul
053             * @author Chris Woolderink
054             * @date 13-03-2007
055             */
056            public static void export(RTuple rTuple, CsvWriter csvWriter, ExportSelection selection) throws IOException {
057            RType rType = rTuple.getRtype();
058            RTypeColumnTypes columnTypes = rType.getColumnTypes();
059    
060            if( selection == ExportSelection.DATA_WITH_HEADERS ) {
061                    exportColumnTypes(columnTypes, csvWriter);
062            }
063            exportData(rTuple, csvWriter);
064            }
065            
066            /**
067             * Writes the data of an rTuple to the CvsWriter
068             * @param rTuple the RTuple to be exported
069             * @param csvWriter the CSV writer to produce the output
070             * @author Antoine Savelkoul
071             * @author Chris Woolderink
072             * @date 13-03-2007
073             */
074        private static void exportData( RTuple rTuple, CsvWriter csvWriter ) throws IOException {
075            RElem value = rTuple.getValue();
076            RElemElements valueElements = value.getElements();
077    
078            RType rType = rTuple.getRtype();
079            RTypeColumnTypes columnTypes = rType.getColumnTypes();
080    
081            int columnCount = columnTypes.getLength();
082            int rowCount = valueElements.getLength();
083                    String[] rowData = new String[columnCount];
084    
085            for(int row = 0; row < rowCount; row++) {
086    
087                RElem tuple = valueElements.getRElemAt(row);
088                RElemElements tupleElements = tuple.getElements();
089    
090                for(int column = 0; column < columnCount; column++) {
091                                    // Read all cells from the row
092                    RElem elem = tupleElements.getRElemAt(column);
093                                    rowData[column] = RElemToString(elem);
094                }
095                            // Write the row to the output stream
096                csvWriter.writeRecord(rowData);
097            }
098        }
099    
100        /**
101             * Writes the data types as headers to the CvsWriter
102         * @param columnTypes column types information that will be used as headers
103             * @param writer The CSV writer to produce the output
104             * @author Antoine Savelkoul
105             * @author Chris Woolderink
106             * @date 13-03-2007
107         */
108        private static void exportColumnTypes( RTypeColumnTypes columnTypes, CsvWriter writer ) throws IOException {
109            int numColumns = columnTypes.getLength();
110            String[] columnNames = new String[numColumns];
111            
112            // Copies each of the type names to the array
113            for(int columnNum = 0; columnNum < numColumns; columnNum++) {
114                RType columnType = columnTypes.getRTypeAt(columnNum);
115                columnNames[columnNum] = columnType.toString();
116            }
117            writer.writeRecord( columnNames );
118        }
119        
120        /**
121         * Converts the value of a simple RElem to a String
122         * @param elem the RElem to be converted
123         * @return the converted RElem
124             * @author Antoine Savelkoul
125             * @author Chris Woolderink
126             * @date 13-03-2007
127         */
128        private static String RElemToString(RElem elem) {
129            
130            if( elem.isStr() ) {
131                    return elem.getStrCon();
132            }
133            
134            if( elem.isInt() ) {
135                    return Integer.toString( elem.getInteger().getNatCon() );
136            }
137            
138            if( elem.isBool() ) {
139                    return elem.getBoolCon().toString();
140            }
141                    
142            if( elem.isLoc() ) {
143                    return elem.getLocation().toString();
144            }
145            
146            return elem.toString();
147        }
148    }