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