001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels.groupsmanager;
007:
008: import java.util.Calendar;
009: import java.util.Enumeration;
010: import java.util.HashMap;
011: import java.util.Iterator;
012: import java.util.Map;
013:
014: import org.jasig.portal.ChannelCacheKey;
015: import org.jasig.portal.ChannelRuntimeData;
016: import org.jasig.portal.ChannelRuntimeProperties;
017: import org.jasig.portal.ChannelStaticData;
018: import org.jasig.portal.ICacheable;
019: import org.jasig.portal.IChannel;
020: import org.jasig.portal.IPermissible;
021: import org.jasig.portal.PortalEvent;
022: import org.jasig.portal.PortalException;
023: import org.jasig.portal.groups.IEntityGroup;
024: import org.jasig.portal.groups.IGroupMember;
025: import org.jasig.portal.services.AuthorizationService;
026: import org.jasig.portal.services.EntityNameFinderService;
027: import org.jasig.portal.services.GroupService;
028: import org.jasig.portal.utils.XSLT;
029: import org.w3c.dom.Document;
030: import org.xml.sax.ContentHandler;
031:
032: /**
033: * CGroupsManager allows users to graphically administer all groups for which
034: * user has administrtaive permissions.
035: * @author Don Fracapane
036: * @version $Revision: 36683 $
037: */
038: public class CGroupsManager implements IChannel,
039: GroupsManagerConstants, IPermissible, ICacheable {
040: // Location of the stylesheet list file
041: protected static final String sslLocation = "CGroupsManager.ssl";
042: protected static HashMap activities = null;
043: protected HashMap targets = null;
044: // protected HashMap sessionsMap = new HashMap();
045: private CGroupsManagerSessionData sessionData;
046:
047: /** Creates new GroupsManagerChannel */
048: public CGroupsManager() {
049: init();
050: }
051:
052: /**
053: * put your documentation comment here
054: */
055: protected synchronized void init() {
056: Utility.logMessage("DEBUG", this .getClass().getName()
057: + "::init(): this = " + this );
058: if (activities == null) {
059: activities = new HashMap();
060: try {
061: activities.put(CREATE_PERMISSION,
062: "Create a group in this context");
063: activities.put(VIEW_PERMISSION, "View this group");
064: activities.put(UPDATE_PERMISSION, "Rename this group");
065: activities.put(DELETE_PERMISSION, "Delete this group");
066: activities.put(SELECT_PERMISSION, "Select this group");
067: activities.put(ADD_REMOVE_PERMISSION,
068: "Manage this group's members");
069: activities.put(ASSIGN_PERMISSION,
070: "Assign Permissions for this group");
071: } catch (Exception e) {
072: Utility.logMessage("ERROR",
073: "CGroupsManager.init():: unable to set activities"
074: + e, e);
075: }
076: }
077: try {
078: if (targets == null) {
079: targets = new HashMap();
080: IEntityGroup everyone = GroupService
081: .getDistinguishedGroup(GroupService.EVERYONE);
082: targets.put(everyone.getKey(), everyone.getName());
083: Iterator allgroups = everyone.getAllMembers();
084: while (allgroups.hasNext()) {
085: IGroupMember g = (IGroupMember) allgroups.next();
086: if (g.isGroup()) {
087: if (targets.get(g.getKey()) == null) {
088: try {
089: targets.put(g.getKey(),
090: ((IEntityGroup) g).getName());
091: } catch (Exception e) {
092: Utility.logMessage("ERROR",
093: "CGroupsManager.init():: unable to add target"
094: + e, e);
095: }
096: }
097: }
098: }
099:
100: IEntityGroup allChans = GroupService
101: .getDistinguishedGroup(GroupService.CHANNEL_CATEGORIES);
102: targets.put(allChans.getKey(), allChans.getName());
103: Iterator allcgroups = allChans.getAllMembers();
104: while (allcgroups.hasNext()) {
105: IGroupMember g = (IGroupMember) allcgroups.next();
106: if (g.isGroup()) {
107: if (targets.get(g.getKey()) == null) {
108: try {
109: targets.put(g.getKey(),
110: ((IEntityGroup) g).getName());
111: } catch (Exception e) {
112: Utility.logMessage("ERROR",
113: "CGroupsManager.init():: unable to add target"
114: + e, e);
115: }
116: }
117: }
118: }
119: }
120: } catch (Exception e) {
121: Utility
122: .logMessage("ERROR",
123: "CGroupsManager.init():: unable to set targets"
124: + e, e);
125: }
126: }
127:
128: /**
129: * Acquires ChannelRuntimeProperites from the channel.
130: * This function may be called by the portal framework throughout the session.
131: * @see ChannelRuntimeProperties
132: * @return ChannelRuntimeProperties
133: */
134: public ChannelRuntimeProperties getRuntimeProperties() {
135: return new ChannelRuntimeProperties();
136: }
137:
138: /**
139: * Passes an outside event to a channel.
140: * Events should normally come from the LayoutBean.
141: * @param ev PortalEvent object
142: * @see PortalEvent
143: */
144: public void receiveEvent(PortalEvent ev) {
145: if (ev.getEventNumber() == PortalEvent.SESSION_DONE) {
146: try {
147: CGroupsManagerSessionData sd = sessionData;
148: if (sd.lockedGroup != null) {
149: sd.lockedGroup.getLock().release();
150: sd.lockedGroup = null;
151: //GroupsManagerCommandFactory.get("Unlock").execute(sd);
152: }
153: if (sd.servantChannel != null) {
154: sd.servantChannel.receiveEvent(ev);
155: }
156: } catch (Exception e) {
157: Utility.logMessage("ERROR", this .getClass().getName()
158: + "::receiveEvent(): Exception = " + e, e);
159: }
160: }
161: }
162:
163: /**
164: * Ask channel to render its content.
165: * @param out the SAX ContentHandler to output content to
166: * @throws PortalException
167: */
168: public void renderXML(ContentHandler out) throws PortalException {
169: Utility.logMessage("DEBUG", this .getClass().getName()
170: + "::renderXML(): this = " + this );
171: CGroupsManagerSessionData sessionData = getSessionData();
172: ChannelRuntimeData runtimeData = sessionData.runtimeData;
173: long time1 = Calendar.getInstance().getTime().getTime();
174: long time2 = 0;
175: Document viewDoc = null;
176: try {
177: if (sessionData.servantChannel != null) {
178: ((IChannel) sessionData.servantChannel).renderXML(out);
179: Utility.logMessage("DEBUG", this .getClass().getName()
180: + ".renderXML(): Defering to servant render");
181: } else {
182: viewDoc = sessionData.model;
183: time2 = Calendar.getInstance().getTime().getTime();
184: XSLT xslt = XSLT.getTransformer(this , runtimeData
185: .getLocales());
186: xslt.setXML(viewDoc);
187: xslt.setTarget(out);
188: xslt.setStylesheetParameter("baseActionURL",
189: sessionData.runtimeData.getBaseActionURL());
190: if (sessionData.isAdminUser) {
191: xslt.setStylesheetParameter("ignorePermissions",
192: "true");
193: }
194: if (sessionData.customMessage != null) {
195: xslt.setStylesheetParameter("customMessage",
196: sessionData.customMessage);
197: }
198: xslt.setStylesheetParameter("mode", sessionData.mode);
199: xslt.setStylesheetParameter("page", String
200: .valueOf(sessionData.currentPage));
201: if (sessionData.highlightedGroupID != null) {
202: xslt.setStylesheetParameter("highlightedGroupID",
203: sessionData.highlightedGroupID);
204: }
205: if (sessionData.rootViewGroupID != null) {
206: xslt.setStylesheetParameter("rootViewGroupID",
207: sessionData.rootViewGroupID);
208: } else if (sessionData.defaultRootViewGroupID != null) {
209: xslt.setStylesheetParameter("rootViewGroupID",
210: sessionData.defaultRootViewGroupID);
211: }
212: if (sessionData.feedback != null) {
213: xslt.setStylesheetParameter("feedback",
214: sessionData.feedback);
215: sessionData.feedback = null;
216: }
217: if (sessionData.servantMode) {
218: xslt.setStylesheetParameter("grpServantMode",
219: "true");
220: }
221: if (!sessionData.allowFinish) {
222: xslt.setStylesheetParameter("blockFinishActions",
223: "true");
224: }
225: // now handled in the permissions policy
226: //if (sessionData.blockEntitySelect) {
227: // xslt.setStylesheetParameter("blockEntitySelect", "true");
228: //}
229: try {
230: //Utility.logMessage("DEBUG", this.getClass().getName()
231: // + ".renderXML(): grpView=" + runtimeData.getParameter("grpView"));
232: xslt.setXSL(sslLocation, "main",
233: sessionData.runtimeData.getBrowserInfo());
234: xslt.transform();
235: } catch (PortalException pe) {
236: Utility.logMessage("ERROR", pe.toString(), pe);
237: if (pe.getCause() != null) {
238: Utility.logMessage("ERROR", pe.getCause()
239: .toString(), pe.getCause());
240: }
241: } catch (Exception e) {
242: Utility.logMessage("ERROR", e.toString(), e);
243: }
244: //Utility.printDoc(viewDoc, "viewXMl ready:\n");
245:
246: Utility
247: .logMessage("DEBUG",
248: "CGroupsManager::renderXML(): Servant services complete");
249: /* @todo remove following print statement */
250: Utility
251: .printDoc(viewDoc,
252: "CGroupsManager::renderXML(): Final document state:");
253: }
254: } catch (Exception e) {
255: Utility.logMessage("ERROR", e.toString(), e);
256: }
257: //Utility.logMessage("DEBUG", this.getClass().getName() + "::renderXML(): Finished with Groups Management");
258: //Utility.logMessage("DEBUG", this.getClass().getName() + "::renderXML(): =-+_=-+_=-+_=-+_=-+_=-+_=-+_=-+_ XXXXXXXXXXXXXX _=-+_=-+_=-+_=-+_=-+_=-+_=-+_=-+_");
259: long time3 = Calendar.getInstance().getTime().getTime();
260: Utility.logMessage("DEBUG", this .getClass().getName()
261: + ".renderXML() timer: "
262: + String.valueOf((time3 - time1))
263: + " ms total, xsl took "
264: + String.valueOf((time3 - time2)) + " ms for view "
265: + runtimeData.getParameter("grpView"));
266: Utility.logMessage("DEBUG", this .getClass().getName()
267: + ".renderXML() time since setRD: "
268: + String.valueOf((time3 - sessionData.startRD)));
269: return;
270: }
271:
272: /**
273: * Passes ChannelRuntimeData to the channel.
274: * This function is called prior to the renderXML() call.
275: * @param rd channel runtime data
276: * @see ChannelRuntimeData
277: */
278: public void setRuntimeData(ChannelRuntimeData rd) {
279: Utility.logMessage("DEBUG", this .getClass().getName()
280: + "::setRuntimeData(): this = " + this );
281: CGroupsManagerSessionData sessionData = getSessionData();
282: sessionData.runtimeData = rd;
283: ChannelRuntimeData runtimeData = sessionData.runtimeData;
284: sessionData.startRD = Calendar.getInstance().getTime()
285: .getTime();
286: if (sessionData.servantChannel == null) {
287: if (Utility
288: .hasValue(runtimeData.getParameter("grpCommand"))) {
289: String theCommand = runtimeData
290: .getParameter("grpCommand");
291: Utility
292: .logMessage(
293: "DEBUG",
294: this .getClass().getName()
295: + "::renderXML(): COMMAND PROCESS About to get the'"
296: + theCommand + "' command");
297: IGroupsManagerCommand c = GroupsManagerCommandFactory
298: .get(theCommand);
299: Utility.logMessage("DEBUG", this .getClass().getName()
300: + "::renderXML(): Got the '" + theCommand
301: + "' command = " + (c != null));
302: if (c != null) {
303: Utility
304: .logMessage(
305: "DEBUG",
306: this .getClass().getName()
307: + "::renderXML(): setup parms and about to execute command");
308: try {
309: c.execute(sessionData);
310: } catch (Exception e) {
311: Utility.logMessage("ERROR", e.toString(), e);
312: sessionData.feedback = "Error executing command "
313: + theCommand + ": " + e.getMessage();
314: }
315: }
316: }
317: if (Utility.hasValue(runtimeData
318: .getParameter("grpPageForward"))) {
319: sessionData.currentPage += Integer.parseInt(runtimeData
320: .getParameter("grpPageForward"));
321: }
322: if (Utility.hasValue(runtimeData
323: .getParameter("grpPageBack"))) {
324: sessionData.currentPage -= Integer
325: .parseInt((String) runtimeData
326: .getParameter("grpPageBack"));
327: }
328: }
329:
330: if (sessionData.servantChannel != null) {
331: try {
332: Utility
333: .logMessage(
334: "DEBUG",
335: this .getClass().getName()
336: + ".setRuntimeData(): Setting Servant runtimedata");
337: ((IChannel) sessionData.servantChannel)
338: .setRuntimeData(sessionData.runtimeData);
339: if (sessionData.servantChannel.isFinished()) {
340: sessionData.servantChannel = null;
341: // flushing runtimedata for case where GroupsManager is master and servant, to prevent dirtiness
342: Enumeration rd2 = runtimeData.keys();
343: while (rd2.hasMoreElements()) {
344: runtimeData.remove(rd2.nextElement());
345: }
346: Utility.logMessage("DEBUG", this .getClass()
347: .getName()
348: + ".setRuntimeData(): removed servant");
349: } else {
350: Utility
351: .logMessage(
352: "DEBUG",
353: this .getClass().getName()
354: + ".setRuntimeData(): servant Not Finished");
355: }
356: } catch (Exception e) {
357: Utility
358: .logMessage(
359: "ERROR",
360: this .getClass().getName()
361: + ".setRuntimeDat(): Problem setting servant runtimedata "
362: + e, e);
363: }
364: }
365: }
366:
367: /**
368: * Passes ChannelStaticData to the channel.
369: * This is done during channel instantiation time.
370: * see org.jasig.portal.StaticData
371: * @param sd channel static data
372: * @see ChannelStaticData
373: */
374: public void setStaticData(ChannelStaticData sd) {
375: try {
376: CGroupsManagerSessionData sessionData = getSessionData();
377: Utility.logMessage("DEBUG", this .getClass().getName()
378: + "::setStaticData(): this = " + this );
379: Utility.logMessage("DEBUG", this .getClass().getName()
380: + "::setStaticData(): session Data = "
381: + sessionData);
382: Utility.logMessage("DEBUG", this .getClass().getName()
383: + "::setStaticData(): sd = " + sd);
384: sessionData.staticData = sd;
385: IEntityGroup admin = GroupService
386: .getDistinguishedGroup(GroupService.PORTAL_ADMINISTRATORS);
387: IGroupMember currUser = AuthorizationService.instance()
388: .getGroupMember(
389: sessionData.staticData
390: .getAuthorizationPrincipal());
391: sessionData.isAdminUser = (admin.deepContains(currUser));
392: sessionData.user = sessionData.staticData.getPerson();
393: sessionData.authPrincipal = sd.getAuthorizationPrincipal();
394: sessionData.model = GroupsManagerXML
395: .getGroupsManagerXml(sessionData);
396: Utility.logMessage("DEBUG", this .getClass().getName()
397: + "::setStaticData(): staticData Person ID = "
398: + sessionData.user.getID());
399: Iterator i = sessionData.staticData.entrySet().iterator();
400: while (i.hasNext()) {
401: Map.Entry m = (Map.Entry) i.next();
402: Utility.logMessage("DEBUG", this .getClass().getName()
403: + "::setStaticData(): staticData " + m.getKey()
404: + " = " + m.getValue());
405: }
406: } catch (Exception e) {
407: Utility.logMessage("ERROR", e.toString(), e);
408: }
409: }
410:
411: /**
412: * put your documentation comment here
413: * @return String
414: */
415: public String getOwnerName() {
416: return "Groups Manager";
417: }
418:
419: /**
420: * put your documentation comment here
421: * @return String[]
422: */
423: public String[] getActivityTokens() {
424: init();
425: return (String[]) activities.keySet().toArray(new String[0]);
426: }
427:
428: /**
429: * put your documentation comment here
430: * @return String
431: */
432: public String getOwnerToken() {
433: return OWNER;
434: }
435:
436: /**
437: * put your documentation comment here
438: * @param token
439: * @return String
440: */
441: public String getActivityName(String token) {
442: return (String) activities.get(token);
443: }
444:
445: /**
446: * put your documentation comment here
447: * @return String[]
448: */
449: public String[] getTargetTokens() {
450: init();
451: return (String[]) targets.keySet().toArray(new String[0]);
452: }
453:
454: /**
455: * put your documentation comment here
456: * @param token
457: * @return String
458: */
459: public String getTargetName(String token) {
460: String r = (String) targets.get(token);
461: if (r == null) {
462: try {
463: r = EntityNameFinderService.instance().getNameFinder(
464: IEntityGroup.class).getName(token);
465: } catch (Exception e) {
466: Utility.logMessage("ERROR", e.toString(), e);
467: }
468: }
469: return r;
470: }
471:
472: /**
473: * put your documentation comment here
474: * @return ChannelCacheKey
475: */
476: public ChannelCacheKey generateKey() {
477: Utility.logMessage("DEBUG", this .getClass().getName()
478: + "::generateKey(): this = " + this );
479: CGroupsManagerSessionData sessionData = getSessionData();
480: ChannelStaticData staticData = sessionData.staticData;
481: ChannelCacheKey cck;
482: if (sessionData.servantChannel == null) {
483: cck = new ChannelCacheKey();
484: cck.setKey(staticData.getChannelPublishId() + "-"
485: + staticData.getChannelSubscribeId() + "-"
486: + String.valueOf(staticData.getPerson().getID()));
487: // + "-" + Calendar.getInstance().getTime().getTime()));
488: cck.setKeyValidity(vKey());
489: Utility.logMessage("DEBUG",
490: "CGroupsManager.generateKey():: [NO SERVANT] key = "
491: + cck.getKey());
492: } else {
493: cck = ((ICacheable) sessionData.servantChannel)
494: .generateKey();
495: Utility.logMessage("DEBUG",
496: "CGroupsManager.generateKey():: [SERVANT] key = "
497: + cck.getKey());
498: }
499: Utility.logMessage("DEBUG",
500: "CGroupsManager.generateKey():: ChannelCacheKey.getKeyValidity = "
501: + cck.getKeyValidity());
502: return cck;
503: }
504:
505: /**
506: * put your documentation comment here
507: * @return String
508: */
509: private String vKey() {
510: Utility.logMessage("DEBUG", this .getClass().getName()
511: + "::vKey(): this = " + this );
512: CGroupsManagerSessionData sessionData = getSessionData();
513: String vkey = sessionData.currentPage + " - "
514: + sessionData.feedback + " - "
515: + sessionData.highlightedGroupID + " - "
516: + sessionData.mode + " - "
517: + sessionData.rootViewGroupID;
518: Utility.logMessage("DEBUG", this .getClass().getName()
519: + ".vKey() : vKey returns = " + vkey);
520: return vkey;
521: }
522:
523: /**
524: * put your documentation comment here
525: * @param validity
526: * @return boolean
527: */
528: public boolean isCacheValid(Object validity) {
529: Utility.logMessage("DEBUG", this .getClass().getName()
530: + "::isCacheValid(): this = " + this );
531: CGroupsManagerSessionData sessionData = getSessionData();
532: ChannelRuntimeData runtimeData = sessionData.runtimeData;
533: boolean valid = false;
534: if (sessionData.servantChannel == null) {
535: if (validity != null) {
536: if (validity.equals(vKey())
537: && runtimeData.get("grpCommand") == null) {
538: valid = true;
539: }
540: }
541: long time3 = Calendar.getInstance().getTime().getTime();
542: Utility.logMessage("DEBUG", this .getClass().getName()
543: + ".isCacheValid() time since setRD: "
544: + String.valueOf((time3 - sessionData.startRD))
545: + ", valid=" + valid);
546: } else {
547: valid = ((ICacheable) sessionData.servantChannel)
548: .isCacheValid(validity);
549: }
550: return valid;
551: }
552:
553: /**
554: * Returns the session data for a thread
555: * @return CGroupsManagerSessionData
556: */
557: public synchronized CGroupsManagerSessionData getSessionData() {
558: CGroupsManagerSessionData sd = sessionData;
559: if (sd == null) {
560: sd = new CGroupsManagerSessionData();
561: sd.permissible = this ;
562: sessionData = sd;
563: }
564: Utility.logMessage("DEBUG", this .getClass().getName()
565: + "::getSessionData(): sd = " + sd);
566: return sd;
567: }
568: }
|