/*
* JFolder, Copyright 2001-2006 Gary Steinmetz
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jfolder.workflow.lifecycle;
//base classes
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
//project specific classes
import org.jfolder.common.UnexpectedSystemException;
import org.jfolder.common.tagging.ValueAndClassForConceptTag;
import org.jfolder.common.utils.misc.MiscHelper;
import org.jfolder.common.utils.xml.XMLHelper;
import org.jfolder.workflow.model.attributes.AttributeSet;
import org.jfolder.workflow.model.attributes.WaitHelper;
import org.jfolder.workflow.model.trigger.WorkflowTrigger;
import org.jfolder.workflow.query.BaseDBQueryVendor;
import org.jfolder.workflow.query.ColumnContainer;
import org.jfolder.workflow.query.QueryContainer;
import org.jfolder.workflow.query.ResultSetContainer;
import org.jfolder.workflow.query.StatementContainer;
import org.jfolder.workflow.query.SysDecIsGreaterThanOrEqualQC;
//other classes
/*
//TO DO: make calls transactional where appropriate
public class GenericMySQLWorkflowLifecycleBean
implements WorkflowLifecycle, SessionBean {
private SessionContext sessionContext = null;
public GenericMySQLWorkflowLifecycleBean() {
}
public void close() {
//not implemented, included in WorkflowLifecycle, not used
}
//TO DO: determine good function name and return type
//TO DO: is there a regular expressions library
//TO DO: determine if returning list of string ID's is okay
//TO DO: determine what should be returned in a query
//TO DO: determine what return will work with messaging and database
//TO DO: determine good function name and return type
//TO DO: determine if returning list of string ID's is okay
//TO DO: determine what should be returned in a query
//TO DO: determine what return will work with messaging and database
public ResultSetContainer queryWorkflows(StatementContainer inStatement) {
try {
ResultSetContainer outValue = null;
Connection con = getDatabaseConnection();
//OracleQueryVendorProprietarySyntax oqvps =
// new OracleQueryVendorProprietarySyntax();
MySQLQueryVendorProprietarySyntax hqvps =
new MySQLQueryVendorProprietarySyntax();
//BaseDBQueryVendor bdbqv = BaseDBQueryVendor.newBaseDBQueryVendor(
// oqvps, true);
BaseDBQueryVendor bdbqv = BaseDBQueryVendor.newBaseDBQueryVendor(
hqvps, true);
outValue = bdbqv.executeStatement(con, inStatement);
con.close();
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
public void startWorkflow(WorkflowPrecursor inWp) {
try {
Connection con = getDatabaseConnection();
AttributeSet as = inWp.getAttributeSet();
Trigger t = inWp.getTrigger();
RootScriptTagHolder rth = inWp.getRootScriptTagHolder();
History h = inWp.getHistory();
BigDecimal pfId = getNextIndex(con);
//this is the only attribute added in this function
as.addPublicSysAttr(AttributeSet.ID,
ValueAndClassFactory.newValueAndClass(pfId, BigDecimal.class));
insertWorkflowInstance(con, pfId,
WorkflowFactory.newWorkflow(inWp));
storeWorkflowAttributes(con, pfId, as, true);
con.close();
//send message to queue
QueueConnectionFactory queueFactory =
getWorkflowQueueConnectionFactory();
QueueConnection queueConnection =
queueFactory.createQueueConnection();
Queue queue = getWorkflowQueue();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender sender = queueSession.createSender(queue);
queueConnection.start();
ObjectMessage message = queueSession.createObjectMessage();
message.setObject(pfId);
sender.send(message);
queueConnection.close();
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
catch (JMSException jmse) {
throw new UnexpectedSystemException(jmse);
}
}
public void updateWorkflow(Workflow inWorkflow) {
try {
//MiscHelper.println("GenericMySQLWfLifecycleBean - updateWf");
AttributeSet as = inWorkflow.getAttributeSet();
ValueAndClass idVac = as.getPublicSysAttr(AttributeSet.ID);
ValueAndClass statusVac = as.getPublicSysAttr(AttributeSet.STATUS);
BigDecimal id = (BigDecimal)idVac.getValue();
String status = (String)statusVac.getValue();
String workflowText =
VersionHelper.createXMLRepresentationOfWorkflow(inWorkflow);
//MiscHelper.println("GenericMySQLWfLifecycleBean - workflowTx");
//MiscHelper.println(workflowText);
Connection con = getDatabaseConnection();
PreparedStatement ps = con.prepareStatement(
"update pf_workflows set wf_instance = ? where id = ?");
//ps.setCharacterStream(1, new StringReader(workflowText),
// workflowText.length());
ps.setString(1, new String(workflowText));
ps.setBigDecimal(2, id);
ps.execute();
ps.close();
storeWorkflowAttributes(con, id, as, false);
//con.commit();//TO DO: remove this
con.close();
if (status.equals(Trace.STATUS_ACTIVE)) {
QueueConnectionFactory queueFactory =
getWorkflowQueueConnectionFactory();
QueueConnection queueConnection =
queueFactory.createQueueConnection();
Queue queue = getWorkflowQueue();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender sender = queueSession.createSender(queue);
queueConnection.start();
ObjectMessage message = queueSession.createObjectMessage();
message.setObject(id);
sender.send(message);
queueConnection.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
catch (JMSException jmse) {
throw new UnexpectedSystemException(jmse);
}
////TO DO: should this function return anything?
////TO DO: in the end, is this necessary? consider SYS_STATUS
//Iterator attributeNames = as.getPublicAttrNames();
//
//HashMap properties = new HashMap();
//while (attributeNames.hasNext()) {
// String aName = (String)attributeNames.next();
// properties.put(aName, as.getPublicAttr(aName).getValue());
//}
//
////Workflow workflow = WorkflowFactory.newWorkflow(h, inRth, as, t);
//ServerFunctions sf =
// ServerFunctionsFactory.createNewServerFunctionsInstance();
//
//String wString =
// VersionHelper.createXMLRepresentationOfWorkflow(inWorkflow);
//sf.sendTextMessageToQueue("queue/WorkflowMDB", wString, properties);
}
public WorkflowScript[] getDeployedScripts() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
WorkflowScript outValue[] = null;
ConfigManager cm = ConfigManagerFactory.getConfigManager();
File pfDir =
GenericFileWorkflowApplicationSet.getJFolderDirectory(cm);
File scriptsDir = new File(pfDir, "scripts");
con = getDatabaseConnection();
ps = con.prepareStatement(
"select script_name from pf_deployed_scripts order by id");
rs = ps.executeQuery();
ArrayList scriptList = new ArrayList();
while (rs.next()) {
String nextScript = rs.getString(1);
File nextScriptFile = new File(scriptsDir, nextScript + ".xml");
if (nextScriptFile.exists()) {
scriptList.add(nextScriptFile);
}
}
outValue = new WorkflowScript[scriptList.size()];
for (int i = 0; i < scriptList.size(); i++) {
File nextFile = (File)scriptList.get(i);
String nextName =
MiscHelper.removeFileExtension(nextFile.getName());
outValue[i] = new GenericFileWorkflowScript(nextFile, nextName);
}
cm.close();
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
public void setDeployedScripts(WorkflowScript inWs[]) {
Connection con = null;
PreparedStatement ps = null;
try {
ConfigManager cm = ConfigManagerFactory.getConfigManager();
//delete existing deployed files
File pfDir =
GenericFileWorkflowApplicationSet.getJFolderDirectory(cm);
File scriptsDir = new File(pfDir, "scripts");
if (!scriptsDir.exists()) {
scriptsDir.mkdirs();
}
File deployedScripts[] = scriptsDir.listFiles();
for (int i = 0; i < deployedScripts.length; i++) {
if (deployedScripts[i].isFile()
&& deployedScripts[i].getName().endsWith(".xml")) {
deployedScripts[i].delete();
}
}
con = getDatabaseConnection();
ps = con.prepareStatement("delete from pf_deployed_scripts");
ps.execute();
ps.close();
//create new deployed files
for (int i = 0; i < inWs.length; i++) {
WorkflowScript nextScript = inWs[i];
File nextScriptFile = new File(scriptsDir,
nextScript.getName() + ".xml");
MiscHelper.writeTextFile(nextScriptFile,
nextScript.getContent());
ps = con.prepareStatement(
"insert into pf_deployed_scripts "
+ "(id, script_name) values (null,?)");
ps.setString(1, nextScript.getName());
ps.execute();
}
cm.close();
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
public WorkflowApplicationSet getWorkflowApplicationSet(UserHolder inUh) {
GenericFileWorkflowApplicationSet outValue =
new GenericFileWorkflowApplicationSet(
getApplicationsDirectory(inUh));
//MiscHelper.println(
// "getWorkflowApplicationSet::getApplicationsDirectory(inUh) = "
// + getApplicationsDirectory(inUh).getAbsolutePath());
File appList[] = getApplicationsDirectory(inUh).listFiles();
for (int i = 0; i < appList.length; i++) {
if (appList[i].isDirectory()) {
GenericFileWorkflowApplication nextWa =
new GenericFileWorkflowApplication(appList[i]);
outValue.registerApplication(nextWa);
}
}
outValue.load();
return outValue;
}
public void setWorkflowApplicationSet(WorkflowApplicationSet inWas,
UserHolder inUh) {
//reset deployed applications
File appList[] = getApplicationsDirectory(inUh).listFiles();
for (int i = 0; i < appList.length; i++) {
MiscHelper.deleteFileOrDirectory(appList[i]);
}
((GenericFileWorkflowApplicationSet)inWas).store();
}
protected final static File getApplicationsDirectory(UserHolder inUh) {
File outValue = null;
ConfigManager cm = ConfigManagerFactory.getConfigManager();
File pfDir =
GenericFileWorkflowApplicationSet.getJFolderDirectory(cm);
File usersDir = new File(pfDir, "users");
if (!usersDir.exists()) {
usersDir.mkdirs();
}
int id = getFileIdOfUser(inUh);
File userDir = new File(usersDir, id + "");
if (!userDir.exists()) {
userDir.mkdirs();
}
outValue = new File(userDir, "applications");
if (!outValue.exists()) {
outValue.mkdirs();
}
cm.close();
return outValue;
}
private final static int getFileIdOfUser(UserHolder inUh) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
PreparedStatement ps2 = null;
//ResultSet rs2 = null;
PreparedStatement ps3 = null;
ResultSet rs3 = null;
try {
int outValue = 0;
String name = inUh.getName();
String securityClass = inUh.getSecurityClass();
//check if id is already present
con = getDatabaseConnection();
ps = con.prepareStatement(
"select id from pf_users "
+ "where user_name = ? and security_class = ?");
ps.setString(1, name);
ps.setString(2, securityClass);
rs = ps.executeQuery();
//get id
if (rs.next()) {
outValue = rs.getInt(1);
}
else {
//insert user into
ps2 = con.prepareStatement("insert into pf_users "
+ "(id, user_name, security_class) values (null,?,?)");
ps2.setString(1, name);
ps2.setString(2, securityClass);
ps2.execute();
//get user id
ps3 = con.prepareStatement("SELECT LAST_INSERT_ID()");
rs3 = ps3.executeQuery();
//rs3 = ps2.executeQuery("SELECT LAST_INSERT_ID()");
while (rs3.next()) {
outValue = rs3.getInt(1);
}
}
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
//if (rs2 != null) {
// rs.close();
//}
if (ps2 != null) {
ps.close();
}
if (rs3 != null) {
rs.close();
}
if (ps3 != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
public Workflow retrieveWorkflow(Serializable inHandle) {
try {
Workflow outValue = null;
//MiscHelper.println("HANDLE = " + inHandle);
//MiscHelper.println("handle(type) = "
// + inHandle.getClass().getName());
BigDecimal id = null;
if (inHandle instanceof BigDecimal) {
id = (BigDecimal)inHandle;
}
else {
throw new UnexpectedSystemException(
"Handle is not a BigDecimal");
}
Connection con = getDatabaseConnection();
PreparedStatement ps = con.prepareStatement(
"select wf_instance from pf_workflows where id = ?");
ps.setBigDecimal(1, id);
ps.execute();
ResultSet rs = ps.getResultSet();
if (rs.next()) {
//Reader r = rs.getCharacterStream(1);
//StringBuffer sb = new StringBuffer();
//int nextChar = -1;
//while ((nextChar = r.read()) != -1) {
// sb.append(((char)nextChar));
//}
String r = rs.getString(1);
StringBuffer sb = new StringBuffer(r);
outValue = VersionHelper.createWorkflowFromXMLRepresentation(
sb.toString());
}
else {
throw new UnexpectedSystemException(
"No workflow-instance found with id '" + id + "'");
}
rs.close();
ps.close();
con.close();
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
//catch (IOException ioe) {
// throw new UnexpectedSystemException(ioe);
//}
}
//private helper methods
private final static Connection getDatabaseConnection() {
try {
Connection outValue = null;
Context context = new InitialContext();
Object dsObject = context.lookup(
"java:comp/env/jdbc/WorkflowLifecycle/DBStore");
DataSource ds = (DataSource)PortableRemoteObject.narrow(
dsObject, DataSource.class);
////TO DO: move to connection pool
//Class.forName("oracle.jdbc.driver.OracleDriver");
//outValue = DriverManager.getConnection(
// "jdbc:oracle:oci:powerfolder/powerfolder@");
outValue = ds.getConnection();
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
catch (NamingException ne) {
throw new UnexpectedSystemException(ne);
}
//catch (ClassNotFoundException cnfe) {
// throw new UnexpectedSystemException(cnfe);
//}
}
//private helper methods
private final static QueueConnectionFactory
getWorkflowQueueConnectionFactory() {
try {
QueueConnectionFactory outValue = null;
Context context = new InitialContext();
Object object = context.lookup(
"java:comp/env/jms/powerfolder/QueueConnectionFactory");
outValue = (QueueConnectionFactory)PortableRemoteObject.narrow(
object, QueueConnectionFactory.class);
return outValue;
}
catch (NamingException ne) {
throw new UnexpectedSystemException(ne);
}
}
//private helper methods
private final static Queue getWorkflowQueue() {
try {
Queue outValue = null;
Context context = new InitialContext();
Object object = context.lookup(
"java:comp/env/jms/powerfolder/WorkflowQueue");
outValue = (Queue)PortableRemoteObject.narrow(
object, Queue.class);
return outValue;
}
catch (NamingException ne) {
throw new UnexpectedSystemException(ne);
}
}
//private helper methods
private final static Queue getTriggerQueue() {
try {
Queue outValue = null;
Context context = new InitialContext();
Object object = context.lookup(
"java:comp/env/jms/powerfolder/TriggerQueue");
outValue = (Queue)PortableRemoteObject.narrow(
object, Queue.class);
return outValue;
}
catch (NamingException ne) {
throw new UnexpectedSystemException(ne);
}
}
private final static BigDecimal getNextIndex(Connection inCon) {
try {
BigDecimal outValue = null;
//CallableStatement cs =
// inCon.prepareCall("{? = call get_next_index}");
//cs.registerOutParameter(1, Types.NUMERIC);
//cs.executeUpdate();
//
//outValue = cs.getBigDecimal(1);
//
//cs.close();
Statement st = inCon.createStatement();
int resultRow = st.executeUpdate(
"INSERT INTO seqTable(id) VALUES (NULL)");
ResultSet rset = st.executeQuery("SELECT LAST_INSERT_ID()");
int rint = 0;
while (rset.next()) {
rint = rset.getInt(1);
}
outValue = new BigDecimal((double)rint);
st.close();
//MiscHelper.println("568: " + outValue);
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
private final static void insertWorkflowInstance(Connection inCon,
BigDecimal inPfId, Workflow inWorkflow) {
try {
//TO DO: update query variables
Document workflowDocument = XMLHelper.loadDocument(
VersionHelper.createXMLRepresentationOfWorkflow(inWorkflow));
StringWriter sw = new StringWriter();
XMLHelper.writeDocument(workflowDocument, sw);
String workflowXml = sw.toString();
StringReader sr = new StringReader(workflowXml);
PreparedStatement ps = inCon.prepareStatement(
"insert into pf_workflows (id, wf_instance) values (?,?)");
//MiscHelper.println("workflowXml.len() = " + workflowXml.length());
ps.setBigDecimal(1, inPfId);
//ps.setCharacterStream(2, sr, workflowXml.length());
ps.setString(2, workflowXml);
ps.execute();
ps.close();
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
catch (ParserConfigurationException pce) {
throw new UnexpectedSystemException(pce);
}
catch (SAXException saxe) {
throw new UnexpectedSystemException(saxe);
}
catch (IOException ioe) {
throw new UnexpectedSystemException(ioe);
}
}
private final static void storeWorkflowAttributes(Connection inCon,
BigDecimal inPfId, AttributeSet inAs, boolean inIsInsert) {
try {
Iterator iter = null;
//public system attributes
iter = inAs.getPublicSysAttrNames();
while (iter.hasNext()) {
String attrName = (String)iter.next();
ValueAndClass vac = inAs.getPublicSysAttr(attrName);
int pubAttr = BaseDBQueryVendor.PUBLIC;
int sysAttr = BaseDBQueryVendor.SYSTEM;
if (inIsInsert) {
insertWorkflowAttribute(inCon, inPfId, attrName, vac,
pubAttr, sysAttr);
}
else {
updateWorkflowAttribute(inCon, inPfId, attrName, vac,
sysAttr);
}
}
//private system attributes
iter = inAs.getPrivateSysAttrNames();
while (iter.hasNext()) {
String attrName = (String)iter.next();
ValueAndClass vac = inAs.getPrivateSysAttr(attrName);
int pubAttr = BaseDBQueryVendor.PRIVATE;
int sysAttr = BaseDBQueryVendor.SYSTEM;
if (inIsInsert) {
insertWorkflowAttribute(inCon, inPfId, attrName, vac,
pubAttr, sysAttr);
}
else {
updateWorkflowAttribute(inCon, inPfId, attrName, vac,
sysAttr);
}
}
//public application attributes
iter = inAs.getPublicAppAttrNames();
while (iter.hasNext()) {
String attrName = (String)iter.next();
ValueAndClass vac = inAs.getPublicAppAttr(attrName);
int pubAttr = BaseDBQueryVendor.PUBLIC;
int sysAttr = BaseDBQueryVendor.APPLICATION;
if (inIsInsert) {
insertWorkflowAttribute(inCon, inPfId, attrName, vac,
pubAttr, sysAttr);
}
else {
updateWorkflowAttribute(inCon, inPfId, attrName, vac,
sysAttr);
}
}
//private application attributes
iter = inAs.getPrivateAppAttrNames();
while (iter.hasNext()) {
String attrName = (String)iter.next();
ValueAndClass vac = inAs.getPrivateAppAttr(attrName);
int pubAttr = BaseDBQueryVendor.PRIVATE;
int sysAttr = BaseDBQueryVendor.APPLICATION;
if (inIsInsert) {
insertWorkflowAttribute(inCon, inPfId, attrName, vac,
pubAttr, sysAttr);
}
else {
updateWorkflowAttribute(inCon, inPfId, attrName, vac,
sysAttr);
}
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
private static void insertWorkflowAttribute(Connection inCon,
BigDecimal inPfId, String inAttrName, ValueAndClass inVac,
int inPubAttr, int inSysAttr) throws SQLException {
//TO DO: check for attribute type, view, set flag for class type
final String INSERT_QUERY =
"insert into pf_attributes ("
+ QueryConstructor.ID + ", "
+ QueryConstructor.PF_ID + ", "
+ QueryConstructor.ATTR_NAME + ", "
+ QueryConstructor.DECIMAL_VALUE + ", "
+ QueryConstructor.BOOLEAN_VALUE + ", "
+ QueryConstructor.STRING_VALUE + ", "
+ QueryConstructor.LONG_STRING_VALUE + ", "
+ BaseDBQueryVendor.ATTR_TYPE + ", "
+ BaseDBQueryVendor.ATTR_ACCESS + ", "
+ BaseDBQueryVendor.ATTR_CLASS
+ ") values (?,?,?,?,?,?,?,?,?,?)";
//+ ") values (pf_sequence.nextval,?,?,?,?,?,?,?,?,?)";
Object value = inVac.getValue();
Class valueClass = inVac.getValueClass();
PreparedStatement ps = null;
ps = inCon.prepareStatement(INSERT_QUERY);
BigDecimal inID = getNextIndex(inCon);
ps.setBigDecimal(1, inID);
ps.setBigDecimal(2, inPfId);
ps.setString(3, inAttrName.toUpperCase());
ps.setInt(8, inSysAttr);
ps.setInt(9, inPubAttr);
if (MiscHelper.isClassNumber(valueClass)) {
//set class
ps.setInt(10, BaseDBQueryVendor.DECIMAL);
//TO DO: check for null
if (value != null) {
ps.setBigDecimal(4,
MiscHelper.convertNumberToBigDecimal(value));
}
else {
ps.setNull(4, Types.NUMERIC);
}
ps.setNull(5, Types.NUMERIC);
ps.setNull(6, Types.VARCHAR);
ps.setNull(7, Types.CLOB);
}
else if (value.getClass().getName().equals(Boolean.class.getName())) {
//set class
ps.setInt(10, BaseDBQueryVendor.BOOLEAN);
//TO DO: check for null
Boolean bValue = (Boolean)value;
ps.setNull(4, Types.NUMERIC);
if (bValue == null) {
ps.setNull(5, Types.NUMERIC);
}
else if (bValue.booleanValue()) {
ps.setInt(5, 1);
}
else {
ps.setInt(5, 0);
}
ps.setNull(6, Types.VARCHAR);
ps.setNull(7, Types.CLOB);
}
else {
//set class
ps.setInt(10, BaseDBQueryVendor.STRING);
//TO DO: check for null
String sValue = null;
if (value != null) {
sValue = value.toString();
}
ps.setNull(4, Types.NUMERIC);
ps.setNull(5, Types.NUMERIC);
if (sValue == null) {
ps.setNull(6, Types.VARCHAR);
ps.setNull(7, Types.CLOB);
}
else if (sValue.length() <= 250) {
ps.setString(6, sValue);
ps.setNull(7, Types.CLOB);
}
else {
ps.setNull(6, Types.VARCHAR);
//ps.setCharacterStream(7, new StringReader(sValue),
// sValue.length());
ps.setString(7, new String(sValue));
}
}
ps.execute();
ps.close();
}
private static void updateWorkflowAttribute(Connection inCon,
BigDecimal inPfId, String inAttrName, ValueAndClass inVac,
int inSysAttr) throws SQLException {
//TO DO: check for attribute type, view, set flag for class type
final String UPDATE_BOOLEAN =
"update pf_attributes set "
+ QueryConstructor.BOOLEAN_VALUE
+ " = ? where "
+ QueryConstructor.PF_ID
+ " = ? and "
+ QueryConstructor.ATTR_NAME
+ " = ? and "
+ BaseDBQueryVendor.ATTR_TYPE
+ " = ?";
final String UPDATE_DECIMAL =
"update pf_attributes set "
+ QueryConstructor.DECIMAL_VALUE
+ " = ? where "
+ QueryConstructor.PF_ID
+ " = ? and "
+ QueryConstructor.ATTR_NAME
+ " = ? and "
+ BaseDBQueryVendor.ATTR_TYPE
+ " = ?";
final String UPDATE_STRING =
"update pf_attributes set "
+ QueryConstructor.STRING_VALUE
+ " = ?, "
+ QueryConstructor.LONG_STRING_VALUE
+ " = ? where "
+ QueryConstructor.PF_ID
+ " = ? and "
+ QueryConstructor.ATTR_NAME
+ " = ? and "
+ BaseDBQueryVendor.ATTR_TYPE
+ " = ?";
Object value = inVac.getValue();
Class valueClass = inVac.getValueClass();
PreparedStatement ps = null;
if (MiscHelper.isClassNumber(valueClass)) {
ps = inCon.prepareStatement(UPDATE_DECIMAL);
if (value != null) {
ps.setBigDecimal(1,
MiscHelper.convertNumberToBigDecimal(value));
}
else {
ps.setNull(1, Types.NUMERIC);
}
ps.setBigDecimal(2, inPfId);
ps.setString(3, inAttrName);
ps.setInt(4, inSysAttr);
}
else if (value.getClass().getName().equals(Boolean.class.getName())) {
ps = inCon.prepareStatement(UPDATE_BOOLEAN);
Boolean bValue = (Boolean)value;
if (bValue == null) {
ps.setNull(1, Types.NUMERIC);
}
else if (bValue.booleanValue()) {
ps.setInt(1, 1);
}
else {
ps.setInt(1, 0);
}
ps.setBigDecimal(2, inPfId);
ps.setString(3, inAttrName);
ps.setInt(4, inSysAttr);
}
else {
ps = inCon.prepareStatement(UPDATE_STRING);
String sValue = null;
if (value != null) {
sValue = value.toString();
}
if (sValue == null) {
ps.setNull(1, Types.VARCHAR);
//ps.setNull(2, Types.CLOB);
ps.setNull(2, Types.VARCHAR);
}
else if (sValue.length() <= 250) {
ps.setString(1, sValue);
//ps.setNull(2, Types.CLOB);
ps.setNull(2, Types.VARCHAR);
}
else {
ps.setNull(1, Types.VARCHAR);
//ps.setCharacterStream(2, new StringReader(sValue),
// sValue.length());
ps.setString(2, new String(sValue));
}
ps.setBigDecimal(3, inPfId);
ps.setString(4, inAttrName);
ps.setInt(5, inSysAttr);
}
ps.execute();
ps.close();
}
public void fireTrigger(String inTrigger) {
try {
QueueConnectionFactory queueFactory =
getWorkflowQueueConnectionFactory();
QueueConnection queueConnection =
queueFactory.createQueueConnection();
Queue queue = getTriggerQueue();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender sender = queueSession.createSender(queue);
queueConnection.start();
TextMessage message = queueSession.createTextMessage();
message.setText(inTrigger);
sender.send(message);
queueConnection.close();
}
catch (JMSException jmse) {
throw new UnexpectedSystemException(jmse);
}
}
//lifecycle methods
public void setSessionContext(SessionContext sc) {
this.sessionContext = sc;
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void ejbCreate() {
}
public void ejbRemove() {
}
}*/
|