Search in sources :

Example 1 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project xwiki-platform by xwiki.

the class XWiki method prepareResources.

/**
 * Prepares the localized resources, according to the selected locale. Set context "msg" and locale.
 *
 * @param context see {@link XWikiContext}
 */
public void prepareResources(XWikiContext context) {
    if (context.get("msg") == null) {
        Locale locale = getLocalePreference(context);
        context.setLocale(locale);
        if (context.getResponse() != null) {
            context.getResponse().setLocale(locale);
        }
        XWikiMessageTool msg = new XWikiMessageTool(Utils.getComponent(ContextualLocalizationManager.class));
        context.put("msg", msg);
    }
}
Also used : Locale(java.util.Locale) ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool)

Example 2 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class NewsletterReceivers method getUnsubscribeFooter.

private String getUnsubscribeFooter(String emailAddress, XWikiDocument blogDocument) throws XWikiException {
    String unsubscribeFooter = "";
    if (!"".equals(getUnsubscribeLink(blogDocument.getSpace(), emailAddress))) {
        XWikiMessageTool messageTool = getWebUtilsService().getMessageTool(getContext().getLanguage());
        unsubscribeFooter = messageTool.get("cel_newsletter_unsubscribe_footer", Arrays.asList(getUnsubscribeLink(blogDocument.getSpace(), emailAddress)));
    }
    return unsubscribeFooter;
}
Also used : XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool)

Example 3 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class NewsletterReceivers method getHtmlContent.

private String getHtmlContent(XWikiDocument doc, String baseURL) throws XWikiException {
    String header = "";
    if ((baseURL != null) && !"".equals(baseURL.trim())) {
        header = "<base href='" + baseURL + "' />\n";
    }
    String renderLang = getContext().getLanguage();
    VelocityContext vcontext = (VelocityContext) getContext().get("vcontext");
    XWikiMessageTool msgTool = getWebUtilsService().getMessageTool(renderLang);
    DocumentReference headerRef = getWebUtilsService().resolveDocumentReference("LocalMacros.NewsletterHTMLheader");
    if (getContext().getWiki().exists(headerRef, getContext())) {
        LOGGER.debug("Additional header found.");
        LOGGER.debug("doc=" + doc + ", context.language=" + getContext().getLanguage(), doc, getContext());
        LOGGER.debug("context=" + getContext());
        vcontext.put("msg", msgTool);
        vcontext.put("adminMsg", msgTool);
        header += renderCommand.renderDocument(headerRef, renderLang);
        LOGGER.debug("Additional header rendered.");
    } else {
        LOGGER.debug("No additional header. Doc does not exist: " + headerRef);
    }
    LOGGER.debug("rendering content in " + renderLang);
    getContext().setLanguage(renderLang);
    renderCommand.setDefaultPageType("RichText");
    vcontext.put("msg", msgTool);
    vcontext.put("adminMsg", msgTool);
    String content = renderCommand.renderCelementsDocument(doc.getDocumentReference(), renderLang, "view");
    content = Utils.replacePlaceholders(content, getContext());
    if (getContext().getWiki().getXWikiPreferenceAsInt("newsletter_embed_all_images", "celements.newsletter.embedAllImages", 0, getContext()) == 1) {
        content = getNewsletterAttachmentService().embedImagesInContent(content);
    }
    String footer = "";
    DocumentReference footerRef = getWebUtilsService().resolveDocumentReference("LocalMacros.NewsletterHTMLfooter");
    if (getContext().getWiki().exists(footerRef, getContext())) {
        getContext().setLanguage(renderLang);
        LOGGER.debug("Additional footer found.");
        LOGGER.debug("doc={} , context.language={}", doc, getContext().getLanguage());
        LOGGER.debug("context={}", getContext());
        vcontext.put("msg", msgTool);
        vcontext.put("adminMsg", msgTool);
        footer += renderCommand.renderDocument(footerRef, renderLang) + "\n";
        LOGGER.debug("Additional footer rendered.");
    } else {
        LOGGER.debug("No additional footer. Doc does not exist: {} ", footerRef);
    }
    XWikiMessageTool messageTool = getWebUtilsService().getMessageTool(renderLang);
    footer += messageTool.get("cel_newsletter_html_footer_message", Arrays.asList(doc.getExternalURL("view", getContext())));
    LOGGER.debug("Header: [" + header + "]");
    LOGGER.debug("Footer: [" + footer + "]");
    return header + content + footer;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class BlogPluginTest method testBatchImportReceivers_activeAddInactive.

@Test
public void testBatchImportReceivers_activeAddInactive() throws XWikiException {
    Map<String, String> result = new TreeMap<>();
    result.put("myname@email.com", "inactive");
    String importData = "\r\n,My Name <myName@email.com>,,,,";
    String nl = "My.Newsletter";
    XWikiMessageTool messageTool = createMock(XWikiMessageTool.class);
    XWikiContext context = createMock(XWikiContext.class);
    expect(context.getMessageTool()).andReturn(messageTool).anyTimes();
    expect(context.getWiki()).andReturn(xwiki).anyTimes();
    expect(context.getUser()).andReturn("XWiki.Admin").anyTimes();
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(null);
    expect(xwiki.generateRandomString(eq(16))).andReturn("abc");
    expect(xwiki.exists(eq("NewsletterReceivers.abc"), same(context))).andReturn(false);
    XWikiDocument doc1 = createMock(XWikiDocument.class);
    expect(doc1.getObject(eq("Celements2.BlogConfigClass"))).andReturn(null);
    expect(doc1.getObject(eq("XWiki.ArticleClass"))).andReturn(null);
    expect(context.getDoc()).andReturn(doc1);
    expect(xwiki.getDocument(eq("NewsletterReceivers.abc"), same(context))).andReturn(doc1).times(2);
    XWikiDocument nlDoc = createMock(XWikiDocument.class);
    expect(xwiki.exists(eq(nl), same(context))).andReturn(true);
    expect(xwiki.getDocument(eq(nl), same(context))).andReturn(nlDoc);
    expect(nlDoc.getFullName()).andReturn(nl).anyTimes();
    List<Object> list = new ArrayList<>();
    list.add("NewsletterReceivers.abc");
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(list);
    BaseObject obj = new BaseObject();
    obj.setIntValue("isactive", 1);
    expect(doc1.getObject(eq("Celements.NewsletterReceiverClass"), eq("subscribed"), eq(nl), eq(false))).andReturn(obj).times(2);
    expect(messageTool.get(eq("cel_newsletter_subscriber_inactive"))).andReturn("inactive").anyTimes();
    xwiki.saveDocument(same(doc1), same(context));
    expectLastCall();
    replayAll(context, doc1, messageTool, nlDoc);
    assertEquals(result, plugin.batchImportReceivers(true, importData, nl, context));
    verifyAll(context, doc1, messageTool, nlDoc);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) TreeMap(java.util.TreeMap) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 5 with XWikiMessageTool

use of com.xpn.xwiki.web.XWikiMessageTool in project celements-blog by celements.

the class BlogPluginTest method testBatchImportReceivers_inactiveAddActive.

@Test
public void testBatchImportReceivers_inactiveAddActive() throws XWikiException {
    Map<String, String> result = new TreeMap<>();
    result.put("myname@email.com", "inactive");
    String importData = "\r\n,My Name <myName@email.com>,,,,";
    String nl = "My.Newsletter";
    XWikiMessageTool messageTool = createMock(XWikiMessageTool.class);
    XWikiContext context = createMock(XWikiContext.class);
    expect(context.getMessageTool()).andReturn(messageTool).anyTimes();
    expect(context.getWiki()).andReturn(xwiki).anyTimes();
    expect(context.getUser()).andReturn("XWiki.Admin").anyTimes();
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(null);
    expect(xwiki.generateRandomString(eq(16))).andReturn("abc");
    expect(xwiki.exists(eq("NewsletterReceivers.abc"), same(context))).andReturn(false);
    XWikiDocument doc1 = createMock(XWikiDocument.class);
    expect(doc1.getObject(eq("Celements2.BlogConfigClass"))).andReturn(null);
    expect(doc1.getObject(eq("XWiki.ArticleClass"))).andReturn(null);
    expect(context.getDoc()).andReturn(doc1);
    expect(xwiki.getDocument(eq("NewsletterReceivers.abc"), same(context))).andReturn(doc1).times(2);
    XWikiDocument nlDoc = createMock(XWikiDocument.class);
    expect(xwiki.exists(eq(nl), same(context))).andReturn(true);
    expect(xwiki.getDocument(eq(nl), same(context))).andReturn(nlDoc);
    expect(nlDoc.getFullName()).andReturn(nl).anyTimes();
    List<Object> list = new ArrayList<>();
    list.add("NewsletterReceivers.abc");
    expect(xwiki.search((String) anyObject(), same(context))).andReturn(list);
    BaseObject obj = new BaseObject();
    obj.setIntValue("isactive", 0);
    expect(doc1.getObject(eq("Celements.NewsletterReceiverClass"), eq("subscribed"), eq(nl), eq(false))).andReturn(obj).times(2);
    expect(messageTool.get(eq("cel_newsletter_subscriber_inactive"))).andReturn("inactive").anyTimes();
    replayAll(context, doc1, messageTool, nlDoc);
    assertEquals(result, plugin.batchImportReceivers(false, importData, nl, context));
    assertEquals(0, obj.getIntValue("isactive"));
    verifyAll(context, doc1, messageTool, nlDoc);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) TreeMap(java.util.TreeMap) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Aggregations

XWikiMessageTool (com.xpn.xwiki.web.XWikiMessageTool)11 XWikiContext (com.xpn.xwiki.XWikiContext)5 TreeMap (java.util.TreeMap)5 Test (org.junit.Test)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 BaseObject (com.xpn.xwiki.objects.BaseObject)4 ArrayList (java.util.ArrayList)3 VelocityContext (org.apache.velocity.VelocityContext)2 XWikiException (com.xpn.xwiki.XWikiException)1 Locale (java.util.Locale)1 ContextualLocalizationManager (org.xwiki.localization.ContextualLocalizationManager)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1