Search in sources :

Example 56 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class DefaultSearchSuggestCustomConfigDeleterTest method deleteSearchSuggestCustomConfig.

@Test
public void deleteSearchSuggestCustomConfig() throws Exception {
    XWikiDocument searchSuggestConfigDoc = mock(XWikiDocument.class);
    when(xwiki.getDocument(eq(new DocumentReference("mainWiki", "XWiki", "SearchSuggestConfig")), any(XWikiContext.class))).thenReturn(searchSuggestConfigDoc);
    BaseObject objConfig1 = mock(BaseObject.class);
    BaseObject objConfig2 = mock(BaseObject.class);
    BaseObject objConfig3 = mock(BaseObject.class);
    BaseObject objConfig4 = mock(BaseObject.class);
    BaseObject objConfig5 = mock(BaseObject.class);
    BaseObject objConfig6 = mock(BaseObject.class);
    List<BaseObject> objects = new ArrayList<BaseObject>();
    objects.add(objConfig1);
    objects.add(objConfig2);
    objects.add(objConfig3);
    // null objects can be present in the list
    objects.add(null);
    objects.add(objConfig4);
    objects.add(objConfig5);
    objects.add(objConfig6);
    when(searchSuggestConfigDoc.getXObjects(eq(new DocumentReference("mainWiki", "XWiki", "SearchSuggestSourceClass")))).thenReturn(objects);
    // Object 1
    when(objConfig1.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
    when(objConfig1.getStringValue("engine")).thenReturn("solr");
    when(objConfig1.getStringValue("query")).thenReturn("class:XWiki.XWikiServerClass AND " + "propertyname:wikiprettyname AND propertyvalue__:(__INPUT__*)");
    when(objConfig1.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestSolrService");
    // Object 2
    when(objConfig2.getStringValue("name")).thenReturn("Bad name");
    when(objConfig2.getStringValue("engine")).thenReturn("solr");
    when(objConfig2.getStringValue("query")).thenReturn("class:XWiki.XWikiServerClass AND " + "propertyname:wikiprettyname AND propertyvalue__:(__INPUT__*)");
    when(objConfig2.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestSolrService");
    // Object 3
    when(objConfig3.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
    when(objConfig3.getStringValue("engine")).thenReturn("lucene");
    when(objConfig3.getStringValue("query")).thenReturn("XWiki.XWikiServerClass.wikiprettyname:__INPUT__* AND " + "object:WorkspaceManager.WorkspaceClass");
    when(objConfig3.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestLuceneService");
    // Object 4
    when(objConfig4.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
    when(objConfig4.getStringValue("engine")).thenReturn(null);
    when(objConfig4.getStringValue("query")).thenReturn("XWiki.XWikiServerClass.wikiprettyname:__INPUT__* AND " + "object:WorkspaceManager.WorkspaceClass");
    when(objConfig4.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestLuceneService");
    // Object 5
    when(objConfig5.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
    when(objConfig5.getStringValue("engine")).thenReturn(null);
    when(objConfig5.getStringValue("query")).thenReturn("bad query");
    when(objConfig5.getStringValue("url")).thenReturn("xwiki:WorkspaceManager.WorkspacesSuggestLuceneService");
    // Object 6
    when(objConfig6.getStringValue("name")).thenReturn("platform.workspace.searchSuggestSourceWorkspaces");
    when(objConfig6.getStringValue("engine")).thenReturn(null);
    when(objConfig6.getStringValue("query")).thenReturn("XWiki.XWikiServerClass.wikiprettyname:__INPUT__* AND " + "object:WorkspaceManager.WorkspaceClass");
    when(objConfig6.getStringValue("url")).thenReturn("bad URL");
    // Run
    mocker.getComponentUnderTest().deleteSearchSuggestCustomConfig("mainWiki");
    // Verify that the good objects has been removed
    verify(searchSuggestConfigDoc).removeXObject(objConfig1);
    verify(searchSuggestConfigDoc).removeXObject(objConfig3);
    // Verify that the document have been saved
    verify(xwiki).saveDocument(searchSuggestConfigDoc, "Remove object previously introduced by WorkspaceManager.Install", xcontext);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 57 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class WorkspaceMigrationTest method errorWhenRestoringFromXAR.

@Test
public void errorWhenRestoringFromXAR() throws Exception {
    // Mocks about the descriptor
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("workspace");
    XWikiDocument oldDescriptorDocument = mock(XWikiDocument.class);
    when(xwiki.getDocument(eq(new DocumentReference("mainWiki", XWiki.SYSTEM_SPACE, "XWikiServerWorkspace")), any(XWikiContext.class))).thenReturn(oldDescriptorDocument);
    // Mocks about the old workspace object
    BaseObject oldWorkspaceObject = mock(BaseObject.class);
    when(oldDescriptorDocument.getXObject(eq(new DocumentReference("mainWiki", "WorkspaceManager", "WorkspaceClass")))).thenReturn(oldWorkspaceObject);
    doThrow(new XWikiException()).when(documentRestorerFromAttachedXAR).restoreDocumentFromAttachedXAR(any(DocumentReference.class), any(String.class), any(List.class));
    // Run
    mocker.getComponentUnderTest().hibernateMigrate();
    // Verify
    verify(mocker.getMockedLogger()).error(eq("Error while restoring documents from the Workspace XAR"), any(XWikiException.class));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 58 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class WikiTemplatePropertyGroupProvider method save.

@Override
public void save(WikiPropertyGroup group, String wikiId) throws WikiPropertyGroupException {
    XWikiContext context = xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    WikiTemplatePropertyGroup templateGroup = (WikiTemplatePropertyGroup) group;
    try {
        XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
        BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS, true, context);
        object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, templateGroup.isTemplate() ? 1 : 0);
        // The document must have a creator
        if (descriptorDocument.getCreatorReference() == null) {
            descriptorDocument.setCreatorReference(context.getUserReference());
        }
        // The document must have an author
        if (descriptorDocument.getAuthorReference() == null) {
            descriptorDocument.setAuthorReference(context.getUserReference());
        }
        xwiki.saveDocument(descriptorDocument, String.format("Changed property group [%s].", GROUP_NAME), context);
    } catch (WikiManagerException e) {
        throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
    } catch (XWikiException e) {
        throw new WikiPropertyGroupException("Unable to save descriptor document.", e);
    }
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 59 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class WikiTemplateMigration method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    // XWiki objects
    XWikiContext context = getXWikiContext();
    XWiki xwiki = context.getWiki();
    // WikiManager.WikiTemplateClass reference
    DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME);
    // XWiki.XWikiServerClass reference
    DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass");
    // Superadmin reference
    DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin");
    try {
        // Get all the descriptor documents
        String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj";
        Query query = queryManager.createQuery(statement, Query.XWQL);
        List<String> results = query.execute();
        for (String wikiPage : results) {
            XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context);
            // Get the "iswikitemplate" value
            BaseObject descriptorObject = document.getXObject(descriptorClassReference);
            int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0);
            // We remove the deprecated property from the descriptor
            descriptorObject.removeField(OLD_TEMPLATE_PROPERTY);
            // Add the new WikiManager.WikiTemplateClass object
            BaseObject object = document.getXObject(templateClassReference, true, context);
            // The new object might already exists and have a template property already set
            isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // Set the (new) value
            object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // The document must have an author
            document.setAuthorReference(superAdmin);
            // Save the document
            xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context);
        }
    } catch (QueryException e) {
        throw new DataMigrationException("Failed to get the list of all existing descriptors.", e);
    } catch (XWikiException e) {
        throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 60 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class WikiTemplatePropertyGroupProviderTest method get.

@Test
public void get() throws Exception {
    XWikiDocument descriptorDocument = mock(XWikiDocument.class);
    when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId")).thenReturn(descriptorDocument);
    BaseObject object = mock(BaseObject.class);
    when(descriptorDocument.getXObject(eq(WikiTemplateClassDocumentInitializer.SERVER_CLASS))).thenReturn(object);
    when(object.getIntValue("iswikitemplate", 0)).thenReturn(1);
    // Test
    WikiPropertyGroup result = mocker.getComponentUnderTest().get("wikiId");
    // Verify
    assertEquals(true, result.get("isTemplate"));
    assertTrue(result instanceof WikiTemplatePropertyGroup);
    assertTrue(((WikiTemplatePropertyGroup) result).isTemplate());
    XWikiDocument descriptorDocument2 = mock(XWikiDocument.class);
    when(wikiDescriptorDocumentHelper.getDocumentFromWikiId("wikiId2")).thenReturn(descriptorDocument2);
    BaseObject object2 = mock(BaseObject.class);
    when(descriptorDocument2.getXObject(eq(WikiTemplateClassDocumentInitializer.SERVER_CLASS))).thenReturn(object2);
    when(object2.getIntValue("iswikitemplate", 0)).thenReturn(0);
    // Test
    WikiPropertyGroup result2 = mocker.getComponentUnderTest().get("wikiId2");
    // Verify
    assertEquals(false, result2.get("isTemplate"));
    assertTrue(result2 instanceof WikiTemplatePropertyGroup);
    assertFalse(((WikiTemplatePropertyGroup) result2).isTemplate());
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) WikiPropertyGroup(org.xwiki.wiki.properties.WikiPropertyGroup) Test(org.junit.Test)

Aggregations

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16