use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentMergeImporter method saveDocument.
private void saveDocument(XWikiDocument document, String comment, boolean setCreator, PackageConfiguration configuration) throws Exception {
XWikiContext xcontext = this.xcontextProvider.get();
XWikiDocument currentDocument = xcontext.getWiki().getDocument(document.getDocumentReferenceWithLocale(), xcontext);
if (!currentDocument.isNew()) {
if (document != currentDocument) {
if (document.isNew()) {
currentDocument.loadAttachmentsContentSafe(xcontext);
currentDocument.apply(document);
} else {
currentDocument = document;
}
}
} else {
currentDocument = document;
}
// Set document authors
DocumentReference configuredUser = configuration.getUserReference();
if (configuredUser != null) {
if (setCreator) {
currentDocument.setCreatorReference(configuredUser);
}
currentDocument.setAuthorReference(configuredUser);
currentDocument.setContentAuthorReference(configuredUser);
// Set attachments authors
for (XWikiAttachment attachment : currentDocument.getAttachmentList()) {
if (attachment.isContentDirty()) {
attachment.setAuthorReference(currentDocument.getAuthorReference());
}
}
} else {
if (document != currentDocument) {
if (setCreator) {
currentDocument.setCreatorReference(document.getCreatorReference());
}
currentDocument.setAuthorReference(document.getAuthorReference());
currentDocument.setContentAuthorReference(document.getContentAuthorReference());
// Set attachments authors
for (XWikiAttachment attachment : document.getAttachmentList()) {
if (attachment.isContentDirty()) {
currentDocument.getAttachment(attachment.getFilename()).setAuthorReference(attachment.getAuthorReference());
}
}
}
// Make sure to keep the content author we want
currentDocument.setContentDirty(false);
currentDocument.setContentUpdateDate(new Date());
}
saveDocumentSetContextUser(currentDocument, comment);
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentInstanceOutputFilterStreamTest method testImportDocumentsWithoutLocaleAndRevision.
@Test
public void testImportDocumentsWithoutLocaleAndRevision() throws FilterException, XWikiException, ParseException {
DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
outputProperties.setVersionPreserved(true);
outputProperties.setVerbose(false);
importFromXML("document1-2", outputProperties);
XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
Assert.assertFalse(document.isNew());
Assert.assertEquals(Locale.ENGLISH, document.getDefaultLocale());
Assert.assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
Assert.assertEquals("customclass", document.getCustomClass());
Assert.assertEquals("title", document.getTitle());
Assert.assertEquals("defaultTemplate", document.getDefaultTemplate());
Assert.assertEquals("validationScript", document.getValidationScript());
Assert.assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
Assert.assertEquals(true, document.isHidden());
Assert.assertEquals("content", document.getContent());
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
Assert.assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
Assert.assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
Assert.assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
Assert.assertEquals(true, document.isMinorEdit());
Assert.assertEquals("comment", document.getComment());
Assert.assertEquals("1.1", document.getVersion());
// Attachment
Assert.assertEquals(1, document.getAttachmentList().size());
XWikiAttachment attachment = document.getAttachment("attachment.txt");
Assert.assertEquals("attachment.txt", attachment.getFilename());
Assert.assertEquals(10, attachment.getLongSize());
Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
Assert.assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
Assert.assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
Assert.assertEquals("1.1", attachment.getVersion());
Assert.assertEquals("attachment comment", attachment.getComment());
// XClass
BaseClass xclass = document.getXClass();
Assert.assertEquals("customClass", xclass.getCustomClass());
Assert.assertEquals("customMapping", xclass.getCustomMapping());
Assert.assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
Assert.assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
Assert.assertEquals("defaultWeb", xclass.getDefaultWeb());
Assert.assertEquals("nameField", xclass.getNameField());
Assert.assertEquals("validationScript", xclass.getValidationScript());
Assert.assertEquals(1, xclass.getFieldList().size());
NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
Assert.assertEquals("prop1", numberFiled.getName());
Assert.assertEquals(false, numberFiled.isDisabled());
Assert.assertEquals(1, numberFiled.getNumber());
Assert.assertEquals("long", numberFiled.getNumberType());
Assert.assertEquals("Prop1", numberFiled.getPrettyName());
Assert.assertEquals(30, numberFiled.getSize());
Assert.assertEquals(false, numberFiled.isUnmodifiable());
// Objects
Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
Assert.assertEquals(2, objects.size());
// Object 1
List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
Assert.assertEquals(1, documentObjects.size());
BaseObject documentObject = documentObjects.get(0);
Assert.assertEquals(0, documentObject.getNumber());
Assert.assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
Assert.assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
Assert.assertEquals(1, documentObject.getFieldList().size());
Assert.assertEquals(1, documentObject.getIntValue("prop1"));
// Object 2
List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
Assert.assertEquals(1, otherObjects.size());
BaseObject otherObject = otherObjects.get(0);
Assert.assertEquals(0, otherObject.getNumber());
Assert.assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
Assert.assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
Assert.assertEquals(1, otherObject.getFieldList().size());
Assert.assertEquals(2, otherObject.getIntValue("prop2"));
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentInstanceOutputFilterStreamTest method testDocumentwithattachmentwithoutdate.
@Test
public void testDocumentwithattachmentwithoutdate() throws FilterException, XWikiException {
DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
outputProperties.setVerbose(false);
importFromXML("documentwithattachmentwithoutdate", outputProperties);
XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
Assert.assertFalse(document.isNew());
// Attachment
Assert.assertEquals(1, document.getAttachmentList().size());
XWikiAttachment attachment = document.getAttachment("attachment.txt");
Assert.assertEquals("attachment.txt", attachment.getFilename());
Assert.assertEquals(10, attachment.getLongSize());
Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
Assert.assertNotNull(attachment.getDate());
Assert.assertEquals("1.1", attachment.getVersion());
Assert.assertEquals("", attachment.getComment());
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentInstanceOutputFilterStreamTest method testImportDocumentsPreserveVersion.
// Tests
@Test
public void testImportDocumentsPreserveVersion() throws FilterException, XWikiException, ParseException {
DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
outputProperties.setVersionPreserved(true);
outputProperties.setVerbose(false);
importFromXML("document1", outputProperties);
XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
Assert.assertFalse(document.isNew());
Assert.assertEquals(Locale.ENGLISH, document.getDefaultLocale());
Assert.assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
Assert.assertEquals("customclass", document.getCustomClass());
Assert.assertEquals("title", document.getTitle());
Assert.assertEquals("defaultTemplate", document.getDefaultTemplate());
Assert.assertEquals("validationScript", document.getValidationScript());
Assert.assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
Assert.assertEquals(true, document.isHidden());
Assert.assertEquals("content", document.getContent());
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
Assert.assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
Assert.assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
Assert.assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
Assert.assertEquals(true, document.isMinorEdit());
Assert.assertEquals("comment", document.getComment());
Assert.assertEquals("1.42", document.getVersion());
// Attachment
Assert.assertEquals(1, document.getAttachmentList().size());
XWikiAttachment attachment = document.getAttachment("attachment.txt");
Assert.assertEquals("attachment.txt", attachment.getFilename());
Assert.assertEquals(10, attachment.getLongSize());
Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
Assert.assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
Assert.assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
Assert.assertEquals("15.1", attachment.getVersion());
Assert.assertEquals("attachment comment", attachment.getComment());
// XClass
BaseClass xclass = document.getXClass();
Assert.assertEquals(1, xclass.getFieldList().size());
Assert.assertEquals("customClass", xclass.getCustomClass());
Assert.assertEquals("customMapping", xclass.getCustomMapping());
Assert.assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
Assert.assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
Assert.assertEquals("defaultWeb", xclass.getDefaultWeb());
Assert.assertEquals("nameField", xclass.getNameField());
Assert.assertEquals("validationScript", xclass.getValidationScript());
NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
Assert.assertEquals("prop1", numberFiled.getName());
Assert.assertEquals(false, numberFiled.isDisabled());
Assert.assertEquals(1, numberFiled.getNumber());
Assert.assertEquals("long", numberFiled.getNumberType());
Assert.assertEquals("Prop1", numberFiled.getPrettyName());
Assert.assertEquals(30, numberFiled.getSize());
Assert.assertEquals(false, numberFiled.isUnmodifiable());
// Objects
Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
Assert.assertEquals(2, objects.size());
// Object 1
List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
Assert.assertEquals(1, documentObjects.size());
BaseObject documentObject = documentObjects.get(0);
Assert.assertEquals(0, documentObject.getNumber());
Assert.assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
Assert.assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
Assert.assertEquals(1, documentObject.getFieldList().size());
Assert.assertEquals(1, documentObject.getIntValue("prop1"));
// Object 2
List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
Assert.assertEquals(1, otherObjects.size());
BaseObject otherObject = otherObjects.get(0);
Assert.assertEquals(0, otherObject.getNumber());
Assert.assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
Assert.assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
Assert.assertEquals(1, otherObject.getFieldList().size());
Assert.assertEquals(2, otherObject.getIntValue("prop2"));
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class VoidAttachmentVersioningStoreTest method testHistory.
public void testHistory() throws XWikiException {
XWikiDocument doc = new XWikiDocument(new DocumentReference("Wiki", "Main", "Test"));
XWikiAttachment attachment = new XWikiAttachment(doc, "filename");
// 1.1
attachment.setContent(new byte[] { 1 });
attachment.updateContentArchive(this.getContext());
assertEquals(attachment, attachment.getAttachmentRevision("1.1", this.getContext()));
// 1.2
attachment.setContent(new byte[] { 2 });
attachment.updateContentArchive(this.getContext());
assertEquals(attachment, attachment.getAttachmentRevision("1.2", this.getContext()));
// there should be only 1.2 version.
assertNull(attachment.getAttachmentRevision("1.1", this.getContext()));
assertNull(attachment.getAttachmentRevision("1.3", this.getContext()));
assertEquals(1, attachment.getVersions().length);
assertEquals(new Version(1, 2), attachment.getVersions()[0]);
}
Aggregations