public void CanExport_SimpleItem_WithFileAttachment() { XmlableItem destination = new XmlableItem(); XmlableItem item = CreateOneItem <XmlableItem>(1, "item", null); item.TextFile = "/Serialization/TestFile.txt"; string xml = ExportToString(item, CreateExporter(), ExportOptions.Default); string path = AppDomain.CurrentDomain.BaseDirectory + @"\Serialization\TestFile.txt"; File.Delete(path); IImportRecord record = ImportFromString(xml, CreateImporter()); XmlableItem readItem = (XmlableItem)record.RootItem; Assert.AreEqual(item.ID, readItem.ID); Assert.That(!File.Exists(path)); CreateImporter().Import(record, destination, ImportOption.All); Assert.AreEqual(0, readItem.ID); Assert.AreEqual(item.Title, readItem.Title); Assert.AreEqual(item.Name, readItem.Name); Assert.AreEqual("/Serialization/TestFile.txt", readItem.TextFile); Assert.That(File.Exists(path)); Assert.AreEqual("Just a little file.", File.ReadAllText(path)); }
private void Update(Updater updater, IImportRecord record) { try { UpdateOption updateParameter = new UpdateOption(); if (chkSkipAttachments.Checked) { updateParameter = updateParameter | UpdateOption.Attachments; } //if (chkUpdateChilds.Checked) //{ // updateParameter = updateParameter | UpdateOption.Children; //} updater.Update(record, Selection.SelectedItem, updateParameter); Refresh(Selection.SelectedItem, ToolbarArea.Both); ShowErrors(record); } catch (N2Exception ex) { cvUpdate.ErrorMessage = ex.Message; cvUpdate.IsValid = false; btnUpdateUploaded.Enabled = false; } finally { if (File.Exists(UploadedFilePath)) File.Delete(UploadedFilePath); } }
public void ContinueWithImport(string tempFile) { UploadedFilePath = tempFile; try { IImportRecord record = Engine.Resolve <Importer>().Read(UploadedFilePath); importedItems.CurrentItem = record.RootItem; rptAttachments.DataSource = record.Attachments; if (Selection.SelectedItem.Children.FindNamed(record.RootItem.Name) != null) { pnlNewName.Visible = true; txtNewName.Text = record.RootItem.Name; } ShowErrors(record); } catch (WrongVersionException) { using (Stream s = File.OpenRead(UploadedFilePath)) { N2XmlReader xr = new N2XmlReader(N2.Context.Current); importedItems.CurrentItem = xr.Read(s); } } DataBind(); }
private void Import(Importer importer, IImportRecord record) { try { if (pnlNewName.Visible) { record.RootItem.Name = txtNewName.Text; } if (chkSkipRoot.Checked) { importer.Import(record, Selection.SelectedItem, ImportOption.Children); Refresh(Selection.SelectedItem, ToolbarArea.Both); } else { importer.Import(record, Selection.SelectedItem, ImportOption.All); Refresh(record.RootItem, ToolbarArea.Both); } ShowErrors(record); } catch (N2Exception ex) { cvImport.ErrorMessage = ex.Message; cvImport.IsValid = false; btnImportUploaded.Enabled = false; } finally { if (File.Exists(UploadedFilePath)) File.Delete(UploadedFilePath); } }
private void Import(Importer importer, IImportRecord record) { try { if (chkSkipRoot.Checked) { importer.Import(record, Selection.SelectedItem, ImportOption.Children); Refresh(Selection.SelectedItem, ToolbarArea.Both); } else { importer.Import(record, Selection.SelectedItem, ImportOption.All); Refresh(record.RootItem, ToolbarArea.Both); } ShowErrors(record); } catch (N2Exception ex) { cvImport.ErrorMessage = ex.Message; cvImport.IsValid = false; btnImportUploaded.Enabled = false; } finally { if (File.Exists(UploadedFilePath)) { File.Delete(UploadedFilePath); } } }
public ContentItem InsertExportFile(Stream stream, string filename) { IImportRecord record = importer.Read(stream, filename); record.RootItem["Installation.AppPath"] = webContext.ToAbsolute("~/"); record.RootItem["Installation.Host"] = webContext.Url.HostUrl.ToString(); importer.Import(record, null, ImportOption.All); return(record.RootItem); }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOption options) { var security = N2.Context.Current.Resolve <ISecurityManager>(); using (security.Disable()) // TODO restrict to Admin User { ResetIDs(record.ReadItems); if ((options & ImportOption.AllItems) == ImportOption.AllItems) { record.RootItem.AddTo(destination); if (_persister != null) { _persister.SaveRecursive(record.RootItem); } } else if ((options & ImportOption.Children) == ImportOption.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = record.RootItem.Children[0]; child.AddTo(destination); if (_persister != null) { _persister.SaveRecursive(child); } } } else { logger.ErrorFormat("Option {0} isn't supported", options); throw new NotImplementedException("This option isn't implemented, sorry."); } if ((options & ImportOption.Attachments) == ImportOption.Attachments) { foreach (Attachment a in record.Attachments) { try { a.Import(_fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } } }
void ShowErrors(IImportRecord record) { if (record.Errors.Count > 0) { StringBuilder errorText = new StringBuilder("<ul>"); foreach (Exception ex in record.Errors) { errorText.Append("<li>").Append(ex.Message).Append("</li>"); } errorText.Append("</ul>"); cvImport.IsValid = false; cvImport.ErrorMessage = errorText.ToString(); } }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOption options) { var security = N2.Context.Current.Resolve<ISecurityManager>(); using (security.Disable()) // TODO restrict to Admin User { ResetIDs(record.ReadItems); if ((options & ImportOption.AllItems) == ImportOption.AllItems) { record.RootItem.AddTo(destination); if (_persister != null) _persister.SaveRecursive(record.RootItem); } else if ((options & ImportOption.Children) == ImportOption.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = record.RootItem.Children[0]; child.AddTo(destination); if (_persister != null) _persister.SaveRecursive(child); } } else { logger.ErrorFormat("Option {0} isn't supported", options); throw new NotImplementedException("This option isn't implemented, sorry."); } if ((options & ImportOption.Attachments) == ImportOption.Attachments) { foreach (Attachment a in record.Attachments) { try { a.Import(_fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } } }
public virtual ContentItem InsertExportFile(Stream stream, string filename) { IImportRecord record = importer.Read(stream, filename); record.RootItem["Installation.AppPath"] = N2.Web.Url.ToAbsolute("~/"); try { record.RootItem["Installation.Host"] = webContext.Url.HostUrl.ToString(); } catch (HttpException) { // silently ignore "Request is not available in this context" when calling this from init } importer.Import(record, null, ImportOption.All); return(record.RootItem); }
public void CanExport_SimpleItem_WithFileAttachment() { XmlableItem destination = new XmlableItem(); XmlableItem item = CreateOneItem <XmlableItem>(1, "item", null); string file = "TestFile.txt"; string path = "/Serialization/" + file; item.TextFile = "/Serialization/TestFile.txt"; var sourceFs = new FakeMemoryFileSystem(); sourceFs.files.Add(path, new N2.Edit.FileSystem.FileData { Name = file, VirtualPath = path }); sourceFs.contents.Add(path, Encoding.UTF8.GetBytes("Just a little file.")); string xml = ExportToString(item, CreateExporter(sourceFs), ExportOptions.Default); //string path = AppDomain.CurrentDomain.BaseDirectory + @"\Serialization\TestFile.txt"; var destinationFs = new FakeMemoryFileSystem(); IImportRecord record = ImportFromString(xml, CreateImporter(destinationFs)); XmlableItem readItem = (XmlableItem)record.RootItem; Assert.AreEqual(item.ID, readItem.ID); Assert.That(!destinationFs.FileExists(path)); CreateImporter(destinationFs).Import(record, destination, ImportOption.All); Assert.AreEqual(0, readItem.ID); Assert.AreEqual(item.Title, readItem.Title); Assert.AreEqual(item.Name, readItem.Name); Assert.AreEqual("/Serialization/TestFile.txt", readItem.TextFile); var temp = new byte[1000]; var size = destinationFs.OpenFile(path).Read(temp, 0, 1000); var buffer = new byte[size]; Array.Copy(temp, buffer, size); Assert.That(destinationFs.FileExists("/Serialization/TestFile.txt")); Assert.AreEqual("Just a little file.", Encoding.UTF8.GetString(buffer)); }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOption options) { ResetIDs(record.ReadItems); if ((options & ImportOption.AllItems) == ImportOption.AllItems) { record.RootItem.AddTo(destination); persister.SaveRecursive(record.RootItem); } else if ((options & ImportOption.Children) == ImportOption.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = record.RootItem.Children[0]; child.AddTo(destination); persister.SaveRecursive(child); } } else { logger.ErrorFormat("Option {0} isn't supported", options); throw new NotImplementedException("This option isn't implemented, sorry."); } if ((options & ImportOption.Attachments) == ImportOption.Attachments) { foreach (Attachment a in record.Attachments) { try { a.Import(fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOption options) { ResetIDs(record.ReadItems); if ((options & ImportOption.AllItems) == ImportOption.AllItems) { record.RootItem.AddTo(destination); persister.Save(record.RootItem); } else if ((options & ImportOption.Children) == ImportOption.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = record.RootItem.Children[0]; child.AddTo(destination); persister.Save(child); } } else { logger.ErrorFormat("Option {0} isn't supported", options); throw new NotImplementedException("This option isn't implemented, sorry."); } if ((options & ImportOption.Attachments) == ImportOption.Attachments) { foreach(Attachment a in record.Attachments) { try { a.Import(fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } }
public void ContinueWithImport(string tempFile) { UploadedFilePath = tempFile; try { IImportRecord record = Engine.Resolve <Importer>().Read(UploadedFilePath); importedItems.CurrentItem = record.RootItem; rptAttachments.DataSource = record.Attachments; ShowErrors(record); } catch (WrongVersionException) { using (Stream s = File.OpenRead(UploadedFilePath)) { N2XmlReader xr = new N2XmlReader(N2.Context.Current); importedItems.CurrentItem = xr.Read(s); } } DataBind(); }
public virtual void Update(IImportRecord record, ContentItem destination, UpdateOption options) { RemoveReferences(record.ReadItems, record.RootItem); AssociateLanguageKeys(record.ReadItems); UpdateContentItem(destination, record.RootItem); UpdateParts(destination, record.RootItem); if ((options & UpdateOption.Children) == UpdateOption.Children) { //Could be implemented for future use... } if ((options & UpdateOption.Attachments) == UpdateOption.Attachments) { foreach (Attachment a in record.Attachments) { try { a.Import(_fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } destination.DontReOrderSave = true; _persister.SaveRecursive(destination); N2.Context.Current.Resolve<N2.Edit.Trash.ITrashHandler>().PurgeAll(); }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOptions options) { ResetIDs(record.ReadItems); if ((options & ImportOptions.AllItems) == ImportOptions.AllItems) { record.RootItem.AddTo(destination); persister.Save(record.RootItem); } else if ((options & ImportOptions.Children) == ImportOptions.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = record.RootItem.Children[0]; child.AddTo(destination); persister.Save(child); } } else { throw new NotImplementedException("This option isn't implemented, sorry."); } }
protected void btnVerify_Click(object sender, EventArgs e) { UploadedFilePath = System.IO.Path.GetTempFileName() + System.IO.Path.GetExtension(fuImport.PostedFile.FileName); fuImport.PostedFile.SaveAs(UploadedFilePath); try { IImportRecord record = Engine.Resolve <Importer>().Read(UploadedFilePath); importedItems.CurrentItem = record.RootItem; rptAttachments.DataSource = record.Attachments; ShowErrors(record); } catch (WrongVersionException) { using (Stream s = File.OpenRead(UploadedFilePath)) { N2XmlReader xr = new N2XmlReader(N2.Context.Current); importedItems.CurrentItem = xr.Read(s); } } uploadFlow.ActiveViewIndex = 1; uploadFlow.Views[1].DataBind(); }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOption options) { ResetIDs(record.ReadItems); if ((options & ImportOption.AllItems) == ImportOption.AllItems) { record.RootItem.AddTo(destination); try { persister.SaveRecursive(record.RootItem); } catch (Exception ex) { logger.Warn(ex); if (record.RootItem != null) { record.FailedContentItems.Add(new Tuple <ContentItem, Exception>(record.RootItem, ex)); } } } else if ((options & ImportOption.Children) == ImportOption.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = null; bool added = false; try { child = record.RootItem.Children[0]; child.AddTo(destination); added = true; persister.SaveRecursive(child); } catch (Exception ex) { logger.Warn(ex); if (child != null) { record.FailedContentItems.Add(new Tuple <ContentItem, Exception>(child, ex)); } // ROLL BACK: Undo child.AddTo if SaveRecursive failed. That way the import can still continue successfully. if (added && destination != null && child != null) { destination.Children.Remove(child); } } } } else { logger.ErrorFormat("Option {0} isn't supported", options); throw new NotImplementedException("This option isn't implemented, sorry."); } if ((options & ImportOption.Attachments) == ImportOption.Attachments) { foreach (Attachment a in record.Attachments) { try { a.Import(fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } }
private void Import(Importer importer, IImportRecord record) { try { if (chkSkipRoot.Checked) { importer.Import(record, SelectedItem, ImportOptions.Children); Refresh(SelectedItem, AdminFrame.Both, false); } else { importer.Import(record, SelectedItem, ImportOptions.AllItems); Refresh(record.RootItem, AdminFrame.Both, false); } ShowErrors(record); } catch (ZeusException ex) { cvImport.ErrorMessage = ex.Message; cvImport.IsValid = false; btnImportUploaded.Enabled = false; } finally { if (File.Exists(UploadedFilePath)) File.Delete(UploadedFilePath); } }
void ShowErrors(IImportRecord record) { if(record.Errors.Count > 0) { StringBuilder errorText = new StringBuilder("<ul>"); foreach(Exception ex in record.Errors) { errorText.Append("<li>").Append(ex.Message).Append("</li>"); } errorText.Append("</ul>"); cvImport.IsValid = false; cvImport.ErrorMessage = errorText.ToString(); } }
public virtual void Import(IImportRecord record, ContentItem destination, ImportOption options) { ResetIDs(record.ReadItems); if ((options & ImportOption.AllItems) == ImportOption.AllItems) { record.RootItem.AddTo(destination); try { persister.SaveRecursive(record.RootItem); } catch (Exception ex) { logger.Warn(ex); if (record.RootItem != null) record.FailedContentItems.Add(new Tuple<ContentItem, Exception>(record.RootItem, ex)); } } else if ((options & ImportOption.Children) == ImportOption.Children) { RemoveReferences(record.ReadItems, record.RootItem); while (record.RootItem.Children.Count > 0) { ContentItem child = null; bool added = false; try { child = record.RootItem.Children[0]; child.AddTo(destination); added = true; persister.SaveRecursive(child); } catch (Exception ex) { logger.Warn(ex); if (child != null) record.FailedContentItems.Add(new Tuple<ContentItem, Exception>(child, ex)); // ROLL BACK: Undo child.AddTo if SaveRecursive failed. That way the import can still continue successfully. if (added && destination != null && child != null) destination.Children.Remove(child); } } } else { logger.ErrorFormat("Option {0} isn't supported", options); throw new NotImplementedException("This option isn't implemented, sorry."); } if ((options & ImportOption.Attachments) == ImportOption.Attachments) { foreach(Attachment a in record.Attachments) { try { a.Import(fs); } catch (Exception ex) { logger.Warn(ex); record.FailedAttachments.Add(a); } } } }