use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.
the class DefaultWikiDescriptorBuilder method save.
@Override
public XWikiDocument save(WikiDescriptor descriptor) throws WikiDescriptorBuilderException {
XWikiContext context = xcontextProvider.get();
XWiki xwiki = context.getWiki();
XWikiDocument descriptorDoc = null;
try {
// Create the descriptor document
descriptorDoc = wikiDescriptorDocumentHelper.getDocumentFromWikiId(descriptor.getId());
// Create the server class object
BaseObject obj = descriptorDoc.getXObject(DefaultWikiDescriptor.SERVER_CLASS, true, context);
obj.set(XWikiServerClassDocumentInitializer.FIELD_SERVER, descriptor.getDefaultAlias(), context);
obj.set(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE, referenceSerializer.serialize(descriptor.getMainPageReference()), context);
obj.set(XWikiServerClassDocumentInitializer.FIELD_OWNER, getFullReference(descriptor.getOwnerId(), descriptor.getId()), context);
obj.set(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, descriptor.getPrettyName(), context);
obj.set(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION, descriptor.getDescription(), context);
// Create the aliases
List<String> aliases = descriptor.getAliases();
for (int i = 1; i < aliases.size(); ++i) {
String alias = aliases.get(i);
BaseObject objAlias = descriptorDoc.getXObject(DefaultWikiDescriptor.SERVER_CLASS, XWikiServerClassDocumentInitializer.FIELD_SERVER, alias, true);
objAlias.set(XWikiServerClassDocumentInitializer.FIELD_SERVER, alias, context);
}
// Set the meta-data (creator, hidden flag, parent, etc...)
setDescriptorDocMetadata(descriptorDoc);
// Save the document
xwiki.saveDocument(descriptorDoc, context);
// Save the property groups
WikiPropertyGroupManager wikiPropertyGroupManager = wikiPropertyGroupManagerProvider.get();
wikiPropertyGroupManager.saveForDescriptor(descriptor);
} catch (WikiManagerException e) {
throw new WikiDescriptorBuilderException("Unable to load the descriptor document", e);
} catch (XWikiException e) {
throw new WikiDescriptorBuilderException("Unable to save the descriptor document", e);
} catch (WikiPropertyGroupException e) {
throw new WikiDescriptorBuilderException("Unable to save the property groups", e);
}
return descriptorDoc;
}
use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.
the class DefaultWikiDescriptorDocumentHelper method findXWikiServerClassDocumentReference.
@Override
public DocumentReference findXWikiServerClassDocumentReference(String wikiAlias) throws WikiManagerException {
if (wikiAlias == null) {
throw new IllegalArgumentException("Wiki alias cannot be null");
}
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
DocumentReference result = null;
try {
Query query = this.queryManager.createQuery("where doc.object(XWiki.XWikiServerClass).server = :wikiAlias and doc.name like 'XWikiServer%'", Query.XWQL);
query.bindValue("wikiAlias", wikiAlias);
query.setWiki(wikiDescriptorManager.getMainWikiId());
List<String> documentNames = query.execute();
// Resolve the document name into a references
if (documentNames != null && !documentNames.isEmpty()) {
result = documentReferenceResolver.resolve(documentNames.get(0));
}
} catch (QueryException e) {
throw new WikiManagerException(String.format("Failed to locate XWiki.XWikiServerClass document for wiki alias [%s]", wikiAlias), e);
}
return result;
}
use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.
the class DefaultWikiDescriptorDocumentHelper method getDocument.
private XWikiDocument getDocument(DocumentReference reference) throws WikiManagerException {
XWikiContext context = xcontextProvider.get();
com.xpn.xwiki.XWiki xwiki = context.getWiki();
try {
return xwiki.getDocument(reference, context);
} catch (XWikiException e) {
throw new WikiManagerException(String.format("Failed to get document [%s] containing a XWiki.XWikiServerClass object", reference), e);
}
}
use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.
the class DefaultWikiCopier method copyDocuments.
@Override
public void copyDocuments(String fromWikiId, String toWikiId, boolean withHistory) throws WikiManagerException {
XWikiContext context = xcontextProvider.get();
XWiki xwiki = context.getWiki();
this.progress.pushLevelProgress(2, this);
try {
// Get documents
this.progress.startStep(this, "Get documents to copy");
Query query = queryManager.createQuery("select distinct doc.fullName from Document as doc", Query.XWQL);
query.setWiki(fromWikiId);
List<String> documentFullnames = query.execute();
this.progress.endStep(this);
// Copy documents
this.progress.startStep(this, "Copy documents");
this.progress.pushLevelProgress(documentFullnames.size(), this);
WikiReference fromWikiReference = new WikiReference(fromWikiId);
try {
for (String documentFullName : documentFullnames) {
this.progress.startStep(this);
DocumentReference origDocReference = documentReferenceResolver.resolve(documentFullName, fromWikiReference);
DocumentReference newDocReference = origDocReference.setWikiReference(new WikiReference(toWikiId));
logger.info("Copying document [{}] to [{}].", origDocReference, newDocReference);
xwiki.copyDocument(origDocReference, newDocReference, null, !withHistory, true, context);
logger.info("Done copying document [{}] to [{}].", origDocReference, newDocReference);
this.progress.endStep(this);
}
} finally {
this.progress.popLevelProgress(this);
this.progress.endStep(this);
}
} catch (QueryException e) {
WikiManagerException thrownException = new WikiManagerException("Unable to get the list of wiki documents to copy.", e);
logger.error(thrownException.getMessage(), thrownException);
throw thrownException;
} catch (XWikiException e) {
WikiManagerException thrownException = new WikiManagerException("Failed to copy documents.", e);
logger.error(thrownException.getMessage(), thrownException);
throw thrownException;
} finally {
this.progress.popLevelProgress(this);
}
}
use of org.xwiki.wiki.manager.WikiManagerException in project xwiki-platform by xwiki.
the class WikiManagerScriptService method canDeleteWiki.
/**
* Test if a given user can delete a given wiki.
*
* @param userId the id of the user to test
* @param wikiId the id of the wiki
* @return whether or not the user can delete the specified wiki
*/
public boolean canDeleteWiki(String userId, String wikiId) {
try {
// Get target wiki descriptor
WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
if (descriptor == null) {
error(new Exception(String.format("Could not find descriptor for wiki [%s]]", wikiId)));
return false;
}
// Get the full reference of the given user
DocumentReference userReference = documentReferenceResolver.resolve(userId);
String fullUserId = entityReferenceSerializer.serialize(userReference);
// If the user is the owner
String owner = descriptor.getOwnerId();
if (fullUserId.equals(owner)) {
return true;
}
// If the user is an admin
WikiReference wikiReference = new WikiReference(wikiId);
if (authorizationManager.hasAccess(Right.ADMIN, userReference, wikiReference)) {
return true;
}
} catch (WikiManagerException e) {
error(String.format("Error while getting the descriptor of wiki [%s]", wikiId), e);
}
return false;
}
Aggregations