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 String primaryKeyValue = null;
72
73 /** The table this record wants to be written to */
74 Table<?> table = null;
75
76 /** The Poem Persistent corresponding to writing this record to the db */
77 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 super();
90 this.table = table;
91 this.fields = new Vector<CSVField>();
92 }
93
94 /**
95 * Add a field to this record.
96 */
97 public synchronized void addField(CSVField field) {
98 if (field.column.isPrimaryKey)
99 primaryKeyValue = field.value;
100 fields.addElement(field);
101 }
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 if (poemRecord != null)
110 return;
111 Persistent newObj = table.newPersistent();
112
113 for(int j = 0; j < fields.size(); j++) {
114 CSVColumn col = ((CSVField)fields.elementAt(j)).column;
115 String csvValue = ((CSVField)fields.elementAt(j)).value;
116 if (col.foreignTable == null) {
117 if (col.poemName != null) {
118 try {
119 if (csvValue != null && !csvValue.equals("")) {
120 newObj.setRawString(col.poemName, csvValue);
121 }
122 } catch (Exception e) {
123 throw new CSVWriteDownException(table.getName(), getLineNo(),
124 new RuntimeException("Problem processing column " +
125 col.poemName +
126 " of table " +
127 table.getName() +
128 " on line " + getLineNo() +
129 " value :" + csvValue +
130 ": " + e.toString()));
131 }
132 }
133 }
134 // Lookup up value in the foreign Table
135 else {
136 if (csvValue == null || csvValue.trim().equals("")) {
137 if (!newObj.getTable().getColumn(col.poemName).getColumnInfo().
138 getNullable().booleanValue()) {
139 throw new RuntimeException("CSV value missing for required column " +
140 col.poemName +
141 " of table " +
142 table.getName() +
143 " on line " + getLineNo()
144 );
145 }
146 } else {
147 Persistent lookup = col.foreignTable.getRecordWithID(csvValue);
148 if (lookup == null) {
149 throw new RuntimeException("No persistent found with primary key " +
150 csvValue +
151 " for column " +
152 col.poemName +
153 " of table " +
154 table.getName() +
155 " on line " + getLineNo()
156 );
157 }
158 newObj.setCooked(col.poemName, lookup);
159 }
160 }
161 }
162 poemRecord = newObj;
163 }
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 if (poemRecord != null)
175 return poemRecord;
176 createPersistent();
177 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 getPersistent();
187 poemRecord.makePersistent();
188 } catch (NoPrimaryKeyInCSVTableException e1) {
189 throw e1;
190 } catch (CSVWriteDownException e2) {
191 throw e2;
192 } catch (Exception e) {
193 e.printStackTrace(System.err);
194 throw new RuntimeException(e.toString()
195 +
196 " in table " +
197 table.getName() +
198 " on line " + getLineNo()
199 );
200 }
201
202 if (PoemThread.inSession())
203 PoemThread.writeDown();
204 PoemThread.commit();
205 }
206
207 /**
208 * @param recordNo The recordNo to set.
209 */
210 public void setRecordNo(int recordNo) {
211 this.recordNo = recordNo;
212 }
213
214 /**
215 * @return Returns the recordNo.
216 */
217 public int getRecordNo() {
218 return recordNo;
219 }
220
221 /**
222 * @param lineNo The lineNo to set.
223 */
224 public void setLineNo(int lineNo) {
225 this.lineNo = lineNo;
226 }
227
228 /**
229 * @return Returns the lineNo.
230 */
231 public int getLineNo() {
232 return lineNo;
233 }
234
235 /**
236 * @return the fields
237 */
238 public Vector<CSVField> getFields() {
239 return fields;
240 }
241
242 }
243
244