/*
* 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.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
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.project.model.ProjectScript;
import org.jfolder.security.audit.SecurityAudit;
import org.jfolder.security.lifecycle.SecurityLifecycle;
import org.jfolder.security.lifecycle.SecurityLifecycleHelper;
import org.jfolder.security.model.UserHolder;
import org.jfolder.security.model.UserHolderContext;
import org.jfolder.services.config.ConfigServiceCaller;
import org.jfolder.services.config.ConfigServiceCallerFactory;
import org.jfolder.workflow.lifecycle.stores.base.WorkflowDatabase;
import org.jfolder.workflow.lifecycle.stores.base.WorkflowStore;
import org.jfolder.workflow.lifecycle.stores.base.WorkflowStoreFactory;
import org.jfolder.workflow.model.dms.WorkflowDocumentSetUpdates;
import org.jfolder.workflow.model.instance.WorkflowInstance;
import org.jfolder.workflow.model.instance.WorkflowInstanceUpdates;
import org.jfolder.workflow.model.trigger.WorkflowTrigger;
import org.jfolder.workflow.model.trigger.WorkflowTriggerUpdates;
import org.jfolder.workflow.query.BaseDBQueryVendor;
import org.jfolder.workflow.query.BaseDBQueryVendorProprietarySyntax;
//other classes
/*
//TO DO: make calls transactional where appropriate
public class GenericHSQLWorkflowLifecycleBean
extends BaseDBWorkflowLifecycleBean {
public GenericHSQLWorkflowLifecycleBean() {
}
protected BaseDBQueryVendorProprietarySyntax
getBaseDBQueryVendorProprietarySyntax() {
return new HSQLQueryVendorProprietarySyntax();
}
public String updateWorkflowInstance(WorkflowInstanceUpdates inWiu) {
Connection con = null;
try {
String outValue = null;
con = getDatabaseConnection();
boolean notNew = (inWiu.getId() != null);
BigDecimal workflowInstanceId = null;
if (!notNew) {
workflowInstanceId = getNextIndex(con);
}
else {
workflowInstanceId = new BigDecimal(inWiu.getId());
}
//
//SECURITY AUDIT
//
SecurityAudit sa = inWiu.getSecurityAudit();
BigDecimal saId = processSecurityAudit(sa, con);
MiscHelper.println("saId = " + saId);
//
//DOCUMENT GROUP SET
//
WorkflowDocumentSetUpdates wdsu =
inWiu.getWorkflowDocumentSetUpdates();
BigDecimal wdsuId = processDocumentSet(
notNew, wdsu, workflowInstanceId, con);
//
//TRIGGER
//
WorkflowTriggerUpdates wtu = inWiu.getWorkflowTriggerUpdates();
BigDecimal wtuId = processTrigger(
notNew, wtu, workflowInstanceId, con);
//
//
//INSTANCE
//
processWorkflowInstance(notNew, workflowInstanceId, con);
//UserHolder user = sa.getAuditUser();
//MiscHelper.println("user.getName() = " + user.getName() );
//MiscHelper.println("user.getSc() = " + user.getSecurityClass());
//MiscHelper.println("sa.getId() = " + sa.getAuditId());
//MiscHelper.println("sa.getComment() = " + sa.getAuditComment());
//MiscHelper.println("sa.getStatus() = " + sa.getAuditStatus());
//MiscHelper.println("sa.getTime() = " + sa.getAuditTimestamp());
//if (sa.isAuditExceptionPresent()) {
// MiscHelper.println("sa.getAuditExceptionMessage() = "
// + sa.getAuditExceptionMessage());
// MiscHelper.println("sa.getAuditExceptionSource() = "
// + sa.getAuditExceptionSource());
//}
//
//for (int i = 0; i < wtu.getPropertyUpdateCount(); i++) {
// MiscHelper.println("propCommand = "
// + wtu.getPropertyCommand(i));
// MiscHelper.println("propName = " + wtu.getPropertyName(i));
// MiscHelper.println("propValue = " + wtu.getPropertyValue(i));
//}
//
outValue = workflowInstanceId.toString();
return outValue;
}
//catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
//}
finally {
closeDbResources(con, null, null);
}
}
private BigDecimal processDocumentSet(boolean inNotNew,
WorkflowDocumentSetUpdates inWdsu, BigDecimal inWorkflowInstanceId,
Connection inCon) {
PreparedStatement ps = null;
try {
BigDecimal outValue = null;
if (!inNotNew) {
outValue = getNextIndex(inCon);
ps = inCon.prepareStatement(
"insert into T_JF_DOCUMENT_SETS"
+ " (id_pk, workflow_id_fk)"
+ " values (?, ?)");
ps.setBigDecimal(1, outValue);
ps.setBigDecimal(2, inWorkflowInstanceId);
}
else {
//ps = inCon.prepareStatement(
// "update T_JF_DOCUMENT_SETS"
// + " set workflow_template_id_fk = ?,"
// + " document_set_id_fk = ?,"
// + " trigger_data_id_fk = ?"
// + " where id_pk = ?");
//ps.setBigDecimal(1, new BigDecimal(1));
//ps.setBigDecimal(2, new BigDecimal(1));
//ps.setBigDecimal(3, new BigDecimal(1));
//ps.setBigDecimal(4, inWorkflowInstanceId);
//do nothing
}
if (ps != null) {
int updateCount = ps.executeUpdate();
if (updateCount < 1) {
throw new UnexpectedSystemException("Old Copy");
}
else if (updateCount > 1) {
throw new UnexpectedSystemException("Inconsistent State");
}
else {
}
}
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
closeDbResources(null, ps, null);
}
}
private BigDecimal processTrigger(boolean inNotNew,
WorkflowTriggerUpdates inWdsu, BigDecimal inWorkflowInstanceId,
Connection inConnection) {
BigDecimal outValue = null;
return outValue;
}
private void processWorkflowInstance(boolean inNotNew,
BigDecimal inWorkflowInstanceId, Connection inCon) {
PreparedStatement ps = null;
try {
if (!inNotNew) {
ps = inCon.prepareStatement(
"insert into T_JF_WORKFLOW_INSTANCES"
+ " (id_pk, workflow_template_id_fk, document_set_id_fk,"
+ " trigger_data_id_fk)"
+ " values (?, ?, ?, ?)");
ps.setBigDecimal(1, inWorkflowInstanceId);
ps.setBigDecimal(2, new BigDecimal(1));
ps.setBigDecimal(3, new BigDecimal(1));
ps.setBigDecimal(4, new BigDecimal(1));
}
else {
ps = inCon.prepareStatement(
"update T_JF_WORKFLOW_INSTANCES"
+ " set workflow_template_id_fk = ?,"
+ " document_set_id_fk = ?,"
+ " trigger_data_id_fk = ?"
+ " where id_pk = ?");
ps.setBigDecimal(1, new BigDecimal(1));
ps.setBigDecimal(2, new BigDecimal(1));
ps.setBigDecimal(3, new BigDecimal(1));
ps.setBigDecimal(4, inWorkflowInstanceId);
}
int updateCount = ps.executeUpdate();
if (updateCount < 1) {
throw new UnexpectedSystemException("Old Copy");
}
else if (updateCount > 1) {
throw new UnexpectedSystemException("Inconsistent State");
}
else {
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
closeDbResources(null, ps, null);
}
}
private BigDecimal processSecurityAudit(SecurityAudit inSa,
Connection inCon) {
PreparedStatement ps = null;
try {
BigDecimal outValue = null;
outValue = getNextIndex(inCon);
UserHolder uh = inSa.getAuditUser();
ps = inCon.prepareStatement("insert into T_JF_SECURITY_AUDITS "
+ "(id_pk, audit_version, audit_user_name, "
+ "audit_user_security_class, audit_id, audit_status, "
+ "audit_comment, audit_timestamp, "
+ "audit_exception_message, audit_exception_source) "
+ "values (?,?,?,?,?,?,?,?,?,?)");
ps.setBigDecimal(1, outValue);
//TO DO: change below
ps.setInt(2, 1);
ps.setString(3, uh.getName());
ps.setString(4, uh.getSecurityClass());
ps.setInt(5, inSa.getAuditId());
ps.setString(6, inSa.getAuditStatus());
ps.setString(7, inSa.getAuditComment());
ps.setLong(8, inSa.getAuditTimestamp());
ps.setString(9, inSa.getAuditExceptionMessage());
ps.setString(10, inSa.getAuditExceptionSource());
int updateCount = ps.executeUpdate();
//MiscHelper.println("update Count = " + updateCount);
//con.commit();
//MiscHelper.println("autoCommit = " + con.getAutoCommit());
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
closeDbResources(null, ps, null);
}
}
private final static void closeDbResources(Connection inCon,
Statement inStatement, ResultSet inRs) {
StringBuffer sb = new StringBuffer();
try {
if (inRs != null) {
inRs.close();
}
}
catch (SQLException sqle) {
sb.append("|" + sqle.getMessage() + "|");
}
try {
if (inStatement != null) {
inStatement.close();
}
}
catch (SQLException sqle) {
sb.append("|" + sqle.getMessage() + "|");
}
try {
if (inCon != null) {
inCon.close();
}
}
catch (SQLException sqle) {
sb.append("|" + sqle.getMessage() + "|");
}
if (sb.length() > 0) {
throw new UnexpectedSystemException(sb.toString());
}
}
public WorkflowInstance retrieveWorkflowInstance(String inId) {
throw UnexpectedSystemException.notImplemented();
}
public String submitTriggerData(String inHandle, WorkflowTrigger inTd) {
Connection con = null;
PreparedStatement ps = null;
try {
String outValue = inHandle;
con = getDatabaseConnection();
if (outValue == null) {
outValue = getNextIndex(con).toString();
}
ps = con.prepareStatement("insert into " + T_TRIGGERS + " (" + ID
+ ", " + WORKFLOW_ID + ", " + HANDLE + ", " + CONTENT + ", "
+ AUDIT_ID + ", " + AUDIT_USER_ID + ", " + AUDIT_TIMESTAMP
+ ", " + AUDIT_STATUS + ", " + AUDIT_COMMENT + ", "
+ AUDIT_EXCEPTION_MESSAGE + ", " + AUDIT_EXCEPTION_SOURCE
+ ") values (null,?,?,?,?,?,?,?,?,?,?)");
//ps.setString(1, inTd.getWorkflowId());//TO DO: correct
ps.setString(2, outValue);
ps.setString(3, inTd.getDataAsString());
//ps.setInt(4, inTd.getAuditId());
//ps.setInt(5, getFileIdOfUser(inTd.getAuditUser()));
//ps.setLong(6, inTd.getAuditTimestamp());
//ps.setString(7, inTd.getAuditStatus());
//ps.setString(8, inTd.getAuditComment());
//ps.setString(9, inTd.getAuditExceptionMessage());
//ps.setString(10, inTd.getAuditExceptionSource());
ps.execute();
putTextMessageOnQueue(outValue, null, getTriggerQueue());
return outValue;
}
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 void updateTriggerDataStatus(String inHandle, WorkflowTrigger inTd) {
Connection con = null;
PreparedStatement ps = null;
try {
con = getDatabaseConnection();
String update = "update " + T_TRIGGERS;
String set = " set " + WORKFLOW_ID + " = ?, "
+ AUDIT_STATUS + " = ?,"
+ AUDIT_EXCEPTION_MESSAGE + " = ?,"
+ AUDIT_EXCEPTION_SOURCE + " = ?";
String where = " where " + HANDLE + " = ? and " + AUDIT_ID + " = ?";
String query = update + set + where;
ps = con.prepareStatement(query);
//
//ps.setString(1, inTd.getWorkflowId());//TO DO: correct
//ps.setString(2, inTd.getAuditStatus());
//ps.setString(3, inTd.getAuditExceptionMessage());
//ps.setString(4, inTd.getAuditExceptionSource());
//
ps.setString(5, inHandle);
//ps.setInt(6, inTd.getAuditId());
//
ps.execute();
}
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 WorkflowTrigger retrieveTriggerData(String inHandle) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
WorkflowTrigger outValue = null;
con = getDatabaseConnection();
String select = ("select " + WORKFLOW_ID + ", " + CONTENT + ", "
+ AUDIT_ID + ", " + AUDIT_TIMESTAMP + ", " + AUDIT_STATUS + ", "
+ AUDIT_COMMENT + ", " + AUDIT_EXCEPTION_MESSAGE + ", "
+ AUDIT_EXCEPTION_SOURCE + ", " + USER_NAME + ", "
+ SECURITY_CLASS);
String from = (" from " + T_TRIGGERS + ", " + T_USERS);
String where = (" where " + (T_TRIGGERS + "." + HANDLE)
+ " = ? and " + (T_TRIGGERS + "." + AUDIT_USER_ID) + " = "
+ (T_USERS + "." + ID));
String orderBy = (" order by " + AUDIT_ID + " desc");
String query = select + from + where + orderBy;
//MiscHelper.println("hsql.retrieveTriggerData.query - " + query);
ps = con.prepareStatement(query);
ps.setString(1, inHandle);
rs = ps.executeQuery();
if (rs.next()) {
String workflowId = rs.getString(1);
String content = rs.getString(2);
int auditId = rs.getInt(3);
long auditTimestamp = rs.getLong(4);
String auditStatus = rs.getString(5);
String auditComment = rs.getString(6);
String auditExceptionMessage = rs.getString(7);
String auditExceptionSource = rs.getString(8);
String auditUserName = rs.getString(9);
String auditSecurityClass = rs.getString(10);
UserHolderContext uhc =
SecurityLifecycleHelper.getUserHolderContext(
auditUserName, auditSecurityClass);
SecurityLifecycle sl =
SecurityLifecycleHelper.getSecurityLifecycle();
UserHolder uh = sl.getUser(uhc);
SecurityAudit wa = SecurityAudit.newInstance(
uh, auditId, auditComment, auditStatus, auditTimestamp);
//TO DO: change below
if (auditExceptionMessage != null
|| auditExceptionSource != null) {
wa.setAuditException(auditExceptionMessage,
auditExceptionSource);
}
outValue = WorkflowTrigger.newInstance();
}
else {
throw new UnexpectedSystemException(
"No trigger for handle - '" + inHandle + "'");
}
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 ProjectScript[] getDeployedScripts() {
MiscHelper.println("<!--- GenHSQLWfLcBean getDeployedScripts DB --->");
WorkflowStore ws = WorkflowStoreFactory.getWorkflowStore();
WorkflowDatabase wd = WorkflowDatabase.newInstance();
Connection sampleConn = ws.getConnection();
try {
wd.dropAllEntities(sampleConn, ws);
wd.createAllEntities(sampleConn, ws);
}
catch (Exception e) {
MiscHelper.println(e.getMessage());
e.printStackTrace();
}
finally {
ws.closeSQLObjects(sampleConn, null, null);
}
MiscHelper.println("<!--------------------------------------->");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ProjectScript outValue[] = null;
ConfigServiceCaller csc =
ConfigServiceCallerFactory.getConfigServiceCaller();
//ConfigLifecycle cm = ConfigLifecycleFactory.getConfigLifecycle();
File scriptsDir = getScriptsDir();
con = getDatabaseConnection();
ps = con.prepareStatement("select " + SCRIPT_NAME + " from "
+ T_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 ProjectScript[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 GenericFileProjectScript(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(ProjectScript inWs[]) {
Connection con = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
try {
ConfigServiceCaller csc =
ConfigServiceCallerFactory.getConfigServiceCaller();
//ConfigLifecycle cm = ConfigLifecycleFactory.getConfigLifecycle();
//delete existing deployed files
File scriptsDir = getScriptsDir();
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 " + T_DEPLOYED_SCRIPTS);
ps.execute();
//create new deployed files
for (int i = 0; i < inWs.length; i++) {
ProjectScript nextScript = inWs[i];
File nextScriptFile = new File(scriptsDir,
nextScript.getName() + ".xml");
MiscHelper.writeTextFile(nextScriptFile,
nextScript.getContent());
ps2 = con.prepareStatement("insert into " + T_DEPLOYED_SCRIPTS
+ " (" + ID + ", " + SCRIPT_NAME + ") values (null,?)");
ps2.setString(1, nextScript.getName());
ps2.execute();
//TO DO: fix below, use array
ps2.close();
ps2 = null;
}
//cm.close();
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
try {
if (ps != null) {
ps.close();
}
if (ps2 != null) {
ps2.close();
}
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
protected 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 " + T_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 " + T_USERS
+ " (" + ID + ", " + USER_NAME + ", " + SECURITY_CLASS
+ ") values (null,?,?)");
ps2.setString(1, name);
ps2.setString(2, securityClass);
ps2.execute();
//get user id
ps3 = con.prepareStatement("CALL IDENTITY()");
rs3 = ps3.executeQuery();
//rs3 = ps2.executeQuery("CALL IDENTITY()");
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 WorkflowInstance retrieveWorkflowInstance(String inId) {
//
// Connection con = null;
// PreparedStatement ps = null;
// ResultSet rs = null;
//
// try {
// WorkflowInstance outValue = null;
//
// con = getDatabaseConnection();
//
// ps = con.prepareStatement(
// "select " + WF_INSTANCE + " from " + T_WORKFLOWS
// + " where " + ID + " = ?");
// ps.setString(1, inId);
// ps.execute();
//
// rs = ps.getResultSet();
// if (rs.next()) {
//
// String r = rs.getString(1);
// StringBuffer sb = new StringBuffer(r);
//
// //MiscHelper.println("hsql.retrieveWi = " + sb);
//
// outValue = VersionHelper.fromXMLToWorkflowInstance(
// sb.toString());
// }
// else {
// throw new UnexpectedSystemException(
// "No workflow-instance found with id '" + inId + "'");
// }
//
// 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);
// }
// }
//}
protected BigDecimal getNextIndex(Connection inCon) {
Statement st = null;
try {
BigDecimal outValue = null;
st = inCon.createStatement();
int resultRow = st.executeUpdate(
"INSERT INTO seqTable(id) VALUES (NULL)");
ResultSet rset = st.executeQuery("SELECT MAX(id) FROM seqTable");
int rint = 0;
while (rset.next()) {
rint = rset.getInt(1);
}
outValue = new BigDecimal((double)rint);
st.close();
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
finally {
try {
if (st != null) {
st.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
//protected void updateWorkflowInstance(Connection inCon,
// String inPfId, WorkflowInstance inWorkflow) {
//
// PreparedStatement ps = null;
//
// try {
// BigInteger currentStateCode = inWorkflow.getStateCode();
// BigInteger updatedStateCode =
// currentStateCode.add(new BigInteger("1"));
//
// String workflowText = VersionHelper.fromWorkflowInstanceToXML(
// inWorkflow, updatedStateCode);
// //MiscHelper.println(
// // "GenericHSQLWorkflowLifecycleBean - workflowTx");
// //MiscHelper.println(workflowText);
//
// ps = inCon.prepareStatement(
// "update " + T_WORKFLOWS + " set "
// + WF_INSTANCE + " = ?, " + STATE_CODE + " = ? where "
// + ID + " = ? and " + STATE_CODE + " = ?");
//
// ps.setString(1, new String(workflowText));
// ps.setBigDecimal(2, new BigDecimal(updatedStateCode));
// ps.setBigDecimal(3, new BigDecimal(inPfId));
// ps.setBigDecimal(4, new BigDecimal(currentStateCode));
//
// ps.execute();
// int updateCount = ps.getUpdateCount();
//
// if (updateCount != 1) {
// throw new UnexpectedSystemException(
// "Workflow-instance with id (" + inPfId
// + ") has already been updated");
// }
// }
// catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
// }
// catch (IOException ioe) {
// throw new UnexpectedSystemException(ioe);
// }
// finally {
// try {
// if (ps != null) {
// ps.close();
// }
// }
// catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
// }
// }
//}
//protected void insertWorkflowInstance(Connection inCon,
// String inPfId, WorkflowInstance inWorkflow) {
//
// PreparedStatement ps = null;
//
// try {
// //TO DO: update query variables
//
// //MiscHelper.println("inWorkflow = " + inWorkflow);
// //MiscHelper.println("getStateCode = "
// //+ inWorkflow.getStateCode());
// //MiscHelper.println("xml = "
// // + VersionHelper.fromWorkflowInstanceToXML(inWorkflow,
// // inWorkflow.getStateCode()));
// Document workflowDocument = XMLHelper.loadDocument(
// VersionHelper.fromWorkflowInstanceToXML(inWorkflow,
// inWorkflow.getStateCode()));
// StringWriter sw = new StringWriter();
//
// XMLHelper.writeDocument(workflowDocument, sw);
//
// String workflowXml = sw.toString();
//
// StringReader sr = new StringReader(workflowXml);
//
// ps = inCon.prepareStatement(
// "insert into " + T_WORKFLOWS
// + " (" + ID + ", " + WF_INSTANCE + ", " + STATE_CODE
// + ") values (?,?,?)");
//
// //MiscHelper.println("workflowXml.len() = "
// //+ workflowXml.length());
// ps.setString(1, inPfId);
// //ps.setCharacterStream(2, sr, workflowXml.length());
// ps.setString(2, workflowXml);
// ps.setBigDecimal(3, new BigDecimal(inWorkflow.getStateCode()));
// ps.execute();
//
// }
// 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);
// }
// finally {
// try {
// if (ps != null) {
// ps.close();
// }
// }
// catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
// }
// }
//}
protected void insertWorkflowAttribute(Connection inCon,
String inPfId, String inAttrName, ValueAndClassForConceptTag inVac,
int inPubAttr, int inSysAttr) throws SQLException {
//TO DO: check for attribute type, view, set flag for class type
PreparedStatement ps = null;
try {
final String INSERT_QUERY =
"insert into " + T_ATTRIBUTES + " (" + ID + ", " + JF_ID + ", "
+ ATTR_NAME + ", " + DECIMAL_VALUE + ", " + BOOLEAN_VALUE + ", "
+ STRING_VALUE + ", " + 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();
ps = inCon.prepareStatement(INSERT_QUERY);
BigDecimal inId = getNextIndex(inCon);
ps.setBigDecimal(1, inId);
ps.setString(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.fromNumberToBigDecimal(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();
}
finally {
try {
if (ps != null) {
ps.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
protected void updateWorkflowAttribute(Connection inCon,
String inPfId, String inAttrName, ValueAndClassForConceptTag inVac,
int inSysAttr) throws SQLException {
//TO DO: check for attribute type, view, set flag for class type
PreparedStatement ps = null;
try {
final String UPDATE_BOOLEAN =
"update " + T_ATTRIBUTES + " set "
+ BOOLEAN_VALUE + " = ? where "
+ JF_ID + " = ? and "
+ ATTR_NAME + " = ? and "
+ BaseDBQueryVendor.ATTR_TYPE + " = ?";
final String UPDATE_DECIMAL =
"update " + T_ATTRIBUTES + " set " + DECIMAL_VALUE
+ " = ? where "
+ JF_ID + " = ? and " + ATTR_NAME + " = ? and "
+ ATTR_TYPE + " = ?";
final String UPDATE_STRING =
"update " + T_ATTRIBUTES + " set " + STRING_VALUE + " = ?, "
+ LONG_STRING_VALUE + " = ? where " + JF_ID + " = ? and "
+ ATTR_NAME + " = ? and " + ATTR_TYPE + " = ?";
Object value = inVac.getValue();
Class valueClass = inVac.getValueClass();
inAttrName = inAttrName.toUpperCase();
if (MiscHelper.isClassNumber(valueClass)) {
ps = inCon.prepareStatement(UPDATE_DECIMAL);
if (value != null) {
ps.setBigDecimal(1,
MiscHelper.fromNumberToBigDecimal(value));
}
else {
ps.setNull(1, Types.NUMERIC);
}
ps.setString(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.setString(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.setString(3, inPfId);
ps.setString(4, inAttrName);
ps.setInt(5, inSysAttr);
}
ps.execute();
}
finally {
try {
if (ps != null) {
ps.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
}*/
|