voyent
How to invoke a method before the intervalRender fires the requestRender()?  XML
Forum Index -> General Help
Author Message
caioformiga

Joined: 12/Oct/2009 00:00:00
Messages: 9
Offline


I have a Portlet A which has a comboBox and a Portle B where I must access the database and retrieve the values related to the value selected on Portlet A.

Once my Portlet B starts to "run" must update/refresh every 3seconds. I`m using a tintervalRender as follow below:

Code:
        //my intervalRender object
        public static IntervalRenderer groupAsyncIntervalRender;
 
        //this is my interval time
        public static final Long RENDER_INTERVAL = 3000L;
 
        //this method is invoked every time a Portlet A changes the value of a selectItem
         public static void scheduleIntervalRender() throws RenderingException{		
 		if (groupAsyncIntervalRender != null){		
 			groupAsyncIntervalRender.requestRender();
 		}			
 	}	
 
        //this method is used to set my interval (3seconds)
 	public static void setIntervalRender(long interval){
 		if (groupAsyncIntervalRender != null){
 			groupAsyncIntervalRender.setInterval(interval);
 		}				
 	}
 


When I invoke the method scheduleIntervalRender() it will schedule a update/refresh according to the time specified by setIntervalRender(), which is inside the constructor of my ManagedBean that is my controller for Portlet B.

My portlet B is been refreshed (InternalRender is working) but I`m not getting the new values from the database. I have a preparePortlet() method which guarantee that all the demanded conditions are valid, so I can't access my database straight through get and set from my ManagedBean, I must use this method.

So, how to force my portlet to invoke this method (preparePortlet()) before let intervalRender refresh the page?

caioformiga

Joined: 12/Oct/2009 00:00:00
Messages: 9
Offline


I was reading about PhaseListerner and decided to check if it would solve my problem. So I created the class MyPortletBPhaseListener listed below and register at faces-config.xml inside the element <lifecycle>. Besides that I used one onDemmandRender (Portlat A) and one intervalRender (Portlat B) both inside a AjaxUtil class.

Code:
 public class MyPortletBPhaseListener implements PhaseListener {
 
 	private static MonitorController controller = getInstance();
 
         //it's necessary to have a intance of MonitorController class cuase this class has the 
         //method that should be invoked before render the page
 	public static MonitorController getInstance() {
 		if (controller == null) {
 			controller = new MonitorController();
 		}
 		return controller;
 	}
 
 	public static void setController(MonitorController controller) {
 		MonitorPortletPhaseListener.controller = controller;
 	}
 
 	public void beforePhase(PhaseEvent event) {				
 		String rootViewIdInvoked = event.getFacesContext().getViewRoot().getViewId();
                 //determines when the request render was fired by PortletA or PortletB, the value 
                 //of controller.ROOT_VIEW_ID is the identifier of Portlet B
 		if (controller.ROOT_VIEW_ID.equals(rootViewIdInvoked)) {
 			controller.getPreparePortlet();			
 		}					
 	}
 
 	public void afterPhase(PhaseEvent event) {
 		//do nothig
 	}
 	public PhaseId getPhaseId() {
 		return PhaseId.INVOKE_APPLICATION;
 	}
 }
 


That way I start my application...select one item in the combo box (Portlet A) and fires a AjaxUtil.immediateRender(), which will run the JSF lifecycle. Once I have my PhaseListener registered it will intercept the lifecycle execution when it reach the PhaseId.INVOKE_APPLICATION, but this request render was fired by PortletA so don't do anything. Just wait.

In Portlet B, by the other hand, it listen that one item was selected and then fires AjaxUtil.scheduleRender(), which will wait for 3 seconds (time define by intervalRender.setInterval()) and also will run the JSF lifecycle, but this time it will execute controller.getPreparePortlet(), cause it was the PortletB which request render. This method follows below, and will run preparePortlet(getInstitutionSelectedName()) to get the new values from the database and a new AjaxUtil.scheduleIntervalRender(), then I will have a loop, every 3 seconds the method controller.getPreparePortlet() will be invoked.

Code:
 public void getPreparePortlet(){
 		try {			
 			if (getIsInstitutionSelected()) {	
                                 //getting the new values from the database	
 				preparePortlet(getInstitutionSelectedName());
                                 //schedule a request render for 3 seconds
 				AjaxUtil.scheduleIntervalRender();
 			}
 		} catch (Exception ex) {
 			ex.printStackTrace();
 		}
 	}
 


For some unknown reason my data is not been updated. But I think that is the way to do it. Any suggestions are welcomed.
 
Forum Index -> General Help
Go to:   
Powered by JForum 2.1.7ice © JForum Team