/// <summary> /// Find the nearest ancestor DebtClass that has a value for a particular field. /// </summary> /// <param name="debtClassId">The DebtClassId of the debt class to start with.</param> /// <param name="field">The column to look at.</param> /// <returns>The row of the first debt class found to have a value for the indicated field (including the initial debt class).</returns> private static DebtHolderRow FindParentWithField(Guid debtClassId, DataColumn field) { DebtHolderRow parent = null; DebtHolderRow debtHolder = DataModel.DebtHolder.DebtHolderKey.Find(debtClassId); Guid typeId = DataModel.Entity.EntityKey.Find(debtClassId).TypeId; while (debtHolder != null && parent == null) { EntityRow child = DataModel.Entity.EntityKey.Find(debtHolder.DebtHolderId); EntityTreeRow[] parents = child.GetEntityTreeRowsByFK_Entity_EntityTree_ChildId(); if (!debtHolder.IsNull(field)) { parent = debtHolder; } if (parents.Length != 0) { debtHolder = DataModel.DebtHolder.DebtHolderKey.Find(parents[0].ParentId); } else { debtHolder = null; } } return(parent); }
/// <summary> /// Load debt holder specific information. /// </summary> protected override void FinishLoad() { DebtHolderRow debtHolderRow = DataModel.DebtHolder.DebtHolderKey.Find(this.EntityId); base.FinishLoad(); this.rowVersion = debtHolderRow.RowVersion; }
/// <summary> /// Find the effective (inherited) value of a column in a debt class. /// </summary> /// <typeparam name="T">The type of the column.</typeparam> /// <param name="debtClassId">The DebtClassId of the debt class in question.</param> /// <param name="field">The column to retrieve.</param> /// <returns>Effective value of the indicated column, eg. the value in the debt class with the indicated ID, or, if that debt class has no /// value for the column, the value of the column in nearest ancestor debt class that has a value for that column. If no value can be found, /// returns null.</returns> private static object FindEffectiveField <T>(Guid debtClassId, DataColumn field) { object value = null; lock (DataModel.SyncRoot) { DebtHolderRow parent = DebtHolder.FindParentWithField(debtClassId, field); if (parent != null) { value = (object)parent.Field <T>(field); } } return(value); }
/// <summary> /// Delete a debt holder /// </summary> /// <returns>True for sucess</returns> public override ErrorCode Delete(DebtHolder record) { DataModel dataModel = new DataModel(); DataModelTransaction transaction = DataModelTransaction.Current; WorkingOrderPersistence workingOrderPersistence = new WorkingOrderPersistence(); DebtRulePersistence debtRulePersistence = new DebtRulePersistence(); DebtHolderRow debtHolderRow = DataModel.DebtHolder.DebtHolderKey.Find(record.RowId); DebtHolderImportTranslationRow debtHolderImportTranslationRow; DebtClassRow debtClassRow; BlotterRow blotterRow; WorkingOrderRow[] workingOrderRows; DebtRuleMapRow[] debtRuleMapRows; EntityRow entityRow; EntityTreeRow[] children; Guid entityId; Int64 entityRowVersion; Guid debtHolderImportTranslationId = Guid.Empty; Int64 debtHolderImportTranslationRowVersion = 0; if (record.RowId == null || debtHolderRow == null) { return(ErrorCode.RecordNotFound); } if (!DataModelFilters.HasAccess(transaction, TradingSupport.UserId, record.RowId, AccessRight.Write)) { return(ErrorCode.AccessDenied); } debtHolderRow.AcquireReaderLock(transaction); debtClassRow = debtHolderRow.DebtClassRow; debtHolderImportTranslationRow = debtHolderRow.DebtHolderImportTranslationRow; debtHolderRow.ReleaseReaderLock(transaction.TransactionId); if (debtHolderImportTranslationRow != null) { debtHolderImportTranslationRow.AcquireReaderLock(transaction); debtHolderImportTranslationId = debtHolderImportTranslationRow.DebtHolderImportTranslationId; debtHolderImportTranslationRowVersion = debtHolderImportTranslationRow.RowVersion; debtHolderImportTranslationRow.ReleaseReaderLock(transaction.TransactionId); } 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.DestroyDebtHolderImportTranslation(new object[] { debtHolderImportTranslationId }, debtHolderImportTranslationRowVersion); } // 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); }