001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/presence/tags/sakai_2-4-1/presence-tool/tool/src/java/org/sakaiproject/presence/tool/PresenceToolAction.java $
003: * $Id: PresenceToolAction.java 9914 2006-05-25 03:06:26Z 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.presence.tool;
021:
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Vector;
027:
028: import org.sakaiproject.authz.cover.SecurityService;
029: import org.sakaiproject.cheftool.Context;
030: import org.sakaiproject.cheftool.JetspeedRunData;
031: import org.sakaiproject.cheftool.RunData;
032: import org.sakaiproject.cheftool.VelocityPortlet;
033: import org.sakaiproject.cheftool.VelocityPortletPaneledAction;
034: import org.sakaiproject.cheftool.api.Menu;
035: import org.sakaiproject.cheftool.menu.MenuDivider;
036: import org.sakaiproject.cheftool.menu.MenuEntry;
037: import org.sakaiproject.cheftool.menu.MenuImpl;
038: import org.sakaiproject.event.api.SessionState;
039: import org.sakaiproject.event.cover.UsageSessionService;
040: import org.sakaiproject.presence.cover.PresenceService;
041: import org.sakaiproject.tool.api.Placement;
042: import org.sakaiproject.tool.cover.ToolManager;
043: import org.sakaiproject.util.PresenceObservingCourier;
044: import org.sakaiproject.util.ResourceLoader;
045:
046: /**
047: * <p>
048: * PresenceToolAction is the presence display tool showing everyone everywhere.
049: * </p>
050: */
051: public class PresenceToolAction extends VelocityPortletPaneledAction {
052: /** Resource bundle using current language locale */
053: private static ResourceLoader rb = new ResourceLoader("admin");
054:
055: /** The display modes. */
056: protected static final String STATE_DISPLAY_MODE = "display_mode";
057:
058: protected static final String MODE_LOCATIONS = "locations";
059:
060: protected static final String MODE_SESSIONS = "sessions";
061:
062: protected static final String MODE_SERVERS = "servers";
063:
064: /**
065: * Populate the state object, if needed.
066: */
067: protected void initState(SessionState state,
068: VelocityPortlet portlet, JetspeedRunData rundata) {
069: super .initState(state, portlet, rundata);
070:
071: // setup the observer to notify our main panel
072: if (state.getAttribute(STATE_OBSERVER) == null) {
073: // get the current tool placement
074: Placement placement = ToolManager.getCurrentPlacement();
075:
076: // location is just placement
077: String location = placement.getId();
078:
079: // setup the observer to watch for all presence, disabled so we start in manual mode
080: PresenceObservingCourier courier = new PresenceObservingCourier(
081: location);
082: courier.setResourcePattern(null);
083: courier.disable();
084: state.setAttribute(STATE_OBSERVER, courier);
085:
086: // init the display mode
087: state.setAttribute(STATE_DISPLAY_MODE, MODE_SERVERS);
088: }
089:
090: } // initState
091:
092: /**
093: * build the context for the Main (List) panel
094: *
095: * @return (optional) template name for this panel
096: */
097: public String buildMainPanelContext(VelocityPortlet portlet,
098: Context context, RunData rundata, SessionState state) {
099: context.put("tlang", rb);
100:
101: // if not logged in as the super user, we won't do anything
102: if (!SecurityService.isSuperUser()) {
103: return (String) getContext(rundata).get("template")
104: + "_noaccess";
105: }
106:
107: String template = (String) getContext(rundata).get("template");
108:
109: if (!SecurityService.isSuperUser()) {
110: addAlert(state, rb.getString("presence.import"));
111: return template;
112: }
113:
114: // inform the observing courier that we just updated the page...
115: // if there are pending requests to do so they can be cleared
116: PresenceObservingCourier observer = (PresenceObservingCourier) state
117: .getAttribute(STATE_OBSERVER);
118: observer.justDelivered();
119:
120: // build the menu
121: Menu bar = new MenuImpl();
122: bar.add(new MenuEntry(rb.getString("presence.locations"),
123: "doLocations"));
124: bar.add(new MenuEntry(rb.getString("presence.sessions"),
125: "doSessions"));
126: bar.add(new MenuEntry(rb.getString("presence.servers"),
127: "doServers"));
128: bar.add(new MenuDivider());
129: bar.add(new MenuEntry((observer.getEnabled() ? rb
130: .getString("presence.manualref") : rb
131: .getString("presence.autoref")), "doAuto"));
132: if (!observer.getEnabled()) {
133: bar.add(new MenuEntry(rb.getString("presence.ref"),
134: "doRefresh"));
135: }
136: context.put(Menu.CONTEXT_MENU, bar);
137:
138: // for locations list mode
139: if (MODE_LOCATIONS.equals(state
140: .getAttribute(STATE_DISPLAY_MODE))) {
141: template += "-List";
142:
143: // get the list of all presence locations
144: List locations = PresenceService.getLocations();
145: context.put("locations", locations);
146:
147: context.put("service", PresenceService.getInstance());
148: }
149:
150: // for sessions display mode
151: else if (MODE_SESSIONS.equals(state
152: .getAttribute(STATE_DISPLAY_MODE))) {
153: template += ".sessions-List";
154:
155: // get sessions by server (keys are already sorted by server)
156: Map sessionsByServer = UsageSessionService
157: .getOpenSessionsByServer();
158: context.put("servers", sessionsByServer);
159:
160: List serverList = new Vector();
161: serverList.addAll(sessionsByServer.keySet());
162: context.put("serverList", serverList);
163:
164: int count = 0;
165: for (Iterator i = sessionsByServer.values().iterator(); i
166: .hasNext();) {
167: List sessions = (List) i.next();
168: count += sessions.size();
169: }
170: context.put("total", new Integer(count));
171: }
172:
173: // for servers display mode
174: else if (MODE_SERVERS.equals(state
175: .getAttribute(STATE_DISPLAY_MODE))) {
176: template += ".servers-List";
177:
178: // get the set of all servers with current presence
179: Map servers = UsageSessionService.getOpenSessionsByServer();
180: context.put("servers", servers);
181:
182: List serverList = new Vector();
183: serverList.addAll(servers.keySet());
184: Collections.sort(serverList);
185: context.put("serverList", serverList);
186:
187: int count = 0;
188: for (Iterator i = servers.values().iterator(); i.hasNext();) {
189: List sessions = (List) i.next();
190: count += sessions.size();
191: }
192: context.put("total", new Integer(count));
193: }
194:
195: // the url for the online courier, using a 30 second refresh
196: setVmCourier(rundata.getRequest(), 30);
197:
198: return template;
199:
200: } // buildMainPanelContext
201:
202: /**
203: * Switch to locations mode
204: */
205: public void doLocations(RunData data, Context context) {
206: // access the portlet element id to find our state
207: String peid = ((JetspeedRunData) data).getJs_peid();
208: SessionState state = ((JetspeedRunData) data)
209: .getPortletSessionState(peid);
210:
211: // go to remove confirm mode
212: state.setAttribute(STATE_DISPLAY_MODE, MODE_LOCATIONS);
213:
214: } // doLocations
215:
216: /**
217: * Switch to sessions mode
218: */
219: public void doSessions(RunData data, Context context) {
220: // access the portlet element id to find our state
221: String peid = ((JetspeedRunData) data).getJs_peid();
222: SessionState state = ((JetspeedRunData) data)
223: .getPortletSessionState(peid);
224:
225: // go to remove confirm mode
226: state.setAttribute(STATE_DISPLAY_MODE, MODE_SESSIONS);
227:
228: } // doSessions
229:
230: /**
231: * Switch to servers mode
232: */
233: public void doServers(RunData data, Context context) {
234: // access the portlet element id to find our state
235: String peid = ((JetspeedRunData) data).getJs_peid();
236: SessionState state = ((JetspeedRunData) data)
237: .getPortletSessionState(peid);
238:
239: // go to remove confirm mode
240: state.setAttribute(STATE_DISPLAY_MODE, MODE_SERVERS);
241:
242: } // doServers
243:
244: /**
245: * Toggle auto-update
246: */
247: public void doAuto(RunData data, Context context) {
248: // access the portlet element id to find our state
249: String peid = ((JetspeedRunData) data).getJs_peid();
250: SessionState state = ((JetspeedRunData) data)
251: .getPortletSessionState(peid);
252:
253: // get the observer
254: PresenceObservingCourier observer = (PresenceObservingCourier) state
255: .getAttribute(STATE_OBSERVER);
256: boolean enabled = observer.getEnabled();
257: if (enabled) {
258: observer.disable();
259: } else {
260: observer.enable();
261: }
262:
263: } // doAuto
264:
265: /**
266: * The action for when the user want's an update
267: */
268: public void doRefresh(RunData data, Context context) {
269: // access the portlet element id to find our state
270: String peid = ((JetspeedRunData) data).getJs_peid();
271: SessionState state = ((JetspeedRunData) data)
272: .getPortletSessionState(peid);
273:
274: } // doRefresh
275:
276: } // PresenceToolAction
|