elstar IT

Fullstack | Java | Tech Speaker | Tech Coach | Frank van der Linden

  • About me
  • Blog license
  • My Open source projects

My take on a Multi Growl messages phaselistner for XPages

20-02-2015 2 responses flinden68 development

I was triggered by the blog entry of Michael Smith about BootAlert share my code I am using currently in some XPages apps.

Back in august 2014 I wrote already a blog about adding Growl messages to my XPages. There is also described which Bootstrap Growl library I am using.

It was easy to implement, but lets take it a step further.
The Multi Growl PhaseListner will intercept all the message

To accomplish it I added a method to my Utility class, who add a message to the FacesContext


public static void addMessage(Severity type, String msg){

    FacesContext.getCurrentInstance().addMessage(null, new javax.faces.application.FacesMessage(type, msg, ""));

}

In my Controller classes, I added one line of code to add the specific message to the FacesContext.

JSFUtil.addMessage(FacesMessage.SEVERITY_INFO, "Configuration has been updated");

The heavy lifting will be done by the phaselistner. Here is the class

package nl.elstarit.phaseListener;

import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import nl.elstarit.utils.JSFUtil;

import com.ibm.commons.util.StringUtil;
import com.ibm.xsp.util.JSUtil;

/*
 * Enables messages to be rendered on different pages from which they were set.
 * To produce this behaviour, this class acts as a <code>PhaseListener</code>.
 * 
 * This is performed by moving the FacesMessage objects:
 * <li>After each phase where messages may be added, this moves the messages
 * from the page-scoped FacesContext to the session-scoped session map.
 * <li>Before messages are rendered, this moves the messages from the
 * session-scoped session map back to the pag e-scoped FacesContext.
 *
 * Only messages that are not associated with a particular component are ever
 * moved. These are the only messages that can be rendered on a page that is
 * different from where they originated. * To enable this behaviour, add a
 * <code>lifecycle</code> block to your faces-config.xml file. That block
 * should contain a single <code>phase-listener</code> block containing the
 * fully-qualified classname of this file.
 *
 * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a>
 *
 * @version This version have the bug corrected: The bug was:
 * In the same page, the FacesMessage was displayed many times,
 * with this patch, only different messages is added into new context.
 */
public class MultiGrowlMessages implements PhaseListener {

	private static final long serialVersionUID = 3328743500652081238L;

	/** a name to save messages in the session under */
	//private static final String sessionToken = "MULTI_GROWL_MESSAGES_SUPPORT";

	/**
	 * Return the identifier of the request processing phase during which this
	 * listener is interested in processing PhaseEvent events.
	 */
	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

	/**
	 * Handle a notification that the processing for a particular phase of the
	 * request processing lifecycle is about to begin.
	 */
	public void beforePhase(PhaseEvent event) {
		if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {

		}
	}

	/**
	 * Handle a notification that the processing for a particular phase has just
	 * been completed.
	 */
	public void afterPhase(PhaseEvent event) {
		if (event.getPhaseId() == PhaseId.APPLY_REQUEST_VALUES
				|| event.getPhaseId() == PhaseId.PROCESS_VALIDATIONS
				|| event.getPhaseId() == PhaseId.INVOKE_APPLICATION) {
			FacesContext facesContext = event.getFacesContext();
			handleMessages(facesContext);
		}
	}
	
	@SuppressWarnings("unchecked")
	    public static void handleMessages(FacesContext facesContext) {
        	    for (Iterator i = facesContext.getMessages(); i.hasNext();) {
        	        FacesMessage message = (FacesMessage) i.next();
        	        handleMessage(message);
                        i.remove();
        	    }
	    }
	
	public static void handleMessage(final FacesMessage message) {
	        String type = null;
	        if(FacesMessage.SEVERITY_ERROR.equals(message.getSeverity())) {
	            type = "danger";
	        } else if(FacesMessage.SEVERITY_FATAL.equals(message.getSeverity())) {
	            type = "danger";
	        } else if(FacesMessage.SEVERITY_INFO.equals(message.getSeverity())) {
	            type = "info";
	        } else if(FacesMessage.SEVERITY_WARN.equals(message.getSeverity())) {
	            type = "warning";
	        } else {
	            type = "success";
	        }

	        createGrowlMessage(message.getSummary(), type);
	    }
	
	public static void createGrowlMessage(final String message, final String type) {
	        StringBuilder result = new StringBuilder();
	        result.append("$.growl(\n");
	        
	        if(StringUtil.isNotEmpty(message)) {          
	            JSUtil.addString(result, message);
	        }
	        
	        if(StringUtil.isNotEmpty(type)) {
	            result.append(",{");
	            result.append("\ttype: ");
	            JSUtil.addString(result, type);
	            result.append("}");
	        }
	        
	        result.append(")\n");
	     
	        JSFUtil.getViewRoot().postScript(result.toString());
	    }
}

To use the phaselistner it needs to be registered at the Faces-Config.xml

<lifecycle>
    <phase-listener>nl.elstarit.phaseListener.MultiGrowlMessages</phase-listener>
  </lifecycle>

Happy coding……..

Tags: growl, java, xpages

2 thoughts on “My take on a Multi Growl messages phaselistner for XPages”

  1. Nathan T. Freeman says:
    20-02-2015 at 14:44

    Frank, would you mind either 1) putting this in XSnippets; or 2) attaching a license agreement to this page that indicates the terms for your code (preferable Apache 2)?

    That way others will be free to reuse it without having to get copyright permission from you and it can be included in OpenNTF projects.

    Thanks!

    Reply
    1. Frank van der Linden says:
      20-02-2015 at 19:26

      Nathan, good point about the license. When I wrote the post the idea was already in the back in my head. So I will do it this weekend

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • « Jackson JSON mapper plugin for XPages
  • under Apache 2.0 license »

Contact me

My name is Frank van der Linden and I am an independent software developer based in the Netherlands. The last 2 years I was awarded as IBM Champion. Also I am on the board of OpenNTF. My specialisations are Java, Web development and Domino.


If you want to hire me, please fill in the Contact form


IBM Champion web badge
Apache Logo

All the code on this blog are under the Apache License 2.0. For more details, see Apache License 2.0

Most recent posts

  • Engage 2020: Hello are you listening, There is stream for everything
  • Spring Cloud Function on Azure run locally
  • Deploy Spring Cloud Function to IBM Cloud
  • Speaking (again) at Engage in a Zoo
  • Congratulations, you’re an IBM Champion (again)!

Latest reactions

  • Spring Cloud Function on Azure run locally - elstar IT on Deploy Spring Cloud Function to IBM Cloud
  • flinden68 on Quick XPages tip: add Fullcalendar plugin to your application
  • Rajesh samal on Quick tip: Swagger support for Spring Webflux
  • dsieyx on Quick XPages tip: add Fullcalendar plugin to your application
  • John on Named as IBM Champion 2019

Archive

  • March 2020
  • February 2020
  • January 2020
  • October 2019
  • September 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • December 2018
  • October 2018
  • September 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014

Category

  • bluemix
  • business
  • cloudant
  • community
  • development
  • hrassistant
  • openntf
  • running
  • salesforce
  • Springboot
  • Tesla
  • trailrunning
  • Uncategorized
  • watson
  • OpenNTF
  • Collaboration Today
  • XSnippets
  • Stackoverflow
  • IBM Collaboration Solutions
  • Social Business Toolkit
  • About me
  • Dutch curriculum vitae
  • English curriculum vitae
  • Google+
  • LinkedIn profile
  • Twitter
  • Slideshare
  • Blog license
  • My open source projects