KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > scheduler > Atom


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.scheduler;
34
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.ResultSetMetaData JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.Statement JavaDoc;
40 import java.sql.PreparedStatement JavaDoc;
41 import java.sql.Types JavaDoc;
42
43 import java.util.HashMap JavaDoc;
44
45 import com.knowgate.jdc.JDCConnection;
46 import com.knowgate.dataobjs.DB;
47 import com.knowgate.dataobjs.DBPersist;
48 import com.knowgate.debug.DebugFile;
49 import com.knowgate.misc.Gadgets;
50
51 /**
52  * <p>Job Atom</p>
53  * Atoms hold single transaction units for Jobs.
54  * @author Sergio Montoro Ten
55  * @version 1.0
56  */

57 public class Atom extends DBPersist {
58
59   /**
60    * <p>Load Atom from an open ResultSet</p>
61    * When loading an Atom standard alises are created for several database fields.<br>
62    * These aliases allow referencing database fields from document templates with a
63    * user friendly syntax.<br>
64    * When processing the Atom, all document references will be resolved to actual database
65    * values for corresponding fields.<br>
66    * <table border=1 cellpadding=4>
67    * <tr><td><b>Database Field</b></td><td><b>English Alias</b></td><td><b>Spanish Alias</b></td></tr>
68    * <tr><td>tx_name</td><td>Data.Name</td><td>Datos.Nombre</td></tr>
69    * <tr><td>tx_surname</td><td>Data.Surname</td><td>Datos.Apellidos</td></tr>
70    * <tr><td>tx_salutation</td><td>Data.Salutation</td><td>Datos.Saludo</td></tr>
71    * <tr><td>nm_commercial</td><td>Data.Legal_Name</td><td>Datos.Razon_Social</td></tr>
72    * <tr><td>tx_email</td><td>Address.EMail</td><td>Direccion.EMail</td></tr>
73    * <tr><td>tp_street</td><td>Address.Street_Type</td><td>Direccion.Tipo_Via</td></tr>
74    * <tr><td>nm_street</td><td>Address.Street_Name</td><td>Direccion.Nombre_Via</td></tr>
75    * <tr><td>nu_street</td><td>Address.Street_Num</td><td>Direccion.Numero_Via</td></tr>
76    * <tr><td>tx_addr1</td><td>Address.Line1</td><td>Direccion.Linea1</td></tr>
77    * <tr><td>tx_addr2</td><td>Address.Line2</td><td>Direccion.Linea2</td></tr>
78    * <tr><td>nm_country</td><td>Address.Country</td><td>Direccion.Pais</td></tr>
79    * <tr><td>nm_state</td><td>Address.State</td><td>Direccion.Provincia</td></tr>
80    * <tr><td>mn_city</td><td>Address.City</td><td>Direccion.Ciudad</td></tr>
81    * <tr><td>zipcode</td><td>Address.Zipcode</td><td>Direccion.Codigo_Postal</td></tr>
82    * <tr><td>fax_phone</td><td>Address.Fax_Phone</td><td>Direccion.Telf_Fax</td></tr>
83    * <tr><td>work_phone</td><td>Address.Proffesional_Phone</td><td>Direccion.Telf_Profesional</td></tr>
84    * </table>
85    * @param oRow Open ResultSet positioned at the row that must be loaded in this Atom
86    * @param oMetaData ResultSetMetaData
87    * @throws SQLException
88    */

89   public Atom(ResultSet JavaDoc oRow, ResultSetMetaData JavaDoc oMetaData) throws SQLException JavaDoc {
90     super(DB.k_job_atoms, "Atom");
91
92     int iCols = oMetaData.getColumnCount();
93     String JavaDoc sCol;
94     Object JavaDoc oCol;
95
96     for (int c=1; c<=iCols; c++) {
97
98       sCol = oMetaData.getColumnName(c);
99       oCol = oRow.getObject(c);
100
101       if (!oRow.wasNull()) {
102
103         if (sCol.equalsIgnoreCase(DB.tx_name)) {
104
105           put(DB.tx_name, oRow.getString(c));
106           put("Data.Name", oRow.getString(c));
107           put("Datos.Nombre", oRow.getString(c));
108         }
109         else if (sCol.equalsIgnoreCase(DB.tx_surname)) {
110
111           put(DB.tx_surname, oRow.getString(c));
112           put("Data.Surname", oRow.getString(c));
113           put("Datos.Apellidos", oRow.getString(c));
114         }
115         else if (sCol.equalsIgnoreCase(DB.tx_salutation)) {
116
117           put(DB.tx_salutation, oRow.getString(c));
118           put("Data.Salutation", oRow.getString(c));
119           put("Datos.Saludo", oRow.getString(c));
120         }
121         else if (sCol.equalsIgnoreCase(DB.nm_commercial)) {
122
123           put(DB.nm_commercial, oRow.getString(c));
124           put("Data.Legal_Name", oRow.getString(c));
125           put("Datos.Razon_Social", oRow.getString(c));
126         }
127         else if (sCol.equalsIgnoreCase(DB.tx_email)) {
128
129           put(DB.tx_email, oRow.getString(c));
130           put("Address.EMail", oRow.getString(c));
131           put("Direccion.EMail", oRow.getString(c));
132         }
133         else if (sCol.equalsIgnoreCase(DB.tp_street)) {
134
135           put(DB.tp_street, oRow.getString(c));
136           put("Address.Street_Type", oRow.getString(c));
137           put("Direccion.Tipo_Via", oRow.getString(c));
138         }
139         else if (sCol.equalsIgnoreCase(DB.nm_street)) {
140
141           put(DB.nm_street, oRow.getString(c));
142           put("Address.Street_Name", oRow.getString(c));
143           put("Direccion.Nombre_Via", oRow.getString(c));
144         }
145         else if (sCol.equalsIgnoreCase(DB.nu_street)) {
146
147           put(DB.nu_street, oRow.getString(c));
148           put("Address.Street_Num", oRow.getString(c));
149           put("Direccion.Numero_Via", oRow.getString(c));
150         }
151         else if (sCol.equalsIgnoreCase(DB.tx_addr1)) {
152
153           put(DB.tx_addr1, oRow.getString(c));
154           put("Address.Line1", oRow.getString(c));
155           put("Direccion.Linea1", oRow.getString(c));
156         }
157         else if (sCol.equalsIgnoreCase(DB.tx_addr2)) {
158
159           put(DB.tx_addr2, oRow.getString(c));
160           put("Address.Line2", oRow.getString(c));
161           put("Direccion.Linea2", oRow.getString(c));
162         }
163         else if (sCol.equalsIgnoreCase(DB.nm_country)) {
164
165           put(DB.nm_country, oRow.getString(c));
166           put("Address.Country", oRow.getString(c));
167           put("Direccion.Pais", oRow.getString(c));
168         }
169         else if (sCol.equalsIgnoreCase(DB.nm_state)) {
170
171           put(DB.nm_state, oRow.getString(c));
172           put("Address.State", oRow.getString(c));
173           put("Direccion.Provincia", oRow.getString(c));
174         }
175         else if (sCol.equalsIgnoreCase(DB.mn_city)) {
176
177           put(DB.mn_city, oRow.getString(c));
178           put("Address.City", oRow.getString(c));
179           put("Direccion.Ciudad", oRow.getString(c));
180         }
181         else if (sCol.equalsIgnoreCase(DB.zipcode)) {
182
183           put(DB.zipcode, oRow.getString(c));
184           put("Address.Zipcode", oRow.getString(c));
185           put("Direccion.Codigo_Postal", oRow.getString(c));
186         }
187         else if (sCol.equalsIgnoreCase(DB.work_phone)) {
188
189           put(DB.work_phone, oRow.getString(c));
190           put("Address.Proffesional_Phone", oRow.getString(c));
191           put("Direccion.Telf_Profesional", oRow.getString(c));
192         }
193         else if (sCol.equalsIgnoreCase(DB.fax_phone)) {
194
195           put(DB.fax_phone, oRow.getString(c));
196           put("Address.Fax_Phone", oRow.getString(c));
197           put("Direccion.Telf_Fax", oRow.getString(c));
198         }
199         else if (sCol.equalsIgnoreCase(DB.mov_phone)) {
200
201           put(DB.mov_phone, oRow.getString(c));
202           put("Address.Mobile_Phone", oRow.getString(c));
203           put("Direccion.Telf_Movil", oRow.getString(c));
204         }
205         else if (sCol.equalsIgnoreCase(DB.tx_parameters))
206
207           // Si el campo recibido se llama tx_parameters
208
// parsearlo para convertir en propiedades del objeto Atom
209
// los campos empotrados dentro del texto.
210
parseParameters(oRow.getString(c));
211
212         else
213           put(oMetaData.getColumnName(c).toLowerCase(), oRow.getObject(c));
214
215       } // fi (wasNull())
216
} // next (c)
217
}
218
219   // ----------------------------------------------------------
220

221   private void parseParameters(String JavaDoc sTxParams) {
222     String JavaDoc aVariable[];
223     String JavaDoc aParams[] = Gadgets.split(sTxParams, ",");
224
225     for (int p=0; p<aParams.length; p++) {
226       aVariable = Gadgets.split(aParams[p], ":");
227       put(aVariable[0], aVariable[1]);
228     } // next (p)
229

230   } // parseParameters
231

232   // ----------------------------------------------------------
233

234   /**
235    * <p>Move Atom from k_job_atoms table to k_job_atoms_archived</p>
236    * @param oConn Database Connection
237    * @throws SQLException
238    */

239   public void archive(JDCConnection oConn) throws SQLException JavaDoc {
240     final String JavaDoc COLUMNS_LIST = DB.gu_job + "," + DB.pg_atom + "," + DB.dt_execution + "," + DB.id_status + "," + DB.id_format + "," + DB.gu_company + "," + DB.gu_contact + "," + DB.tx_email + "," + DB.tx_name + "," + DB.tx_surname + "," + DB.tx_salutation + "," + DB.nm_commercial + "," + DB.tp_street + "," + DB.nm_street + "," + DB.nu_street + "," + DB.tx_addr1 + "," + DB.tx_addr2 + "," + DB.nm_country + "," + DB.nm_state + "," + DB.mn_city + "," + DB.zipcode + "," + DB.work_phone + "," + DB.direct_phone + "," + DB.home_phone + "," + DB.mov_phone + "," + DB.fax_phone + "," + DB.other_phone + "," + DB.po_box + "," + DB.tx_log;
241     String JavaDoc sWhere, sSQL;
242     Statement JavaDoc oStmt;
243
244     if (DebugFile.trace) {
245        DebugFile.writeln("Begin Atom.archive([Connection])");
246        DebugFile.incIdent();
247      }
248
249     oStmt = oConn.createStatement();
250
251     sWhere = " WHERE gu_job='" + getString(DB.gu_job) + "' AND pg_atom=" + String.valueOf(getInt(DB.pg_atom));
252
253     sSQL = "UPDATE " + DB.k_job_atoms + " SET " + DB.id_status + "=" + String.valueOf(Atom.STATUS_FINISHED) + "," + DB.tx_log + "=NULL " + sWhere;
254
255     if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
256
257     oStmt.executeUpdate(sSQL);
258
259     sSQL = "INSERT INTO " + DB.k_job_atoms_archived + " (" + COLUMNS_LIST + ") " +
260            "SELECT " + COLUMNS_LIST + " FROM " + DB.k_job_atoms + sWhere;
261
262     if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
263
264     oStmt.executeUpdate(sSQL);
265
266     sSQL = "DELETE FROM " + DB.k_job_atoms + sWhere;
267
268     if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
269
270     oStmt.executeUpdate(sSQL);
271
272     oStmt.close();
273
274     if (DebugFile.trace) {
275        DebugFile.decIdent();
276        DebugFile.writeln("End Atom.archive()");
277      }
278
279   } // archive
280

281   // ----------------------------------------------------------
282

283   /**
284    * Set atom status both in memory and at table k_job_atoms
285    * @param oConn JDCConnection
286    * @param iStatus short [STATUS_ABORTED | STATUS_FINISHED | STATUS_PENDING | STATUS_SUSPENDED | STATUS_RUNNING | STATUS_INTERRUPTED]
287    * @param sLog Text to be logged as the cause of status change
288    * @throws SQLException
289    * @throws NullPointerException
290    * @throws NumberFormatException
291    */

292   public void setStatus (JDCConnection oConn, short iStatus, String JavaDoc sLog)
293     throws SQLException JavaDoc, NullPointerException JavaDoc,NumberFormatException JavaDoc {
294
295     if (isNull(DB.gu_job))
296       throw new NullPointerException JavaDoc("Atom.setStatus() Job GUID not set");
297     if (isNull(DB.pg_atom))
298       throw new NullPointerException JavaDoc("Atom.setStatus() Atom ordinal not set");
299
300     int iPgAtom = getInt(DB.pg_atom);
301
302     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("UPDATE "+DB.k_job_atoms+" SET "+DB.id_status+"=?,"+DB.tx_log+"=? WHERE "+DB.gu_job+"=? AND "+DB.pg_atom+"=?");
303     oStmt.setShort(1, iStatus);
304     if (null==sLog)
305       oStmt.setNull(2, Types.VARCHAR);
306     else
307       oStmt.setString(2, Gadgets.left(sLog,254));
308     oStmt.setString(3, getString(DB.gu_job));
309     oStmt.setInt(4, iPgAtom);
310     oStmt.executeUpdate();
311     oStmt.close();
312
313     replace(DB.id_status, iStatus);
314   } // setStatus
315

316   // ----------------------------------------------------------
317

318   public static final short STATUS_ABORTED = Job.STATUS_ABORTED;
319   public static final short STATUS_FINISHED = Job.STATUS_FINISHED;
320   public static final short STATUS_PENDING = Job.STATUS_PENDING;
321   public static final short STATUS_SUSPENDED = Job.STATUS_SUSPENDED;
322   public static final short STATUS_RUNNING = Job.STATUS_RUNNING;
323   public static final short STATUS_INTERRUPTED = Job.STATUS_INTERRUPTED;
324
325 } // Atom
326
Popular Tags