001: /**
002: * $Id: AddRemoveBean.java,v 1.3 2005/11/09 23:27:45 rt94277 Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.console.desktop;
014:
015: import java.util.Arrays;
016: import java.util.Collection;
017: import java.util.Set;
018: import java.util.HashSet;
019: import java.util.List;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.logging.Level;
023: import java.util.Locale;
024: import java.text.MessageFormat;
025:
026: import java.net.URLDecoder;
027: import java.io.UnsupportedEncodingException;
028:
029: import javax.management.ObjectName;
030: import javax.management.MBeanServerConnection;
031: import javax.management.MBeanException;
032:
033: import javax.faces.context.FacesContext;
034: import javax.faces.el.VariableResolver;
035:
036: import javax.servlet.http.HttpServletRequest;
037:
038: import com.sun.web.ui.model.Option;
039:
040: import com.sun.portal.admin.common.util.AdminClientUtil;
041: import com.sun.portal.admin.common.PSMBeanException;
042: import com.sun.portal.admin.console.common.PortalBaseBean;
043:
044: public class AddRemoveBean extends PortalBaseBean {
045:
046: /**
047: * Cache flag. If the lists are ready then this flag is set to true
048: */
049: private boolean listsReady = false;
050:
051: /**
052: * Current container into which add or remove
053: */
054: private String currentContainer = null;
055:
056: public String getContainer() {
057: return currentContainer;
058: }
059:
060: public AddRemoveBean() {
061: FacesContext context = FacesContext.getCurrentInstance();
062:
063: // Try to get the currentContainer from the request param
064: HttpServletRequest req = (HttpServletRequest) context
065: .getExternalContext().getRequest();
066: currentContainer = (String) req
067: .getParameter(ATTR_SELECTED_CHANNEL_NAME);
068: if (currentContainer != null) {
069: try {
070: currentContainer = URLDecoder.decode(currentContainer,
071: DEFAULT_CHARSET);
072: } catch (UnsupportedEncodingException uee) {
073: }
074: }
075:
076: // If its not available in the param then try to get it from the
077: // EditProperties bean
078: if (currentContainer == null) {
079: VariableResolver vr = context.getApplication()
080: .getVariableResolver();
081: Object obj = (Object) vr.resolveVariable(context,
082: "EditPropertiesBean");
083: if ((obj != null) && (obj instanceof EditPropertiesBean)) {
084: EditPropertiesBean epropsbean = (EditPropertiesBean) obj;
085: currentContainer = epropsbean.getChannelName();
086: }
087: }
088: log(Level.FINEST, "AddRemoveBean.init: Current Container : "
089: + currentContainer);
090: }
091:
092: public void buildChannelList() {
093: //execute only once, if the lists are not ready.
094: if (listsReady) {
095: return;
096: }
097:
098: Set assignableSet = null;
099: List availableList = null;
100: List visibleList = null;
101: Option[] channels = null;
102:
103: // Setting the params and signature
104: String[] signature = { "java.lang.String", "java.lang.String" };
105: Object[] params = { getCurrentDN(), currentContainer };
106:
107: try {
108: log(Level.FINEST,
109: "Invoking get method on Display Profile MBean");
110: // Get the Display Profile MBean Object Name
111: ObjectName objName = AdminClientUtil
112: .getDisplayProfileMBeanObjectName(
113: AdminClientUtil.DEFAULT_DOMAIN,
114: getPortalId());
115: // Invoke the get method on the Display Profile MBean
116: MBeanServerConnection msc = getMBeanServerConnection();
117: assignableSet = (Set) msc.invoke(objName,
118: "getAssignableChannels", params, signature);
119: availableList = (List) msc.invoke(objName,
120: "getAvailableChannels", params, signature);
121: visibleList = (List) msc.invoke(objName,
122: "getSelectedChannels", params, signature);
123: assignableSet = assignableSet == null ? new HashSet()
124: : assignableSet;
125: availableList = availableList == null ? new ArrayList()
126: : availableList;
127: visibleList = visibleList == null ? new ArrayList()
128: : visibleList;
129: } catch (MBeanException mbe) {
130: log(
131: Level.SEVERE,
132: "AddRemoveBean.buildChannelList(): Failed to get data due to MBeanException: ",
133: mbe);
134: alert = true;
135: alertType = "error";
136: alertSummary = getI18NString("generic.error.summary");
137: if (mbe.getCause() instanceof PSMBeanException) {
138: PSMBeanException psmbe = (PSMBeanException) mbe
139: .getCause();
140: String i18nKey = psmbe.getErrorKey();
141: if (i18nKey != null && i18nKey.length() != 0) {
142: alertDetail = getI18NString(i18nKey);
143: //if it was not possible to localize the message from the
144: //mbean then show a generic message
145: if (alertDetail.startsWith("???")) {
146: alertDetail = getI18NString("generic.error.detail");
147: } else {
148: Object[] tokens = psmbe.getTokens();
149: if (tokens != null && tokens.length > 0) {
150: Locale locale = FacesContext
151: .getCurrentInstance().getViewRoot()
152: .getLocale();
153: MessageFormat mf = new MessageFormat(
154: alertDetail, locale);
155: StringBuffer msg = mf.format(tokens,
156: new StringBuffer(), null);
157: alertDetail = msg.toString();
158: }
159: }
160: } else {
161: alertDetail = getI18NString("generic.error.detail");
162: }
163: logMessage = psmbe.getMessage();
164: } else {
165: alertDetail = getI18NString("generic.error.detail");
166: logMessage = getStackTrace(mbe);
167: }
168: } catch (Exception e) {
169: log(
170: Level.SEVERE,
171: "AddRemoveBean.buildChannelList(): Failed to get data due to Exception: ",
172: e);
173: alert = true;
174: alertType = "error";
175: alertSummary = getI18NString("generic.error.summary");
176: alertDetail = getI18NString("generic.error.detail");
177: logMessage = getStackTrace(e);
178: }
179:
180: if (assignableSet != null && !assignableSet.isEmpty()) {
181: log(Level.FINEST, "Assignable Set: " + assignableSet);
182: // Remove all available channels from list
183: if (availableList != null && !availableList.isEmpty()) {
184: log(Level.FINEST, "Available List: " + availableList);
185: assignableSet.removeAll(availableList);
186: }
187: // Remove all visible channels from assignable list
188: if (visibleList != null && !visibleList.isEmpty()) {
189: log(Level.FINEST, "Visible List: " + visibleList);
190: assignableSet.removeAll(visibleList);
191: //Remove all visible channels from available list
192: if (availableList != null && !availableList.isEmpty()) {
193: availableList.removeAll(visibleList);
194: }
195: }
196: }
197:
198: assignable = getAsStringArray(assignableSet);
199: available = getAsStringArray(availableList);
200: visible = getAsStringArray(visibleList);
201: listsReady = true;
202: }
203:
204: private String[] getAsStringArray(Collection collection) {
205: String[] channels = null;
206: if (collection == null || collection.isEmpty()) {
207: channels = new String[0];
208: } else {
209: channels = new String[collection.size()];
210: Iterator iter = collection.iterator();
211: int i = 0;
212: while (iter.hasNext()) {
213: channels[i++] = (String) iter.next();
214: }
215: }
216: return channels;
217: }
218:
219: private Option[] getOptions(String[] ch) {
220: Option[] channels = null;
221: if (ch == null || ch.length == 0) {
222: channels = new Option[0];
223: } else {
224: channels = new Option[ch.length];
225: int i = 0;
226: while (i < ch.length) {
227: channels[i] = new Option(ch[i], ch[i]);
228: i++;
229: }
230: }
231: return channels;
232: }
233:
234: /**
235: * Assignable channels
236: */
237: private String[] assignable = null;
238:
239: public Option[] getAssignable() {
240: buildChannelList();
241: return getOptions(assignable);
242: }
243:
244: /**
245: * Available channels
246: */
247: private String[] available = null;
248:
249: public Option[] getAvailable() {
250: buildChannelList();
251: return getOptions(available);
252: }
253:
254: public String[] getSelectedAvailable() {
255: return null;
256: }
257:
258: public void setSelectedAvailable(String[] channels) {
259: available = channels;
260: }
261:
262: /**
263: * Visisble channels
264: */
265: private String[] visible = null;
266:
267: public Option[] getVisible() {
268: buildChannelList();
269: return getOptions(visible);
270: }
271:
272: public String[] getSelectedVisible() {
273: return null;
274: }
275:
276: public void setSelectedVisible(String[] channels) {
277: visible = channels;
278: }
279:
280: private String modified = "0";
281:
282: public String getModified() {
283: return modified;
284: }
285:
286: public void setModified(String s) {
287: }
288:
289: private String logMessage = null;
290:
291: public String getLogMessage() {
292: buildChannelList();
293: return logMessage;
294: }
295:
296: public void setLogMessage(String s) {
297: }
298:
299: private boolean alert = false;
300:
301: public boolean isAlert() {
302: buildChannelList();
303: return alert;
304: }
305:
306: private String alertType = null;
307:
308: public String getAlertType() {
309: buildChannelList();
310: return alertType;
311: }
312:
313: private String alertSummary = null;
314:
315: public String getAlertSummary() {
316: buildChannelList();
317: return alertSummary;
318: }
319:
320: private String alertDetail = null;
321:
322: public String getAlertDetail() {
323: buildChannelList();
324: return alertDetail;
325: }
326:
327: public String save() {
328: available = available == null ? new String[0] : available;
329: visible = visible == null ? new String[0] : visible;
330:
331: try {
332: // Get the Display Profile MBean Object Name
333: ObjectName objName = AdminClientUtil
334: .getDisplayProfileMBeanObjectName(
335: AdminClientUtil.DEFAULT_DOMAIN,
336: getPortalId());
337: // Invoke the get method on the portal Desktop MBean
338: MBeanServerConnection msc = getMBeanServerConnection();
339:
340: String[] sign = { "java.lang.String", "java.util.List",
341: "java.lang.String" };
342:
343: Object[] params = { getCurrentDN(), null, getContainer() };
344: List vlist = new ArrayList(Arrays.asList(visible));
345: List alist = new ArrayList(Arrays.asList(available));
346:
347: log(Level.FINEST, "Setting Visible Channel List: " + vlist);
348: params[1] = vlist;
349: msc.invoke(objName, "setSelectedChannels", params, sign);
350: //once visible is set, also make sure those are in available list
351: alist.addAll(vlist);
352: log(Level.FINEST, "Setting Available Channel List: "
353: + alist);
354: params[1] = alist;
355: msc.invoke(objName, "setAvailableChannels", params, sign);
356: //set hidden flag
357: modified = "1";
358: //show success alert
359: alert = true;
360: alertType = "information";
361: alertSummary = getI18NString("addRemove.success.summary");
362: alertDetail = getI18NString("addRemove.success.detail");
363: } catch (MBeanException mbe) {
364: log(
365: Level.SEVERE,
366: "AddRemoveBean.save(): Failed to save due to MBeanException: ",
367: mbe);
368: alert = true;
369: alertType = "error";
370: alertSummary = getI18NString("generic.error.summary");
371: if (mbe.getCause() instanceof PSMBeanException) {
372: PSMBeanException psmbe = (PSMBeanException) mbe
373: .getCause();
374: String i18nKey = psmbe.getErrorKey();
375: if (i18nKey != null && i18nKey.length() != 0) {
376: alertDetail = getI18NString(i18nKey);
377: //if it was not possible to localize the message from the
378: //mbean then show a generic message
379: if (alertDetail.startsWith("???")) {
380: alertDetail = getI18NString("generic.error.detail");
381: } else {
382: Object[] tokens = psmbe.getTokens();
383: if (tokens != null && tokens.length > 0) {
384: Locale locale = FacesContext
385: .getCurrentInstance().getViewRoot()
386: .getLocale();
387: MessageFormat mf = new MessageFormat(
388: alertDetail, locale);
389: StringBuffer msg = mf.format(tokens,
390: new StringBuffer(), null);
391: alertDetail = msg.toString();
392: }
393: }
394: } else {
395: alertDetail = getI18NString("generic.error.detail");
396: }
397: logMessage = psmbe.getMessage();
398: } else {
399: alertDetail = getI18NString("generic.error.detail");
400: logMessage = getStackTrace(mbe);
401: }
402: } catch (Exception e) {
403: log(
404: Level.SEVERE,
405: "AddRemoveBean.save(): Failed to save due to Exception: ",
406: e);
407: alert = true;
408: alertType = "error";
409: alertSummary = getI18NString("generic.error.summary");
410: alertDetail = getI18NString("generic.error.detail");
411: logMessage = getStackTrace(e);
412: }
413: //rebuild the channel list if required to display again
414: buildChannelList();
415: return null;
416: }
417:
418: /**
419: * Gets a localized string from the resoucebundle
420: * @param key A key string
421: * @return A localized String for the key
422: */
423: private String getI18NString(String key) {
424: return getLocalizedString("desktop", key);
425: }
426: }
|