protected void SetRowstampIfBigger(String key, long?rowstamp, long?oldRowstamp)
 {
     if (rowstamp == null || (oldRowstamp != null && rowstamp < oldRowstamp))
     {
         return;
     }
     ConfigFacade.SetValue(key, rowstamp);
 }
        public override void ExecuteJob()
        {
            var lowerRowstamp = ConfigFacade.Lookup <long>(ConfigurationConstants.R0042Rowstamp);
            var now           = DateTime.Now;

            now = new DateTime(now.Year, now.Month, 1);
            var lastMonth = DateUtil.BeginOfDay(now.AddMonths(-1));


            var beginOfMonth   = DateUtil.BeginOfDay(now);
            var compMetadata   = MetadataProvider.Application("asset");
            var schema         = compMetadata.Schemas()[new ApplicationMetadataSchemaKey("R0042Export")];
            var slicedMetadata = MetadataProvider.SlicedEntityMetadata(schema.GetSchemaKey(), "asset");
            var dto            = new PaginatedSearchRequestDto {
                PageSize = PageSize
            };

            var needsMore   = true;
            var initOfBatch = true;
            var i           = 1;

            while (needsMore)
            {
                Log.InfoFormat("R0042: fetching first {0} items restricted to rowstamp {1}".Fmt(i * PageSize, lowerRowstamp));
                var searchEntityResult          = FetchMore(dto, lastMonth, slicedMetadata, lowerRowstamp, initOfBatch);
                IList <R0042AssetHistory> items = ConvertItems(searchEntityResult.ResultList, beginOfMonth);
                if (!items.Any())
                {
                    break;
                }
                DAO.BulkSave(items);
                var greatestRowstamp = items[items.Count - 1].Rowstamp;

                //there´s at least one extra item, but could be thousands
                needsMore = items.Count == PageSize;
                i++;
                if (greatestRowstamp.HasValue)
                {
                    ConfigFacade.SetValue(ConfigurationConstants.R0042Rowstamp, greatestRowstamp);
                    Log.InfoFormat("R0042: updating rowstamp to {0}".Fmt(greatestRowstamp.Value));
                    lowerRowstamp = greatestRowstamp.Value;
                }

                initOfBatch = false;
            }
        }