| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 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 | |
|
| 65 | |
|
| 66 | |
public class CSVRecord { |
| 67 | |
|
| 68 | |
private Vector<CSVField> fields; |
| 69 | |
|
| 70 | |
|
| 71 | 0 | String primaryKeyValue = null; |
| 72 | |
|
| 73 | |
|
| 74 | 0 | Table<?> table = null; |
| 75 | |
|
| 76 | |
|
| 77 | 0 | Persistent poemRecord = null; |
| 78 | |
|
| 79 | |
|
| 80 | |
private int lineNo; |
| 81 | |
|
| 82 | |
|
| 83 | |
private int recordNo; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 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 | |
|
| 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 | |
|
| 105 | |
|
| 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 | |
|
| 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 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 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 | |
|
| 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 | |
|
| 209 | |
|
| 210 | |
public void setRecordNo(int recordNo) { |
| 211 | 0 | this.recordNo = recordNo; |
| 212 | 0 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
public int getRecordNo() { |
| 218 | 0 | return recordNo; |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public void setLineNo(int lineNo) { |
| 225 | 0 | this.lineNo = lineNo; |
| 226 | 0 | } |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public int getLineNo() { |
| 232 | 0 | return lineNo; |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public Vector<CSVField> getFields() { |
| 239 | 0 | return fields; |
| 240 | |
} |
| 241 | |
|
| 242 | |
} |
| 243 | |
|
| 244 | |
|