Hi Ivan,
The fields that I want to capture are:
- DocumentCode
- TransactionType
- ChangesDescription
- UpdateType
- TableSourceName
- UserCreated
- DateCreated
- UserModified
- DateModified
I´ve created a custom method the retreive Audit data, I´m not using SF Audit methods yet.
I´m currently using the Audit logic at the OnBeforeSave And OnBeforeDelete Events, I´m still having some problems.
1) In the OnBeforeSave Event the Object is not saved so if an error happens how can I stop the Auditing to be captured.
2) As the BO is not yet saved I can´t retrieve the PK generated for this Object.
3) If I put the Audit logic in the OnAfterSave then I do get the PK but can not log Modified BO´s because the state has changed.
4) I can´t Audit deleted BO´s
This are the methods I´m using on the Base BO
Private Sub AuditChanges(ByVal table As DataTable, ByVal transactionType As String, ByVal transactionDescription As String)
If (Not table.GetChanges Is Nothing) Then
Dim num4 As Integer = (table.Rows.Count - 1)
Dim i As Integer = 0
Do While (i <= num4)
Dim loTable As DataTable = table
If loTable.Rows.Item(i).RowState <> DataRowState.Unchanged Then
If (loTable.Rows.Item(i).RowState <> DataRowState.Deleted) Then
loTable.Rows.Item(i).BeginEdit()
If ((loTable.Rows.Item(i).RowState = DataRowState.Detached) OrElse (loTable.Rows.Item(i).RowState = DataRowState.Added)) Then
If (loTable.Columns.Contains("UserCreated")) Then
loTable.Rows.Item(i).Item("UserCreated") = MicroFour.StrataFrame.Security.SecurityBasics.CurrentUser.UserName
End If
If loTable.Columns.Contains("DateCreated") Then
loTable.Rows.Item(i).Item("DateCreated") = DateTime.Now
End If
End If
If loTable.Columns.Contains("UserModified") Then
loTable.Rows.Item(i).Item("UserModified") = MicroFour.StrataFrame.Security.SecurityBasics.CurrentUser.UserName
End If
If loTable.Columns.Contains("DateModified") Then
loTable.Rows.Item(i).Item("DateModified") = DateTime.Now
End If
loTable.Rows.Item(i).EndEdit()
End If
If (table.TableName <> "AuditTrail") Then
Me.CreateAuditTrail(loTable.Rows.Item(i), transactionType, transactionDescription)
End If
loTable = Nothing
End If
i += 1
Loop
End If
End Sub
Private Sub CreateAuditTrail(ByVal rowValue As DataRow, ByVal transactionType As String, ByVal transactionDescription As String)
If Me.EnableAuditTrail Then
Using bo As New AuditTrailBO
bo.NewRow()
Dim current As DataRowVersion = DataRowVersion.Current
Dim builder As New StringBuilder
If (rowValue.RowState = DataRowState.Deleted) Then
current = DataRowVersion.Original
End If
bo.CurrentRow.BeginEdit()
If rowValue.Table.Columns.Contains("Counter") Then
bo.CurrentRow.Item("DocumentID") = System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(rowValue.Item("Counter", current))
End If
Dim num2 As Integer = (rowValue.Table.PrimaryKey.Length - 1)
Dim i As Integer = 0
Do While (i <= num2)
If (i = 0) Then
builder.Append(rowValue.Item(rowValue.Table.PrimaryKey(i).ColumnName, current).ToString)
Else
builder.Append(("," & rowValue.Item(rowValue.Table.PrimaryKey(i).ColumnName, current).ToString))
End If
i += 1
Loop
bo.CurrentRow.Item("AuditTrailGUID") = Guid.NewGuid
bo.CurrentRow.Item("DocumentCode") = builder.ToString
bo.CurrentRow.Item("TransactionType") = transactionType
If String.IsNullOrEmpty(transactionDescription) Then
bo.CurrentRow.Item("TransactionDescription") = GetCurrentDescription(rowValue, True)
Else
bo.CurrentRow.Item("TransactionDescription") = transactionDescription
End If
bo.CurrentRow.Item("UpdateAction") = rowValue.RowState.ToString
bo.CurrentRow.Item("TableSourceName") = rowValue.Table.TableName
bo.CurrentRow.Item("UserCreated") = MicroFour.StrataFrame.Security.SecurityBasics.CurrentUser.UserName
bo.CurrentRow.Item("DateCreated") = DateTime.Now
bo.CurrentRow.Item("UserModified") = MicroFour.StrataFrame.Security.SecurityBasics.CurrentUser.UserName
bo.CurrentRow.Item("DateModified") = DateTime.Now
bo.CurrentRow.EndEdit()
bo.Save()
End Using
End If
End Sub
Private Function GetCurrentDescription(ByVal row As DataRow, ByVal formatVertical As Boolean) As String
Dim builder As New StringBuilder
Dim column As DataColumn
For Each column In row.Table.Columns
If (row.RowState = DataRowState.Deleted) Then
If formatVertical Then
builder.AppendLine(String.Concat(New String() {"[-]", "", column.ColumnName, ": ", row.Item(column, DataRowVersion.Original).ToString}))
Else
builder.Append(String.Concat(New String() {"[-]", " ", column.ColumnName, ": ", row.Item(column, DataRowVersion.Original).ToString, ChrW(9)}))
End If
Continue For
End If
If (row.RowState = DataRowState.Added) Then
If formatVertical Then
builder.AppendLine(String.Concat(New String() {"[+]", " ", column.ColumnName, ": ", row.Item(column, DataRowVersion.Current).ToString}))
Else
builder.Append(String.Concat(New String() {"[+]", " ", column.ColumnName, ": ", row.Item(column, DataRowVersion.Current).ToString, ChrW(9)}))
End If
Continue For
End If
If ((row.RowState = DataRowState.Modified) Or (row.RowState = DataRowState.Unchanged)) Then
If Not row.Item(column, DataRowVersion.Original).ToString.Equals(row.Item(column, DataRowVersion.Current).ToString) Then
If formatVertical Then
builder.AppendLine(String.Concat(New String() {"[*]", " ", column.ColumnName, ": ", row.Item(column, DataRowVersion.Original).ToString, " -> ", row.Item(column, DataRowVersion.Current).ToString}))
Else
builder.Append(String.Concat(New String() {"[*]", " ", column.ColumnName, ": ", row.Item(column, DataRowVersion.Original).ToString, " -> ", row.Item(column, DataRowVersion.Current).ToString, ChrW(9)}))
End If
Continue For
End If
If formatVertical Then
builder.AppendLine((column.ColumnName & ": " & row.Item(column, DataRowVersion.Original).ToString))
Else
builder.Append((column.ColumnName & ": " & row.Item(column, DataRowVersion.Original).ToString & ChrW(9)))
End If
End If
Next
Return builder.ToString
End Function
Protected Overrides Sub OnBeforeSave(ByVal e As MicroFour.StrataFrame.Data.BeforeSaveUndoEventArgs)
MyBase.OnBeforeSave(e)
If Me.AuditDataChanges Then
If (Not e.Cancel) Then
Me.AuditChanges(Me.CurrentDataTable, Me.TableName, "")
End If
End If
End Sub
Protected Overrides Sub OnBeforeDelete(ByVal e As MicroFour.StrataFrame.Business.BeforeDeleteEventArgs)
MyBase.OnBeforeDelete(e)
If Me.AuditDataChanges Then
If (Not e.Cancel) Then
Me.AuditChanges(Me.CurrentDataTable, Me.TableName, "")
End If
End If
End Sub
I´ll keep searching the forum for SF Audit implementations
------------------------------------------------------------------------
I would like to change the world, but they don´t give me the source code.
MS Windows 7 Ultimate 64-Bit
Intel(R) Core(TM)2 Quad CPU Q9300 2.50 GHz
6.00 GB of RAM, NVIDIA GeForce 9800 GT
MacBook Pro i5 OSX Lion