001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.app.communityportlets.faces;
038:
039: import com.sun.portal.community.CommunityUser;
040: import com.sun.portal.community.RoleId;
041: import com.sun.portal.log.common.PortalLogger;
042: import java.util.ArrayList;
043: import java.util.Collections;
044: import java.util.HashSet;
045: import java.util.Iterator;
046: import java.util.Set;
047: import java.util.logging.Logger;
048: import javax.faces.model.ArrayDataModel;
049:
050: /**
051: *
052: * @author #ac120954
053: */
054: public class MembershipHandler extends CommunityBaseHandler {
055: private static Logger logger = PortalLogger
056: .getLogger(MembershipHandler.class);
057: public static final RoleId[] roleList = new RoleId[] {
058: RoleId.BANNED_ROLE, RoleId.OWNER_ROLE, RoleId.MEMBER_ROLE,
059: RoleId.INVITED_ROLE, RoleId.REJECTED_ROLE,
060: RoleId.PENDING_ROLE, RoleId.VISITOR_ROLE };
061:
062: private static final String[] ACTION_NAMES = new String[] {
063: "APPROVAL", "DENY", "REMOVE", "BAN", "UNBAN", "MAKEOWNER",
064: "REMOVEOWNER" };
065: private static final int ACTION_APPROVAL = 0;
066: private static final int ACTION_DENY = 1;
067: private static final int ACTION_REMOVE = 2;
068: private static final int ACTION_BAN = 3;
069: private static final int ACTION_UNBAN = 4;
070: private static final int ACTION_MAKEOWNER = 5;
071: private static final int ACTION_REMOVEOWNER = 6;
072: private String actionMenuSelectedItem;
073:
074: private PaginatingTable memberTable;
075: private PaginatingTable requestTable;
076:
077: /**
078: * Creates a new instance of MembershipHandler
079: */
080: public MembershipHandler() {
081: }
082:
083: /** Get user's highest role in current community */
084: private RoleId getRole(String userId) {
085:
086: for (int i = 0; i < roleList.length; i++) {
087: try {
088: if (getCommunityUser(userId).hasRole(getCommunityId(),
089: roleList[i])) {
090: return roleList[i];
091:
092: }
093: } catch (Exception e) {
094: //System.err.println("getActiveRole():" + e);
095: }
096: }
097: return RoleId.VISITOR_ROLE;
098: }
099:
100: public String doApproveAll() {
101: try {
102: Set users = getCommunity().getUsers(
103: Collections.singleton(RoleId.PENDING_ROLE));
104: if (users != null) {
105: Iterator it = users.iterator();
106: while (it.hasNext()) {
107: String userID = (String) it.next();
108: getCommunity().approveMembershipRequest(userID);
109: }
110: }
111: } catch (Exception e) {
112: //todo: log
113: }
114: return null;
115: }
116:
117: private void doApproveDeny(boolean approve) {
118: int first = requestTable.getDataTable().getFirst();
119: int rows = requestTable.getDataTable().getRows();
120: for (int i = 0; i < rows; i++) {
121: requestTable.getDataModel().setRowIndex(i + first);
122: try {
123: User r = (User) requestTable.getDataModel()
124: .getRowData();
125: if (r.isSelected()) {
126: if (approve) {
127: getCommunity().approveMembershipRequest(
128: r.getId());
129: } else {
130: getCommunity().rejectMembershipRequest(
131: r.getId());
132: }
133: }
134: } catch (Exception e) {
135: //ToDo: log
136: }
137: }
138: requestTable.setDataModel(getRequestsDataModel());
139: return;
140: }
141:
142: public void doApprove() {
143: doApproveDeny(true);
144: }
145:
146: public void doDeny() {
147: doApproveDeny(false);
148: }
149:
150: public String showApprovalPage() {
151: this .requestTable = null;
152: return "approve";
153: }
154:
155: public String showManageMemberPage() {
156: this .memberTable = null;
157: return "managemembers";
158: }
159:
160: private void handleAction(int action) {
161: int first = memberTable.getDataTable().getFirst();
162: int rows = memberTable.getDataTable().getRows();
163: for (int i = 0; i < rows; i++) {
164: memberTable.getDataModel().setRowIndex(i + first);
165: try {
166: User r = (User) memberTable.getDataModel().getRowData();
167: if (r.isSelected()) {
168: switch (action) {
169: //case ACTION_APPROVAL: getCommunity().approveMembershipRequest(r.getUid());break;
170: //case ACTION_DENY: getCommunity().rejectMembershipRequest(r.getUid());break;
171: case ACTION_REMOVE:
172: getCommunity().removeUser(r.getId(),
173: RoleId.MEMBER_ROLE);
174: break;
175: case ACTION_BAN:
176: getCommunity().banUser(r.getId());
177: break;
178: case ACTION_UNBAN:
179: getCommunity().unbanUser(r.getId());
180: break;
181: case ACTION_MAKEOWNER:
182: getCommunity().addOwner(r.getId());
183: break;
184: case ACTION_REMOVEOWNER:
185: getCommunity().removeUser(r.getId(),
186: RoleId.OWNER_ROLE);
187: break;
188: default: //todo: logging
189: }
190: }
191: } catch (Exception e) {
192: //ToDo: log
193: }
194: }
195: memberTable.setDataModel(this .getMembersDataModel());
196: return;
197: }
198:
199: public void doRemove() {
200: handleAction(ACTION_REMOVE);
201: }
202:
203: public void doBan() {
204: handleAction(ACTION_BAN);
205: }
206:
207: public void doRemoveBan() {
208: handleAction(ACTION_UNBAN);
209: }
210:
211: public void doMakeOwner() {
212: handleAction(ACTION_MAKEOWNER);
213: }
214:
215: public void doRemoveAsAnOwner() {
216: handleAction(ACTION_REMOVEOWNER);
217: }
218:
219: public int getNumOfPending() {
220: try {
221: Set users = getCommunity().getUsers(
222: Collections.singleton(RoleId.PENDING_ROLE));
223: if (users != null) {
224: return users.size();
225: }
226: } catch (Exception e) {
227: //ToDo: log
228: }
229: return 0;
230: }
231:
232: public String getActionName() {
233: return "";
234: }
235:
236: public void setActionName(String action) {
237: for (int i = 0; i < ACTION_NAMES.length; i++) {
238: if (ACTION_NAMES[i].equalsIgnoreCase(action)) {
239: handleAction(i);
240: }
241:
242: }
243: }
244:
245: private ArrayDataModel getRequestsDataModel() {
246: ArrayList requestList = new ArrayList();
247: try {
248: Set users = getCommunity().getUsers(
249: Collections.singleton(RoleId.PENDING_ROLE));
250: if (users != null) {
251: Iterator it = users.iterator();
252: while (it.hasNext()) {
253: String userID = (String) it.next();
254: CommunityUser user = this .getCommunityUser(userID);
255: User req = new User(userID, user.getFullName(), "",
256: RoleId.PENDING_ROLE);
257: requestList.add(req);
258: }
259: }
260: } catch (Exception e) {
261: //ToDo: log
262: }
263: return new ArrayDataModel(requestList.toArray());
264: }
265:
266: private ArrayDataModel getMembersDataModel() {
267: ArrayList membersList = new ArrayList();
268: try {
269: Set roles = new HashSet();
270: roles.add(RoleId.BANNED_ROLE);
271: roles.add(RoleId.MEMBER_ROLE);
272: roles.add(RoleId.OWNER_ROLE);
273: Set users = getCommunity().getUsers(roles);
274: if (users != null) {
275: CommunityUser myself = getCommunityUser();
276: Iterator it = users.iterator();
277: while (it.hasNext()) {
278: String userID = (String) it.next();
279: if (!userID.equalsIgnoreCase(myself.getUserId())) {
280: CommunityUser user = this
281: .getCommunityUser(userID);
282: User req = new User(userID, user.getFullName(),
283: user.getEmailAddr(), getRole(userID));
284: membersList.add(req);
285: }
286: }
287: }
288: } catch (Exception e) {
289: //ToDo: log
290: }
291:
292: return new ArrayDataModel(membersList.toArray());
293:
294: }
295:
296: public PaginatingTable getMemberTable() {
297: if (memberTable == null) {
298: memberTable = new PaginatingTable(this
299: .getMembersDataModel());
300: }
301: return memberTable;
302: }
303:
304: public void setMemberTable(PaginatingTable memberTable) {
305: this .memberTable = memberTable;
306: }
307:
308: public PaginatingTable getRequestTable() {
309: if (requestTable == null) {
310: requestTable = new PaginatingTable(this
311: .getRequestsDataModel());
312: }
313: return requestTable;
314: }
315:
316: public void setRequestTable(PaginatingTable requestTable) {
317: this.requestTable = requestTable;
318: }
319:
320: }
|