1 /* 2 * $Source$ 3 * $Revision$ 4 * 5 * Copyright (C) 2001 Myles Chippendale 6 * 7 * Part of Melati (http://melati.org), a framework for the rapid 8 * development of clean, maintainable web applications. 9 * 10 * Melati is free software; Permission is granted to copy, distribute 11 * and/or modify this software under the terms either: 12 * 13 * a) the GNU General Public License as published by the Free Software 14 * Foundation; either version 2 of the License, or (at your option) 15 * any later version, 16 * 17 * or 18 * 19 * b) any version of the Melati Software License, as published 20 * at http://melati.org 21 * 22 * You should have received a copy of the GNU General Public License and 23 * the Melati Software License along with this program; 24 * if not, write to the Free Software Foundation, Inc., 25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the 26 * GNU General Public License and visit http://melati.org to obtain the 27 * Melati Software License. 28 * 29 * Feel free to contact the Developers of Melati (http://melati.org), 30 * if you would like to work out a different arrangement than the options 31 * outlined here. It is our intention to allow Melati to be used by as 32 * wide an audience as possible. 33 * 34 * This program is distributed in the hope that it will be useful, 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of 36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 37 * GNU General Public License for more details. 38 * 39 * Contact details for copyright holder: 40 * 41 * Myles Chippendale <mylesc@paneris.org> 42 */ 43 package org.melati.poem.csv; 44 45 import java.util.Vector; 46 import java.io.File; 47 import java.io.Writer; 48 import java.io.IOException; 49 50 import org.melati.poem.Database; 51 import org.melati.poem.PoemBugPoemException; 52 import org.melati.poem.PoemThread; 53 import org.melati.poem.Table; 54 55 /** 56 * A class to define a sequence of {@link CSVTable}s and process them by 57 * parsing the files and writing the data to the database. 58 * 59 * @author MylesC AT paneris.org 60 * 61 */ 62 public class CSVFilesProcessor { 63 64 protected Vector<CSVTable> tables = new Vector<CSVTable>(); 65 Database db = null; 66 67 /** 68 * Constructor. 69 * 70 * @param db the target database 71 */ 72 public CSVFilesProcessor(Database db) { 73 this.db = db; 74 } 75 76 /** 77 * Convenience method. 78 * 79 * @param tablename the name of a POEM table 80 * @param file a CSV file, with first line containing field names 81 * @return a new CSVTable 82 */ 83 public CSVTable addTable(String tablename, File file) { 84 return addTable(db.getTable(tablename), file); 85 } 86 87 /** 88 * Add a table to this processor. 89 * 90 * @param tab a POEM table 91 * @param file a CSV file, with first line containing field names 92 * @return a new CSVTable 93 */ 94 public CSVTable addTable(Table<?> tab, File file) { 95 if (!file.exists()) 96 throw new RuntimeException("File not found: " + file.getPath()); 97 CSVTable table = new CSVTable(tab, file); 98 tables.addElement(table); 99 return table; 100 } 101 102 /** 103 * Load all the data from the files, empty the tables if necessary and then 104 * write the new data into the tables. 105 * <p> 106 * Write a report of the progress to the Writer. 107 * 108 * @param writeOnFly flag whether to write down to db when all files read in 109 * if set then it is the programmers responsibility to ensure that 110 * there are no references to yet to be created fields 111 * @param emptyTables flag whether to remove remains from last run 112 * @param recordDetails flag passed in to table.report 113 * @param fieldDetails flag passed in to table.report 114 * @param output to write report to 115 * @throws IOException if file stuff goes wrong 116 * @throws CSVParseException if csv file has an error 117 * @throws NoPrimaryKeyInCSVTableException not thrown 118 * @throws CSVWriteDownException thrown when a persistent cannot be created 119 */ 120 public void process(boolean writeOnFly, boolean emptyTables, 121 boolean recordDetails, boolean fieldDetails, Writer output) 122 throws IOException, CSVParseException, NoPrimaryKeyInCSVTableException, 123 CSVWriteDownException { 124 125 output.write("Trying to get exclusive lock on the database\n"); 126 db.beginExclusiveLock(); 127 output.write("Got exclusive lock on the database!!!\n"); 128 129 // Delete all records from the tables, if necessary 130 if (emptyTables) { 131 for (int i = 0; i < tables.size(); i++) { 132 CSVTable t = (CSVTable) tables.elementAt(i); 133 t.emptyTable(); 134 output.write("Emptied table :" + t.getName() + "\n"); 135 System.err.println("Emptied table :" + t.getName()); 136 } 137 PoemThread.writeDown(); 138 } 139 140 141 // Load in data 142 for (int i = 0; i < tables.size(); i++) { 143 CSVTable t = ((CSVTable) tables.elementAt(i)); 144 t.load(writeOnFly); 145 output.write("Loaded table :" + t.getName() + "\n"); 146 System.err.println("Loaded table :" + t.getName()); 147 } 148 149 // We must have loaded in all the data before we 150 // try writing records, otherwise Foreign Key lookups 151 // defined in this set of CSVs won't work 152 if (!writeOnFly) { 153 writeData(output); 154 output.write("Written records\n"); 155 } 156 157 db.endExclusiveLock(); 158 159 output.write("Ended exclusive lock on the database!!!\n"); 160 output.write("***** REPORT ******\n"); 161 162 // Write a report about how many records are in each table 163 for (int i = 0; i < tables.size(); i++) 164 ((CSVTable) tables.elementAt(i)).report(recordDetails, fieldDetails, 165 output); 166 167 } 168 /** 169 * With write on the fly false. 170 * <p> 171 * Load all the data from the files, empty the tables if necessary and then 172 * write the new data into the tables. 173 * <p> 174 * Write a report of the progress to the Writer. 175 * 176 * @param emptyTables flag whether to remove remains from last run 177 * @param recordDetails flag passed in to table.report 178 * @param fieldDetails flag passed in to table.report 179 * @param output to write report to 180 * @throws IOException if file stuff goes wrong 181 * @throws CSVParseException if csv file has an error 182 * @throws NoPrimaryKeyInCSVTableException not thrown 183 * @throws CSVWriteDownException thrown when a persistent cannot be created 184 */ 185 public void process(boolean emptyTables, boolean recordDetails, 186 boolean fieldDetails, Writer output) 187 throws IOException, 188 CSVParseException, NoPrimaryKeyInCSVTableException, CSVWriteDownException { 189 process(false, emptyTables, recordDetails, fieldDetails, output); 190 } 191 192 /** 193 * @param o output log to write to 194 * @throws NoPrimaryKeyInCSVTableException 195 * @throws CSVWriteDownException 196 */ 197 protected void writeData(Writer o) throws NoPrimaryKeyInCSVTableException, 198 CSVWriteDownException { 199 for (int i = 0; i < tables.size(); i++) { 200 CSVTable t = (CSVTable) tables.elementAt(i); 201 try { 202 o.write("Writing " + t.getName() + ".\n"); 203 } catch (IOException e) { 204 throw new PoemBugPoemException("Problem writing log", e); 205 } 206 t.writeRecords(); 207 } 208 } 209 210 } 211