6 Replies Latest reply on Mar 7, 2012 10:37 AM by Paul Taylor

    Comparing audited record date with manually set date

    Paul Taylor Newbie

      1>I load a file into the database and set a field called lastModified to the last Modified value of the File.

      2>Then I do various analysis on the entity which means it written back to database a few times, however it is possible that nothing has actually changed.

      3>Finally I save the entity back to the actual file, as part of this I set the lastModified value to the current date/time and save the entiry to the database.

       

      This means that if I try to load the file again I can check for it first in the database and if the lastModified value is later than the last modified date of the file I can just load the files metadata directly from the database without having to parse the file again.

       

      So far, so good.

       

      Now I add another condition to the save process I only want to save the entity back to the file (3) if there have actually been any revisions since when the file was loaded into the program, my idea was to use do AudioReader to get date of latest revision and ONLY if it is later then lastModifedDate to save changes.

       

                  AuditReader reader = AuditReaderFactory.get(session);

                  List<Number> revisions = reader.getRevisions(Song.class,song.getRecNo());

                  Number lastRevision = revisions.get(revisions.size() - 1);

                  Date lastDate = reader.getRevisionDate(lastRevision);

                 

                  if(!lastDate.after(song.getLastModified()))

                  {

                      //Dont Save

                      return;

                  }

       

      but the trouble is that when I actually do a save the date set set by Envers is always going to be later than the lastModifiedDate I set so my logic fails and I always save changes even when not neccessary.

       

      Seems like a trivial problem but cant see the solution.