Coverage Report - org.melati.poem.csv.CSVRecord
 
Classes in this File Line Coverage Branch Coverage Complexity
CSVRecord
0%
0/68
0%
0/26
3.5
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Part of Melati (http://melati.org), a framework for the rapid
 6  
  * development of clean, maintainable web applications.
 7  
  *
 8  
  *  Copyright (C) 2001 Myles Chippendale
 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 At paneris.org>
 42  
  *
 43  
  *
 44  
  * ------
 45  
  *  Note
 46  
  * ------
 47  
  *
 48  
  * I will assign copyright to PanEris (http://paneris.org) as soon as
 49  
  * we have sorted out what sort of legal existence we need to have for
 50  
  * that to make sense. 
 51  
  * In the meantime, if you want to use Melati on non-GPL terms,
 52  
  * contact me!
 53  
  */
 54  
 
 55  
 package org.melati.poem.csv;
 56  
 
 57  
 import java.util.Vector;
 58  
 
 59  
 import org.melati.poem.Persistent;
 60  
 import org.melati.poem.PoemThread;
 61  
 import org.melati.poem.Table;
 62  
 
 63  
 /**
 64  
  * A record within a CSV File.
 65  
  */
 66  
 public class CSVRecord {
 67  
 
 68  
   private Vector<CSVField> fields;
 69  
   
 70  
   /** The value of the primary key of this record, from the csv file */
 71  0
   String primaryKeyValue = null;
 72  
 
 73  
   /** The table this record wants to be written to */
 74  0
   Table<?> table = null;
 75  
 
 76  
   /** The Poem Persistent corresponding to writing this record to the db */
 77  0
   Persistent poemRecord = null;
 78  
   
 79  
   /** The line number of the CSV file. */
 80  
   private int lineNo;
 81  
   
 82  
   /** The record number of the CSV file. */
 83  
   private int recordNo;
 84  
   
 85  
   /**
 86  
    * Constructor.
 87  
    */
 88  
   public CSVRecord(Table<?> table) {
 89  0
     super();
 90  0
     this.table = table;
 91  0
     this.fields = new Vector<CSVField>();
 92  0
   }
 93  
 
 94  
   /**
 95  
    * Add a field to this record.
 96  
    */
 97  
   public synchronized void addField(CSVField field) {
 98  0
     if (field.column.isPrimaryKey)
 99  0
       primaryKeyValue = field.value;
 100  0
     fields.addElement(field);
 101  0
   }
 102  
 
 103  
   /**
 104  
    * Write the data in this record into a new Persistent.
 105  
    * @throws CSVWriteDownException
 106  
    */
 107  
   private void createPersistent() 
 108  
       throws NoPrimaryKeyInCSVTableException, CSVWriteDownException {
 109  0
     if (poemRecord != null)
 110  0
       return;
 111  0
     Persistent newObj = table.newPersistent();
 112  
 
 113  0
     for(int j = 0; j < fields.size(); j++) {
 114  0
       CSVColumn col = ((CSVField)fields.elementAt(j)).column;
 115  0
       String csvValue = ((CSVField)fields.elementAt(j)).value;
 116  0
       if (col.foreignTable == null) {
 117  0
         if (col.poemName != null) {
 118  
           try {
 119  0
             if (csvValue != null && !csvValue.equals("")) {
 120  0
               newObj.setRawString(col.poemName, csvValue);
 121  
             }
 122  0
           } catch (Exception e) {
 123  0
             throw new CSVWriteDownException(table.getName(), getLineNo(), 
 124  
                 new RuntimeException("Problem processing column " + 
 125  
                 col.poemName + 
 126  
                 " of table " + 
 127  0
                 table.getName() +
 128  0
                 " on line " + getLineNo() + 
 129  
                 " value :" + csvValue + 
 130  0
                 ": " + e.toString()));
 131  0
           }
 132  
         }
 133  
       }
 134  
       // Lookup up value in the foreign Table
 135  
       else {
 136  0
         if (csvValue == null || csvValue.trim().equals("")) {
 137  0
           if (!newObj.getTable().getColumn(col.poemName).getColumnInfo().
 138  0
              getNullable().booleanValue()) {
 139  0
             throw new RuntimeException("CSV value missing for required column " + 
 140  
               col.poemName + 
 141  
               " of table " + 
 142  0
               table.getName() +
 143  0
               " on line " + getLineNo() 
 144  
               );
 145  
           }
 146  
         } else { 
 147  0
           Persistent lookup = col.foreignTable.getRecordWithID(csvValue);
 148  0
           if (lookup == null) {
 149  0
             throw new RuntimeException("No persistent found with primary key " + 
 150  
                 csvValue +
 151  
                 " for column " + 
 152  
                 col.poemName + 
 153  
                 " of table " + 
 154  0
                 table.getName() +
 155  0
                 " on line " + getLineNo() 
 156  
                 );
 157  
           }
 158  0
           newObj.setCooked(col.poemName, lookup);
 159  
         }
 160  
       }
 161  
     }
 162  0
     poemRecord = newObj;
 163  0
   }
 164  
   
 165  
  /**
 166  
   * Retreive the Persistent corresponding to this CSVRecord, if there
 167  
   * is one.
 168  
   * @return the existing or newly created Poem Persistent 
 169  
   * @throws NoPrimaryKeyInCSVTableException
 170  
   * @throws CSVWriteDownException
 171  
   */
 172  
   public Persistent getPersistent() 
 173  
       throws NoPrimaryKeyInCSVTableException, CSVWriteDownException {
 174  0
     if (poemRecord != null)
 175  0
       return poemRecord;
 176  0
     createPersistent();
 177  0
     return poemRecord;
 178  
   }
 179  
 
 180  
  /**
 181  
   * Make sure this record is written to the database.
 182  
   */
 183  
   void makePersistent() 
 184  
       throws NoPrimaryKeyInCSVTableException, CSVWriteDownException {
 185  
     try {
 186  0
       getPersistent();
 187  0
       poemRecord.makePersistent();
 188  0
     } catch (NoPrimaryKeyInCSVTableException e1) {
 189  0
       throw e1;
 190  0
     } catch (CSVWriteDownException e2) {
 191  0
       throw e2;
 192  0
     } catch (Exception e) {
 193  0
       e.printStackTrace(System.err);
 194  0
       throw new RuntimeException(e.toString()
 195  
                  + 
 196  
                 " in table " + 
 197  0
                 table.getName() +
 198  0
                 " on line " + getLineNo() 
 199  
                 );
 200  0
     }    
 201  
 
 202  0
     if (PoemThread.inSession())
 203  0
       PoemThread.writeDown();
 204  0
     PoemThread.commit();
 205  0
   }
 206  
 
 207  
  /**
 208  
   * @param recordNo The recordNo to set.
 209  
   */
 210  
   public void setRecordNo(int recordNo) {
 211  0
     this.recordNo = recordNo;
 212  0
   }
 213  
 
 214  
  /**
 215  
   * @return Returns the recordNo.
 216  
   */
 217  
   public int getRecordNo() {
 218  0
     return recordNo;
 219  
   }
 220  
 
 221  
  /**
 222  
   * @param lineNo The lineNo to set.
 223  
   */
 224  
   public void setLineNo(int lineNo) {
 225  0
    this.lineNo = lineNo;
 226  0
   }
 227  
 
 228  
   /**
 229  
    * @return Returns the lineNo.
 230  
    */
 231  
   public int getLineNo() {
 232  0
     return lineNo;
 233  
   }
 234  
 
 235  
   /**
 236  
    * @return the fields 
 237  
    */
 238  
   public Vector<CSVField> getFields() {
 239  0
     return fields;
 240  
   }
 241  
 
 242  
 }
 243  
 
 244