From b20c09c158b8ed0dbc5597dd5ca8a78760085580 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 10 Jun 2013 22:15:44 +0000 Subject: [PATCH] Committing work at EOD - working on new modular outbound comms framework --- .../Types/Communications/BasicOutboundComm.cs | 8 +- Database/Database-OutboundComms.cs | 496 ++++++++++++++++++ Database/Database.csproj | 1 + 3 files changed, 502 insertions(+), 3 deletions(-) create mode 100644 Database/Database-OutboundComms.cs diff --git a/Basic/Types/Communications/BasicOutboundComm.cs b/Basic/Types/Communications/BasicOutboundComm.cs index 1ab7aff0..cf350012 100644 --- a/Basic/Types/Communications/BasicOutboundComm.cs +++ b/Basic/Types/Communications/BasicOutboundComm.cs @@ -9,11 +9,12 @@ namespace Swarmops.Basic.Types.Communications { public class BasicOutboundComm: IHasIdentity { - public BasicOutboundComm (int outboundCommId, int senderPersonId, int organizationId, DateTime createdDateTime, string resolverClass, string recipientDataXml, bool resolved, DateTime resolvedDateTime, OutboundCommPriority priority, string transmitterClass, string payloadXml, + public BasicOutboundComm (int outboundCommId, int senderPersonId, int fromPersonId, int organizationId, DateTime createdDateTime, string resolverClass, string recipientDataXml, bool resolved, DateTime resolvedDateTime, OutboundCommPriority priority, string transmitterClass, string payloadXml, bool open, DateTime startTransmitDateTime, DateTime closedDateTime, int recipientCount, int recipientsSuccess, int recipientsFail) { this.OutboundCommId = outboundCommId; - this.SenderPersonId = senderPersonId; // should we include a "fromPersonId" in basedata too, when deputies are sending on behalf of avatars? + this.SenderPersonId = senderPersonId; + this.FromPersonId = fromPersonId; this.OrganizationId = organizationId; this.CreatedDateTime = createdDateTime; this.ResolverClass = resolverClass; @@ -33,7 +34,7 @@ namespace Swarmops.Basic.Types.Communications public BasicOutboundComm (BasicOutboundComm original) : - this (original.OutboundCommId, original.SenderPersonId, original.OrganizationId, original.CreatedDateTime, original.ResolverClass, original.RecipientDataXml, original.Resolved, original.ResolvedDateTime, original.Priority, original.TransmitterClass, original.PayloadXml, + this (original.OutboundCommId, original.SenderPersonId, original.FromPersonId, original.OrganizationId, original.CreatedDateTime, original.ResolverClass, original.RecipientDataXml, original.Resolved, original.ResolvedDateTime, original.Priority, original.TransmitterClass, original.PayloadXml, original.Open, original.StartTransmitDateTime, original.ClosedDateTime, original.RecipientCount, original.RecipientsSuccess, original.RecipeintsFail) { // copy ctor @@ -43,6 +44,7 @@ namespace Swarmops.Basic.Types.Communications public int OutboundCommId { get; private set; } public int SenderPersonId { get; private set; } + public int FromPersonId { get; private set; } public int OrganizationId { get; private set; } public DateTime CreatedDateTime { get; private set; } public string ResolverClass { get; private set; } diff --git a/Database/Database-OutboundComms.cs b/Database/Database-OutboundComms.cs new file mode 100644 index 00000000..a035af09 --- /dev/null +++ b/Database/Database-OutboundComms.cs @@ -0,0 +1,496 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Text; +using Swarmops.Basic.Enums; +using Swarmops.Basic.Types; +using Swarmops.Basic.Types.Communications; + +namespace Swarmops.Database +{ + public partial class SwarmDb + { + + #region Database field reading + + private const string outboundCommFieldSequence = + " OutboundCommId,SenderPersonId,FromPersonId,OrganizationId,CreatedDateTime," + // 0-4 + "ResolverClassId,RecipientDataXml,Resolved,ResolvedDateTime,Priority," + // 5-9 + "TransmitterClassId,PayloadXml,Open,StartTransmitDateTime,ClosedDateTime," + // 10-14 + "RecipientCount,RecipientsSuccess,RecipientsFail " + // 15-17 + "FROM OutboundComms "; + + private static BasicOutboundComm ReadOutboundCommFromDataReader(IDataRecord reader) + { + int outboundCommId = reader.GetInt32(0); + int senderPersonId = reader.GetInt32(1); + int fromPersonId = reader.GetInt32(2); + int organizationId = reader.GetInt32(3); + DateTime createdDateTime = reader.GetDateTime(4); + + int resolverClassId = reader.GetInt32(5); // Resolve to class name -- cached call more efficient than Join in Select + string recipientDataXml = reader.GetString(6); // Interpreted by ResolverClass + bool resolved = reader.GetBoolean(7); + DateTime resolvedDateTime = reader.GetDateTime(8); + OutboundCommPriority priority = (OutboundCommPriority) reader.GetInt32(9); + + int transmitterClassId = reader.GetInt32(10); // Resolve to class name -- cached call more efficient than Join in Select + string payloadXml = reader.GetString(11); // Interpreted by TransmitterClass + bool open = reader.GetBoolean(12); + DateTime startTransmitDateTime = reader.GetDateTime(13); + DateTime closedDateTime = reader.GetDateTime(14); + + int recipientCount = reader.GetInt32(15); // Set by resolver + int recipientSuccessCount = reader.GetInt32(16); + int recipientFailCount = reader.GetInt32(17); + + // TODO: resolve resolverClass, transmitterClass + + string resolverClass = null; + string transmitterClass = null; + + return new BasicOutboundComm + (outboundCommId, senderPersonId, fromPersonId, organizationId, createdDateTime, + resolverClass, recipientDataXml, resolved, resolvedDateTime, priority, + transmitterClass, payloadXml, open, startTransmitDateTime, closedDateTime, + recipientCount, recipientSuccessCount, recipientFailCount); + + + } + + #endregion + + + #region Database record reading -- SELECT clauses + + + public BasicOutboundComm GetOutboundComm(int outboundCommId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT" + outboundCommFieldSequence + + "WHERE OutboundCommId=" + outboundCommId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadOutboundCommFromDataReader(reader); + } + + throw new ArgumentException("No such OutboundCommId:" + outboundCommId.ToString()); + } + } + + } + + + + public BasicOutboundComm[] GetOutboundComms(params object[] conditions) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + outboundCommFieldSequence + ConstructWhereClause("OutboundComms", conditions) + " ORDER BY Priority ASC", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadOutboundCommFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + #endregion + + + #region Creation and manipulation -- stored procedures + + + public int CreateOutboundComm(int senderPersonId, int fromPersonId, int organizationId, string resolverClass, string recipientDataXml, string transmitterClass, string payloadXml, OutboundCommPriority priority) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreateOutboundComm", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "senderPersonId", senderPersonId); + AddParameterWithName(command, "fromPersonId", fromPersonId); + AddParameterWithName(command, "organizationId", organizationId); + AddParameterWithName(command, "resolverClass", resolverClass); // Will be turned into ID by stored procedure + AddParameterWithName(command, "recipientDataXml", recipientDataXml); + AddParameterWithName(command, "transmitterClass", transmitterClass); // Will be turned into ID by stored procedure + AddParameterWithName(command, "payloadXml", payloadXml); + AddParameterWithName(command, "priority", (int) priority); // convert enum to integerized priority; convert back on field read + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + #endregion + + + #region Dead template code + + /* + public BasicExpenseClaim GetExpenseClaim(int expenseClaimId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand( + "SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE ExpenseClaimId=" + expenseClaimId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadExpenseFromDataReader(reader); + } + + throw new ArgumentException("Unknown ExpenseClaim Id"); + } + } + } + + public BasicExpenseClaim[] GetExpenseClaimsByClaimer(int claimingPersonId) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE ClaimingPersonId=" + claimingPersonId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadExpenseFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + public BasicExpenseClaim[] GetExpenseClaimsByBudgetAndYear(int budgetId, int budgetYear) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE BudgetId=" + budgetId.ToString() + " AND BudgetYear=" + budgetYear.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadExpenseFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + public BasicExpenseClaim[] GetExpenseClaimsByOrganization(int organizationId) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT ExpenseClaimId,ClaimingPersonId,CreatedDateTime,Open,Attested," + + "Validated,Claimed,OrganizationId,GeographyId,BudgetId,BudgetYear," + + "ExpenseDate,Description,PreApprovedAmount,Amount,Repaid " + + "FROM ExpenseClaims WHERE OrganizationId=" + organizationId.ToString(), + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadExpenseFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + private BasicExpenseClaim ReadExpenseFromDataReader(DbDataReader reader) + { + int expenseClaimId = reader.GetInt32(0); + int claimingPersonId = reader.GetInt32(1); + DateTime createdDateTime = reader.GetDateTime(2); + bool open = reader.GetBoolean(3); + bool attested = reader.GetBoolean(4); + bool documented = reader.GetBoolean(5); + bool claimed = reader.GetBoolean(6); + int organizationId = reader.GetInt32(7); + int geographyId = reader.GetInt32(8); // obsolete field + int budgetId = reader.GetInt32(9); + int budgetYear = reader.GetInt32(10); + DateTime expenseDate = reader.GetDateTime(11); + string description = reader.GetString(12); + double preApprovedAmount = reader.GetDouble(13); + double amount = reader.GetDouble(14); + bool repaid = reader.GetBoolean(15); + + return new BasicExpenseClaim(expenseClaimId, claimingPersonId, createdDateTime, + open, attested, documented, claimed, organizationId, geographyId, + budgetId, budgetYear, expenseDate, description, preApprovedAmount, + amount, repaid); + } + + + public int CreateExpenseClaim(int claimingPersonId, int organizationId, int budgetId, int budgetYear, + DateTime expenseDate, string description, double amount) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreateExpenseClaim", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "claimingPersonId", claimingPersonId); + AddParameterWithName(command, "createdDateTime", DateTime.Now); + AddParameterWithName(command, "organizationId", organizationId); + AddParameterWithName(command, "budgetId", budgetId); + AddParameterWithName(command, "budgetYear", budgetYear); + AddParameterWithName(command, "expenseDate", expenseDate); + AddParameterWithName(command, "description", description); + AddParameterWithName(command, "amount", amount); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + public int SetExpenseClaimDescription(int expenseClaimId, string description) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimDescription", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "description", description); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public int SetExpenseClaimBudget(int expenseClaimId, int budgetId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimBudget", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "budgetId", budgetId); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public int SetExpenseClaimBudgetYear(int expenseClaimId, int budgetYear) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimBudgetYear", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "budgetYear", budgetYear); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public int SetExpenseClaimDate(int expenseClaimId, DateTime expenseDate) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimDate", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "expenseDate", expenseDate); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public int SetExpenseClaimAmount(int expenseClaimId, double amount) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimAmount", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "amount", amount); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + + public int SetExpenseClaimAttested(int expenseClaimId, bool attested) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimAttested", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "attested", attested); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + + public int SetExpenseClaimValidated(int expenseClaimId, bool validated) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimValidated", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "validated", validated); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + + public int SetExpenseClaimOpen(int expenseClaimId, bool open) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimOpen", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "open", open); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + + + public int SetExpenseClaimRepaid(int expenseClaimId, bool repaid) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetExpenseClaimRepaid", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseClaimId", expenseClaimId); + AddParameterWithName(command, "repaid", repaid); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + + + public int CreateExpenseEvent(int expenseId, ExpenseEventType eventType, int personId) + { + // WARNING: NOT CONVERTED TO V4 YET + + using (DbConnection connection = GetSqlServerDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreateExpenseEvent", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "expenseId", expenseId); + AddParameterWithName(command, "dateTime", DateTime.Now); + AddParameterWithName(command, "eventPersonId", personId); + AddParameterWithName(command, "eventType", eventType.ToString()); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + */ + + #endregion + + + } +} \ No newline at end of file diff --git a/Database/Database.csproj b/Database/Database.csproj index cd889940..94b279e4 100644 --- a/Database/Database.csproj +++ b/Database/Database.csproj @@ -92,6 +92,7 @@ +