/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: ObserverRegistry.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.4 2007/02/06 08:35:34 schnelle
* Started automatic generation of wsdl description.
*
* Revision 1.3 2007/01/31 12:24:04 drmlipp
* Design revisited.
*
* Revision 1.2 2007/01/30 11:56:14 drmlipp
* Merged Wf-XML branch.
*
* Revision 1.1.2.1 2007/01/29 15:04:21 schnelle
* Renaming of Observer to ObserverRegistry and URIDecoder to ResourceReference.
*
* Revision 1.1.2.1 2007/01/16 11:05:42 schnelle
* Refactoring: Moved subscription handling methods to own class.
*
*/
package de.danet.an.workflow.clients.wfxml;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import javax.naming.NamingException;
import javax.sql.DataSource;
import de.danet.an.util.JDBCUtil;
import de.danet.an.util.UniversalPrepStmt;
/**
* This class encapsulates the database for the subscriptions.
*
* @author Dirk Schnelle
*/
class ObserverRegistry {
/** Database name */
private static final String DB_NAME = "java:comp/env/jdbc/WfEngine";
/** The datasource. */
private DataSource ds = null;
public ObserverRegistry() throws NamingException {
ds = JDBCUtil.refreshDS(null, DB_NAME);
}
/**
* Adds the observer for the specified process.
* @param observer the observer key
* @param packageId the package id of the process instance
* @param processId process id
* @param processKey key of the process instance
* @throws SQLException
* database error.
*/
public void subscribe(String observer, String packageId,
String processId, String processKey, String senderBase)
throws SQLException {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = ds.getConnection();
ps = new UniversalPrepStmt
(ds, con,
"SELECT COUNT(*) FROM WFXMLSUBSCRIPTIONS WHERE "
+ "OBSERVERKEY=? AND PACKAGEID=? AND PROCESSID=? "
+ "AND PROCESSKEY=?");
ps.setString(1, observer);
ps.setString(2, packageId);
ps.setString(3, processId);
ps.setString(4, processKey);
rs = ps.executeQuery();
rs.next();
int n = rs.getInt(1);
rs.close();
rs = null;
ps.close();
ps = null;
if (n==0) {
ps = new UniversalPrepStmt
(ds, con,
"INSERT INTO WFXMLSUBSCRIPTIONS "
+ "(OBSERVERKEY, PACKAGEID, PROCESSID, PROCESSKEY, "
+ "SENDERBASE) VALUES (?, ?, ?, ?, ?)");
ps.setString(1, observer);
ps.setString(2, packageId);
ps.setString(3, processId);
ps.setString(4, processKey);
ps.setString(5, senderBase);
ps.executeUpdate();
}
} finally {
JDBCUtil.closeAll(rs, ps, con);
}
}
/**
* Removes the observer for the specified process.
* @param observer the obeserver key
* @param packageId the package id of the process instance
* @param processId process id
* @param processKey key of the process instance
* @throws SQLException
* database error.
*/
public void unsubscribe(String observer, String packageId,
String processId, String processKey)
throws SQLException {
Connection con = null;
PreparedStatement ps = null;
try {
con = ds.getConnection();
ps = new UniversalPrepStmt
(ds, con,
"DELETE FROM WFXMLSUBSCRIPTIONS WHERE "
+ "OBSERVERKEY=? AND PACKAGEID=? AND PROCESSID=? "
+ "AND PROCESSKEY=?");
ps.setString(1, observer);
ps.setString(2, packageId);
ps.setString(3, processId);
ps.setString(4, processKey);
ps.execute();
} finally {
JDBCUtil.closeAll(null, ps, con);
}
}
/**
* Removes all observers for this process instance.
* @param packageId the package id
* @param processId the process id
* @param processKey the process key
* @return collection of all observers.
*/
public void unsubscribe(String packageId, String processId,
String processKey)
throws SQLException {
Connection con = null;
PreparedStatement ps = null;
try {
con = ds.getConnection();
ps = new UniversalPrepStmt
(ds, con,
"DELETE FROM WFXMLSUBSCRIPTIONS WHERE "
+ "PACKAGEID=? AND PROCESSID=? AND PROCESSKEY=?");
ps.setString(1, packageId);
ps.setString(2, processId);
ps.setString(3, processKey);
ps.executeUpdate();
} finally {
JDBCUtil.closeAll(null, ps, con);
}
}
/**
* Removes all notifications for this observer.
* @param observer the observer.
*/
public void unsubscribe(String observer) throws SQLException {
Connection con = null;
PreparedStatement ps = null;
try {
con = ds.getConnection();
ps = new UniversalPrepStmt
(ds, con,
"DELETE FROM WFXMLSUBSCRIPTIONS WHERE OBSERVERKEY=?");
ps.setString(1, observer);
ps.executeUpdate();
} finally {
JDBCUtil.closeAll(null, ps, con);
}
}
public static class ObserverInfo {
private String observerKey;
private String senderBase;
/**
* Create a new instance with all attributes initialized
* to defaults or the given values.
*
* @param observerKey
* @param senderBase
*/
public ObserverInfo(String observerKey, String senderBase) {
this.observerKey = observerKey;
this.senderBase = senderBase;
}
/**
* @return Returns the observerKey.
*/
public String getObserverKey() {
return observerKey;
}
/**
* @return Returns the senderBase.
*/
public String getSenderBase() {
return senderBase;
}
}
/**
* Retrieves a collection of all observers for this process instance.
* @param packageId the package id
* @param processId the process id
* @param processKey the process key
* @return collection of all observers.
*/
public Collection getObservers(String packageId, String processId,
String processKey) throws SQLException {
Collection observers = new java.util.ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = ds.getConnection();
ps = new UniversalPrepStmt
(ds, con,
"SELECT OBSERVERKEY, SENDERBASE "
+ "FROM WFXMLSUBSCRIPTIONS "
+ "WHERE PACKAGEID=? AND PROCESSID=? AND PROCESSKEY=?");
ps.setString(1, packageId);
ps.setString(2, processId);
ps.setString(3, processKey);
rs = ps.executeQuery();
while (rs.next()) {
observers.add
(new ObserverInfo(rs.getString(1), rs.getString(2)));
}
} finally {
JDBCUtil.closeAll(rs, ps, con);
}
return observers;
}
}