Search in sources :

Example 11 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class DocumentInstanceInputEventGenerator method beginWikiDocument.

@Override
public void beginWikiDocument(String name, FilterEventParameters parameters) throws FilterException {
    super.beginWikiDocument(name, parameters);
    DocumentReference reference = new DocumentReference(this.currentReference);
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument defaultDocument;
    try {
        defaultDocument = xcontext.getWiki().getDocument(reference, xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get document [" + reference + "]", e);
    }
    // Default document locale
    this.documentLocaleParser.write(defaultDocument, this.filter, this.properties);
    List<Locale> locales;
    try {
        locales = defaultDocument.getTranslationLocales(xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get translations of document [" + reference + "]", e);
    }
    // Translations
    for (Locale locale : locales) {
        try {
            XWikiDocument translationDocument = defaultDocument.getTranslatedDocument(locale, xcontext);
            this.documentLocaleParser.write(translationDocument, this.filter, this.properties);
        } catch (XWikiException e) {
            throw new FilterException("Failed to get document [" + reference + "] for locale [" + locale + "]", e);
        }
    }
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 12 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class DocumentInstanceOutputFilterStream method maybeSaveDocument.

private void maybeSaveDocument() throws FilterException {
    XWikiDocument inputDocument = this.documentListener.getEntity();
    this.documentListener.setEntity(null);
    if (this.currentRevisionParameters == null) {
        return;
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument document = xcontext.getWiki().getDocument(inputDocument.getDocumentReferenceWithLocale(), xcontext);
        if (!this.documentDeleted && !document.isNew() && this.properties.isPreviousDeleted()) {
            XWikiDocument originalDocument = document;
            // Save current context wiki
            WikiReference currentWiki = xcontext.getWikiReference();
            try {
                // Make sure the store is executed in the right context
                xcontext.setWikiReference(document.getDocumentReference().getWikiReference());
                // Put previous version in recycle bin
                if (xcontext.getWiki().hasRecycleBin(xcontext)) {
                    xcontext.getWiki().getRecycleBinStore().saveToRecycleBin(document, xcontext.getUser(), new Date(), xcontext, true);
                }
                // Make sure to not generate DocumentDeletedEvent since from listener point of view it's not
                xcontext.getWiki().getStore().deleteXWikiDoc(document, xcontext);
                this.documentDeleted = true;
            } finally {
                // Restore current context wiki
                xcontext.setWikiReference(currentWiki);
            }
            document = xcontext.getWiki().getDocument(inputDocument.getDocumentReferenceWithLocale(), xcontext);
            // Remember deleted document as the actual previous version of the document (to simulate an update
            // instead of a creation)
            document.setOriginalDocument(originalDocument);
        } else {
            // Make sure to remember that the document should not be deleted anymore
            this.documentDeleted = true;
        }
        // Remember if it's a creation or an update
        boolean isnew = document.isNew();
        // Safer to clone for thread safety and in case the save fail
        document = document.clone();
        document.loadAttachmentsContentSafe(xcontext);
        document.apply(inputDocument);
        // Get the version from the input document
        document.setMinorEdit(inputDocument.isMinorEdit());
        if (!this.properties.isAuthorPreserved()) {
            if (this.properties.isAuthorSet()) {
                setAuthorReference(document, this.properties.getAuthor());
            } else {
                setAuthorReference(document, xcontext.getUserReference());
            }
            document.setContentAuthorReference(document.getAuthorReference());
            if (document.isNew()) {
                document.setCreatorReference(document.getAuthorReference());
            }
        } else {
            setAuthors(document, inputDocument);
        }
        if (this.properties.isVersionPreserved()) {
            // Make sure to use metadata coming from the input document
            document.setVersion(inputDocument.getVersion());
            document.setDate(inputDocument.getDate());
            document.setContentUpdateDate(inputDocument.getContentUpdateDate());
            for (XWikiAttachment attachment : document.getAttachmentList()) {
                attachment.setVersion(inputDocument.getAttachment(attachment.getFilename()).getVersion());
            }
            if (document.isNew()) {
                document.setCreationDate(inputDocument.getCreationDate());
                document.setDocumentArchive(inputDocument.getDocumentArchive());
            }
            // Make sure the document won't be modified by the store
            document.setMetaDataDirty(false);
            document.setContentDirty(false);
            xcontext.getWiki().saveDocument(document, inputDocument.getComment(), inputDocument.isMinorEdit(), xcontext);
        } else {
            // Forget the input history to let the store do its standard job
            document.setDocumentArchive((XWikiDocumentArchive) null);
            xcontext.getWiki().saveDocument(document, this.properties.getSaveComment(), xcontext);
        }
        if (this.properties.isVerbose()) {
            if (isnew) {
                this.logger.info(LOG_DOCUMENT_CREATED, "Created document [{}]", document.getDocumentReferenceWithLocale());
            } else {
                this.logger.info(LOG_DOCUMENT_UPDATED, "Updated document [{}]", document.getDocumentReferenceWithLocale());
            }
        }
    } catch (Exception e) {
        this.logger.error(LOG_DOCUMENT_FAILSAVE, "Failed to save document [{}]", inputDocument.getDocumentReferenceWithLocale(), e);
        if (this.properties.isStoppedWhenSaveFail()) {
            throw new FilterException("Failed to save document", e);
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) WikiReference(org.xwiki.model.reference.WikiReference) Date(java.util.Date) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException)

Example 13 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class UserInstanceOutputFilterStream method endGroupContainer.

@Override
public void endGroupContainer(String name, FilterEventParameters parameters) throws FilterException {
    XWikiContext xcontext = this.xcontextProvider.get();
    XWikiDocument groupDocument;
    try {
        groupDocument = getGroupDocument(name);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get an XWikiDocument for group name [" + name + "]", e);
    }
    BaseClass groupClass;
    try {
        groupClass = xcontext.getWiki().getGroupClass(xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get group class", e);
    }
    if (this.members.isEmpty()) {
        // Put an empty member so that the document is "marked" as group
        addMember("", groupDocument, groupClass, xcontext);
    } else {
        for (String member : this.members) {
            addMember(member, groupDocument, groupClass, xcontext);
        }
    }
    // Save
    try {
        xcontext.getWiki().saveDocument(groupDocument, this.properties.getSaveComment(), xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to save group document", e);
    }
    // Reset members
    this.members = null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) XWikiException(com.xpn.xwiki.XWikiException)

Example 14 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class UserInstanceOutputFilterStream method beginUser.

@Override
public void beginUser(String name, FilterEventParameters parameters) throws FilterException {
    XWikiDocument userDocument;
    try {
        userDocument = getUserDocument(name);
        // Safer to clone for thread safety and in case the save fail
        userDocument = userDocument.clone();
    } catch (XWikiException e) {
        throw new FilterException("Failed to get an XWikiDocument for user name [" + name + "]", e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // First name
    if (parameters.containsKey(PARAMETER_FIRSTNAME)) {
        map.put("first_name", getString(PARAMETER_FIRSTNAME, parameters, ""));
    }
    // Last name
    if (parameters.containsKey(PARAMETER_LASTNAME)) {
        map.put("last_name", getString(PARAMETER_LASTNAME, parameters, ""));
    }
    // Email
    if (parameters.containsKey(PARAMETER_EMAIL)) {
        map.put("email", getString(PARAMETER_EMAIL, parameters, ""));
    }
    // Active
    map.put("active", getBoolean(PARAMETER_ACTIVE, parameters, true) ? 1 : 0);
    XWikiContext xcontext = this.xcontextProvider.get();
    BaseClass userClass;
    try {
        userClass = xcontext.getWiki().getUserClass(xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to get user class", e);
    }
    BaseObject userObject = userDocument.getXObject(userClass.getReference());
    if (userObject == null) {
        // Create object
        try {
            userObject = userDocument.newXObject(userClass.getReference(), xcontext);
        } catch (XWikiException e) {
            throw new FilterException("Failed to create user object", e);
        }
        // Setup right on user profile
        try {
            xcontext.getWiki().protectUserPage(userDocument.getFullName(), "edit", userDocument, xcontext);
        } catch (XWikiException e) {
            throw new FilterException("Failed to initialize user", e);
        }
    }
    // Update user properties
    userClass.fromValueMap(map, userObject);
    if (userDocument.isNew()) {
        // Authors
        userDocument.setCreatorReference(userDocument.getDocumentReference());
        userDocument.setAuthorReference(userDocument.getDocumentReference());
        userDocument.setContentAuthorReference(userDocument.getDocumentReference());
        // Dates
        if (this.properties.isVersionPreserved()) {
            if (parameters.containsKey(UserFilter.PARAMETER_CREATION_DATE)) {
                userDocument.setCreationDate(getDate(UserFilter.PARAMETER_CREATION_DATE, parameters, new Date()));
            }
            if (parameters.containsKey(UserFilter.PARAMETER_REVISION_DATE)) {
                userDocument.setDate(getDate(UserFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
                userDocument.setContentUpdateDate(getDate(UserFilter.PARAMETER_REVISION_DATE, parameters, new Date()));
            }
        }
        // Set false to force the date and authors we want
        userDocument.setMetaDataDirty(false);
        userDocument.setContentDirty(false);
    }
    // Save
    try {
        xcontext.getWiki().saveDocument(userDocument, this.properties.getSaveComment(), xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to save user document", e);
    }
    // Add the user to default groups
    try {
        xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext);
    } catch (XWikiException e) {
        throw new FilterException("Failed to add user to default groups", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) HashMap(java.util.HashMap) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) FilterException(org.xwiki.filter.FilterException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiException(com.xpn.xwiki.XWikiException) Date(java.util.Date) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 15 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class DefaultInstanceModel method getDocumentReferences.

@Override
public List<DocumentReference> getDocumentReferences(SpaceReference spaceReference) throws FilterException {
    List<String> documentNames;
    try {
        Query query = this.queryManager.createQuery("select distinct doc.name from Document doc where doc.space = :space order by doc.name asc", Query.XWQL);
        query.bindValue("space", localSerializer.serialize(spaceReference));
        query.setWiki(spaceReference.getWikiReference().getName());
        documentNames = query.execute();
    } catch (QueryException e) {
        throw new FilterException(String.format("Failed to get the list of documents in space [%s]", spaceReference), e);
    }
    List<DocumentReference> documentReferences = new ArrayList<>(documentNames.size());
    for (String documentName : documentNames) {
        documentReferences.add(new DocumentReference(documentName, spaceReference));
    }
    return documentReferences;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) FilterException(org.xwiki.filter.FilterException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

FilterException (org.xwiki.filter.FilterException)35 IOException (java.io.IOException)17 XWikiException (com.xpn.xwiki.XWikiException)10 XWikiContext (com.xpn.xwiki.XWikiContext)7 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 Date (java.util.Date)6 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ArrayList (java.util.ArrayList)4 InputFilterStream (org.xwiki.filter.input.InputFilterStream)3 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)3 WikiReference (org.xwiki.model.reference.WikiReference)3 QueryException (org.xwiki.query.QueryException)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2