diff --git a/Basic/Basic.csproj b/Basic/Basic.csproj index 27cb55bc..1b61a3b0 100644 --- a/Basic/Basic.csproj +++ b/Basic/Basic.csproj @@ -57,6 +57,7 @@ + diff --git a/Basic/DbColumnAwareBase.cs b/Basic/DbColumnAwareBase.cs new file mode 100644 index 00000000..12c28213 --- /dev/null +++ b/Basic/DbColumnAwareBase.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +/* 2018-Dec-22: This is a started refactoring of the Database namespace into separate derived classes + * from a generic that contains most of the gruntwork. It is not nearly complete and + * the code is still experimental, not taken into production yet. + */ + +namespace Swarmops.Basic +{ + public class DbColumnAwareBase + { + static DbColumnAwareBase() + { + if (_classColumnOrdinalLookup == null) + { + _classColumnOrdinalLookup = new Dictionary>(); + _classColumnSequenceLookup = new Dictionary(); + } + } + + public DbColumnAwareBase() + { + // ReSharper disable once DoNotCallOverridableMethodsInConstructor + VerifyLookupInitialized(); + } + + private static Dictionary> _classColumnOrdinalLookup; + private static Dictionary _classColumnSequenceLookup; + + + public virtual void VerifyLookupInitialized() + { + string className = this.GetType().Name; + if (_classColumnOrdinalLookup.ContainsKey(className)) + { + return; + } + + // The key of the instantiated class is not in the dictionary, so initialize dictionary + + // This is done with reflection, so is very expensive, which is why we only do it once for + // every class that derives from DbColumnAwareBase + ; + } + } +} diff --git a/Common/Attributes/DbColumnName.cs b/Common/Attributes/DbColumnName.cs new file mode 100644 index 00000000..836d054c --- /dev/null +++ b/Common/Attributes/DbColumnName.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Drawing.Text; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +/* 2018-Dec-22: This is a started refactoring of the Database namespace into separate derived classes + * from a generic that contains most of the gruntwork. It is not nearly complete and + * the code is still experimental, not taken into production yet. + */ + + +namespace Swarmops.Common.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class DbColumnName: Attribute + { + public DbColumnName() + { + // means the column name is the same as the property name that houses the attribute + this.ColumnName = string.Empty; + } + public DbColumnName(string columnName) + { + this.ColumnName = columnName; + } + + public string ColumnName { get; private set; } + } +} diff --git a/Common/Common.csproj b/Common/Common.csproj index 06194146..3ea2b9bc 100644 --- a/Common/Common.csproj +++ b/Common/Common.csproj @@ -41,6 +41,7 @@ + @@ -82,6 +83,7 @@ + diff --git a/Common/Enums/FinancialAccountDocumentType.cs b/Common/Enums/FinancialAccountDocumentType.cs index ba80cb98..c76f95dc 100644 --- a/Common/Enums/FinancialAccountDocumentType.cs +++ b/Common/Enums/FinancialAccountDocumentType.cs @@ -10,6 +10,7 @@ namespace Swarmops.Common.Enums { Unknown = 0, BankStatement, - BankTransactionData + BankTransactionData, + BankPaymentData } } diff --git a/Common/Interfaces/IDatabaseColumnAware.cs b/Common/Interfaces/IDatabaseColumnAware.cs new file mode 100644 index 00000000..57106c72 --- /dev/null +++ b/Common/Interfaces/IDatabaseColumnAware.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +/* 2018-Dec-22: This is a started refactoring of the Database namespace into separate derived classes + * from a generic that contains most of the gruntwork. It is not nearly complete and + * the code is still experimental, not taken into production yet. + */ + + +namespace Swarmops.Common.Interfaces +{ + [SuppressMessage("ReSharper", "TypeParameterCanBeVariant")] + public interface IDatabaseColumnAware + { + string DbTableName { get; } + string[] DbFieldSequence { get; } + string DbIdentityField { get; } + TBasic FromDataReader (IDataReader reader); // this could be static but C# doesn't allow static methods in interfaces + } +} diff --git a/Database/Database-FinancialAccountDocuments.cs b/Database/Database-FinancialAccountDocuments.cs new file mode 100644 index 00000000..cb18f7aa --- /dev/null +++ b/Database/Database-FinancialAccountDocuments.cs @@ -0,0 +1,524 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using Swarmops.Basic.Types; +using Swarmops.Basic.Types.Financial; +using Swarmops.Common.Enums; + +namespace Swarmops.Database +{ + public partial class SwarmDb + { + #region Database field reading + + private const string financialAccountDocumentFieldSequence = + " PaymentId,PaymentGroupId,AmountCents,Reference,FromAccount," + // 0-4 + "PaymentKey,HasImage,OutboundInvoiceId " + // 5-7 + "FROM Payments "; + + private static BasicPayment ReadFinancialAccountDocumentFromDataReader(IDataRecord reader) + { + int paymentId = reader.GetInt32(0); + int paymentGroupId = reader.GetInt32(1); + Int64 amountCents = reader.GetInt64(2); + string reference = reader.GetString(3); + string fromAccount = reader.GetString(4); + string key = reader.GetString(5); + bool hasImage = reader.GetBoolean(6); + int outboundInvoiceId = reader.GetInt32(7); + + return new BasicPayment(paymentId, paymentGroupId, amountCents, reference, fromAccount, key, hasImage, + outboundInvoiceId); + } + + #endregion + + #region Database record reading -- SELECT clauses + + public BasicPayment GetFinancialAccountDocument(int financialAccountDocumentId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT" + paymentFieldSequence + + "WHERE FinancialAccountDocumentId=" + financialAccountDocumentId, + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadPaymentFromDataReader(reader); + } + + throw new ArgumentException("No such FinancialAccountDocumentId:" + financialAccountDocumentId); + } + } + } + + + public BasicPayment[] GetFinancialAccountDocuments(params object[] conditions) + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + paymentFieldSequence + ConstructWhereClause("Payments", conditions), connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadPaymentFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + #endregion + + #region Creation and manipulation -- stored procedures + + public int CreateFinancialAccountDocument(int financialAccountId, FinancialAccountDocumentType type, int uploadedByPersonId, + DateTime concernsPeriodStart, DateTime concernsPeriodEnd, string rawText) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreateFinancialAccountDocument", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "financialAccountId", financialAccountId); + AddParameterWithName(command, "financialAccountDocumentType", type.ToString()); + AddParameterWithName(command, "uploadedDateTime", DateTime.UtcNow); + AddParameterWithName(command, "uploadedByPersonId", uploadedByPersonId); + AddParameterWithName(command, "concernsPeriodStart", concernsPeriodStart); // inclusive endpoint + AddParameterWithName(command, "concernsPeriodEnd", concernsPeriodEnd); // exclusive endpoint + AddParameterWithName(command, "outboundInvoiceId", outboundInvoiceId); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public int CreatePayment(int paymentGroupId, Int64 amountCents, string reference, string fromAccount, + string key, + bool hasImage, int outboundInvoiceId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreatePaymentPrecise", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "paymentGroupId", paymentGroupId); + AddParameterWithName(command, "amountCents", amountCents); + AddParameterWithName(command, "reference", reference); + AddParameterWithName(command, "fromAccount", fromAccount); + AddParameterWithName(command, "paymentKey", key); + AddParameterWithName(command, "hasImage", hasImage); + AddParameterWithName(command, "outboundInvoiceId", outboundInvoiceId); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + + public int CreatePaymentInformation(int paymentId, PaymentInformationType type, string data) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("CreatePaymentInformation", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "paymentId", paymentId); + AddParameterWithName(command, "dataTypeString", type.ToString()); + AddParameterWithName(command, "data", data); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + + [Obsolete("This should never be called, ever: it ruins trackability.")] + public void DeletePayment(int paymentId) + { + // This function is necessary since the Swedish bank system only allows for a dupe check AFTER all payments + // have been communicated, so they are created as data is received. If the data was a dupe, it is therefore + // deleted after the dupecheck triggers. + + // That code was later re-written to do in-memory dupechecking. This delete function should never be called. + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("DeletePayment", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "paymentId", paymentId); + + command.ExecuteNonQuery(); + } + } + + #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 e1e0c0f1..7a981f39 100644 --- a/Database/Database.csproj +++ b/Database/Database.csproj @@ -62,6 +62,7 @@ + @@ -141,6 +142,7 @@ + Designer diff --git a/Database/TableBase.cs b/Database/TableBase.cs new file mode 100644 index 00000000..35744805 --- /dev/null +++ b/Database/TableBase.cs @@ -0,0 +1,56 @@ +using System; +using System.CodeDom; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Common.Interfaces; + + +/* 2018-Dec-22: This is a started refactoring of the Database namespace into separate derived classes + * from a generic that contains most of the gruntwork. It is not nearly complete and + * the code is still experimental, not taken into production yet. + */ + + +namespace Swarmops.Database +{ + public class TableBase where TBasic: IDatabaseColumnAware, new() + { + public TBasic GetRecordByIdentity(int identity, bool aggressive = false) + { + throw new NotImplementedException(); + /* + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand("SELECT" + paymentFieldSequence + + "WHERE PaymentId=" + paymentId, + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return new TBasic().FromDataReader(reader); + } + + throw new ArgumentException("No such PaymentId:" + paymentId); + } + }*/ + } + + public TBasic[] GetRecordsByIdentity(int[] identities) + { + throw new NotImplementedException(); + } + + public TBasic[] GetRecords(params object[] conditions) + { + throw new NotImplementedException(); + } + } +}