Example #1
0
        public AuditEntry CreateAuditEntry(string auditOperation, string tableName, Dictionary <string, string> entityKeys)
        {
            this.LogMessage("Creating audit entry for operation:" + auditOperation + ", table:" + tableName + ", key1:" + entityKeys.First().Value);

            AuditEntry auditEntry = null;

            DataRow rowData = null;

            if (auditOperation != "D")
            {
                rowData = DataAbstractLayer.GetDataForRow(tableName, entityKeys);
            }
            if (rowData != null || auditOperation == "D")    // We might get null rowData here if the target row has been deleted
            {
                auditEntry = new AuditEntry();

                switch (auditOperation)
                {
                case "U":
                    auditEntry.type = "update";
                    break;

                case "I":
                    auditEntry.type = "insert";
                    break;

                case "D":
                    auditEntry.type = "delete";
                    break;
                }

                auditEntry.table = tableName;
                int pkCounter = 0; //primary key counter
                foreach (KeyValuePair <string, string> tmpKVP in entityKeys)
                {
                    //Need to get the Column Name, which is the Key
                    switch (pkCounter)
                    {
                    case 0: auditEntry.primaryKeyName0 = tmpKVP.Key; break;

                    case 1: auditEntry.primaryKeyName1 = tmpKVP.Key; break;

                    case 2: auditEntry.primaryKeyName2 = tmpKVP.Key; break;
                    }
                    pkCounter++;
                }

                if (auditOperation != "D")
                {
                    // Add data retrieved from database to the audit entry
                    foreach (DataColumn item in rowData.Table.Columns)
                    {
                        auditEntry.row.Add(item.ColumnName, rowData.IsNull(item.ColumnName) ? "dbnull" : rowData[item.ColumnName].ToString());
                    }
                }
                else
                {
                    // Add key data to the audit entry
                    foreach (KeyValuePair <string, string> tmpKVP in entityKeys)
                    {
                        auditEntry.row.Add(tmpKVP.Key, tmpKVP.Value);
                    }
                }
            }
            return(auditEntry);
        }
Example #2
0
        public void CreateAuditFiles(DateTime lastExportDate)
        {
            this.LogMessage("Audit File Creation Starting");

            List <AuditEntry> auditEntryList = new List <AuditEntry>();

            IDataReader tbAuditRecords = DataAbstractLayer.RecentAuditTable(lastExportDate);

            int rowCount = 0;

            while (tbAuditRecords.Read())
            {
                // Save the date from the last audit record
                this.lastAuditDate = tbAuditRecords.GetDateTime(tbAuditRecords.GetOrdinal("AuditDate"));

                string auditOperation = tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("Operation"));  // dr["Operation"].ToString();
                string auditEntity    = tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityName")); // dr["EntityName"].ToString();
                Dictionary <string, string> entityKeys = new Dictionary <string, string>();

                entityKeys.Add(primaryKeyNames[tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityName"))], tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityKey1")));
                //this relies on key count, not as flexible as rest of system

                if (!tbAuditRecords.IsDBNull(tbAuditRecords.GetOrdinal("EntityKey2")))
                {
                    if (!string.IsNullOrEmpty(tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityKey2"))))
                    {
                        entityKeys.Add(primaryKeyNames[tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityName")) + 1], tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityKey2")));
                    }
                }

                if (!tbAuditRecords.IsDBNull(tbAuditRecords.GetOrdinal("EntityKey3")))
                {
                    if (!string.IsNullOrEmpty(tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityKey3"))))
                    {
                        entityKeys.Add(primaryKeyNames[tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityName")) + 2], tbAuditRecords.GetString(tbAuditRecords.GetOrdinal("EntityKey3")));
                    }
                }

                AuditEntry auditEntry = CreateAuditEntry(auditOperation, auditEntity, entityKeys);
                if (auditEntry != null)
                {
                    auditEntryList.Add(auditEntry);
                    auditRows.Add(auditOperation);

                    rowCount++;
                    if (rowCount >= 100000)
                    {
                        // Serialize the audit records into a JSON object
                        string retJSON = JsonConvert.SerializeObject(auditEntryList);

                        // Write JSON to a file
                        this.CreateExportFile(retJSON);

                        auditEntryList.Clear();
                        rowCount = 0;
                    }
                }
            }

            if (rowCount > 0)
            {
                // Serialize the audit records into a JSON object
                string retJSON = JsonConvert.SerializeObject(auditEntryList);

                // Write JSON to a file
                this.CreateExportFile(retJSON);
            }

            this.LogMessage("Audit File Creation Complete");
        }