Search in sources :

Example 21 with WikiManagerException

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;
}
Also used : WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiPropertyGroupManager(org.xwiki.wiki.internal.descriptor.properties.WikiPropertyGroupManager) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 22 with WikiManagerException

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;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 23 with WikiManagerException

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);
    }
}
Also used : XWiki(com.xpn.xwiki.XWiki) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 24 with WikiManagerException

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);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 25 with WikiManagerException

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;
}
Also used : WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiReference(org.xwiki.model.reference.WikiReference) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) AuthorizationException(org.xwiki.security.authorization.AuthorizationException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)40 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)17 Test (org.junit.Test)13 XWikiContext (com.xpn.xwiki.XWikiContext)12 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)11 XWiki (com.xpn.xwiki.XWiki)7 XWikiException (com.xpn.xwiki.XWikiException)7 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)6 WikiTemplateManagerException (org.xwiki.wiki.template.WikiTemplateManagerException)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 WikiPropertyGroupException (org.xwiki.wiki.properties.WikiPropertyGroupException)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 WikiCreationException (org.xwiki.platform.wiki.creationjob.WikiCreationException)4 WikiTemplatePropertyGroup (org.xwiki.wiki.template.WikiTemplatePropertyGroup)4 ExtensionId (org.xwiki.extension.ExtensionId)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 WikiReference (org.xwiki.model.reference.WikiReference)3 Query (org.xwiki.query.Query)3 QueryException (org.xwiki.query.QueryException)3 DefaultWikiDescriptor (org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor)3