001: package com.sun.portal.app.communityportlets.faces;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.HashSet;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.logging.Level;
010: import java.util.logging.Logger;
011: import java.util.logging.LogRecord;
012: import java.util.Map;
013: import java.util.MissingResourceException;
014: import java.util.ResourceBundle;
015: import java.util.Set;
016: import java.text.MessageFormat;
017:
018: import javax.faces.application.*;
019: import javax.faces.component.*;
020: import javax.faces.context.*;
021: import javax.faces.el.*;
022: import javax.faces.event.*;
023: import javax.servlet.http.HttpServletRequest;
024:
025: import com.sun.data.provider.impl.ObjectListDataProvider;
026: import com.sun.data.provider.impl.*;
027: import com.sun.data.provider.*;
028:
029: import com.sun.portal.community.CommunityManager;
030: import com.sun.portal.community.CommunityFactory;
031: import com.sun.portal.community.CommunityException;
032: import com.sun.portal.community.CommunityServiceException;
033: import com.sun.portal.community.RoleId;
034: import com.sun.portal.community.CommunityUser;
035: import com.sun.portal.community.CommunityId;
036: import com.sun.portal.community.Community;
037: import com.sun.portal.community.urlmanager.CommunityURLManagerFactory;
038:
039: import com.sun.portal.log.common.PortalLogger;
040:
041: /**
042: * Backing bean for membership used in CommunityMembership.jsp.
043: */
044: public class CommunityMembershipHandler extends CommunityBaseHandler {
045:
046: private List _communities = null;
047: private int pageSize = 10;
048: private boolean _showAlert = false;
049: private String _alertSummary = "";
050: private CommunityMembershipPaging communityMembershipPaging = null;
051: private static Logger _logger = PortalLogger
052: .getLogger(CommunityMembershipHandler.class);
053:
054: public CommunityMembershipHandler() {
055: retrieveCommunities();
056: }
057:
058: public String getAlertSummary() {
059: return _alertSummary;
060: }
061:
062: public boolean getShowAlert() {
063: return _showAlert;
064: }
065:
066: public CommunityMembershipPaging getCommunityMembershipPaging() {
067: return communityMembershipPaging;
068: }
069:
070: public void setCommunityMembershipPaging(
071: CommunityMembershipPaging communityMembershipPaging) {
072: this .communityMembershipPaging = communityMembershipPaging;
073: }
074:
075: public List getCommunities() {
076: return _communities;
077: }
078:
079: public int getPageSize() {
080: return pageSize;
081: }
082:
083: public int getFirstRow() {
084: return getCommunityMembershipPaging().getFirstRow();
085: }
086:
087: public int getTotalPages() {
088: return ((_communities.size() - 1) / getPageSize()) + 1;
089: }
090:
091: public String getPaginationText() {
092: int page = (getFirstRow() / getPageSize()) + 1;
093: int totalPages = getTotalPages();
094: try {
095: FacesContext fc = FacesContext.getCurrentInstance();
096: ResourceBundle rb = ResourceBundle
097: .getBundle(
098: "com.sun.portal.app.communityportlets.bundle.CommunityMembership",
099: fc.getViewRoot().getLocale());
100: Object[] args = { Integer.toString(page),
101: Integer.toString(totalPages) };
102: return MessageFormat.format(rb.getString("pagination"),
103: args);
104: } catch (MissingResourceException mre) {
105: return " " + Integer.toString(page) + " of "
106: + Integer.toString(totalPages) + " ";
107: }
108: }
109:
110: public boolean getRenderPrevious() {
111: return getFirstRow() != getPreviousFirstRow();
112: }
113:
114: public boolean getRenderNext() {
115: return getFirstRow() != getNextFirstRow();
116: }
117:
118: public String previousPageAction() {
119: getCommunityMembershipPaging().setFirstRow(
120: getPreviousFirstRow());
121: return "self";
122: }
123:
124: public String nextPageAction() {
125: getCommunityMembershipPaging().setFirstRow(getNextFirstRow());
126: return "self";
127: }
128:
129: public String acceptInvitationAction() {
130: try {
131: String communityName = (String) getFacesParamValue("community.name");
132: CommunityId cId = new CommunityId("jdo", communityName);
133:
134: CommunityUser cu = getCommunityUser();
135: Map m = cu.getMembership();
136: if (m.containsKey(cId)) {
137: Set s = (Set) m.get(cId);
138: if (s.contains(RoleId.INVITED_ROLE)) {
139: Community c = getCommunity(cId);
140: c.acceptInvitation(cu.getUserId());
141: }
142: }
143:
144: retrieveCommunities();
145:
146: _showAlert = false;
147: _alertSummary = "";
148: } catch (Exception e) {
149: logRecord(_logger, Level.SEVERE, "PSCPL_CSPACF00600", e,
150: getCommunityUser().getUserId());
151:
152: _showAlert = true;
153: _alertSummary = getExceptionMessage();
154: }
155:
156: return "self";
157: }
158:
159: public String declineInvitationAction() {
160: try {
161: String communityName = (String) getFacesParamValue("community.name");
162: CommunityId cId = new CommunityId("jdo", communityName);
163:
164: CommunityUser cu = getCommunityUser();
165: Map m = cu.getMembership();
166: if (m.containsKey(cId)) {
167: Set s = (Set) m.get(cId);
168: if (s.contains(RoleId.INVITED_ROLE)) {
169: Community c = getCommunity(cId);
170: c.denyInvitation(cu.getUserId());
171: }
172: }
173:
174: retrieveCommunities();
175:
176: _showAlert = false;
177: _alertSummary = "";
178: } catch (Exception e) {
179: logRecord(_logger, Level.SEVERE, "PSCPL_CSPACF00600", e,
180: getCommunityUser().getUserId());
181:
182: _showAlert = true;
183: _alertSummary = getExceptionMessage();
184: }
185:
186: return "self";
187: }
188:
189: public String hideRejectedInvitationAction() {
190: try {
191: String communityName = (String) getFacesParamValue("community.name");
192: CommunityId cId = new CommunityId("jdo", communityName);
193:
194: CommunityUser cu = getCommunityUser();
195: Map m = cu.getMembership();
196: if (m.containsKey(cId)) {
197: Set s = (Set) m.get(cId);
198: if (s.contains(RoleId.REJECTED_ROLE)) {
199: Community c = getCommunity(cId);
200: c.removeUser(cu.getUserId(), RoleId.REJECTED_ROLE);
201: }
202: }
203:
204: retrieveCommunities();
205:
206: _showAlert = false;
207: _alertSummary = "";
208: } catch (Exception e) {
209: logRecord(_logger, Level.SEVERE, "PSCPL_CSPACF00600", e,
210: getCommunityUser().getUserId());
211:
212: _showAlert = true;
213: _alertSummary = getExceptionMessage();
214: }
215:
216: return "self";
217: }
218:
219: private void retrieveCommunities() {
220: _communities = new ArrayList();
221: try {
222: CommunityUser cu = getCommunityUser();
223:
224: Map m = cu.getMembership();
225: Iterator i = m.keySet().iterator();
226: while (i.hasNext()) {
227: CommunityId cId = (CommunityId) i.next();
228:
229: String url = CommunityURLManagerFactory.getInstance()
230: .getCommunityURLManager()
231: .getCommunityAccessURL(getHttpServletRequest(),
232: cId);
233:
234: Set s = (Set) m.get(cId);
235: if (s.contains(RoleId.BANNED_ROLE)) {
236: CommunityMembershipEntry cme = new CommunityMembershipEntry(
237: cId, url, RoleId.BANNED_ROLE);
238: _communities.add(cme);
239: } else if (s.contains(RoleId.OWNER_ROLE)) {
240: Community c = getCommunity(cId);
241: HashSet hs = new HashSet();
242: hs.add(RoleId.PENDING_ROLE);
243: CommunityMembershipEntry cme = new CommunityMembershipEntry(
244: cId, url, RoleId.OWNER_ROLE, c.getUsers(hs)
245: .size());
246: _communities.add(cme);
247: } else if (s.contains(RoleId.MEMBER_ROLE)) {
248: CommunityMembershipEntry cme = new CommunityMembershipEntry(
249: cId, url, RoleId.MEMBER_ROLE);
250: _communities.add(cme);
251: } else if (s.contains(RoleId.INVITED_ROLE)) {
252: CommunityMembershipEntry cme = new CommunityMembershipEntry(
253: cId, url, RoleId.INVITED_ROLE);
254: _communities.add(cme);
255: } else if (s.contains(RoleId.REJECTED_ROLE)) {
256: CommunityMembershipEntry cme = new CommunityMembershipEntry(
257: cId, url, RoleId.REJECTED_ROLE);
258: _communities.add(cme);
259: } else if (s.contains(RoleId.PENDING_ROLE)) {
260: CommunityMembershipEntry cme = new CommunityMembershipEntry(
261: cId, url, RoleId.PENDING_ROLE);
262: _communities.add(cme);
263: }
264: }
265:
266: Collections.sort(_communities);
267:
268: _showAlert = false;
269: _alertSummary = "";
270: } catch (Exception e) {
271: logRecord(_logger, Level.SEVERE, "PSCPL_CSPACF00600", e,
272: getCommunityUser().getUserId());
273:
274: _showAlert = true;
275: _alertSummary = getExceptionMessage();
276: }
277: }
278:
279: private int getNextFirstRow() {
280: int x = getFirstRow();
281: int y = getPageSize();
282: int z = x + y;
283: if (z < _communities.size()) {
284: return z;
285: } else {
286: return x;
287: }
288: }
289:
290: private int getPreviousFirstRow() {
291: return Math.max(getFirstRow() - getPageSize(), 0);
292: }
293:
294: public static String getFacesParamValue(String name) {
295: return (String) FacesContext.getCurrentInstance()
296: .getExternalContext().getRequestParameterMap()
297: .get(name);
298: }
299:
300: private String getExceptionMessage() {
301: try {
302: FacesContext fc = FacesContext.getCurrentInstance();
303: ResourceBundle rb = ResourceBundle
304: .getBundle(
305: "com.sun.portal.app.communityportlets.bundle.CommunityMembership",
306: fc.getViewRoot().getLocale());
307: return rb.getString("error_membership");
308: } catch (MissingResourceException mre) {
309: return "Failed to get membership information.";
310: }
311: }
312:
313: }
|