001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: ObserverRegistry.java,v 1.5 2007/03/27 21:59:42 mlipp Exp $
021: *
022: * $Log: ObserverRegistry.java,v $
023: * Revision 1.5 2007/03/27 21:59:42 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.4 2007/02/06 08:35:34 schnelle
027: * Started automatic generation of wsdl description.
028: *
029: * Revision 1.3 2007/01/31 12:24:04 drmlipp
030: * Design revisited.
031: *
032: * Revision 1.2 2007/01/30 11:56:14 drmlipp
033: * Merged Wf-XML branch.
034: *
035: * Revision 1.1.2.1 2007/01/29 15:04:21 schnelle
036: * Renaming of Observer to ObserverRegistry and URIDecoder to ResourceReference.
037: *
038: * Revision 1.1.2.1 2007/01/16 11:05:42 schnelle
039: * Refactoring: Moved subscription handling methods to own class.
040: *
041: */
042: package de.danet.an.workflow.clients.wfxml;
043:
044: import java.sql.Connection;
045: import java.sql.PreparedStatement;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.util.Collection;
049:
050: import javax.naming.NamingException;
051: import javax.sql.DataSource;
052:
053: import de.danet.an.util.JDBCUtil;
054: import de.danet.an.util.UniversalPrepStmt;
055:
056: /**
057: * This class encapsulates the database for the subscriptions.
058: *
059: * @author Dirk Schnelle
060: */
061: class ObserverRegistry {
062: /** Database name */
063: private static final String DB_NAME = "java:comp/env/jdbc/WfEngine";
064:
065: /** The datasource. */
066: private DataSource ds = null;
067:
068: public ObserverRegistry() throws NamingException {
069: ds = JDBCUtil.refreshDS(null, DB_NAME);
070: }
071:
072: /**
073: * Adds the observer for the specified process.
074: * @param observer the observer key
075: * @param packageId the package id of the process instance
076: * @param processId process id
077: * @param processKey key of the process instance
078: * @throws SQLException
079: * database error.
080: */
081: public void subscribe(String observer, String packageId,
082: String processId, String processKey, String senderBase)
083: throws SQLException {
084:
085: Connection con = null;
086: PreparedStatement ps = null;
087: ResultSet rs = null;
088: try {
089: con = ds.getConnection();
090: ps = new UniversalPrepStmt(
091: ds,
092: con,
093: "SELECT COUNT(*) FROM WFXMLSUBSCRIPTIONS WHERE "
094: + "OBSERVERKEY=? AND PACKAGEID=? AND PROCESSID=? "
095: + "AND PROCESSKEY=?");
096: ps.setString(1, observer);
097: ps.setString(2, packageId);
098: ps.setString(3, processId);
099: ps.setString(4, processKey);
100: rs = ps.executeQuery();
101:
102: rs.next();
103: int n = rs.getInt(1);
104: rs.close();
105: rs = null;
106: ps.close();
107: ps = null;
108:
109: if (n == 0) {
110: ps = new UniversalPrepStmt(
111: ds,
112: con,
113: "INSERT INTO WFXMLSUBSCRIPTIONS "
114: + "(OBSERVERKEY, PACKAGEID, PROCESSID, PROCESSKEY, "
115: + "SENDERBASE) VALUES (?, ?, ?, ?, ?)");
116: ps.setString(1, observer);
117: ps.setString(2, packageId);
118: ps.setString(3, processId);
119: ps.setString(4, processKey);
120: ps.setString(5, senderBase);
121: ps.executeUpdate();
122: }
123: } finally {
124: JDBCUtil.closeAll(rs, ps, con);
125: }
126:
127: }
128:
129: /**
130: * Removes the observer for the specified process.
131: * @param observer the obeserver key
132: * @param packageId the package id of the process instance
133: * @param processId process id
134: * @param processKey key of the process instance
135: * @throws SQLException
136: * database error.
137: */
138: public void unsubscribe(String observer, String packageId,
139: String processId, String processKey) throws SQLException {
140: Connection con = null;
141: PreparedStatement ps = null;
142: try {
143: con = ds.getConnection();
144: ps = new UniversalPrepStmt(
145: ds,
146: con,
147: "DELETE FROM WFXMLSUBSCRIPTIONS WHERE "
148: + "OBSERVERKEY=? AND PACKAGEID=? AND PROCESSID=? "
149: + "AND PROCESSKEY=?");
150: ps.setString(1, observer);
151: ps.setString(2, packageId);
152: ps.setString(3, processId);
153: ps.setString(4, processKey);
154: ps.execute();
155: } finally {
156: JDBCUtil.closeAll(null, ps, con);
157: }
158: }
159:
160: /**
161: * Removes all observers for this process instance.
162: * @param packageId the package id
163: * @param processId the process id
164: * @param processKey the process key
165: * @return collection of all observers.
166: */
167: public void unsubscribe(String packageId, String processId,
168: String processKey) throws SQLException {
169: Connection con = null;
170: PreparedStatement ps = null;
171: try {
172: con = ds.getConnection();
173: ps = new UniversalPrepStmt(
174: ds,
175: con,
176: "DELETE FROM WFXMLSUBSCRIPTIONS WHERE "
177: + "PACKAGEID=? AND PROCESSID=? AND PROCESSKEY=?");
178:
179: ps.setString(1, packageId);
180: ps.setString(2, processId);
181: ps.setString(3, processKey);
182: ps.executeUpdate();
183: } finally {
184: JDBCUtil.closeAll(null, ps, con);
185: }
186: }
187:
188: /**
189: * Removes all notifications for this observer.
190: * @param observer the observer.
191: */
192: public void unsubscribe(String observer) throws SQLException {
193: Connection con = null;
194: PreparedStatement ps = null;
195: try {
196: con = ds.getConnection();
197: ps = new UniversalPrepStmt(ds, con,
198: "DELETE FROM WFXMLSUBSCRIPTIONS WHERE OBSERVERKEY=?");
199: ps.setString(1, observer);
200: ps.executeUpdate();
201: } finally {
202: JDBCUtil.closeAll(null, ps, con);
203: }
204: }
205:
206: public static class ObserverInfo {
207: private String observerKey;
208: private String senderBase;
209:
210: /**
211: * Create a new instance with all attributes initialized
212: * to defaults or the given values.
213: *
214: * @param observerKey
215: * @param senderBase
216: */
217: public ObserverInfo(String observerKey, String senderBase) {
218: this .observerKey = observerKey;
219: this .senderBase = senderBase;
220: }
221:
222: /**
223: * @return Returns the observerKey.
224: */
225: public String getObserverKey() {
226: return observerKey;
227: }
228:
229: /**
230: * @return Returns the senderBase.
231: */
232: public String getSenderBase() {
233: return senderBase;
234: }
235:
236: }
237:
238: /**
239: * Retrieves a collection of all observers for this process instance.
240: * @param packageId the package id
241: * @param processId the process id
242: * @param processKey the process key
243: * @return collection of all observers.
244: */
245: public Collection getObservers(String packageId, String processId,
246: String processKey) throws SQLException {
247:
248: Collection observers = new java.util.ArrayList();
249: Connection con = null;
250: PreparedStatement ps = null;
251: ResultSet rs = null;
252: try {
253: con = ds.getConnection();
254: ps = new UniversalPrepStmt(
255: ds,
256: con,
257: "SELECT OBSERVERKEY, SENDERBASE "
258: + "FROM WFXMLSUBSCRIPTIONS "
259: + "WHERE PACKAGEID=? AND PROCESSID=? AND PROCESSKEY=?");
260: ps.setString(1, packageId);
261: ps.setString(2, processId);
262: ps.setString(3, processKey);
263: rs = ps.executeQuery();
264:
265: while (rs.next()) {
266: observers.add(new ObserverInfo(rs.getString(1), rs
267: .getString(2)));
268: }
269: } finally {
270: JDBCUtil.closeAll(rs, ps, con);
271: }
272:
273: return observers;
274: }
275: }
|