Search in sources :

Example 21 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class SendMailConfigClassDocumentConfigurationSourceTest method getPropertyWhenSendMailConfigClassXObjectExists.

@Test
public void getPropertyWhenSendMailConfigClassXObjectExists() throws Exception {
    ConverterManager converterManager = this.mocker.getInstance(ConverterManager.class);
    when(converterManager.convert(String.class, "value")).thenReturn("value");
    Cache<Object> cache = mock(Cache.class);
    CacheManager cacheManager = this.mocker.getInstance(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wiki");
    LocalDocumentReference classReference = new LocalDocumentReference("Mail", "SendMailConfigClass");
    BaseProperty property = mock(BaseProperty.class);
    when(property.toText()).thenReturn("value");
    BaseObject object = mock(BaseObject.class);
    when(object.getField("key")).thenReturn(property);
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getXObject(classReference)).thenReturn(object);
    DocumentReference documentReference = new DocumentReference("wiki", "Mail", "MailConfig");
    XWiki xwiki = mock(XWiki.class);
    when(xwiki.getDocument(eq(documentReference), any(XWikiContext.class))).thenReturn(document);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(xcontext);
    assertEquals("value", this.mocker.getComponentUnderTest().getProperty("key", "defaultValue"));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ConverterManager(org.xwiki.properties.ConverterManager) CacheManager(org.xwiki.cache.CacheManager) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Example 22 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class AbstractTemplateMimeBodyPartFactory method create.

@Override
public MimeBodyPart create(DocumentReference documentReference, Map<String, Object> parameters) throws MessagingException {
    Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
    Object localeValue = parameters.get("language");
    String textContent = getTemplateManager().evaluate(documentReference, "text", velocityVariables, localeValue);
    String htmlContent = getTemplateManager().evaluate(documentReference, "html", velocityVariables, localeValue);
    Map<String, Object> htmlParameters = new HashMap<>();
    htmlParameters.put("alternate", textContent);
    // Handle attachments:
    // - if the user has passed an "attachments" property with a list of attachment then add them
    // - if the user has set the "includeTemplateAttachments" property then add all attachments found in the
    // template document too
    List<Attachment> attachments = new ArrayList<>();
    List<Attachment> parameterAttachments = (List<Attachment>) parameters.get(ATTACHMENT_PROPERTY_NAME);
    if (parameterAttachments != null) {
        attachments.addAll(parameterAttachments);
    }
    Boolean includeTemplateAttachments = (Boolean) parameters.get(INCLUDE_TEMPLATE_ATTACHMENTS_PROPERTY_NAME);
    if (includeTemplateAttachments != null && includeTemplateAttachments) {
        try {
            List<XWikiAttachment> xwikiAttachments = ((XWikiDocument) this.bridge.getDocumentInstance(documentReference)).getAttachmentList();
            attachments.addAll(this.attachmentConverter.convert(xwikiAttachments));
        } catch (Exception e) {
            throw new MessagingException(String.format("Failed to include attachments from the Mail Template [%s]", documentReference), e);
        }
    }
    if (!attachments.isEmpty()) {
        htmlParameters.put(ATTACHMENT_PROPERTY_NAME, attachments);
    }
    return this.htmlBodyPartFactory.create(htmlContent, htmlParameters);
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) MessagingException(javax.mail.MessagingException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument 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 24 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument 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)

Example 25 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class WikiSkinUtils method getSkinDocument.

public XWikiDocument getSkinDocument(String skin) {
    XWikiContext xcontext = this.xcontextProvider.get();
    if (xcontext != null) {
        DocumentReference skinReference = this.currentMixedDocumentReferenceResolver.resolve(skin);
        XWiki xwiki = xcontext.getWiki();
        if (xwiki != null && xwiki.getStore() != null) {
            XWikiDocument doc;
            try {
                doc = xwiki.getDocument(skinReference, xcontext);
            } catch (XWikiException e) {
                this.logger.error("Faied to get document [{}]", skinReference, e);
                return null;
            }
            if (!doc.isNew()) {
                return doc;
            }
        }
    }
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23