Ejemplo n.º 1
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);
        }