View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   *
19   */
20  
21  package org.apache.myfaces.orchestra.viewController.jsf;
22  
23  import org.apache.myfaces.orchestra.viewController.ViewControllerManager;
24  import org.apache.myfaces.orchestra.viewController._ViewControllerUtils;
25  
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.context.FacesContext;
28  import javax.faces.event.PhaseEvent;
29  import javax.faces.event.PhaseId;
30  import javax.faces.event.PhaseListener;
31  
32  /***
33   * Causes lifecycle methods to be invoked on backing beans that are associated with
34   * the current view.
35   * <p>
36   * See the javadoc for class ViewControllerManager on how to configure this.
37   */
38  public class ViewControllerPhaseListener implements PhaseListener
39  {
40  	private static final long serialVersionUID = -3975277433747722402L;
41  
42  	public void beforePhase(PhaseEvent event)
43  	{
44  		if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId()) ||
45  			PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
46  		{
47  			assertConversationState(event.getFacesContext());
48  			if (event.getFacesContext().getResponseComplete())
49  			{
50  				// we have a redirect ... stop now
51  				return;
52  			}
53  		}
54  
55  		if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
56  		{
57  			preRenderResponse(event.getFacesContext());
58  		}
59  
60  		if (PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId()))
61  		{
62  			preInvokeApplication(event.getFacesContext());
63  		}
64  	}
65  
66  	public void afterPhase(PhaseEvent event)
67  	{
68  		if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId()))
69  		{
70  			assertConversationState(event.getFacesContext());
71  			if (event.getFacesContext().getResponseComplete())
72  			{
73  				// we have a redirect ... stop now
74  				return;
75  			}
76  
77  			postRestoreView(event.getFacesContext());
78  		}
79  	}
80  
81  	public PhaseId getPhaseId()
82  	{
83  		return PhaseId.ANY_PHASE;
84  	}
85  
86  	protected String getViewId(FacesContext facesContext)
87  	{
88  		UIViewRoot viewRoot = facesContext.getViewRoot();
89  		if (viewRoot == null)
90  		{
91  			return null;
92  		}
93  		return viewRoot.getViewId();
94  	}
95  
96  	/***
97  	 * invoked multiple times during the lifecycle to ensure the conversation(s)
98  	 * to the associated viewController are running.
99  	 *
100 	 * @param facesContext
101 	 */
102 	protected void assertConversationState(FacesContext facesContext)
103 	{
104 		ViewControllerManager manager = _ViewControllerUtils.getViewControllerManager(facesContext);
105 		if (manager == null)
106 		{
107 			return;
108 		}
109 
110 		String viewId = getViewId(facesContext);
111 		if (viewId == null)
112 		{
113 			return;
114 		}
115 
116 		manager.assertConversationState(viewId);
117 	}
118 
119 	/***
120 	 * invokes the preRenderView method on your view controller
121 	 */
122 	protected void preRenderResponse(FacesContext facesContext)
123 	{
124 		ViewControllerManager manager = _ViewControllerUtils.getViewControllerManager(facesContext);
125 		if (manager == null)
126 		{
127 			return;
128 		}
129 
130 		String viewId = getViewId(facesContext);
131 		if (viewId == null)
132 		{
133 			return;
134 		}
135 
136 		manager.executePreRenderView(viewId);
137 	}
138 
139 	/***
140 	 * invokes the initView method on your view controller
141 	 */
142 	protected void postRestoreView(FacesContext facesContext)
143 	{
144 		ViewControllerManager manager = _ViewControllerUtils.getViewControllerManager(facesContext);
145 		if (manager == null)
146 		{
147 			return;
148 		}
149 
150 		String viewId = getViewId(facesContext);
151 		if (viewId == null)
152 		{
153 			return;
154 		}
155 
156 		manager.executeInitView(viewId);
157 	}
158 
159 	/***
160 	 * invokes the preProcess method on your view controller
161 	 */
162 	protected void preInvokeApplication(FacesContext facesContext)
163 	{
164 		ViewControllerManager manager = _ViewControllerUtils.getViewControllerManager(facesContext);
165 		if (manager == null)
166 		{
167 			return;
168 		}
169 
170 		String viewId = getViewId(facesContext);
171 		if (viewId == null)
172 		{
173 			return;
174 		}
175 
176 		manager.executePreProcess(viewId);
177 	}
178 }