Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Created March 20, 2015 19:30
Show Gist options
  • Save TrevorBasinger/f656fc370c674b8f6401 to your computer and use it in GitHub Desktop.
Save TrevorBasinger/f656fc370c674b8f6401 to your computer and use it in GitHub Desktop.
//(c) Jeremy Branecky, Price Edwards & Company 2006
//
//WorkflowManager
//
//Used to set the starting state and role for an invoice
//
//public Members:
// Function GetPossibleStates(PropertyHMY) dataset - gets all possible states
// for property with hmy = PropertyHMY
// Function GetStartingState(invoice) integer - gets the starting state for
// invoice
// Function GetStartingRole(invoice) integer - gets the starting role for
// invoice
using System.Data;
using PEC.AP.DAL;
using AP;
using PEC.AP.App_Code.Invoice;
using PEC.AP.App_Code.Workflow;
namespace PEC.AP.App_Code_Shared.WorkflowManager
{
public class WorkflowManager
{
private DataSet _Data;
public Boolean getPossibleStates(int pid) {
if (DataAccess.getPossibleStates(pid)) {
_Data = DataAccess.DataObject;
return True;
}
return False;
}
//Precondition: inv is a valid invoice object
//Postcondition: returns the starting state for the invoice
public int GetStartingState(APInvoice inv)
{
//get all possible states
if (getPossibleStates(inv.PropertyHmy)) {
DataSet possibleStates = _Data;
foreach (DataRow dr in possibleStates.Tables(0).Rows)
{
//execute test for the state
if (ExecuteTest(dr, inv)) {
int tree = dr.Item("tree");
if (GetActionForTree(tree))
{
DataRow action = _Data.Tables(0).Rows(0);
// PerformAction(dr.Item("col"), action, inv)
}
return tree;
}
}
//if no starting state is found, return the default state
if (DataAccess.getDefaultState) {
return Convert.ToInt32(DataAccess.DataObject.Tables(0).Rows(0).Item("tree"));
}
else { return -1; }
}
}
//Precondition: inv is a valid invoice object
//Postcondition: returns the starting state for the invoice
public PEC.AP.App_Code.Workflow.APState GetStartingState_S(APInvoice inv)
{
//get all possible states
if (getPossibleStates(inv.PropertyHmy)) {
DataSet possibleStates = _Data;
foreach (DataRow dr in possibleStates.Tables(0).Rows)
{
//execute test for the state
if (ExecuteTest(dr, inv)) {
int tree = dr.Item("tree");
if (GetActionForTree(tree)) {
DataRow action = _Data.Tables(0).Rows(0);
//perform the specified action for the tree
//PerformAction(dr.Item("col"), action, inv);
}
return new PEC.AP.App_Code.Workflow.APState(tree);
}
}
//if no starting state is found, return the default state
if (DataAccess.getDefaultState) {
return new PEC.AP.App_Code.Workflow.APState(Convert.ToInt32(DataAccess.DataObject.Tables(0).Rows(0).Item("tree")));
}
else { return new PEC.AP.App_Code.Workflow.APState(-1); }
}
}
public String GetStartingStatus(int state)
{
//get all possible states
if (DataAccess.getState(state)) {
DataRow row = DataAccess.DataObject.Tables(0).Rows(0);
return row.Item("status");
}
return null;
}
//Precondition: inv is a valid invoice object
//Postcondition: returns the starting role for the invoice
public int GetStartingRole(APInvoice inv)
{
if (DataAccess.getRoleForPropState(inv.State, inv.PropertyHmy)) {
return Convert.ToInt32(DataAccess.DataObject.Tables(0).Rows(0).Item("hrole"));
}
else { return -1; }
}
//Precondition: tree is a valid integer that represents a tree id
//Postcondition: returns the datarow associated with the tree id
private Boolean GetActionForTree(int tree)
{
if (DataAccess.getActionForTree(tree)) {
_Data = DataAccess.DataObject;
return True;
}
return False;
}
public Boolean GetNextState(int state)
{
if (DataAccess.getNextState(state)) {
_Data = DataAccess.DataObject;
return True;
}
return False;
}
public APState GetNextState_S(int state, PEC.AP.App_Code.Invoice.APInvoice inv)
{
APState currentState = new APState(state);
currentState.Bind();
APState startingState = GetStartingState_S(inv);
startingState.Bind();
APState searchForState = startingState.WalkThroughTree(currentState);
if (searchForState.Hmy != -1) {
if (DataAccess.getNextState(searchForState.Hmy)) {
_Data = DataAccess.DataObject;
return new APState(Convert.ToInt32(_Data.Tables(0).Rows(0).Item(0)));
}
else {
APState searchForStateByRole = startingState.WalkThroughTree(currentState.Role);
if (DataAccess.getNextState(searchForStateByRole.Hmy)) {
_Data = DataAccess.DataObject;
return new APState(Convert.ToInt32(_Data.Tables(0).Rows(0).Item(0)));
}
}
return new APState(-1);
}
}
public Boolean GetPreviousState(int state)
{
if (DataAccess.getPreviousState(state)) {
_Data = DataAccess.DataObject;
return True;
}
return False;
}
public Boolean gotoNextState(int state, int hmy)
{
if (GetNextState(state)) {
DataRow nextstate = _Data.Tables(0).Rows(0);
if (nextstate != null) {
Boolean setState = DataAccess.setState(hmy, nextstate.Item("hmy"), nextstate.Item("status"));
if (!setState) {
return False;
}
DataAccess.setRoleForInvoice(hmy, nextstate.Item("hrole"));
if (!setRole) { return False; }
ExecuteAction(nextstate.Item("haction"), hmy);
return True;
}
}
return False;
}
public Boolean gotoPreviousState(int state, int hmy)
{
if (GetPreviousState(state)) {
DataRow previousstate = _Data.Tables(0).Rows(0);
if (previousstate != null) {
if (DataAccess.setState(hmy, previousstate.Item("hmy"), previousstate.Item("status"))
&& DataAccess.setRoleForInvoice(hmy, previousstate.Item("hrole"))) {
return True;
}
}
}
return False;
}
//Precondition: dr is a valid datarow, inv is a valid invoice
//Postcondition: compares the specific invoice value with
// dr.item("value")
private Boolean ExecuteTest(DataRow dr, PEC.AP.App_Code.Invoice.APInvoice inv)
{
String col = dr.Item("col");
if (col == "total") { return Execute(dr.Item("value"), inv.Total, dr.Item("operator")); }
if (col == "none") { return True; }
if (col.Contains(",")) {
String splitColumnString = col.Split(new Char() {","});
String splitValueString = dr.Item("value").ToString().Split(new Char() {","});
String splitCompareString = dr.Item("operator").ToString().Split(new Char() {","});
if (! (splitColumn.Length == splitValue.Length)
&& (splitColumn.Length = splitCompare.Length)
&& (splitValue.Length == splitCompare.Length)) {
return False;
}
else {
int i = 0;
Boolean valid = True;
Boolean lineItems = False;
for (i = 0; i < splitColumn.Length - 1; i++)
{
try {
if (splitColumn(i) == "total")
{ valid = Execute(splitValue(i), inv.Total, splitCompare(i)); }
if (splitColumn(i) == "invoiceNum")
{ valid = Execute(splitValue(i), inv.InvoiceNumber, splitCompare(i)); }
if (splitColumn(i) == "vendor")
{ valid = Execute(splitValue(i), inv.VendorHmy, splitCompare(i)); }
if (splitColumn(i) == "property")
{ valid = Execute(splitValue(i), inv.PropertyHmy, splitCompare(i)); }
if (splitColumn(i) == "lineitem")
{ lineItems = True; valid = ExecuteLineItemTest(splitValue(i), inv.LineItems, splitCompare(i)); }
} catch (Exception ex) {
valid = False;
}
if (!valid && !lineItems) { return False; };
if (valid && lineItems) { return True; }
}
return valid;
}
}
}
public Boolean ExecuteLineItemTest (Object val1, ArrayList list, String op)
{
li = new PEC.AP.App_Code.Invoice.LineItemObject();
Boolean val = True;
foreach (PEC.AP.App_Code.Invoice.LineItemObject li in list)
{
if (op.ToString() == "=") {
if (li.GLAccount.Contains(val1.ToString())) { return True; }
}
if (op.ToString() == "!") {
if (li.GLAccount.Contains(val1.ToString()))
{ val = False; }
}
if (val == False) { return False; }
}
return False;
}
public Boolean Execute(Object val1, Object val2, String op)
{
if (op == "<")
{
try { return CType(val1, Double) < CType(val2, Double); }
catch (Exception ex) {
try { return CType(val1, Date) < CType(val2, Date); }
catch (Exception ex2) { return False; }
}
}
if (op == ">")
{
try { return CType(val1, Double) < CType(val2, Double); }
catch (Exception ex) {
try { return CType(val1, Date) < CType(val2, Date); }
catch (Exception ex2) { return False; }
}
}
if (op == "=")
{
try { return CType(val1, Double) < CType(val2, Double); }
catch (Exception ex) {
try { return CType(val1, Date) < CType(val2, Date); }
catch (Exception ex2) { return False; }
}
}
if (op == "!")
{
try { return CType(val1, Double) < CType(val2, Double); }
catch (Exception ex) {
try { return CType(val1, Date) < CType(val2, Date); }
catch (Exception ex2) {
try { return val1.toString() != val2.toString(); }
catch (Exception ex3) { return False; }
}
}
}
if (op == "%")
{
try { return val2.ToString().ToLower().Contains(val1.ToString.ToLower()); }
catch (Exception ex) { return False; }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment