Search in sources :

Example 6 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class XWikiTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.xwiki = new com.xpn.xwiki.XWiki();
    getContext().setWiki(this.xwiki);
    this.xwiki.setConfig(new XWikiConfig());
    this.apiXWiki = new XWiki(this.xwiki, getContext());
    this.mockXWikiStore = mock(XWikiHibernateStore.class, new java.lang.Class[] { com.xpn.xwiki.XWiki.class, XWikiContext.class }, new java.lang.Object[] { this.xwiki, getContext() });
    this.mockXWikiStore.stubs().method("loadXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {

        @Override
        public java.lang.Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
            if (XWikiTest.this.docs.containsKey(shallowDoc.getName())) {
                return XWikiTest.this.docs.get(shallowDoc.getName());
            } else {
                return shallowDoc;
            }
        }
    });
    this.mockXWikiStore.stubs().method("saveXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") {

        @Override
        public java.lang.Object invoke(Invocation invocation) throws Throwable {
            XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
            document.setNew(false);
            document.setStore((XWikiStoreInterface) XWikiTest.this.mockXWikiStore.proxy());
            document.setId(rand.nextLong());
            XWikiTest.this.docs.put(document.getName(), document);
            return null;
        }
    });
    this.mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
    this.mockXWikiVersioningStore = mock(XWikiHibernateVersioningStore.class, new java.lang.Class[] { com.xpn.xwiki.XWiki.class, XWikiContext.class }, new java.lang.Object[] { this.xwiki, getContext() });
    this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(new XWikiDocumentArchive()));
    this.mockXWikiVersioningStore.stubs().method("saveXWikiDocArchive").will(returnValue(null));
    this.mockXWikiRightService = mock(XWikiRightServiceImpl.class, new java.lang.Class[] {}, new java.lang.Object[] {});
    this.mockXWikiRightService.stubs().method("hasAccessLevel").will(returnValue(true));
    this.mockXWikiRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
    this.xwiki.setStore((XWikiStoreInterface) this.mockXWikiStore.proxy());
    this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) this.mockXWikiVersioningStore.proxy());
    this.xwiki.setRightService((XWikiRightService) this.mockXWikiRightService.proxy());
    getContext().setUser("Redtail");
    this.apiDocument = new Document(new XWikiDocument(new DocumentReference("Wiki", "MilkyWay", "Fidis")), getContext());
    this.apiDocument.getDocument().setCreator("c" + getContext().getUser());
    this.apiDocument.getDocument().setAuthor("a" + getContext().getUser());
    this.apiDocument.save();
    getContext().setUser("Earth");
}
Also used : Invocation(org.jmock.core.Invocation) XWikiHibernateVersioningStore(com.xpn.xwiki.store.XWikiHibernateVersioningStore) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) CustomStub(org.jmock.core.stub.CustomStub) XWikiRightServiceImpl(com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl) XWikiConfig(com.xpn.xwiki.XWikiConfig) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 7 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class XWikiHibernateVersioningStore method getXWikiDocumentArchive.

@Override
public XWikiDocumentArchive getXWikiDocumentArchive(XWikiDocument doc, XWikiContext inputxcontext) throws XWikiException {
    XWikiDocumentArchive archiveDoc = doc.getDocumentArchive();
    if (archiveDoc != null) {
        return archiveDoc;
    }
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    String db = context.getWikiId();
    try {
        if (doc.getDatabase() != null) {
            context.setWikiId(doc.getDatabase());
        }
        archiveDoc = new XWikiDocumentArchive(doc.getId());
        loadXWikiDocArchive(archiveDoc, true, context);
        doc.setDocumentArchive(archiveDoc);
    } finally {
        context.setWikiId(db);
        restoreExecutionXContext();
    }
    return archiveDoc;
}
Also used : XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 8 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class XWiki method copyDocument.

/**
 * @since 2.2M2
 */
public boolean copyDocument(DocumentReference sourceDocumentReference, DocumentReference targetDocumentReference, String wikilocale, boolean reset, boolean force, boolean resetCreationData, XWikiContext context) throws XWikiException {
    String db = context.getWikiId();
    String sourceWiki = sourceDocumentReference.getWikiReference().getName();
    String targetWiki = targetDocumentReference.getWikiReference().getName();
    String sourceStringReference = getDefaultEntityReferenceSerializer().serialize(sourceDocumentReference);
    try {
        context.setWikiId(sourceWiki);
        XWikiDocument sdoc = getDocument(sourceDocumentReference, context);
        if (!sdoc.isNew()) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Copying document [" + sourceDocumentReference + "] to [" + targetDocumentReference + "]");
            }
            // Let's switch to the other database to verify if the document already exists
            context.setWikiId(targetWiki);
            XWikiDocument tdoc = getDocument(targetDocumentReference, context);
            // There is already an existing document
            if (!tdoc.isNew()) {
                if (force) {
                    // We need to delete the previous document
                    deleteDocument(tdoc, context);
                } else {
                    return false;
                }
            }
            // Let's switch back again to the original db
            context.setWikiId(sourceWiki);
            if (wikilocale == null) {
                tdoc = sdoc.copyDocument(targetDocumentReference, context);
                // We know the target document doesn't exist and we want to save the attachments without
                // incrementing their versions.
                // See XWIKI-8157: The "Copy Page" action adds an extra version to the attached file
                tdoc.setNew(true);
                // forget past versions
                if (reset) {
                    tdoc.setVersion("1.1");
                }
                if (resetCreationData) {
                    Date now = new Date();
                    tdoc.setCreationDate(now);
                    tdoc.setContentUpdateDate(now);
                    tdoc.setDate(now);
                    tdoc.setCreatorReference(context.getUserReference());
                    tdoc.setAuthorReference(context.getUserReference());
                }
                // We don't want to trigger a new version otherwise the version number will be wrong.
                tdoc.setMetaDataDirty(false);
                tdoc.setContentDirty(false);
                saveDocument(tdoc, "Copied from " + sourceStringReference, context);
                if (!reset) {
                    context.setWikiId(sourceWiki);
                    XWikiDocumentArchive txda = getVersioningStore().getXWikiDocumentArchive(sdoc, context);
                    context.setWikiId(targetWiki);
                    txda = txda.clone(tdoc.getId(), context);
                    getVersioningStore().saveXWikiDocArchive(txda, true, context);
                } else {
                    context.setWikiId(targetWiki);
                    getVersioningStore().resetRCSArchive(tdoc, true, context);
                }
                // Now we need to copy the translations
                context.setWikiId(sourceWiki);
                List<String> tlist = sdoc.getTranslationList(context);
                for (String clanguage : tlist) {
                    XWikiDocument stdoc = sdoc.getTranslatedDocument(clanguage, context);
                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info("Copying document [" + sourceWiki + "], language [" + clanguage + "] to [" + targetDocumentReference + "]");
                    }
                    context.setWikiId(targetWiki);
                    XWikiDocument ttdoc = tdoc.getTranslatedDocument(clanguage, context);
                    // There is already an existing document
                    if (ttdoc != tdoc) {
                        return false;
                    }
                    // Let's switch back again to the original db
                    context.setWikiId(sourceWiki);
                    ttdoc = stdoc.copyDocument(targetDocumentReference, context);
                    // forget past versions
                    if (reset) {
                        ttdoc.setNew(true);
                        ttdoc.setVersion("1.1");
                    }
                    if (resetCreationData) {
                        Date now = new Date();
                        ttdoc.setCreationDate(now);
                        ttdoc.setContentUpdateDate(now);
                        ttdoc.setDate(now);
                        ttdoc.setCreatorReference(context.getUserReference());
                        ttdoc.setAuthorReference(context.getUserReference());
                    }
                    // we don't want to trigger a new version
                    // otherwise the version number will be wrong
                    tdoc.setMetaDataDirty(false);
                    tdoc.setContentDirty(false);
                    saveDocument(ttdoc, "Copied from " + sourceStringReference, context);
                    if (!reset) {
                        context.setWikiId(sourceWiki);
                        XWikiDocumentArchive txda = getVersioningStore().getXWikiDocumentArchive(sdoc, context);
                        context.setWikiId(targetWiki);
                        txda = txda.clone(tdoc.getId(), context);
                        getVersioningStore().saveXWikiDocArchive(txda, true, context);
                    } else {
                        getVersioningStore().resetRCSArchive(tdoc, true, context);
                    }
                }
            } else {
                // We want only one language in the end
                XWikiDocument stdoc = sdoc.getTranslatedDocument(wikilocale, context);
                tdoc = stdoc.copyDocument(targetDocumentReference, context);
                // We know the target document doesn't exist and we want to save the attachments without
                // incrementing their versions.
                // See XWIKI-8157: The "Copy Page" action adds an extra version to the attached file
                tdoc.setNew(true);
                // forget language
                tdoc.setDefaultLanguage(wikilocale);
                tdoc.setLanguage("");
                // forget past versions
                if (reset) {
                    tdoc.setVersion("1.1");
                }
                if (resetCreationData) {
                    Date now = new Date();
                    tdoc.setCreationDate(now);
                    tdoc.setContentUpdateDate(now);
                    tdoc.setDate(now);
                    tdoc.setCreatorReference(context.getUserReference());
                    tdoc.setAuthorReference(context.getUserReference());
                }
                // we don't want to trigger a new version
                // otherwise the version number will be wrong
                tdoc.setMetaDataDirty(false);
                tdoc.setContentDirty(false);
                saveDocument(tdoc, "Copied from " + sourceStringReference, context);
                if (!reset) {
                    context.setWikiId(sourceWiki);
                    XWikiDocumentArchive txda = getVersioningStore().getXWikiDocumentArchive(sdoc, context);
                    context.setWikiId(targetWiki);
                    txda = txda.clone(tdoc.getId(), context);
                    getVersioningStore().saveXWikiDocArchive(txda, true, context);
                } else {
                    getVersioningStore().resetRCSArchive(tdoc, true, context);
                }
            }
        }
        return true;
    } finally {
        context.setWikiId(db);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) Date(java.util.Date)

Aggregations

XWikiDocumentArchive (com.xpn.xwiki.doc.XWikiDocumentArchive)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 XWikiException (com.xpn.xwiki.XWikiException)3 Version (org.suigeneris.jrcs.rcs.Version)3 HibernateException (org.hibernate.HibernateException)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiConfig (com.xpn.xwiki.XWikiConfig)1 XWikiRCSNodeInfo (com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiHibernateVersioningStore (com.xpn.xwiki.store.XWikiHibernateVersioningStore)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiRightServiceImpl (com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 Date (java.util.Date)1 Invocation (org.jmock.core.Invocation)1 CustomStub (org.jmock.core.stub.CustomStub)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 BatchOperationExecutor (org.xwiki.refactoring.batch.BatchOperationExecutor)1