01: package com.xoetrope.service.replication.hsqldb;
02:
03: import java.sql.Statement;
04: import net.xoetrope.optional.data.sql.ConnectionObject;
05: import net.xoetrope.optional.data.sql.NamedConnectionManager;
06: import org.hsqldb.Trigger;
07:
08: public class XRemoteUpdateTrigger implements Trigger {
09: private static String CONNECTION_NAME = "default";
10: protected NamedConnectionManager connMgr;
11:
12: public XRemoteUpdateTrigger() {
13: connMgr = (NamedConnectionManager) NamedConnectionManager
14: .getInstance();
15: }
16:
17: public void fire(int type, String trigName, String tableName,
18: Object[] oldRow, Object[] newRow) {
19: // the timestamp column (the last one) could have fired the trigger,
20: // if so ignore the trigger to avoid an infinite recursion.
21: boolean updated = false;
22: for (int i = 0; !updated && (i < oldRow.length - 1); i++) {
23: Object ov = oldRow[i];
24: Object nv = newRow[i];
25: updated = (ov == null ? nv != null : !ov.equals(nv));
26: }
27: if (!updated)
28: return;
29:
30: ConnectionObject connObj = null;
31:
32: try {
33: connObj = connMgr.getConnection(CONNECTION_NAME);
34: } catch (Exception ex) {
35: ex.printStackTrace();
36: throw new RuntimeException(
37: "Cannot establish a new connection.");
38: }
39:
40: try {
41: String sql = ("UPDATE XSYSSERVERTIMESTAMPS SET lastUpdate=CURRENT_TIMESTAMP "
42: + "WHERE tableName='" + tableName + "'");
43:
44: Statement stmt = connObj.createStatement();
45: stmt.executeUpdate(sql);
46: } catch (Exception ex) {
47: ex.printStackTrace();
48: throw new RuntimeException(
49: "Error while updating the XSYSSERVERTIMESTAMPS table.");
50: } finally {
51: if (connObj != null)
52: connObj.closeStatement();
53: }
54:
55: try {
56: if (oldRow.length < 3)
57: return;
58: Object pseudoId = oldRow[oldRow.length - 2];
59: String sql = ("UPDATE " + tableName + " SET " + tableName
60: + "_ts=CURRENT_TIMESTAMP " + "WHERE PseudoId=" + String
61: .valueOf(pseudoId));
62:
63: Statement stmt = connObj.createStatement();
64: stmt.executeUpdate(sql);
65: } catch (Exception ex) {
66: ex.printStackTrace();
67: throw new RuntimeException("Error while updating the "
68: + tableName + " table");
69: } finally {
70: if (connObj != null) {
71: connObj.closeStatement();
72: }
73: }
74:
75: }
76:
77: }
|