public ChangeLogHandler(StoredChangelog sclog, Logger logger)
 {
     //m_storedChangelog = sclog;
     //m_db = db;
     _mLogger           = logger;
     _mWorkingDirectory = Path.GetTempPath();
 }
        public OrderChangelog CreateChangeLog(int startIndex, int count, int datasetId)
        {
            StoredChangelog ldbo = new StoredChangelog();

            ldbo.Stored = false;

            ldbo.Status     = ((string)System.Enum.GetName(typeof(Kartverket.GeosyncWCF.ChangelogStatusType), Kartverket.GeosyncWCF.ChangelogStatusType.queued));
            ldbo.StartIndex = startIndex;
            ldbo.EndIndex   = startIndex + count; //TODO fix

            ldbo.DatasetId   = datasetId;
            ldbo.DateCreated = DateTime.Now;

            //TODO make filter
            //TODO check if similar stored changelog is already done
            db.StoredChangelogs.AddObject(ldbo);
            db.SaveChanges();

            OrderChangelog resp = new OrderChangelog();

            resp.changelogId = ldbo.ChangelogId.ToString();
            return(resp);
        }
        public OrderChangelog GenerateInitialChangelog(int datasetId)
        {
            string downloadUriBase = ServerConfigData.DownloadUriBase().TrimEnd('/');

            using (geosyncEntities db = new geosyncEntities())
            {
                var initialChangelog = (from d in db.StoredChangelogs
                                        where d.DatasetId == datasetId && d.StartIndex == 1 && d.Stored == true && d.Status == "finished"
                                        orderby d.DateCreated descending
                                        select d).FirstOrDefault();
                if (initialChangelog != null && initialChangelog.DownloadUri != null)
                {
                    Uri uri = new Uri(initialChangelog.DownloadUri);
                    ChangelogManager.DeleteFileOnServer(uri);
                    db.StoredChangelogs.DeleteObject(initialChangelog);
                    db.SaveChanges();
                }
            }
            LastChangeId = 1;    // StartIndex always 1 on initial changelog
            int endIndex = Convert.ToInt32(GetLastIndex(datasetId));
            int count    = 1000; // TODO: Get from dataset table

            Logger.Info("GenerateInitialChangelog START");
            StoredChangelog ldbo = new StoredChangelog();

            ldbo.Stored     = true;
            ldbo.Status     = "queued";
            ldbo.StartIndex = (int)LastChangeId;

            ldbo.DatasetId   = datasetId;
            ldbo.DateCreated = DateTime.Now;

            //TODO make filter
            //TODO check if similar stored changelog is already done
            using (geosyncEntities db = new geosyncEntities())
            {
                // Store changelog info in database
                db.StoredChangelogs.AddObject(ldbo);

                OrderChangelog resp = new OrderChangelog();
                resp.changelogId = ldbo.ChangelogId.ToString();

                //New thread and do the work....
                // We're coming back to the thread handling later...
                //string sourceFileName = "Changelogfiles/41_changelog.xml";


                Directory.CreateDirectory(destPath);
                // Loop and create xml files
                while (OptimizedChangelLogIndex < OptimizedChangeLog.Count)
                {
                    string partFileName = DateTime.Now.Ticks + ".xml";

                    string fullPathWithFile = Path.Combine(destPath, partFileName);
                    MakeChangeLog((int)LastChangeId, count, PDbConnectInfo, _pWfsUrl, fullPathWithFile, datasetId);
                    LastChangeId += 1;
                }

                // Save endIndex to database
                ldbo.EndIndex = endIndex;

                // New code to handle FTP download
                ChangeLogHandler chgLogHandler = new ChangeLogHandler(ldbo, Logger);
                string           inFile        = "";
                try
                {
                    inFile = destPath;
                    chgLogHandler.CreateZipFileFromFolder(inFile, zipFile, destFileName);
                    ldbo.Status = "queued";
                    File.Copy(tmpzipFile, streamFileLocation);
                    File.Delete(tmpzipFile);
                    ldbo.Status = "finished";
                }
                catch (Exception ex)
                {
                    Logger.ErrorException(string.Format("Failed to create or upload file {0}", zipFile), ex);
                    throw ex;
                }

                try
                {
                    string downLoadUri = string.Format(@"{0}/{1}", downloadUriBase, zipFile);

                    ldbo.DownloadUri = downLoadUri;
                }
                catch (Exception ex)
                {
                    Logger.ErrorException(string.Format("Failed to create or upload file {0}", zipFile), ex);
                    throw ex;
                }


                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.ErrorException(
                        string.Format(
                            "Failed on SaveChanges, Kartverket.Geosynkronisering.ChangelogProviders.PostGISChangelog.OrderChangelog startIndex:{0} count:{1} changelogId:{2}",
                            LastChangeId, count, ldbo.ChangelogId), ex);
                    throw ex;
                }
                Logger.Info(
                    "Kartverket.Geosynkronisering.ChangelogProviders.PostGISChangelog.OrderChangelog" +
                    " startIndex:{0}" + " count:{1}" + " changelogId:{2}", LastChangeId, count, ldbo.ChangelogId);

                Logger.Info("GenerateInitialChangelog END");
                return(resp);
            }
        }