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);
}
}
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);
}
}
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());
}
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);
}
}
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;
}
Aggregations