/// <summary>
        /// Create a new consumer.
        /// </summary>
        /// <returns>The ConsumerId of the consumer.</returns>
        private Guid CreateConsumer()
        {
            DataModel            dataModel            = new DataModel();
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            Guid       userId         = TradingSupport.UserId;
            Guid       tenantId       = PersistenceHelper.GetTenantForEntity(DataModelTransaction.Current, this.Record.Blotter);
            Guid       entityId       = Guid.NewGuid();
            Guid       consumerId     = Guid.NewGuid();
            Guid       creditCardId   = Guid.NewGuid();
            Guid       workingOrderId = Guid.NewGuid();
            CountryRow country;
            Guid       countryId;
            Guid?      provinceId = null;
            EntityRow  dollars;
            Guid       dollarsId;
            TypeRow    type;
            Guid       typeId;
            ImageRow   image;
            Guid       imageId;
            DateTime   currentUTCTime = DateTime.UtcNow;

            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);

            image = TradingSupport.FindImageByKey(
                this.Record.ConfigurationId,
                "FK_Image_Entity",
                new object[] { "OBJECT" });
            imageId = image.ImageId;
            image.ReleaseReaderLock(dataModelTransaction.TransactionId);

            type = TradingSupport.FindTypeByKey(
                this.Record.ConfigurationId,
                "FK_Type_Entity",
                new object[] { "CONSUMER TRUST" });
            typeId = type.TypeId;
            type.ReleaseReaderLock(dataModelTransaction.TransactionId);

            dataModel.CreateEntity(
                currentUTCTime,
                null,
                entityId,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                this.Record.CustomerCode,
                imageId,
                false,
                false,
                currentUTCTime,
                this.Record.SavingsEntityCode,
                tenantId,
                typeId);

            dataModel.CreateSecurity(
                null,
                countryId,
                null,
                null,
                null,
                1,
                1,
                entityId,
                this.Record.AccountCode,
                tenantId,
                VolumeCategoryMap.FromCode(VolumeCategory.Unknown));

            dataModel.CreateConsumer(
                this.Record.Address1,
                this.Record.Address2,
                this.Record.BankAccountNumber,
                this.Record.BankRoutingNumber,
                this.Record.City,
                consumerId,
                this.Record.DateOfBirth != null ? (object)this.Record.DateOfBirth.Value : null,
                null,
                null,
                this.Record.FirstName,
                this.Record.IsEmployed != null ? (object)this.Record.IsEmployed.Value : null,
                this.Record.LastName,
                this.Record.MiddleName,
                StringUtilities.CleanUpAlphaNumericString(this.Record.PhoneNumber),
                this.Record.PostalCode,
                provinceId,
                this.Record.Salutation,
                StringUtilities.CleanUpAlphaNumericString(this.Record.SocialSecurityNumber),
                this.Record.Suffix);
            dataModel.CreateConsumerTrust(
                consumerId,
                entityId,
                null,
                null,
                this.Record.SavingsAccount,
                this.Record.SavingsBalance,
                this.Record.Tag,
                tenantId,
                this.Record.VendorCode);

            //If this not found, there will be an exception. Let the exception propagate up.
            dataModel.CreateWorkingOrder(
                null,
                this.Record.Blotter,
                currentUTCTime,
                userId,
                CrossingMap.FromCode(Crossing.AlwaysMatch),
                null,
                null,
                null,
                null,
                true,
                true,
                true,
                null,
                DateTime.UtcNow,
                TradingSupport.UserId,
                OrderTypeMap.FromCode(OrderType.Market),
                entityId,
                DateTime.UtcNow,
                dollarsId,
                SideMap.FromCode(Side.Sell),
                DateTime.UtcNow,
                StatusMap.FromCode(Status.New),
                null,
                null,
                null,
                null,
                TimeInForceMap.FromCode(TimeInForce.GoodTillCancel),
                DateTime.UtcNow,
                DateTime.UtcNow,
                workingOrderId);

            // Create the access control record for this new entity.
            dataModel.CreateAccessControl(
                Guid.NewGuid(),
                AccessRightMap.FromCode(AccessRight.FullControl),
                entityId,
                userId,
                tenantId);

            return(consumerId);
        }
        /// <summary>
        /// Create a new Debt Holder Record
        /// </summary>
        /// <returns></returns>
        internal Guid Create()
        {
            DataModel            dataModel            = new DataModel();
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;
            Guid       userId   = TradingSupport.UserId;
            Guid       tenantId = PersistenceHelper.GetTenantForEntity(dataModelTransaction, this.Record.Blotter);
            Guid       entityId = Guid.Empty;
            Guid       consumerId;
            Guid       creditCardId;
            Guid       workingOrderId;
            CountryRow country;
            Guid       countryId;
            Guid?      provinceId = null;
            TypeRow    type;
            Guid       typeId;
            ImageRow   image;
            Guid       imageId;

            // 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;
            EntityRow dollars;
            Guid      dollarsId;

            // We need write access to the containing blotter in order to add a record to it.
            if (!DataModelFilters.HasAccess(dataModelTransaction, userId, this.Record.Blotter, AccessRight.Write))
            {
                throw new SecurityException("Current user does not have write access to the selected blotter");
            }

            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);

            image = TradingSupport.FindImageByKey(
                this.Record.ConfigurationId,
                "FK_Image_Entity",
                new object[] { "OBJECT" });
            imageId = image.ImageId;
            image.ReleaseReaderLock(dataModelTransaction.TransactionId);

            type = TradingSupport.FindTypeByKey(
                this.Record.ConfigurationId,
                "FK_Type_Entity",
                new object[] { "CONSUMER DEBT" });
            typeId = type.TypeId;
            type.ReleaseReaderLock(dataModelTransaction.TransactionId);

            entityId       = Guid.NewGuid();
            consumerId     = Guid.NewGuid();
            creditCardId   = Guid.NewGuid();
            workingOrderId = Guid.NewGuid();

            dataModel.CreateEntity(
                createdTime,
                null,
                entityId,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                this.Record.AccountCode,
                imageId,
                false,
                false,
                modifiedTime,
                this.Record.OriginalAccountNumber,
                tenantId,
                typeId);

            dataModel.CreateSecurity(
                null,
                countryId,
                null,
                null,
                null,
                1,
                1,
                entityId,
                this.Record.AccountCode,
                tenantId,
                VolumeCategoryMap.FromCode(VolumeCategory.Unknown));

            dataModel.CreateConsumer(
                this.Record.Address1,
                this.Record.Address2,
                null,
                null,
                this.Record.City,
                consumerId,
                this.Record.DateOfBirth != null ? (object)this.Record.DateOfBirth.Value : null,
                null,
                null,
                this.Record.FirstName,
                null,
                this.Record.LastName,
                this.Record.MiddleName,
                StringUtilities.CleanUpAlphaNumericString(this.Record.PhoneNumber),
                this.Record.PostalCode,
                provinceId,
                null,
                StringUtilities.CleanUpAlphaNumericString(this.Record.SocialSecurityNumber),
                this.Record.Suffix);
            dataModel.CreateCreditCard(
                this.Record.AccountBalance,
                this.Record.AccountCode,
                consumerId,
                creditCardId,
                this.Record.DebtHolder,
                null,
                null,
                this.Record.AccountCode,
                StringUtilities.CleanUpAlphaNumericString(this.Record.OriginalAccountNumber),
                tenantId);
            dataModel.CreateConsumerDebt(
                this.Record.CollectionDate,
                entityId,
                consumerId,
                creditCardId,
                this.Record.DateOfDelinquency != null ? (object)this.Record.DateOfDelinquency.Value : null,
                null,
                null,
                this.Record.Representative,
                this.Record.Tag,
                tenantId,
                this.Record.VendorCode);

            dataModel.CreateWorkingOrder(
                null,
                this.Record.Blotter,
                createdTime,
                createdUserId,
                CrossingMap.FromCode(Crossing.AlwaysMatch),
                null,
                null,
                null,
                null,
                true,
                true,
                true,
                null,
                modifiedTime,
                modifiedUserId,
                OrderTypeMap.FromCode(OrderType.Market),
                entityId,
                createdTime,
                dollarsId,
                SideMap.FromCode(Side.Sell),
                createdTime,
                StatusMap.FromCode(Status.New),
                null,
                null,
                null,
                null,
                TimeInForceMap.FromCode(TimeInForce.GoodTillCancel),
                createdTime,
                createdTime,
                workingOrderId);

            // Create the access control record for this new entity.
            dataModel.CreateAccessControl(
                Guid.NewGuid(),
                AccessRightMap.FromCode(AccessRight.FullControl),
                entityId,
                userId,
                tenantId);

            return(entityId);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create most of a debt class. This should be kicked off by children of DebtClass.
        /// </summary>
        /// <param name="record">The record, complete with TypeId and ImageId.</param>
        /// <returns>The entityId of the new debt class.</returns>
        public override Guid Create(DebtClass record)
        {
            DataModel dataModel = new DataModel();
            TypeRow   typeRow   = DataModel.Type.TypeKey.Find(record.TypeId.Value);
            EntityRow parentEntity;

            AssetViewerTemplateRow[] templates;
            Guid     entityId       = Guid.Empty;
            Guid     organizationId = Guid.Empty;
            DateTime createdTime    = DateTime.UtcNow;

            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;

            Guid ruleId = Guid.NewGuid();

            entityId = Guid.NewGuid();

            if (record.ParentId == null)
            {
                throw new FaultException <ArgumentFault>(new ArgumentFault("Parent EntityId not specified"), "Parent EntityId not specified");
            }
            if (!DataModelFilters.HasAccess(dataModelTransaction, TradingSupport.UserId, record.ParentId.Value, AccessRight.Write))
            {
                throw new FaultException <SecurityFault>(
                          new SecurityFault("The current user doesn't have write permission to the specified parent"),
                          "The current user doesn't have write permission to the specified parent");
            }

            parentEntity = DataModel.Entity.EntityKey.Find(record.ParentId.Value);

            typeRow.AcquireReaderLock(dataModelTransaction);
            templates = typeRow.GetAssetViewerTemplateRows();
            if (record.ImageId == null && !typeRow.IsImageIdNull())
            {
                record.ImageId = typeRow.ImageId;
            }
            typeRow.ReleaseReaderLock(dataModelTransaction.TransactionId);

            if (record.ImageId == null)
            {
                ImageRow imageRow = DataModel.Image[0];
                imageRow.AcquireReaderLock(dataModelTransaction);
                record.ImageId = imageRow.ImageId;
                imageRow.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            parentEntity.AcquireReaderLock(dataModelTransaction.TransactionId, DataModel.LockTimeout);
            try
            {
                record.Name = this.GetUniqueName(dataModelTransaction, parentEntity, record.Name as String);
            }
            finally
            {
                parentEntity.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }


            dataModel.CreateEntity(
                createdTime,
                record.Description,
                entityId,
                Guid.NewGuid().ToString(),
                null,
                null,
                null,
                null,
                null,
                null,
                Guid.NewGuid().ToString(),
                record.ImageId.Value,
                false,
                false,
                createdTime,
                record.Name,
                record.TenantId.GetValueOrDefault(),
                record.TypeId.Value);

            dataModel.CreateEntityTree(entityId, Guid.NewGuid(), null, record.ParentId.Value);

            dataModel.CreateBlotter(entityId, PartyTypeMap.FromCode(PartyType.UseParent));

            foreach (AssetViewerTemplateRow template in templates)
            {
                template.AcquireReaderLock(dataModelTransaction);

                dataModel.CreateBlotterConfiguration(Guid.NewGuid(), entityId, null, template.ReportId, template.ReportTypeId);

                template.ReleaseReaderLock(dataModelTransaction.TransactionId);
            }

            dataModel.CreateDebtClass(
                record.Address1,
                record.Address2,
                record.BankAccountNumber,
                record.BankRoutingNumber,
                record.City,
                null,
                record.CompanyName,
                record.ContactName,
                entityId,
                record.DebtRuleId,
                record.Department,
                record.Email,
                record.Fax,
                record.ForBenefitOf,
                record.Phone,
                record.PostalCode,
                record.Province,
                null);

            organizationId = this.FindOrganization(dataModelTransaction);
            Guid tenantId = record.TenantId.HasValue ? record.TenantId.GetValueOrDefault() : organizationId;

            PersistenceHelper.AddGroupPermissions(dataModel, dataModelTransaction, organizationId, entityId, tenantId);
            dataModel.CreateAccessControl(
                Guid.NewGuid(),
                AccessRightMap.FromCode(AccessRight.FullControl),
                entityId,
                TradingSupport.UserId,
                tenantId);

            return(entityId);
        }
        /// <summary>
        /// Add default permissions to an object.
        /// </summary>
        /// <param name="dataModel">The data model.</param>
        /// <param name="transaction">The current transaction.</param>
        /// <param name="organizationId">The TenentId of the organization where the scan should start (eg. where the entity is).</param>
        /// <param name="entityId">The entityId of the object to modify.</param>
        ///<param name="tenantId">The root TenantId for which all the records will be created for</param>
        public static void AddGroupPermissions(DataModel dataModel, DataModelTransaction transaction, Guid organizationId, Guid entityId, Guid tenantId)
        {
            TenantRow organizationRow = DataModel.Tenant.TenantKey.Find(organizationId);

            RightsHolderRow[] rightsHolders;
            TenantTreeRow[]   tenantTreeRows;

            if (organizationRow == null)
            {
                throw new FaultException <RecordNotFoundFault>(
                          new RecordNotFoundFault("Organization", new object[] { organizationId }),
                          "The organization has been deleted.");
            }

            organizationRow.AcquireReaderLock(transaction);
            tenantTreeRows = organizationRow.GetTenantTreeRowsByFK_Tenant_TenantTree_ChildId();
            rightsHolders  = organizationRow.GetRightsHolderRows();
            organizationRow.ReleaseLock(transaction.TransactionId);

            foreach (RightsHolderRow rightsHolderRow in rightsHolders)
            {
                GroupRow[] groupRows;

                rightsHolderRow.AcquireReaderLock(transaction);
                groupRows = rightsHolderRow.GetGroupRows();
                rightsHolderRow.ReleaseLock(transaction.TransactionId);

                if (groupRows.Length > 0)
                {
                    Guid         groupId;
                    GroupRow     group = groupRows[0];
                    GroupTypeRow groupType;

                    group.AcquireReaderLock(transaction);
                    groupId   = group.GroupId;
                    groupType = group.GroupTypeRow;
                    group.ReleaseReaderLock(transaction.TransactionId);

                    groupType.AcquireReaderLock(transaction);
                    if (groupType.GroupTypeCode == GroupType.ExchangeAdmin ||
                        groupType.GroupTypeCode == GroupType.FluidTradeAdmin ||
                        groupType.GroupTypeCode == GroupType.SiteAdmin)
                    {
                        dataModel.CreateAccessControl(
                            Guid.NewGuid(),
                            AccessRightMap.FromCode(AccessRight.FullControl),
                            entityId,
                            groupId,
                            tenantId);
                    }
                }
            }

            foreach (TenantTreeRow tenantTreeRow in tenantTreeRows)
            {
                Guid parentId;

                tenantTreeRow.AcquireReaderLock(transaction);
                parentId = tenantTreeRow.ParentId;
                tenantTreeRow.ReleaseLock(transaction.TransactionId);

                AddGroupPermissions(dataModel, transaction, parentId, entityId, tenantId);
            }
        }