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: /*
038: * RoleMgmHandler.java
039: *
040: * Created on May 19, 2005, 10:49 AM
041: *
042: */
043:
044: package com.sun.portal.app.communityportlets.faces;
045:
046: import java.util.*;
047:
048: import com.sun.data.provider.impl.ObjectListDataProvider;
049: import com.sun.portal.community.CommunityException;
050: import com.sun.portal.community.RoleId;
051: import com.sun.portal.community.CommunityId;
052: import com.sun.portal.community.CommunityUser;
053: import com.sun.portal.community.urlmanager.CommunityURLManager;
054: import com.sun.portal.community.urlmanager.CommunityURLManagerFactory;
055: import com.sun.web.ui.model.Option;
056: import java.util.logging.Logger;
057: import java.util.logging.Level;
058: import com.sun.portal.log.common.PortalLogger;
059: import com.sun.web.ui.component.Checkbox;
060: import javax.faces.context.ExternalContext;
061: import javax.faces.context.FacesContext;
062: import javax.portlet.ActionResponse;
063:
064: /**
065: *
066: * @author ac120954
067: */
068: public class RoleMgmHandler extends CommunityBaseHandler {
069:
070: private boolean initError = false;
071: private CommunityId communityId;
072: private ObjectListDataProvider members;
073: private String newMember;
074: /** Action menu items. */
075: private Option[] actionOptions = null;
076: private String selectedAction;
077: private ResourceBundle resourceBundle = null;
078: private RoleId activeRole;
079: public static final String ROLEMANAGEMENT_PROPERTIES = "com.sun.portal.app.communityportlets.bundle.CommunityRoleMgmt";
080: public static final RoleId[] roleList = new RoleId[] {
081: RoleId.OWNER_ROLE, RoleId.MEMBER_ROLE };
082: private static Logger logger = PortalLogger
083: .getLogger(RoleMgmHandler.class);
084: private String alertMessage = null;
085: private boolean hasAlert = false;
086: private long memberListRefreshTime = 60 * 1000 * 1; //1 minutes
087: long memberListLastUpdate;
088:
089: /** Creates a new instance of RoleMgmHandler */
090: public RoleMgmHandler() {
091: try {
092: resourceBundle = ResourceBundle.getBundle(
093: ROLEMANAGEMENT_PROPERTIES, this .getUserLocale());
094: } catch (MissingResourceException mre) {
095: logger.log(Level.SEVERE, "PSCPL_CSPACF00500", mre);
096: initError = true;
097: }
098: }
099:
100: private String getLocalizedMessage(String key) {
101: if (resourceBundle != null) {
102: try {
103: return resourceBundle.getString(key);
104: } catch (Exception e) {
105: logger.log(Level.WARNING, "PSCPL_CSPACF00507", e);
106: }
107: }
108: return key;
109: }
110:
111: public String getCommunityName() {
112: try {
113: CommunityId cid = getCommunityId();
114: if (communityId == null
115: || !communityId.toString().equals(cid.toString())) {
116: communityId = cid;
117: activeRole = null;
118: }
119: return cid.getName();
120: } catch (Exception e) {
121: logger.log(Level.SEVERE, "PSCPL_CSPACF00506", e);
122: }
123: return "";
124: }
125:
126: public RoleId getActiveRole() {
127: if (activeRole == null) {
128: for (int i = 0; i < this .roleList.length; i++) {
129: try {
130: if (getCommunityUser().hasRole(communityId,
131: roleList[i])) {
132: activeRole = roleList[i];
133: break;
134: }
135: } catch (Exception e) {
136: logger.log(Level.WARNING, "PSCPL_CSPACF00501", e);
137: activeRole = RoleId.VISITOR_ROLE;
138: }
139: }
140: }
141:
142: return activeRole;
143: }
144:
145: public boolean getInitError() {
146: return initError;
147: }
148:
149: public boolean getHasAlert() {
150: return hasAlert;
151: }
152:
153: public String getAlertMessage() {
154: hasAlert = false;
155: return this .alertMessage;
156: }
157:
158: /** Get Data util for group A. */
159: public ObjectListDataProvider getMemberGroup() {
160: long leap = java.util.Calendar.getInstance().getTimeInMillis()
161: - this .memberListLastUpdate;
162: if (members != null && leap > this .memberListRefreshTime) {
163: members = null;
164: }
165: if (members == null) {
166: members = getUserEntries(RoleId.MEMBER_ROLE);
167: this .memberListLastUpdate = java.util.Calendar
168: .getInstance().getTimeInMillis();
169: }
170: return members;
171: }
172:
173: public void setMemberGroup(ObjectListDataProvider members) {
174: this .members = members;
175: }
176:
177: protected ObjectListDataProvider getUserEntries(RoleId rid) {
178: List userList = new ArrayList();
179: try {
180: Set users = getCommunity().getUsers(
181: Collections.singleton(rid));
182: if (users != null) {
183: Iterator uit = users.iterator();
184: while (uit.hasNext()) {
185: String userID = (String) uit.next();
186: if (!userID.equalsIgnoreCase(this .getUserName())) {
187: String fullName;
188: CommunityUser user = this
189: .getCommunityUser(userID);
190: try {
191: fullName = user.getFullName();
192: } catch (CommunityException ce) {
193: fullName = userID;
194: }
195: User u = new User(userID, fullName);
196: userList.add(u);
197: }
198: }
199: }
200: } catch (Exception e) {
201: logger.log(Level.WARNING, "PSCPL_CSPACF00502", e);
202: }
203: ObjectListDataProvider users = new ObjectListDataProvider(
204: userList);
205: users.setObjectType(User.class);
206: return users;
207: }
208:
209: public String getNewMember() {
210: return newMember;
211: }
212:
213: public void setNewMember(String value) {
214: newMember = value;
215: }
216:
217: public String addNewMember() {
218: if (newMember != null && newMember.trim().length() > 0) {
219: try {
220: getCommunity().addMember(newMember.trim());
221: newMember = "";
222: this .members = null;
223: } catch (Exception e) {
224: logger.log(Level.WARNING, "PSCPL_CSPACF00503", e);
225: hasAlert = true;
226: alertMessage = getLocalizedMessage("error_fail_add");
227: }
228: }
229: return "";
230: }
231:
232: public boolean getCanDoDelete() {
233: return (RoleId.OWNER_ROLE.equals(this .getActiveRole()));
234: }
235:
236: public boolean getCanDoOwnerTransfer() {
237: return (RoleId.OWNER_ROLE.equals(this .getActiveRole()));
238: }
239:
240: public boolean getCanDoAdd() {
241: return (RoleId.OWNER_ROLE.equals(this .getActiveRole()));
242: }
243:
244: /** Get action menu options. */
245: public Option[] getActionOptions() {
246: ArrayList optionList = new ArrayList();
247: getActiveRole();
248: optionList.add(new Option("selectaction",
249: getLocalizedMessage("label_selectaction")));
250: if (RoleId.OWNER_ROLE.equals(this .getActiveRole())) {
251: optionList.add(new Option("owner",
252: getLocalizedMessage("action_changeowner")));
253: optionList.add(new Option("delete",
254: getLocalizedMessage("action_delete")));
255: }
256: return (Option[]) optionList.toArray(new Option[0]);
257: }
258:
259: public String getSelectedAction() {
260: return selectedAction;
261: }
262:
263: public void setSelectedAction(String value) {
264: selectedAction = value;
265: }
266:
267: public void actionSelected() {
268: if (selectedAction != null) {
269: if (selectedAction.equals("delete")) {
270: memberDelete();
271: } else if (selectedAction.equals("owner")) {
272: memberOwner();
273: }
274: selectedAction = "";
275: }
276: }
277:
278: public String memberOwner() {
279: try {
280: List l = Checkbox.getSelected("checkbox");
281: if (l.size() > 0) {
282: String id = (String) l.get(0);
283: getCommunity().addOwner(id);
284: getCommunity().removeUser(getUserName(),
285: RoleId.OWNER_ROLE);
286: }
287: this .activeRole = null;
288: // redirecting to current community as member
289: CommunityURLManager cum = CommunityURLManagerFactory
290: .getInstance().getCommunityURLManager();
291: ExternalContext eContext = getExternalContext();
292: ActionResponse aRes = (ActionResponse) eContext
293: .getResponse();
294: FacesContext.getCurrentInstance().responseComplete();
295: aRes.sendRedirect(cum.getCommunityAccessURL(
296: getHttpServletRequest(), getCommunityId()));
297: } catch (Exception e) {
298: logger.log(Level.WARNING, "PSCPL_CSPACF00504", e);
299: hasAlert = true;
300: alertMessage = getLocalizedMessage("error_fail_owner");
301: }
302: return null;
303: }
304:
305: public String memberDelete() {
306: try {
307: List l = Checkbox.getSelected("checkbox");
308: for (int index = 0; index < l.size(); index++) {
309: String id = (String) l.get(index);
310: getCommunity().removeUser(id, RoleId.MEMBER_ROLE);
311: }
312: members = null;
313: } catch (Exception e) {
314: logger.log(Level.WARNING, "PSCPL_CSPACF00505", e);
315: hasAlert = true;
316: alertMessage = getLocalizedMessage("error_fail_delete");
317: }
318: return null;
319: }
320:
321: }
|