001: package com.sun.portal.app.communityportlets.faces;
002:
003: import com.sun.portal.community.urlmanager.CommunityURLManagerFactory;
004: import com.sun.portal.community.urlmanager.CommunityURLManager;
005: import java.text.MessageFormat;
006: import java.io.IOException;
007: import java.util.ResourceBundle;
008: import java.util.MissingResourceException;
009: import java.util.logging.Logger;
010: import java.util.logging.Level;
011:
012: import com.sun.portal.community.CommunityManager;
013: import com.sun.portal.community.CommunityException;
014: import com.sun.portal.community.RoleId;
015: import com.sun.portal.community.Community;
016: import com.sun.portal.community.CommunityServiceException;
017: import com.sun.portal.desktop.DesktopParameters;
018:
019: import javax.faces.context.FacesContext;
020: import javax.faces.context.ExternalContext;
021: import javax.faces.event.AbortProcessingException;
022:
023: import com.sun.portal.log.common.PortalLogger;
024:
025: import javax.portlet.ActionResponse;
026:
027: import com.sun.portal.search.soif.*;
028: import com.sun.portal.search.demo.Search;
029: import java.util.Collections;
030: import java.util.StringTokenizer;
031: import javax.portlet.PortletRequest;
032: import javax.portlet.PortletSession;
033:
034: /**
035: * Backing bean for table examples.
036: */
037: public class CommunityInfoHandler extends CommunityBaseHandler {
038: private boolean isOwner;
039: private RoleId role;
040: private int members = -1; // number of members
041: private String description = null;
042: private boolean showAlert = false;
043: private String alertSummary = "";
044: private String alertDetail = "";
045: private static Logger logger = PortalLogger
046: .getLogger(CommunityInfoHandler.class);
047: public static final RoleId[] roleList = new RoleId[] {
048: RoleId.BANNED_ROLE, RoleId.OWNER_ROLE, RoleId.MEMBER_ROLE,
049: RoleId.INVITED_ROLE, RoleId.REJECTED_ROLE,
050: RoleId.PENDING_ROLE, RoleId.VISITOR_ROLE };
051: public static final String COMMUNITY_INFO_MESSAGES_RESOURCE_BUNDLE_NAME = "com.sun.portal.app.communityportlets.bundle.CommunityInfo";
052: private String accessType;
053: private String modeMembership;
054: private String modeListing;
055: private String modeSecure;
056: private String searchRequestUrl = null;
057:
058: /** Default constructor. */
059: public CommunityInfoHandler() {
060: //role = getRole();
061: members = getMembers();
062: description = getDescription();
063: }
064:
065: /** default constructor. */
066: public CommunityInfoHandler(boolean isOwner, int members) {
067: this .isOwner = isOwner;
068: this .members = members;
069: }
070:
071: /** Get user's highest role in current community
072: * since this bean will be a session bean, we need getRole() to get the latest role
073: * However, it will only be called once, and rest of rendering process should use local
074: * variable role for performance purpose.
075: */
076: public RoleId getRole() {
077: role = RoleId.VISITOR_ROLE;
078: for (int i = 0; i < this .roleList.length; i++) {
079: try {
080: if (getCommunityUser().hasRole(getCommunityId(),
081: roleList[i])) {
082: role = roleList[i];
083: break;
084: }
085: } catch (Exception e) {
086: //System.err.println("getActiveRole():" + e);
087: }
088: }
089: return role;
090: }
091:
092: public String getName() {
093: return getCommunityId().getName();
094: }
095:
096: /** Get community number of members */
097: public int getMembers() {
098: try {
099: Community c = getCommunity();
100: members = c.getUsers(
101: Collections.singleton(RoleId.MEMBER_ROLE)).size();
102: } catch (CommunityException ce) {
103: // TODO: what the heck?!
104: members = 1;
105: }
106: return members;
107: }
108:
109: /** show join button only if
110: * not in alert mode
111: * user has visitor role
112: * community is not membership restricted
113: */
114: public boolean isShowJoin() {
115: try {
116:
117: if (!showAlert && RoleId.VISITOR_ROLE.equals(role)) {
118:
119: Community c = getCommunity();
120: return !c.isMembershipRestricted();
121: }
122: } catch (Exception e) {
123:
124: }
125: return false;
126: }
127:
128: /** show membership request only if
129: * not in alert mode
130: * user has visitor role
131: * community is membership restricted
132: */
133: public boolean isShowRequest() {
134: try {
135:
136: if (!showAlert && RoleId.VISITOR_ROLE.equals(role)) {
137:
138: Community c = getCommunity();
139: return c.isMembershipRestricted();
140: }
141: } catch (Exception e) {
142:
143: }
144: return false;
145: }
146:
147: /** show invitation only if
148: * not in alert mode and user has invited role
149: */
150: public boolean isShowInvitation() {
151: try {
152:
153: if (!showAlert && RoleId.INVITED_ROLE.equals(role)) {
154: return true;
155: }
156: } catch (Exception e) {
157:
158: }
159: return false;
160: }
161:
162: /** check if leave action is possible
163: * owner cannot leave
164: */
165: public boolean isShowLeave() {
166: return (!showAlert && RoleId.MEMBER_ROLE.equals(role));
167: }
168:
169: /** check if leave action is possible
170: * only owner can delete
171: */
172: public boolean isShowDelete() {
173: return (!showAlert && RoleId.OWNER_ROLE.equals(role));
174: }
175:
176: public boolean isManagePrivilege() {
177: return (!showAlert && RoleId.OWNER_ROLE.equals(role));
178: }
179:
180: public void setShowAlert(boolean b) {
181: showAlert = b;
182: }
183:
184: /** return true if request param "infoAlert=true"
185: * false otherwise
186: */
187: public boolean isShowAlert() {
188: return showAlert;
189: }
190:
191: public void acceptInvitation() {
192: try {
193: Community c = getCommunity();
194: c.acceptInvitation(getUserName());
195: role = null;
196: setRedirectToCommunityURL();
197: } catch (CommunityServiceException cse) {
198: setAlert(cse, "accept invitation");
199: } catch (CommunityException ce) {
200: setAlert(ce, "accept invitation");
201: } catch (Exception e) {
202: setAlert(e, "accept invitation");
203: }
204: }
205:
206: public void denyInvitation() {
207: try {
208: Community c = getCommunity();
209: c.denyInvitation(getUserName());
210: role = null;
211: } catch (CommunityServiceException cse) {
212: setAlert(cse, "deny invitation");
213: } catch (CommunityException ce) {
214: setAlert(ce, "deny invitation");
215: } catch (Exception e) {
216: setAlert(e, "deny invitation");
217: }
218: }
219:
220: public void requestMembership() {
221: try {
222: Community c = getCommunity();
223: c.requestMembership(this .getUserName());
224: role = null;
225: } catch (CommunityServiceException cse) {
226: setAlert(cse, "request");
227: } catch (CommunityException ce) {
228: setAlert(ce, "request");
229: } catch (Exception e) {
230: setAlert(e, "request");
231: }
232: }
233:
234: /**
235: * After a user's role changed,
236: * it needs a new community url to refresh the dp
237: * @throws com.sun.portal.community.CommunityException
238: */
239: private void setRedirectToCommunityURL() throws CommunityException {
240: try {
241: // redirecting to current community as member
242: CommunityURLManager cum = CommunityURLManagerFactory
243: .getInstance().getCommunityURLManager();
244: ExternalContext eContext = getExternalContext();
245: ActionResponse aRes = (ActionResponse) eContext
246: .getResponse();
247: FacesContext.getCurrentInstance().responseComplete();
248: aRes.sendRedirect(cum.getCommunityAccessURL(
249: getHttpServletRequest(), getCommunityId()));
250: } catch (IOException ioe) {
251: setAlert(ioe, "join");
252: }
253: }
254:
255: private String communityCurrentMembershipParameter(String url) {
256: StringTokenizer tokenizer = new StringTokenizer(url, "&");
257: while (tokenizer.hasMoreTokens()) {
258: String p = tokenizer.nextToken();
259: if (p.startsWith(DesktopParameters.CURRENT_MEMBERSHIP)) {
260: return "dt." + p;
261: }
262: }
263: return null;
264: }
265:
266: /* actions */
267: /** Join the current community
268: * if already member do nothing
269: */
270: public String join() {
271: try {
272: Community c = getCommunity();
273: //TODO verify the user is not blocked
274: c.addUser(getCommunityUser().getUserId(),
275: RoleId.MEMBER_ROLE);
276: role = null;
277: if (this .searchRequestUrl != null) {
278: ExternalContext eContext = getExternalContext();
279: ActionResponse aRes = (ActionResponse) eContext
280: .getResponse();
281: FacesContext.getCurrentInstance().responseComplete();
282: String tmp = searchRequestUrl;
283: searchRequestUrl = null;
284: aRes.sendRedirect(tmp);
285: } else {
286: setRedirectToCommunityURL();
287: }
288: } catch (CommunityServiceException cse) {
289: setAlert(cse, "join");
290: } catch (CommunityException ce) {
291: setAlert(ce, "join");
292: } catch (Exception e) {
293: setAlert(e, "join");
294: }
295: return "info";
296: }
297:
298: /** leave the current community
299: * if not member do nothing
300: */
301: public String leave() throws Throwable {
302: try {
303: Community c = getCommunity();
304: // removing user from community
305: c.removeUser(getCommunityUser().getUserId(),
306: RoleId.MEMBER_ROLE);
307: role = null;
308: } catch (CommunityServiceException cse) {
309: setAlert(cse, "leave");
310: } catch (CommunityException ce) {
311: setAlert(ce, "leave");
312: } catch (Exception e) {
313: setAlert(e, "leave");
314: }
315:
316: try {
317: // redireting to community home
318: ExternalContext eContext = getExternalContext();
319: ActionResponse aRes = (ActionResponse) eContext
320: .getResponse();
321: FacesContext.getCurrentInstance().responseComplete();
322: aRes.sendRedirect(getHomeURL());
323: } catch (IOException ioe) {
324: setAlert(ioe, "leave");
325: }
326: return "info";
327: }
328:
329: /** delete community
330: * if not owner, show error message
331: */
332: public String delete() {
333: try {
334: getCommunity().setDeleted(true);
335: getHttpServletRequest()
336: .getSession()
337: .setAttribute(
338: this .COMMUNITY_CREATED_DELETE_NOTIFICATION_SESSION_ATTRIBUTE_KEY,
339: "true");
340: } catch (CommunityServiceException cse) {
341: setAlert(cse, "delete");
342: } catch (CommunityException ce) {
343: setAlert(ce, "delete");
344: } catch (Exception e) {
345: setAlert(e, "delete");
346: }
347: try {
348: // redirecting home
349: ExternalContext eContext = getExternalContext();
350: ActionResponse aRes = (ActionResponse) eContext
351: .getResponse();
352: FacesContext.getCurrentInstance().responseComplete();
353: aRes.sendRedirect(getHomeURL());
354: } catch (Exception e) {
355: setAlert(e, "delete");
356: }
357: return "info";
358: }
359:
360: private void setAlert(Throwable e, String action) {
361: logRecord(logger, Level.SEVERE, "PSCPL_CSPACF00300", e,
362: new Object[] { action });
363: setShowAlert(true);
364: setAlertSummary(getExceptionMessage(action));
365: setAlertDetail(".");
366: }
367:
368: public String getHomeURL() {
369: CommunityURLManager cum = CommunityURLManagerFactory
370: .getInstance().getCommunityURLManager();
371: return cum.getCommunityHomeURL(getHttpServletRequest());
372: }
373:
374: public String goCommunityHome() throws AbortProcessingException {
375: // redirect to community home page
376: String homeURL = getHomeURL();
377: try {
378: // clear bean as we go back CommunityHome
379: clear();
380: ExternalContext eContext = getExternalContext();
381: ActionResponse aRes = (ActionResponse) eContext
382: .getResponse();
383: FacesContext.getCurrentInstance().responseComplete();
384: aRes.sendRedirect(homeURL);
385: return "return";
386: } catch (IOException ioe) {
387: logRecord(logger, Level.SEVERE, "PSCPL_CSPACF00301", ioe,
388: new Object[] { homeURL });
389: FacesContext context = FacesContext.getCurrentInstance();
390: throw new AbortProcessingException(getMessage(context,
391: "community_redirect_failed"), ioe);
392: }
393: }
394:
395: private void clear() {
396: showAlert = false;
397: alertSummary = "";
398: alertDetail = "";
399: }
400:
401: private String getExceptionMessage(String str) {
402: try {
403: FacesContext ctx = FacesContext.getCurrentInstance();
404: ResourceBundle rb = ResourceBundle.getBundle(
405: COMMUNITY_INFO_MESSAGES_RESOURCE_BUNDLE_NAME, ctx
406: .getViewRoot().getLocale());
407: String failed_msg = rb.getString("failed_action");
408: Object[] args = { rb.getString(str), getName() };
409: showAlert = true;
410: return MessageFormat.format(failed_msg, args);
411: } catch (MissingResourceException mre) {
412: //TODO log missing resource bundle
413: return ("failed to " + str + " selected community : " + getName());
414: }
415: }
416:
417: public String getAlertSummary() {
418: return alertSummary;
419: }
420:
421: public void setAlertSummary(String alertSummary) {
422: this .alertSummary = alertSummary;
423: }
424:
425: public String getAlertDetail() {
426: return alertDetail;
427: }
428:
429: public void setAlertDetail(String alertDetail) {
430: this .alertDetail = alertDetail;
431: }
432:
433: /** cancel current operation
434: */
435: public String cancel() {
436: return "cancel";
437: }
438:
439: public String getDescription() {
440: if (description == null) {
441: try {
442: Community c = getCommunity();
443: description = c.getDescription();
444: } catch (Exception e) {
445: description = "";
446: //todo: loggin
447: }
448:
449: }
450: return description;
451: }
452:
453: public void setDescription(String description) {
454: this .description = description;
455: }
456:
457: public String getMembershipMode() {
458: try {
459: if (getCommunity().isMembershipRestricted()) {
460: return "private";
461: } else {
462: return "public";
463: }
464: } catch (Exception e) {
465: //todo: loggin
466: }
467: return "";
468: }
469:
470: public void setMembershipMode(String value) {
471: modeMembership = value;
472: }
473:
474: public String getListingMode() {
475: try {
476: if (getCommunity().isListed()) {
477: return "public";
478: } else {
479: return "private";
480: }
481: } catch (Exception e) {
482: //todo: loggin
483: }
484: return "";
485: }
486:
487: public void setListingMode(String value) {
488: modeListing = value;
489: }
490:
491: public String getSecureMode() {
492: try {
493: if (getCommunity().isSecure()) {
494: return "private";
495: } else {
496: return "public";
497: }
498: } catch (Exception e) {
499: //todo: loggin
500: }
501: return "";
502:
503: }
504:
505: public void setSecureMode(String value) {
506: modeSecure = value;
507: }
508:
509: public String getAccessType() {
510: try {
511: boolean m = getCommunity().isMembershipRestricted();
512: boolean l = getCommunity().isListed();
513: boolean s = getCommunity().isSecure();
514: if (m && !l && s) {
515: return "private";
516: } else if (!m && l && !s) {
517: return "public";
518: }
519: return "custom";
520: } catch (Exception e) {
521: //todo: loggin
522: }
523: return "";
524:
525: }
526:
527: public void setAccessType(String value) {
528: accessType = value;
529: }
530:
531: public String doSaveAccess() {
532: try {
533: Community cty = getCommunity();
534: if ("private".equals(accessType)) {
535: cty.setListed(false);
536: cty.setSecure(true);
537: cty.setMembershipRestricted(true);
538: } else if ("public".equals(accessType)) {
539: cty.setListed(true);
540: cty.setSecure(false);
541: cty.setMembershipRestricted(false);
542: } else if ("custom".equals(accessType)) {
543: if ("public".equals(modeMembership)) {
544: cty.setMembershipRestricted(false);
545: } else if ("private".equals(modeMembership)) {
546: cty.setMembershipRestricted(true);
547: }
548: if ("public".equals(modeListing)) {
549: cty.setListed(true);
550: } else if ("private".equals(modeListing)) {
551: cty.setListed(false);
552: }
553: if ("public".equals(modeSecure)) {
554: cty.setSecure(false);
555: } else if ("private".equals(modeSecure)) {
556: cty.setSecure(true);
557: }
558:
559: }
560: return "info";
561: } catch (Exception e) {
562: //todo: loggin
563:
564: }
565: return "";
566: }
567:
568: protected Object getPortletSessionAttribute(String key) {
569: Object value = null;
570: FacesContext context = FacesContext.getCurrentInstance();
571: Object pReq = context.getExternalContext().getRequest();
572: if (pReq instanceof PortletRequest) {
573: PortletSession pSession = ((PortletRequest) pReq)
574: .getPortletSession();
575: if (pSession != null) {
576: value = pSession.getAttribute(key);
577: if (value != null) {
578: pSession.removeAttribute(key);
579: }
580: }
581: }
582: return value;
583: }
584:
585: public boolean isSearchRequested() {
586: if (this .searchRequestUrl == null) {
587: searchRequestUrl = (String) getPortletSessionAttribute("SearchRequestUrl");
588: }
589: return (searchRequestUrl != null);
590: }
591:
592: }
|