Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override void Update(Consumer record)
        {
            // These variables are used for auditing the changes to this record.
            DateTime createdTime    = DateTime.UtcNow;
            Guid     createdUserId  = TradingSupport.UserId;
            DateTime modifiedTime   = createdTime;
            Guid     modifiedUserId = createdUserId;

            DataModel            dataModel            = new DataModel();
            DataModelTransaction datamodelTransaction = DataModelTransaction.Current;

            if (record.RowId == null || DataModel.Consumer.ConsumerKey.Find(record.RowId) == null)
            {
                record.RowId = Create(record);
            }

            dataModel.UpdateConsumer(
                record.Address1,
                record.Address2,
                null,
                null,
                record.City,
                null,
                new object[] { record.RowId },
                record.DateOfBirth,
                record.ExternalId0,
                record.ExternalId1,
                record.FirstName,
                record.IsEmployed,
                record.LastName,
                record.MiddleName,
                record.PhoneNumber,
                record.PostalCode,
                record.ProvinceId,
                record.RowVersion,
                record.Salutation,
                record.SocialSecurityNumber,
                record.Suffix);

            //If a working order is given, Update modifyTime.
            Guid workingOrderId = record.WorkingOrderId.GetValueOrDefault(Guid.Empty);

            if (workingOrderId != Guid.Empty)
            {
                WorkingOrderPersistence workingOrderPersistence = new WorkingOrderPersistence();
                workingOrderPersistence.UpdateModifyTime(workingOrderId);
            }

            //If a working order is given, Update modifyTime.
            //Guid matchId = record.MatchId.GetValueOrDefault(Guid.Empty);
            //if (matchId != Guid.Empty)
            //{
            //    MatchPersistence matchPersistence = new MatchPersistence();
            //    matchPersistence.UpdateModifyTime(workingOrderId);
            //}
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Delete a debt holder
        /// </summary>
        /// <returns>True for sucess</returns>
        public override ErrorCode Delete(DebtNegotiator record)
        {
            DataModel               dataModel               = new DataModel();
            DataModelTransaction    transaction             = DataModelTransaction.Current;
            WorkingOrderPersistence workingOrderPersistence = new WorkingOrderPersistence();
            DebtRulePersistence     debtRulePersistence     = new DebtRulePersistence();
            DebtNegotiatorRow       debtNegotiatorRow       = DataModel.DebtNegotiator.DebtNegotiatorKey.Find(record.RowId);
            // If/when we get these translations, we'll need this stuff.
            //DebtNegotiatorImportTranslationRow debtNegotiatorImportTranslationRow;
            DebtClassRow debtClassRow;
            BlotterRow   blotterRow;

            WorkingOrderRow[] workingOrderRows;
            DebtRuleMapRow[]  debtRuleMapRows;
            EntityRow         entityRow;

            EntityTreeRow[] children;
            Guid            entityId;
            Int64           entityRowVersion;

            //Guid debtNegotiatorImportTranslationId = Guid.Empty;
            //Int64 debtNegotiatorImportTranslationRowVersion = 0;

            if (record.RowId == null || debtNegotiatorRow == null)
            {
                return(ErrorCode.RecordNotFound);
            }
            if (!DataModelFilters.HasAccess(transaction, TradingSupport.UserId, record.RowId, AccessRight.Write))
            {
                return(ErrorCode.AccessDenied);
            }

            debtNegotiatorRow.AcquireReaderLock(transaction);
            debtClassRow = debtNegotiatorRow.DebtClassRow;
            //debtNegotiatorImportTranslationRow = debtNegotiatorRow.DebtNegotiatorImportTranslationRow;
            debtNegotiatorRow.ReleaseReaderLock(transaction.TransactionId);

#if false   // If/when we get these translations, we'll need this stuff.
            if (debtHolderImportTranslationRow != null)
            {
                debtNegotiatorImportTranslationRow.AcquireReaderLock(transaction);
                debtNegotiatorImportTranslationId         = debtNegotiatorImportTranslationRow.DebtNegotiatorImportTranslationId;
                debtNegotiatorImportTranslationRowVersion = debtNegotiatorImportTranslationRow.RowVersion;
                debtNegotiatorImportTranslationRow.ReleaseReaderLock(transaction.TransactionId);
            }
#endif

            debtClassRow.AcquireReaderLock(transaction);
            blotterRow      = debtClassRow.BlotterRow;
            debtRuleMapRows = debtClassRow.GetDebtRuleMapRows();
            debtClassRow.ReleaseReaderLock(transaction.TransactionId);

            blotterRow.AcquireReaderLock(transaction);
            entityRow        = blotterRow.EntityRow;
            workingOrderRows = blotterRow.GetWorkingOrderRows();
            blotterRow.ReleaseLock(transaction.TransactionId);

            entityRow.AcquireReaderLock(transaction);
            children         = entityRow.GetEntityTreeRowsByFK_Entity_EntityTree_ParentId();
            entityId         = entityRow.EntityId;
            entityRowVersion = entityRow.RowVersion;
            entityRow.ReleaseReaderLock(transaction.TransactionId);

            // Fail if the debt class has any children.
            if (children.Length != 0)
            {
                return(ErrorCode.AccessDenied);
            }

            // Destroy the import translation.
            //if (debtHolderImportTranslationRow != null)
            //	dataModel.DestroyDebtNegotiatorImportTranslation(new object[] { debtNegotiatorImportTranslationId }, debtNegotiatorImportTranslationRowVersion);

            // Delete any rules this debt class may own.
            foreach (DebtRuleMapRow debtRuleMapRow in debtRuleMapRows)
            {
                DebtRuleRow debtRuleRow;

                debtRuleMapRow.AcquireReaderLock(transaction);
                debtRuleRow = debtRuleMapRow.DebtRuleRow;
                debtRuleMapRow.ReleaseReaderLock(transaction.TransactionId);

                debtRulePersistence.DeleteRow(dataModel, transaction, debtRuleRow);
            }

            // Delete any working orders this debt class may contain.
            foreach (WorkingOrderRow workingOrderRow in workingOrderRows)
            {
                workingOrderPersistence.DeleteRow(dataModel, transaction, workingOrderRow);
            }

            // Delete the entity itself.
            dataModel.DestroyEntity(new object[] { entityId }, entityRowVersion);

            return(ErrorCode.Success);
        }
        /// <summary>
        /// Update a consumer.
        /// </summary>
        /// <param name="entity">The ConsumerTrust's Entity row.</param>
        /// <returns>The ConsumerId of the Consumer row.</returns>
        private Guid UpdateConsumer(EntityRow entity)
        {
            DataModel            dataModel            = new DataModel();
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            DateTime             modified             = DateTime.UtcNow;
            CountryRow           country;
            Guid             countryId;
            Guid?            provinceId = null;
            EntityRow        dollars;
            Guid             dollarsId;
            ConsumerRow      consumer      = null;
            ConsumerTrustRow consumerTrust = null;
            SecurityRow      security      = null;
            WorkingOrderRow  workingOrder  = null;

            MatchRow[] matches;
            Guid       consumerId;
            Guid       consumerTrustId;
            Guid       entityId;
            Guid       securityId;
            Guid       workingOrderId;
            Int64      consumerVersion;
            Int64      consumerTrustVersion;
            Int64      entityVersion;
            Int64      securityVersion;
            Int64      workingOrderVersion;
            Boolean    updateConsumer      = false;
            Boolean    updateConsumerTrust = false;
            Boolean    updateEntity        = false;
            Boolean    updateSecurity      = false;

            country = TradingSupport.FindCountryByKey(
                this.Record.ConfigurationId,
                "FK_Country_Security",
                new object[] { this.Record.CountryCode });
            countryId = country.CountryId;
            country.ReleaseReaderLock(dataModelTransaction.TransactionId);

            if (this.Record.ProvinceCode != null)
            {
                ProvinceRow province = TradingSupport.FindProvinceByKey(
                    this.Record.ConfigurationId,
                    "FK_Province_Consumer",
                    new object[] { this.Record.ProvinceCode });
                provinceId = province.ProvinceId;
                province.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            dollars = TradingSupport.FindEntityByKey(
                this.Record.ConfigurationId,
                "FK_Security_WorkingOrder_SettlementId",
                new object[] { this.Record.Currency });
            dollarsId = dollars.EntityId;
            dollars.ReleaseReaderLock(dataModelTransaction.TransactionId);

            try
            {
                entity.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                entityId      = entity.EntityId;
                entityVersion = entity.RowVersion;
                if (TradingSupport.IsColumnOld(entity, "Name", this.Record.OriginalAccountNumber))
                {
                    updateEntity = true;
                }
            }
            finally
            {
                entity.ReleaseLock(dataModelTransaction.TransactionId);
            }

            try
            {
                security = DataModel.Security.SecurityKey.Find(entityId);
                security.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                securityId      = entityId;
                securityVersion = security.RowVersion;
                workingOrder    = security.GetWorkingOrderRowsByFK_Security_WorkingOrder_SecurityId()[0];
                if (TradingSupport.IsColumnOld(security, "CountryId", countryId))
                {
                    updateSecurity = true;
                }
            }
            finally
            {
                security.ReleaseLock(dataModelTransaction.TransactionId);
            }


            // Control the working order:
            workingOrder.AcquireWriterLock(dataModelTransaction);

            try
            {
                consumerTrust = DataModel.ConsumerTrust.ConsumerTrustKey.Find(entityId);
                consumerTrust.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                consumerTrustId      = consumerTrust.ConsumerTrustId;
                consumerTrustVersion = consumerTrust.RowVersion;
                consumer             = DataModel.Consumer.ConsumerKey.Find(consumerTrust.ConsumerId);
                if (TradingSupport.IsColumnOld(consumerTrust, "SavingsAccount", this.Record.SavingsAccount) ||
                    TradingSupport.IsColumnOld(consumerTrust, "SavingsBalance", this.Record.SavingsBalance) ||
                    TradingSupport.IsColumnOld(consumerTrust, "Tag", this.Record.Tag))
                {
                    updateConsumerTrust = true;
                }
            }
            finally
            {
                consumerTrust.ReleaseLock(dataModelTransaction.TransactionId);
            }

            try
            {
                consumer.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                consumerId      = consumer.ConsumerId;
                consumerVersion = consumer.RowVersion;
                if (TradingSupport.IsColumnOld(consumer, "Address1", this.Record.Address1) ||
                    TradingSupport.IsColumnOld(consumer, "Address2", this.Record.Address2) ||
                    TradingSupport.IsColumnOld(consumer, "BankAccountNumber", this.Record.BankAccountNumber) ||
                    TradingSupport.IsColumnOld(consumer, "BankRoutingNumber", this.Record.BankRoutingNumber) ||
                    TradingSupport.IsColumnOld(consumer, "City", this.Record.City) ||
                    TradingSupport.IsColumnOld(consumer, "DateOfBirth", this.Record.DateOfBirth) ||
                    TradingSupport.IsColumnOld(consumer, "FirstName", this.Record.FirstName) ||
                    TradingSupport.IsColumnOld(consumer, "IsEmployed", this.Record.IsEmployed) ||
                    TradingSupport.IsColumnOld(consumer, "LastName", this.Record.LastName) ||
                    TradingSupport.IsColumnOld(consumer, "PostalCode", this.Record.PostalCode) ||
                    TradingSupport.IsColumnOld(consumer, "MiddleName", this.Record.MiddleName) ||
                    TradingSupport.IsColumnOld(consumer, "PhoneNumber", this.Record.PhoneNumber) ||
                    TradingSupport.IsColumnOld(consumer, "ProvinceId", provinceId) ||
                    TradingSupport.IsColumnOld(consumer, "SocialSecurityNumber", this.Record.SocialSecurityNumber) ||
                    TradingSupport.IsColumnOld(consumer, "Suffix", this.Record.Suffix))
                {
                    updateConsumer = true;
                }
            }
            finally
            {
                consumer.ReleaseLock(dataModelTransaction.TransactionId);
            }

            try
            {
                //workingOrder.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
                workingOrderId      = workingOrder.WorkingOrderId;
                workingOrderVersion = workingOrder.RowVersion;
                matches             = workingOrder.GetMatchRows();
            }
            finally
            {
                //workingOrder.ReleaseLock(dataModelTransaction.TransactionId);
            }

            foreach (MatchRow match in matches)
            {
                if (WorkingOrderPersistence.IsSettled(dataModelTransaction, match))
                {
                    throw new FaultException <SecurityFault>(
                              new SecurityFault("Cannot update account that is settled")
                    {
                        FaultCode = ErrorCode.RecordExists
                    },
                              "Cannot update account that is settled");
                }
            }

            // We need write access to the containing blotter in order to add a record to it.
            if (!TradingSupport.HasAccess(dataModelTransaction, PersistenceHelper.GetBlotterForConsumer(dataModelTransaction, consumerId), AccessRight.Write))
            {
                throw new SecurityException("Current user does not have write access to the original blotter");
            }

            if (updateConsumer)
            {
                dataModel.UpdateConsumer(
                    this.Record.Address1 != null ? (object)this.Record.Address1 : DBNull.Value,
                    this.Record.Address2 != null ? (object)this.Record.Address2 : DBNull.Value,
                    this.Record.BankAccountNumber != null ? (object)this.Record.BankAccountNumber : DBNull.Value,
                    this.Record.BankRoutingNumber != null ? (object)this.Record.BankRoutingNumber : DBNull.Value,
                    this.Record.City != null ? (object)this.Record.City : DBNull.Value,
                    consumerId,
                    new object[] { consumerId },
                    this.Record.DateOfBirth != null ? (object)this.Record.DateOfBirth.Value : DBNull.Value,
                    null,
                    null,
                    this.Record.FirstName != null ? (object)this.Record.FirstName : DBNull.Value,
                    this.Record.IsEmployed != null ? (object)this.Record.IsEmployed.Value : DBNull.Value,
                    this.Record.LastName != null ? (object)this.Record.LastName : DBNull.Value,
                    this.Record.MiddleName != null ? (object)this.Record.MiddleName : DBNull.Value,
                    this.Record.PhoneNumber != null ? (object)StringUtilities.CleanUpAlphaNumericString(this.Record.PhoneNumber) : DBNull.Value,
                    this.Record.PostalCode != null ? (object)this.Record.PostalCode : DBNull.Value,
                    provinceId,
                    consumerVersion,
                    this.Record.Salutation,
                    StringUtilities.CleanUpAlphaNumericString(this.Record.SocialSecurityNumber),
                    this.Record.Suffix != null ? (object)this.Record.Suffix : DBNull.Value);
            }

            if (updateConsumerTrust)
            {
                dataModel.UpdateConsumerTrust(
                    null,
                    consumerTrustId,
                    new object[] { consumerTrustId },
                    null,
                    null,
                    consumerTrustVersion,
                    this.Record.SavingsAccount,
                    this.Record.SavingsBalance,
                    this.Record.Tag != null ? (object)this.Record.Tag : DBNull.Value,
                    null,
                    null);
            }

            if (updateEntity)
            {
                dataModel.UpdateEntity(
                    null,
                    null,
                    entityId,
                    new object[] { entityId },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    modified,
                    this.Record.SavingsEntityCode,
                    entityVersion,
                    null,
                    null);
            }

            if (updateSecurity)
            {
                dataModel.UpdateSecurity(
                    null,
                    countryId,
                    null,
                    null,
                    null,
                    1,
                    1,
                    securityVersion,
                    securityId,
                    new object[] { securityId },
                    null,
                    null,
                    null);
            }

            dataModel.UpdateWorkingOrder(
                null,
                this.Record.Blotter,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                modified,
                TradingSupport.UserId,
                null,
                workingOrderVersion,
                null,
                null,
                dollarsId,
                null,
                null,
                StatusMap.FromCode(Status.New),
                null,
                null,
                null,
                null,
                null,
                null,
                modified,
                workingOrderId,
                new object[] { workingOrderId });

            return(consumerId);
        }