Search in sources :

Example 26 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class DefaultAttachmentConverter method convert.

@Override
public List<Attachment> convert(List<XWikiAttachment> attachments) {
    XWikiContext xwikiContext = getXWikiContext();
    List<Attachment> attachmentList = new ArrayList<>();
    for (XWikiAttachment attachment : attachments) {
        attachmentList.add(new Attachment(new Document(attachment.getDoc(), xwikiContext), attachment, xwikiContext));
    }
    return attachmentList;
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document)

Example 27 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class DefaultMailTemplateManager method getMailObjectsCount.

private int getMailObjectsCount(DocumentReference templateReference, DocumentReference mailClassReference) throws MessagingException {
    XWikiContext context = this.xwikiContextProvider.get();
    int objectsCount;
    try {
        List<BaseObject> objects = context.getWiki().getDocument(templateReference, context).getXObjects(mailClassReference);
        if (objects != null) {
            objectsCount = objects.size();
        } else {
            objectsCount = 0;
        }
    } catch (XWikiException e) {
        throw new MessagingException(String.format("Failed to find number of [%s] objects in Document [%s]", mailClassReference, templateReference), e);
    }
    return objectsCount;
}
Also used : MessagingException(javax.mail.MessagingException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 28 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class SendMailRunnable method prepareContextForQueueItem.

private void prepareContextForQueueItem(SendMailQueueItem mailItem) {
    // Set the current wiki in the context. This is needed for example to be able to locate the configuration
    // properties when processing the mail queue items (in waitSendWaitTime()).
    XWikiContext xcontext = this.contextProvider.get();
    xcontext.setWikiId(mailItem.getWikiId());
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext)

Example 29 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class DefaultModelBridge method setStartDateForUser.

@Override
public void setStartDateForUser(DocumentReference userReference, Date startDate) throws NotificationException {
    try {
        XWikiContext context = contextProvider.get();
        XWiki xwiki = context.getWiki();
        XWikiDocument document = xwiki.getDocument(userReference, context);
        List<BaseObject> objects = document.getXObjects(NOTIFICATION_PREFERENCE_CLASS);
        if (objects != null) {
            for (BaseObject object : objects) {
                if (object != null) {
                    object.setDateValue(START_DATE_FIELD, startDate);
                }
            }
        }
        // Make this change a minor edit so it's not displayed, by default, in notifications
        xwiki.saveDocument(document, NOTIFICATION_START_DATE_UPDATE_COMMENT, true, context);
    } catch (Exception e) {
        throw new NotificationException(String.format(SET_USER_START_DATE_ERROR_MESSAGE, userReference), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 30 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class DefaultModelBridge method getNotificationPreferences.

private List<NotificationPreference> getNotificationPreferences(DocumentReference document, String providerHint) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationPreferencesClass = NOTIFICATION_PREFERENCE_CLASS.setWikiReference(document.getWikiReference());
    List<NotificationPreference> preferences = new ArrayList<>();
    try {
        XWikiDocument doc = xwiki.getDocument(document, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationPreferencesClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null) {
                    String objFormat = obj.getStringValue(FORMAT_FIELD);
                    Date objStartDate = obj.getDateValue(START_DATE_FIELD);
                    Map<NotificationPreferenceProperty, Object> properties = extractNotificationPreferenceProperties(obj);
                    notificationPreferenceBuilder.prepare();
                    notificationPreferenceBuilder.setProperties(properties);
                    notificationPreferenceBuilder.setStartDate((objStartDate != null) ? objStartDate : doc.getCreationDate());
                    notificationPreferenceBuilder.setFormat(StringUtils.isNotBlank(objFormat) ? NotificationFormat.valueOf(objFormat.toUpperCase()) : NotificationFormat.ALERT);
                    notificationPreferenceBuilder.setTarget(document);
                    notificationPreferenceBuilder.setProviderHint(providerHint);
                    notificationPreferenceBuilder.setEnabled(obj.getIntValue(NOTIFICATION_ENABLED_FIELD, 0) != 0);
                    notificationPreferenceBuilder.setCategory(NotificationPreferenceCategory.DEFAULT);
                    preferences.add(notificationPreferenceBuilder.build());
                }
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the notification preferences from the document [%s].", document), e);
    }
    return preferences;
}
Also used : ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)564 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)203 XWikiException (com.xpn.xwiki.XWikiException)195 DocumentReference (org.xwiki.model.reference.DocumentReference)150 XWiki (com.xpn.xwiki.XWiki)106 BaseObject (com.xpn.xwiki.objects.BaseObject)104 Test (org.junit.Test)64 ArrayList (java.util.ArrayList)55 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)43 ExecutionContext (org.xwiki.context.ExecutionContext)43 Session (org.hibernate.Session)37 InitializationException (org.xwiki.component.phase.InitializationException)36 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)34 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)33 WikiReference (org.xwiki.model.reference.WikiReference)31 Before (org.junit.Before)29 EntityReference (org.xwiki.model.reference.EntityReference)28 QueryException (org.xwiki.query.QueryException)28 Execution (org.xwiki.context.Execution)27 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)24