/*
* 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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
//project specific classes
import org.jfolder.common.UnexpectedSystemException;
import org.jfolder.common.tagging.RootConceptTagHolder;
import org.jfolder.common.tagging.ValueAndClassForConceptTag;
import org.jfolder.common.utils.misc.MiscHelper;
import org.jfolder.project.model.ProjectApplicationSet;
import org.jfolder.security.audit.SecurityAuditQuery;
import org.jfolder.security.audit.SecurityAuditSet;
import org.jfolder.security.model.UserHolder;
import org.jfolder.services.config.ConfigServiceCaller;
import org.jfolder.services.config.ConfigServiceCallerFactory;
import org.jfolder.workflow.model.dms.WorkflowDocumentGroup;
import org.jfolder.workflow.model.dms.WorkflowDocumentGroupUpdates;
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.query.BaseDBQueryVendor;
import org.jfolder.workflow.query.BaseDBQueryVendorProprietarySyntax;
import org.jfolder.workflow.query.ResultSetContainer;
import org.jfolder.workflow.query.StatementContainer;
//other classes
/*
//TO DO: make calls transactional where appropriate
public abstract class BaseDBWorkflowLifecycleBean
implements WorkflowLifecycle {
//protected final static String T_JFOLDER_WORKFLOWS = "T_JFOLDER_WORKFLOWS";
protected final static String T_USERS = "T_USERS";
protected final static String T_WORKFLOWS = "T_WORKFLOWS";
protected final static String T_DEPLOYED_SCRIPTS = "T_DEPLOYED_SCRIPTS";
protected final static String T_ATTRIBUTES = BaseDBQueryVendor.T_ATTRIBUTES;
protected final static String T_TRIGGERS = "T_TRIGGERS";
//
protected final static String WF_INSTANCE = "WF_INSTANCE";
protected final static String STATE_CODE = "STATE_CODE";
protected final static String ID = BaseDBQueryVendor.ID;
protected final static String SECURITY_CLASS = "SECURITY_CLASS";
protected final static String USER_NAME = "USER_NAME";
protected final static String SCRIPT_NAME = "SCRIPT_NAME";
protected final static String WORKFLOW_ID = "WORKFLOW_ID";
protected final static String HANDLE = "HANDLE";
protected final static String CONTENT = "CONTENT";
protected final static String AUDIT_ID = "AUDIT_ID";
protected final static String AUDIT_USER_ID = "AUDIT_USER_ID";
protected final static String AUDIT_TIMESTAMP = "AUDIT_TIMESTAMP";
protected final static String AUDIT_STATUS = "AUDIT_STATUS";
protected final static String AUDIT_COMMENT = "AUDIT_COMMENT";
protected final static String AUDIT_EXCEPTION_MESSAGE =
"AUDIT_EXCEPTION_MESSAGE";
protected final static String AUDIT_EXCEPTION_SOURCE =
"AUDIT_EXCEPTION_SOURCE";
//
protected final static String JF_ID = BaseDBQueryVendor.JF_ID;
protected final static String ATTR_NAME = BaseDBQueryVendor.ATTR_NAME;
protected final static String ATTR_TYPE = BaseDBQueryVendor.ATTR_TYPE;
protected final static String DECIMAL_VALUE =
BaseDBQueryVendor.DECIMAL_VALUE;
protected final static String BOOLEAN_VALUE =
BaseDBQueryVendor.BOOLEAN_VALUE;
protected final static String STRING_VALUE =
BaseDBQueryVendor.STRING_VALUE;
protected final static String LONG_STRING_VALUE =
BaseDBQueryVendor.LONG_STRING_VALUE;
public BaseDBWorkflowLifecycleBean() {
}
public void close() {
//not implemented, included in WorkflowLifecycle, not used
}
protected abstract BaseDBQueryVendorProprietarySyntax
getBaseDBQueryVendorProprietarySyntax();
//protected abstract void updateWorkflowAttribute(Connection inCon,
// String inPfId, String inAttrName, ValueAndClassForConceptTag inVac,
// int inSysAttr) throws SQLException;
//protected abstract void insertWorkflowAttribute(Connection inCon,
// String inPfId, String inAttrName, ValueAndClassForConceptTag inVac,
// int inPubAttr, int inSysAttr) throws SQLException;
protected abstract int getFileIdOfUser(UserHolder inUh);
//protected abstract BigDecimal getNextIndex(Connection inCon);
//protected abstract void insertWorkflowInstance(Connection inCon,
// String inPfId, WorkflowInstance inWorkflow);
//protected abstract void updateWorkflowInstance(Connection inCon,
// String inPfId, WorkflowInstance inWorkflow);
//public abstract String submitTriggerData(String inHandle,
// TriggerData inTd);
//public abstract void updateTriggerDataStatus(String inHandle,
// TriggerData inTd);
//public abstract TriggerData retrieveTriggerData(String inHandle);
//properties functions
public void loadApplicationProperties(String inApp, Properties inProps) {
try {
File propsDir = getPropertiesDir();
File propsFile = new File(propsDir, inApp + ".properties");
FileOutputStream fos = new FileOutputStream(propsFile);
inProps.store(fos, "Application - " + inApp);
}
catch (FileNotFoundException fnfe) {
throw new UnexpectedSystemException(fnfe);
}
catch (IOException ioe) {
throw new UnexpectedSystemException(ioe);
}
}
public Properties getApplicationProperties(String inApp) {
try {
Properties outValue = new Properties();
File propsDir = getPropertiesDir();
File propsFile = new File(propsDir, inApp + ".properties");
FileInputStream fis = new FileInputStream(propsFile);
outValue.load(fis);
return outValue;
}
catch (FileNotFoundException fnfe) {
throw new UnexpectedSystemException(fnfe);
}
catch (IOException ioe) {
throw new UnexpectedSystemException(ioe);
}
}
public void unloadApplicationProperties(String inApp) {
File propsDir = getPropertiesDir();
File propsFile = new File(propsDir, inApp + ".properties");
propsFile.delete();
}
//public void updateWorkflowInstance(WorkflowInstance inWi) {
//
// //throw UnexpectedSystemException.notImplemented();
// Connection con = null;
//
// try {
//
// //MiscHelper.println(
// //"GenericHSQLWorkflowLifecycleBean - updateWf");
//
// con = getDatabaseConnection();
//
// AttributeSet as = inWi.getAttributeSet();
// ValueAndClassForConceptTag idVac =
// as.getPublicSysAttr(AttributeSet.ID);
// ValueAndClassForConceptTag statusVac =
// as.getPublicSysAttr(AttributeSet.STATUS);
//
// String id = (String)idVac.getValue();
// String status = (String)statusVac.getValue();
//
// updateWorkflowInstance(con, id, inWi);
// storeWorkflowAttributes(con, id, as, false);
//
// //con.commit();//TO DO: remove this
//
// if (status.equals(TraceAudit.STATUS_ACTIVE)) {
// putTextMessageOnQueue(id, null, getWorkflowQueue());
// }
// }
// //catch (SQLException sqle) {
// // throw new UnexpectedSystemException(sqle);
// //}
// finally {
// try {
// if (con != null) {
// con.close();
// }
// }
// catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
// }
// }
//
// //TO DO: should this function return anything?
//
//}
//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 queryWorkflowInstances(
StatementContainer inStatement) {
Connection con = null;
try {
ResultSetContainer outValue = null;
con = getDatabaseConnection();
BaseDBQueryVendor bdbqv = BaseDBQueryVendor.newBaseDBQueryVendor(
getBaseDBQueryVendorProprietarySyntax(), true);
outValue = bdbqv.executeStatement(con, inStatement);
return outValue;
}
//catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
//}
finally {
try {
if (con != null) {
con.close();
}
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
}
}
public ProjectApplicationSet getProjectApplicationSet(UserHolder inUh) {
GenericFileProjectApplicationSet outValue =
new GenericFileProjectApplicationSet(
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()) {
GenericFileProjectApplication nextWa =
new GenericFileProjectApplication(appList[i]);
outValue.registerApplication(nextWa);
}
}
outValue.load();
return outValue;
}
public void setProjectApplicationSet(ProjectApplicationSet inWas,
UserHolder inUh) {
//reset deployed applications
File appList[] = getApplicationsDirectory(inUh).listFiles();
for (int i = 0; i < appList.length; i++) {
MiscHelper.deleteFileOrDirectory(appList[i]);
}
((GenericFileProjectApplicationSet)inWas).store();
}
protected File getApplicationsDirectory(UserHolder inUh) {
File outValue = null;
ConfigServiceCaller csc =
ConfigServiceCallerFactory.getConfigServiceCaller();
//ConfigLifecycle cm = ConfigLifecycleFactory.getConfigLifecycle();
//File pfDir =
// GenericFileProjectApplicationSet.getJFolderDirectory(cm);
File usersDir =
GenericFileProjectApplicationSet.getRuntimeProjectSubDir("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;
}
//public void fireTriggerData(TriggerDataPrecursor inTrigger) {
//
// throw UnexpectedSystemException.notImplemented();
// //putTextMessageOnQueue(
// // inTrigger.getDataAsString(), null, getTriggerQueue());
//}
//public TriggerDataHistory getTriggerDataHistory(String inHandle) {
// throw UnexpectedSystemException.notImplemented();
//}
//public TriggerDataLabels getFailedTriggerSubmissions() {
// throw UnexpectedSystemException.notImplemented();
//}
//public void startWorkflowInstance(WorkflowInstancePrecursor inWp) {
//
// Connection con = null;
//
// try {
//
// //4 scenarios when starting a workflow-instance
// //Scenario 1 - trigger good, new workflow-instance
// //Scenario 2 - trigger bad, new workflow-instance
// //Scenario 3 - trigger good, existing workflow-instance
// //Scenario 4 - trigger bad, existing workflow-instance
//
// con = getDatabaseConnection();
//
// TriggerData t = inWp.getTriggerData();
//
// //boolean triggerBad = t.isAuditExceptionPresent();
// //boolean existingWorkflow = tc.isWorkflowPresent();
//
// String pfId = getNextIndex(con).toString();
//
// //if (!existingWorkflow) {
// // pfId = getNextIndex(con).toString();
// //}
// //else {
// // pfId = tc.getWorkflowId();
// //}
// //
// //if (!triggerBad) {
//
// AttributeSet as = inWp.getAttributeSet();
// RootConceptTagHolder rth = inWp.getRootScriptTagHolder();
// HistoryAudit h = inWp.getHistoryAudit();
//
// //if (!existingWorkflow) {
// //this is the only attribute added in this function
// as.addPublicSysAttr(AttributeSet.ID,
// ValueAndClassForConceptTagFactory.newValueAndClass(
// pfId, String.class));
// WorkflowInstance w =
// WorkflowInstanceFactory.newWorkflowInstance(
// h, rth, as, t, pfId,
// WorkflowInstance.INITIAL_STATE);
// insertWorkflowInstance(con, pfId, w);
// //}
// //else {
// // WorkflowInstance w = null;
// // updateWorkflowInstance(con, pfId, w);
// //}
//
// storeWorkflowAttributes(con, pfId, as, true);
//
// Object s = as.getPublicSysAttr(
//AttributeSet.STATUS).getValue();
//
// if (s.equals(TraceAudit.STATUS_ACTIVE)) {
// putTextMessageOnQueue(pfId, null, getWorkflowQueue());
// }
// //}
// //else {
// // if (!existingWorkflow) {
// // //this is the only attribute added in this function
// // WorkflowInstance w = null;
// // insertWorkflowInstance(con, pfId, w);
// // }
// // else {
// // WorkflowInstance w = null;
// // updateWorkflowInstance(con, pfId, w);
// // }
// //}
//
// }
// //catch (SQLException sqle) {
// // throw new UnexpectedSystemException(sqle);
// //}
// finally {
// try {
// if (con != null) {
// con.close();
// }
// }
// catch (SQLException sqle) {
// throw new UnexpectedSystemException(sqle);
// }
// }
//}
public SecurityAuditSet getSecurityAudits(SecurityAuditQuery inSaq) {
throw UnexpectedSystemException.notImplemented();
}
//protected void storeWorkflowAttributes(Connection inCon,
// String inPfId, AttributeSet inAs, boolean inIsInsert) {
//
// try {
//
// Iterator iter = null;
//
// //public system attributes
// iter = inAs.getPublicSysAttrNames();
// while (iter.hasNext()) {
// String attrName = (String)iter.next();
// ValueAndClassForConceptTag 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();
// ValueAndClassForConceptTag 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();
// ValueAndClassForConceptTag 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();
// ValueAndClassForConceptTag 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 helper methods
protected 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);
outValue = ds.getConnection();
return outValue;
}
catch (SQLException sqle) {
throw new UnexpectedSystemException(sqle);
}
catch (NamingException ne) {
throw new UnexpectedSystemException(ne);
}
}
protected final static File getScriptsDir() {
return GenericFileProjectApplicationSet.getRuntimeProjectSubDir(
"scripts");
}
protected final static File getPropertiesDir() {
return GenericFileProjectApplicationSet.getRuntimeProjectSubDir(
"properties");
}
//document functions
//public DocumentGroup getDocumentGroup(String inId, Integer inVersion) {
// throw UnexpectedSystemException.notImplemented();
//}
//public void updateDocumentGroup(DocumentGroupUpdates inDgu) {
// throw UnexpectedSystemException.notImplemented();
//}
}*/
|