Auditing and Cancelling Edits

Posts   
 
    
Posts: 112
Joined: 09-Aug-2004
# Posted on: 13-Jul-2009 16:24:07   

LLBLGen Pro. Version 2.6 Final (May 15th 2009) Runtime Version: 2.6.9.616

Given the following pseudoish code


    public class FieldAuditor: AuditorBase {
        public override void AuditEntityFieldSet(IEntityCore entity, int fieldIndex, object originalValue) {
              //record the changes...
              _auditInfoEntities.Add(auditInfo);
        }
    }

    //---

    entity.AuditorToUse = new FieldAuditor();
    entity.Field1 = "New Value";
    entity.Field2 = "Different Value";
    entity.Felds[(int)EntityFieldIndex.Field1].CancelEdit();

    adapter.SaveEntity(entity);

Will the auditor end up saving an auditInfo record for both Field1 and Field2? I haven't tested an isolated case yet, but it seems like the CancelEdit does not also cancel the auditInfo for Field1.

What is the expected behavior in this case? If the expected behavior is NOT to also cancel the auditInfo for the field, what is the best way to make sure that the auditInfo record will not be saved for fields which have had the Edit Canceled?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Jul-2009 04:10:59   

Will the auditor end up saving an auditInfo record for both Field1 and Field2? I haven't tested an isolated case yet, but it seems like the CancelEdit does not also cancel the auditInfo for Field1.

Yes. What you do in the Auditor is up to you. You for instance, want to log the edit intention of the user. In that case you don't want to cancel the audit log.

What you could do is, on AuditInsertOfNewEntity check the state of the fields (IsChanged). If the field isn't changed and you have an auditInfo for that field, that means that a CancelEdit took place on that field, and you should remove it form your auditInfo's.

David Elizondo | LLBLGen Support Team
Posts: 112
Joined: 09-Aug-2004
# Posted on: 14-Jul-2009 15:20:30   

The problem is with AuditEntityFieldSet of an existing entity, the AuditEntityFieldSet method gets called in the entity's field setter. If it was called when the entity is saved, I would be able to check the IsChanged property.

How/when can I check to see if the field was changed after the auditInfo has already been created and has been added to the AuditEntitiesToSave?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 14-Jul-2009 20:50:44   

You could check the fields .Value property (the current value) against the .DBValue property (the value that was originally retrieved from the DB - this will tell you if it is changed.

Matt

Posts: 112
Joined: 09-Aug-2004
# Posted on: 14-Jul-2009 21:05:50   

Maybe this will help explain things better, here is the order of operations


entity.AuditorToUse = new FieldAuditor();
entity.Field1 = "New Value";

//entity.AuditorToUse.AuditEntityFieldSet(entity, 0, null); //this gets called automatically by the LLBLGen Framework -- auditInfo entity is created and added to EntitiesToSave

entity.Field2 = "Different Value";

//entity.AuditorToUse.AuditEntityFieldSet(entity, 1, null); //this gets called automatically by the LLBLGen Framework -- auditInfo entity is created and added to EntitiesToSave

entity.Felds[(int)EntityFieldIndex.Field1].CancelEdit();

Checking to see if the field has changed doesn't work because at the time the AuditEntityFieldSet method is called, the field is changed, and rightly so. Then, after all of that is done, CancelEdit is called. You can't check in the AuditEntityFieldSet method because it has already executed.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Jul-2009 06:01:27   

I said: you can use the _AuditInsertOfNewEntity _method, that is called right after the entity is saved. There you have the saved entity, and you have your collection of AuditInfo entities (to be persisted later). You can find the saved entity on that collection, then based on some comparisons you can decide whether you should remove the AuditInfo entity from your collection. Remember that the AuditInfo entities are saved when you pass them on the GetAuditEntitiesToSave method.

That should work. If not, please attach your auditor wink

David Elizondo | LLBLGen Support Team
Posts: 112
Joined: 09-Aug-2004
# Posted on: 15-Jul-2009 15:25:32   

daelmo wrote:

I said: you can use the _AuditInsertOfNewEntity _method, that is called right after the entity is saved.

lethologica wrote:

The problem is with AuditEntityFieldSet of an existing entity, the AuditEntityFieldSet method gets called in the entity's field setter. If it was called when the entity is saved, I would be able to check the IsChanged property.

Maybe I am missing something, but how can I use the AuditInsertOfNewEntity method for "an existing entity"?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Jul-2009 15:29:39   

Maybe I am missing something, but how can I use the AuditInsertOfNewEntity method for "an existing entity"?

AuditUpdateOfExistingEntity() is the one to use when updating an existing entity.

Posts: 112
Joined: 09-Aug-2004
# Posted on: 15-Jul-2009 15:35:28   

daelmo wrote:

Remember that the AuditInfo entities are saved when you pass them on the GetAuditEntitiesToSave method.

Ohh, So you are saying catch the Auditing of an AuditInfo Entity?


    public class FieldAuditor: AuditorBase {
        public override void AuditInsertOfNewEntity(IEntityCore entity) {
            if (entity is AuditInfoEntity) {
                 //check to see if entity is the one related to entity.Field1
                 //then check to see if that field has been Canceled some how
            }
        }
    }

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Jul-2009 15:51:39   

The original entity being inserted or updated is the one passed to the following methods: AuditUpdateOfExistingEntity() or AuditInsertOfNewEntity()