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