/* public void WriteFile(string name, string data)
         * {
         *  var path = Path.Combine(_path, name);
         *
         *  using (var ms = new MemoryStream())
         *  {
         *      var bytes = Encoding.UTF8.GetBytes(data);
         *      ms.Write(bytes, 0, bytes.Length);
         *      ms.Position = 0;
         *      _fs.WriteFile(path, ms);
         *  }
         * } */

        public IImportRecord SyncItem(ReplicatedItem item)
        {
            lock (this)
            {
                try
                {
                    var reader   = new ItemXmlReader(_definitions, _activator, _persister.Repository);
                    var importer = new Importer(null, reader, null);

                    // TODO download
                    var ins    = _fs.OpenFile(item.Path, true);
                    var record = importer.Read(ins, item.Path);
                    if (record.RootItem != null)
                    {
                        ContentVersion.ReorderBySortOrderRecursive(record.RootItem);
                    }
                    return(record);
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("IMPORT Error: {0} - {1}", item.ID, ex.Message), ex);
                    throw;
                }
            }
        }
        // sync local with remote item
        private void SyncOneItem(ReplicatedItem replicatedItem, ConcurrentContentList items)
        {
            var local = items.Get(replicatedItem.ID); // get local match for item

            if (local != null && local.Published.HasValue)
            {
                if (IsSlave)
                // import if newer than local
                {
                    if (replicatedItem.IsNewerThan(local.Published.Value) || !ExistsLocal(local))
                    {
                        _journal.LogItem("import {0} published {1} vs. {2} update",
                                         replicatedItem.ID, replicatedItem.PublishedDateUtc,
                                         local.Published.Value.ToUniversalTime());
                        ImportItem(replicatedItem);
                    }
                }
                if (IsMaster)
                {
                    if (replicatedItem.IsOlderThan(local.Published.Value))
                    {
                        _journal.LogItem("export {0} published {1} vs. {2} update",
                                         replicatedItem.ID, replicatedItem.PublishedDateUtc,
                                         local.Published.Value.ToUniversalTime());
                        _repstore.ExportItem(local);
                    }
                }

                // TODO check version is bigger than replaced version
                items.Remove(local);
            }
            else
            {
                if (IsSlave) // TODO and newer published date
                {
                    _journal.LogItem("import {0} published {1} create",
                                     replicatedItem.ID, replicatedItem.PublishedDateUtc);
                    ImportItem(replicatedItem);
                }
                if (IsMaster)
                {
                    // item is not published anymore -> delete replicated item
                    // TODO signalling - check edge cases, e.g. non-published items that should stay
                    _journal.LogItem("DELETE item {0} published {1}",
                                     replicatedItem.ID, replicatedItem.PublishedDateUtc);
                    _repstore.DeleteItem(replicatedItem);
                }
            }
            replicatedItem.Processed = true;
        }
        // Import one file and update new items and unresolved links
        private void ImportItem(ReplicatedItem item)
        {
            try
            {
                var record = _repstore.SyncItem(item);
                _stillUnresolved.AddRange(record.UnresolvedLinks);

                foreach (var ri in record.ReadItems)
                {
                    _newItems[ri.ID] = ri;
                }
            }
            catch (Exception e)
            {
                _logger.Error("IMPORT Error: " + e.Message, e);
                _journal.Log(e.Message);
            }
        }
 public void DeleteItem(ReplicatedItem replicatedItem)
 {
     _fs.DeleteFile(replicatedItem.Path);
 }
        /* public void WriteFile(string name, string data)
        {
            var path = Path.Combine(_path, name);

            using (var ms = new MemoryStream())
            {
                var bytes = Encoding.UTF8.GetBytes(data);
                ms.Write(bytes, 0, bytes.Length);
                ms.Position = 0;
                _fs.WriteFile(path, ms);
            }
        } */

        public IImportRecord SyncItem(ReplicatedItem item)
        {
            lock (this)
            {
                try
                {
                    var reader = new ItemXmlReader(_definitions, _activator, _persister.Repository);
                    var importer = new Importer(null, reader, null);

                    // TODO download
                    var ins = _fs.OpenFile(item.Path, true);
                    var record = importer.Read(ins, item.Path);
                    if (record.RootItem != null)
                        ContentVersion.ReorderBySortOrderRecursive(record.RootItem);
                    return record;
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("IMPORT Error: {0} - {1}", item.ID, ex.Message), ex);
                    throw;
                }
            }
        }
 public void ReplicatedItem_CanCompareDates()
 {
     var fd = new FileData();
     fd.Name = "1";
     fd.Updated = DateTime.Now;
     var ri = new ReplicatedItem(fd);
     Assert.That(ri.PublishedDateEquals(fd.Updated));
     Assert.That(ri.PublishedDateEquals(fd.Updated.ToUniversalTime()));
     Assert.That(ri.IsOlderThan(DateTime.Now.AddSeconds(1)));
     Assert.That(ri.IsNewerThan(DateTime.Now.AddMinutes(-1)));
 }
 public void ReplicatedItem_CanParseID()
 {
     var fd = new FileData();
     fd.Name = "page_1234_666.xml";
     fd.Updated = DateTime.Now;
     var ri = new ReplicatedItem(fd);
     Assert.AreEqual(1234, ri.ID);
 }
        // Import one file and update new items and unresolved links
        private void ImportItem(ReplicatedItem item)
        {
            try
            {
                var record = _repstore.SyncItem(item);
                _stillUnresolved.AddRange(record.UnresolvedLinks);

                foreach (var ri in record.ReadItems)
                {
                    _newItems[ri.ID] = ri;
                }
            }
            catch (Exception e)
            {
                _logger.Error("IMPORT Error: " + e.Message, e);
                _journal.Log(e.Message);
            }
        }
        // sync local with remote item
        private void SyncOneItem(ReplicatedItem replicatedItem, ConcurrentContentList items)
        {
            var local = items.Get(replicatedItem.ID); // get local match for item
            if (local != null && local.Published.HasValue)
            {
                if (IsSlave)
                // import if newer than local
                {
                    if (replicatedItem.IsNewerThan(local.Published.Value) || !ExistsLocal(local))
                    {
                        _journal.LogItem("import {0} published {1} vs. {2} update",
                            replicatedItem.ID, replicatedItem.PublishedDateUtc,
                            local.Published.Value.ToUniversalTime());
                        ImportItem(replicatedItem);
                    }
                }
                if (IsMaster)
                {
                    if (replicatedItem.IsOlderThan(local.Published.Value))
                    {
                        _journal.LogItem("export {0} published {1} vs. {2} update",
                            replicatedItem.ID, replicatedItem.PublishedDateUtc,
                                     local.Published.Value.ToUniversalTime());
                        _repstore.ExportItem(local);
                    }
                }

                // TODO check version is bigger than replaced version
                items.Remove(local);
            }
            else
            {
                if (IsSlave) // TODO and newer published date
                {
                    _journal.LogItem("import {0} published {1} create",
                        replicatedItem.ID, replicatedItem.PublishedDateUtc);
                    ImportItem(replicatedItem);
                }
                if (IsMaster)
                {
                    // item is not published anymore -> delete replicated item 
                    // TODO signalling - check edge cases, e.g. non-published items that should stay 
                    _journal.LogItem("DELETE item {0} published {1}",
                        replicatedItem.ID, replicatedItem.PublishedDateUtc);
                    _repstore.DeleteItem(replicatedItem);
                }
            }
            replicatedItem.Processed = true;
        }
 public void DeleteItem(ReplicatedItem replicatedItem)
 {
     _fs.DeleteFile(replicatedItem.Path);
 }