Search in sources :

Example 1 with WikiTemplatePropertyGroup

use of org.xwiki.wiki.template.WikiTemplatePropertyGroup in project xwiki-platform by xwiki.

the class DefaultWikiTemplateManager method setTemplate.

@Override
public void setTemplate(String wikiId, boolean value) throws WikiTemplateManagerException {
    try {
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Get the property group
        WikiTemplatePropertyGroup group = (WikiTemplatePropertyGroup) descriptor.getPropertyGroup(WikiTemplatePropertyGroupProvider.GROUP_NAME);
        // Set the value
        group.setTemplate(value);
        // Save the property groups
        templateWikiPropertyGroupProvider.save(group, wikiId);
    } catch (WikiPropertyGroupException e) {
        throw new WikiTemplateManagerException(String.format("Failed to save the property group [%s]", WikiTemplatePropertyGroupProvider.GROUP_NAME), e);
    } catch (WikiManagerException e) {
        throw new WikiTemplateManagerException(String.format(errorMessageNoDescriptor, wikiId), e);
    }
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) WikiTemplateManagerException(org.xwiki.wiki.template.WikiTemplateManagerException)

Example 2 with WikiTemplatePropertyGroup

use of org.xwiki.wiki.template.WikiTemplatePropertyGroup 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 3 with WikiTemplatePropertyGroup

use of org.xwiki.wiki.template.WikiTemplatePropertyGroup 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)

Example 4 with WikiTemplatePropertyGroup

use of org.xwiki.wiki.template.WikiTemplatePropertyGroup in project xwiki-platform by xwiki.

the class DefaultWikiTemplateManager method isTemplate.

@Override
public boolean isTemplate(String wikiId) throws WikiTemplateManagerException {
    try {
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Get the property group
        WikiTemplatePropertyGroup group = (WikiTemplatePropertyGroup) descriptor.getPropertyGroup(WikiTemplatePropertyGroupProvider.GROUP_NAME);
        // Return the value
        return group.isTemplate();
    } catch (WikiManagerException e) {
        throw new WikiTemplateManagerException(String.format(errorMessageNoDescriptor, wikiId), e);
    }
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) WikiTemplateManagerException(org.xwiki.wiki.template.WikiTemplateManagerException)

Example 5 with WikiTemplatePropertyGroup

use of org.xwiki.wiki.template.WikiTemplatePropertyGroup in project xwiki-platform by xwiki.

the class WikiTemplatePropertyGroupProvider method get.

@Override
public WikiPropertyGroup get(String wikiId) throws WikiPropertyGroupException {
    WikiTemplatePropertyGroup group = new WikiTemplatePropertyGroup(GROUP_NAME);
    try {
        XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
        // Get the object
        BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS);
        if (object != null) {
            group.setTemplate(object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, 0) != 0);
        }
    } catch (WikiManagerException e) {
        throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
    }
    return group;
}
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) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

WikiTemplatePropertyGroup (org.xwiki.wiki.template.WikiTemplatePropertyGroup)5 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 WikiPropertyGroupException (org.xwiki.wiki.properties.WikiPropertyGroupException)3 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)2 WikiTemplateManagerException (org.xwiki.wiki.template.WikiTemplateManagerException)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 Test (org.junit.Test)1 WikiPropertyGroup (org.xwiki.wiki.properties.WikiPropertyGroup)1