001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/velocity/tags/sakai_2-4-1/tool/src/java/org/sakaiproject/cheftool/VelocityPortletStateAction.java $
003: * $Id: VelocityPortletStateAction.java 7247 2006-03-29 21:09:51Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.cheftool;
021:
022: import org.sakaiproject.event.api.SessionState;
023:
024: /**
025: * <p>
026: * VelocityPortletStateAction is an extension of VelocityPortletAction which provides a way to associate Controller state with each instances of the portlet using this action.
027: * </p>
028: */
029:
030: public abstract class VelocityPortletStateAction extends
031: VelocityPortletPaneledAction {
032: /**
033: * Get the proper state for this instance (if portlet is not known, only context).
034: *
035: * @param context
036: * The Template Context (it contains a reference to the portlet).
037: * @param rundata
038: * The Jetspeed (Turbine) rundata associated with the request.
039: * @param stateClass
040: * The Class of the ControllerState to find / create.
041: * @return The proper state object for this instance.
042: */
043: protected ControllerState getState(Context context,
044: RunData rundata, Class stateClass) {
045: return getState(((JetspeedRunData) rundata).getJs_peid(),
046: rundata, stateClass);
047:
048: } // getState
049:
050: /**
051: * Get the proper state for this instance (if portlet is known).
052: *
053: * @param portlet
054: * The portlet being rendered.
055: * @param rundata
056: * The Jetspeed (Turbine) rundata associated with the request.
057: * @param stateClass
058: * The Class of the ControllerState to find / create.
059: * @return The proper state object for this instance.
060: */
061: protected ControllerState getState(VelocityPortlet portlet,
062: RunData rundata, Class stateClass) {
063: if (portlet == null) {
064: Log.warn("chef", this + ".getState(): portlet null");
065: return null;
066: }
067:
068: return getState(portlet.getID(), rundata, stateClass);
069:
070: } // getState
071:
072: /**
073: * Get the proper state for this instance (if portlet id is known).
074: *
075: * @param peid
076: * The portlet id.
077: * @param rundata
078: * The Jetspeed (Turbine) rundata associated with the request.
079: * @param stateClass
080: * The Class of the ControllerState to find / create.
081: * @return The proper state object for this instance.
082: */
083: protected ControllerState getState(String peid, RunData rundata,
084: Class stateClass) {
085: if (peid == null) {
086: Log.warn("chef", this + ".getState(): peid null");
087: return null;
088: }
089:
090: try {
091: // get the PortletSessionState
092: SessionState ss = ((JetspeedRunData) rundata)
093: .getPortletSessionState(peid);
094:
095: // get the state object
096: ControllerState state = (ControllerState) ss
097: .getAttribute("state");
098:
099: if (state != null)
100: return state;
101:
102: // if there's no "state" object in there, make one
103: state = (ControllerState) stateClass.newInstance();
104: state.setId(peid);
105: // TODO: this does not seem used -ggolden
106: // state.setSetId(((JetspeedRunData)rundata).getPageSessionId());
107:
108: // remember it!
109: ss.setAttribute("state", state);
110:
111: return state;
112: } catch (Exception e) {
113: Log.warn("chef", "", e);
114: }
115:
116: return null;
117:
118: } // getState
119:
120: /**
121: * Release the proper state for this instance (if portlet is not known, only context).
122: *
123: * @param context
124: * The Template Context (it contains a reference to the portlet).
125: * @param rundata
126: * The Jetspeed (Turbine) rundata associated with the request.
127: */
128: protected void releaseState(Context context, RunData rundata) {
129: releaseState(((JetspeedRunData) rundata).getJs_peid(), rundata);
130:
131: } // releaseState
132:
133: /**
134: * Release the proper state for this instance (if portlet is known).
135: *
136: * @param portlet
137: * The portlet being rendered.
138: * @param rundata
139: * The Jetspeed (Turbine) rundata associated with the request.
140: */
141: protected void releaseState(VelocityPortlet portlet, RunData rundata) {
142: releaseState(portlet.getID(), rundata);
143:
144: } // releaseState
145:
146: /**
147: * Release the proper state for this instance (if portlet id is known).
148: *
149: * @param peid
150: * The portlet id being rendered.
151: * @param rundata
152: * The Jetspeed (Turbine) rundata associated with the request.
153: */
154: protected void releaseState(String peid, RunData rundata) {
155: try {
156: // get the PortletSessionState
157: SessionState ss = ((JetspeedRunData) rundata)
158: .getPortletSessionState(peid);
159:
160: // get the state object
161: ControllerState state = (ControllerState) ss
162: .getAttribute("state");
163:
164: // recycle the state object
165: state.recycle();
166:
167: // clear out the SessionState for this Portlet
168: ss.removeAttribute("state");
169:
170: ss.clear();
171:
172: } catch (Exception e) {
173: Log.warn("chef", "", e);
174: }
175:
176: } // releaseState
177:
178: } // VelocityPortletStateAction
|