001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.scheduler;
034:
035: import java.sql.ResultSet;
036: import java.sql.ResultSetMetaData;
037: import java.sql.SQLException;
038: import java.sql.Connection;
039: import java.sql.Statement;
040: import java.sql.PreparedStatement;
041: import java.sql.Types;
042:
043: import java.util.HashMap;
044:
045: import com.knowgate.jdc.JDCConnection;
046: import com.knowgate.dataobjs.DB;
047: import com.knowgate.dataobjs.DBPersist;
048: import com.knowgate.debug.DebugFile;
049: import com.knowgate.misc.Gadgets;
050:
051: /**
052: * <p>Job Atom</p>
053: * Atoms hold single transaction units for Jobs.
054: * @author Sergio Montoro Ten
055: * @version 1.0
056: */
057: public class Atom extends DBPersist {
058:
059: /**
060: * <p>Load Atom from an open ResultSet</p>
061: * When loading an Atom standard alises are created for several database fields.<br>
062: * These aliases allow referencing database fields from document templates with a
063: * user friendly syntax.<br>
064: * When processing the Atom, all document references will be resolved to actual database
065: * values for corresponding fields.<br>
066: * <table border=1 cellpadding=4>
067: * <tr><td><b>Database Field</b></td><td><b>English Alias</b></td><td><b>Spanish Alias</b></td></tr>
068: * <tr><td>tx_name</td><td>Data.Name</td><td>Datos.Nombre</td></tr>
069: * <tr><td>tx_surname</td><td>Data.Surname</td><td>Datos.Apellidos</td></tr>
070: * <tr><td>tx_salutation</td><td>Data.Salutation</td><td>Datos.Saludo</td></tr>
071: * <tr><td>nm_commercial</td><td>Data.Legal_Name</td><td>Datos.Razon_Social</td></tr>
072: * <tr><td>tx_email</td><td>Address.EMail</td><td>Direccion.EMail</td></tr>
073: * <tr><td>tp_street</td><td>Address.Street_Type</td><td>Direccion.Tipo_Via</td></tr>
074: * <tr><td>nm_street</td><td>Address.Street_Name</td><td>Direccion.Nombre_Via</td></tr>
075: * <tr><td>nu_street</td><td>Address.Street_Num</td><td>Direccion.Numero_Via</td></tr>
076: * <tr><td>tx_addr1</td><td>Address.Line1</td><td>Direccion.Linea1</td></tr>
077: * <tr><td>tx_addr2</td><td>Address.Line2</td><td>Direccion.Linea2</td></tr>
078: * <tr><td>nm_country</td><td>Address.Country</td><td>Direccion.Pais</td></tr>
079: * <tr><td>nm_state</td><td>Address.State</td><td>Direccion.Provincia</td></tr>
080: * <tr><td>mn_city</td><td>Address.City</td><td>Direccion.Ciudad</td></tr>
081: * <tr><td>zipcode</td><td>Address.Zipcode</td><td>Direccion.Codigo_Postal</td></tr>
082: * <tr><td>fax_phone</td><td>Address.Fax_Phone</td><td>Direccion.Telf_Fax</td></tr>
083: * <tr><td>work_phone</td><td>Address.Proffesional_Phone</td><td>Direccion.Telf_Profesional</td></tr>
084: * </table>
085: * @param oRow Open ResultSet positioned at the row that must be loaded in this Atom
086: * @param oMetaData ResultSetMetaData
087: * @throws SQLException
088: */
089: public Atom(ResultSet oRow, ResultSetMetaData oMetaData)
090: throws SQLException {
091: super (DB.k_job_atoms, "Atom");
092:
093: int iCols = oMetaData.getColumnCount();
094: String sCol;
095: Object oCol;
096:
097: for (int c = 1; c <= iCols; c++) {
098:
099: sCol = oMetaData.getColumnName(c);
100: oCol = oRow.getObject(c);
101:
102: if (!oRow.wasNull()) {
103:
104: if (sCol.equalsIgnoreCase(DB.tx_name)) {
105:
106: put(DB.tx_name, oRow.getString(c));
107: put("Data.Name", oRow.getString(c));
108: put("Datos.Nombre", oRow.getString(c));
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: } else if (sCol.equalsIgnoreCase(DB.tx_salutation)) {
115:
116: put(DB.tx_salutation, oRow.getString(c));
117: put("Data.Salutation", oRow.getString(c));
118: put("Datos.Saludo", oRow.getString(c));
119: } else if (sCol.equalsIgnoreCase(DB.nm_commercial)) {
120:
121: put(DB.nm_commercial, oRow.getString(c));
122: put("Data.Legal_Name", oRow.getString(c));
123: put("Datos.Razon_Social", oRow.getString(c));
124: } else if (sCol.equalsIgnoreCase(DB.tx_email)) {
125:
126: put(DB.tx_email, oRow.getString(c));
127: put("Address.EMail", oRow.getString(c));
128: put("Direccion.EMail", oRow.getString(c));
129: } else if (sCol.equalsIgnoreCase(DB.tp_street)) {
130:
131: put(DB.tp_street, oRow.getString(c));
132: put("Address.Street_Type", oRow.getString(c));
133: put("Direccion.Tipo_Via", oRow.getString(c));
134: } else if (sCol.equalsIgnoreCase(DB.nm_street)) {
135:
136: put(DB.nm_street, oRow.getString(c));
137: put("Address.Street_Name", oRow.getString(c));
138: put("Direccion.Nombre_Via", oRow.getString(c));
139: } else if (sCol.equalsIgnoreCase(DB.nu_street)) {
140:
141: put(DB.nu_street, oRow.getString(c));
142: put("Address.Street_Num", oRow.getString(c));
143: put("Direccion.Numero_Via", oRow.getString(c));
144: } else if (sCol.equalsIgnoreCase(DB.tx_addr1)) {
145:
146: put(DB.tx_addr1, oRow.getString(c));
147: put("Address.Line1", oRow.getString(c));
148: put("Direccion.Linea1", oRow.getString(c));
149: } else if (sCol.equalsIgnoreCase(DB.tx_addr2)) {
150:
151: put(DB.tx_addr2, oRow.getString(c));
152: put("Address.Line2", oRow.getString(c));
153: put("Direccion.Linea2", oRow.getString(c));
154: } else if (sCol.equalsIgnoreCase(DB.nm_country)) {
155:
156: put(DB.nm_country, oRow.getString(c));
157: put("Address.Country", oRow.getString(c));
158: put("Direccion.Pais", oRow.getString(c));
159: } else if (sCol.equalsIgnoreCase(DB.nm_state)) {
160:
161: put(DB.nm_state, oRow.getString(c));
162: put("Address.State", oRow.getString(c));
163: put("Direccion.Provincia", oRow.getString(c));
164: } else if (sCol.equalsIgnoreCase(DB.mn_city)) {
165:
166: put(DB.mn_city, oRow.getString(c));
167: put("Address.City", oRow.getString(c));
168: put("Direccion.Ciudad", oRow.getString(c));
169: } else if (sCol.equalsIgnoreCase(DB.zipcode)) {
170:
171: put(DB.zipcode, oRow.getString(c));
172: put("Address.Zipcode", oRow.getString(c));
173: put("Direccion.Codigo_Postal", oRow.getString(c));
174: } else if (sCol.equalsIgnoreCase(DB.work_phone)) {
175:
176: put(DB.work_phone, oRow.getString(c));
177: put("Address.Proffesional_Phone", oRow.getString(c));
178: put("Direccion.Telf_Profesional", oRow.getString(c));
179: } else if (sCol.equalsIgnoreCase(DB.fax_phone)) {
180:
181: put(DB.fax_phone, oRow.getString(c));
182: put("Address.Fax_Phone", oRow.getString(c));
183: put("Direccion.Telf_Fax", oRow.getString(c));
184: } else if (sCol.equalsIgnoreCase(DB.mov_phone)) {
185:
186: put(DB.mov_phone, oRow.getString(c));
187: put("Address.Mobile_Phone", oRow.getString(c));
188: put("Direccion.Telf_Movil", oRow.getString(c));
189: } else if (sCol.equalsIgnoreCase(DB.tx_parameters))
190:
191: // Si el campo recibido se llama tx_parameters
192: // parsearlo para convertir en propiedades del objeto Atom
193: // los campos empotrados dentro del texto.
194: parseParameters(oRow.getString(c));
195:
196: else
197: put(oMetaData.getColumnName(c).toLowerCase(), oRow
198: .getObject(c));
199:
200: } // fi (wasNull())
201: } // next (c)
202: }
203:
204: // ----------------------------------------------------------
205:
206: private void parseParameters(String sTxParams) {
207: String aVariable[];
208: String aParams[] = Gadgets.split(sTxParams, ",");
209:
210: for (int p = 0; p < aParams.length; p++) {
211: aVariable = Gadgets.split(aParams[p], ":");
212: put(aVariable[0], aVariable[1]);
213: } // next (p)
214:
215: } // parseParameters
216:
217: // ----------------------------------------------------------
218:
219: /**
220: * <p>Move Atom from k_job_atoms table to k_job_atoms_archived</p>
221: * @param oConn Database Connection
222: * @throws SQLException
223: */
224: public void archive(JDCConnection oConn) throws SQLException {
225: final String COLUMNS_LIST = DB.gu_job + "," + DB.pg_atom + ","
226: + DB.dt_execution + "," + DB.id_status + ","
227: + DB.id_format + "," + DB.gu_company + ","
228: + DB.gu_contact + "," + DB.tx_email + "," + DB.tx_name
229: + "," + DB.tx_surname + "," + DB.tx_salutation + ","
230: + DB.nm_commercial + "," + DB.tp_street + ","
231: + DB.nm_street + "," + DB.nu_street + "," + DB.tx_addr1
232: + "," + DB.tx_addr2 + "," + DB.nm_country + ","
233: + DB.nm_state + "," + DB.mn_city + "," + DB.zipcode
234: + "," + DB.work_phone + "," + DB.direct_phone + ","
235: + DB.home_phone + "," + DB.mov_phone + ","
236: + DB.fax_phone + "," + DB.other_phone + "," + DB.po_box
237: + "," + DB.tx_log;
238: String sWhere, sSQL;
239: Statement oStmt;
240:
241: if (DebugFile.trace) {
242: DebugFile.writeln("Begin Atom.archive([Connection])");
243: DebugFile.incIdent();
244: }
245:
246: oStmt = oConn.createStatement();
247:
248: sWhere = " WHERE gu_job='" + getString(DB.gu_job)
249: + "' AND pg_atom=" + String.valueOf(getInt(DB.pg_atom));
250:
251: sSQL = "UPDATE " + DB.k_job_atoms + " SET " + DB.id_status
252: + "=" + String.valueOf(Atom.STATUS_FINISHED) + ","
253: + DB.tx_log + "=NULL " + sWhere;
254:
255: if (DebugFile.trace)
256: DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
257:
258: oStmt.executeUpdate(sSQL);
259:
260: sSQL = "INSERT INTO " + DB.k_job_atoms_archived + " ("
261: + COLUMNS_LIST + ") " + "SELECT " + COLUMNS_LIST
262: + " FROM " + DB.k_job_atoms + sWhere;
263:
264: if (DebugFile.trace)
265: DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
266:
267: oStmt.executeUpdate(sSQL);
268:
269: sSQL = "DELETE FROM " + DB.k_job_atoms + sWhere;
270:
271: if (DebugFile.trace)
272: DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
273:
274: oStmt.executeUpdate(sSQL);
275:
276: oStmt.close();
277:
278: if (DebugFile.trace) {
279: DebugFile.decIdent();
280: DebugFile.writeln("End Atom.archive()");
281: }
282:
283: } // archive
284:
285: // ----------------------------------------------------------
286:
287: /**
288: * Set atom status both in memory and at table k_job_atoms
289: * @param oConn JDCConnection
290: * @param iStatus short [STATUS_ABORTED | STATUS_FINISHED | STATUS_PENDING | STATUS_SUSPENDED | STATUS_RUNNING | STATUS_INTERRUPTED]
291: * @param sLog Text to be logged as the cause of status change
292: * @throws SQLException
293: * @throws NullPointerException
294: * @throws NumberFormatException
295: */
296: public void setStatus(JDCConnection oConn, short iStatus,
297: String sLog) throws SQLException, NullPointerException,
298: NumberFormatException {
299:
300: if (isNull(DB.gu_job))
301: throw new NullPointerException(
302: "Atom.setStatus() Job GUID not set");
303: if (isNull(DB.pg_atom))
304: throw new NullPointerException(
305: "Atom.setStatus() Atom ordinal not set");
306:
307: int iPgAtom = getInt(DB.pg_atom);
308:
309: PreparedStatement oStmt = oConn.prepareStatement("UPDATE "
310: + DB.k_job_atoms + " SET " + DB.id_status + "=?,"
311: + DB.tx_log + "=? WHERE " + DB.gu_job + "=? AND "
312: + DB.pg_atom + "=?");
313: oStmt.setShort(1, iStatus);
314: if (null == sLog)
315: oStmt.setNull(2, Types.VARCHAR);
316: else
317: oStmt.setString(2, Gadgets.left(sLog, 254));
318: oStmt.setString(3, getString(DB.gu_job));
319: oStmt.setInt(4, iPgAtom);
320: oStmt.executeUpdate();
321: oStmt.close();
322:
323: replace(DB.id_status, iStatus);
324: } // setStatus
325:
326: // ----------------------------------------------------------
327:
328: public static final short STATUS_ABORTED = Job.STATUS_ABORTED;
329: public static final short STATUS_FINISHED = Job.STATUS_FINISHED;
330: public static final short STATUS_PENDING = Job.STATUS_PENDING;
331: public static final short STATUS_SUSPENDED = Job.STATUS_SUSPENDED;
332: public static final short STATUS_RUNNING = Job.STATUS_RUNNING;
333: public static final short STATUS_INTERRUPTED = Job.STATUS_INTERRUPTED;
334:
335: } // Atom
|