001: /*
002: * CreateTabBean.java
003: *
004: * Created on July 13, 2005, 10:45 PM
005: */
006:
007: package com.sun.portal.admin.console.desktop;
008:
009: import java.beans.*;
010: import java.io.Serializable;
011: import java.util.Map;
012: import java.util.Set;
013: import java.util.Iterator;
014: import java.util.LinkedList;
015: import java.util.logging.Level;
016: import java.util.Map;
017: import java.util.HashMap;
018:
019: import java.text.MessageFormat;
020:
021: import javax.servlet.http.HttpServletRequest;
022:
023: import javax.management.ObjectName;
024: import javax.management.InstanceNotFoundException;
025: import javax.management.MBeanException;
026: import javax.management.ReflectionException;
027:
028: import javax.faces.context.FacesContext;
029: import javax.faces.validator.Validator;
030: import javax.faces.validator.ValidatorException;
031: import javax.faces.component.UIComponent;
032: import javax.faces.application.FacesMessage;
033:
034: import javax.faces.context.FacesContext;
035: import javax.faces.el.VariableResolver;
036:
037: import com.sun.web.ui.model.Option;
038: import com.sun.web.ui.event.WizardEvent;
039: import com.sun.web.ui.event.WizardEventListener;
040:
041: import com.sun.portal.admin.common.util.AdminClientUtil;
042: import com.sun.portal.admin.common.PSMBeanException;
043: import com.sun.portal.admin.common.DesktopConstants;
044: import com.sun.portal.admin.console.fabric.ListPortalsBean;
045: import com.sun.portal.admin.console.common.PortalBaseBean;
046:
047: /**
048: * @author rt94277
049: */
050: public class CreateTabBean extends AddChannelBean implements
051: DesktopConstants, WizardEventListener {
052: private static final String YES = "Yes";
053: private static final String NO = "No";
054:
055: private String tabTitle = "";
056: private String tabDesc = "";
057: private String containerName = "";
058: private boolean removable = true;
059: private boolean renamable = true;
060: private boolean predefined = true;
061: private String parentContainer = null;
062: private Option[] tabContainers = null;
063: private boolean addChannels = false;
064: private Object addChannel;
065: private String addChannelLabel = null;
066:
067: private Map rbMap = null;
068: private String dn = null;
069: private String portalId = null;
070: private Set assignable = null;
071:
072: //alert member variables
073: private String cancelText = "Cancel";
074: private Boolean displayError;
075: private String alertSummary;
076: private String alertDetail;
077: private String alertType;
078:
079: private Option[] addChannelSelections = { new Option(YES, "Yes"),
080: new Option(NO, "No"), };
081:
082: public CreateTabBean() {
083: rbMap = getResourceStringMap("desktop");
084: displayError = Boolean.FALSE;
085: portalId = (String) getSessionAttribute(ATTR_SELECTED_PORTAL);
086: dn = (String) getCurrentDN();
087: if (dn == null) {
088: dn = GLOBAL_LOCATION_DN;
089: }
090: if (portalId == null) {
091: FacesContext context = FacesContext.getCurrentInstance();
092: VariableResolver vr = context.getApplication()
093: .getVariableResolver();
094: Object obj = (Object) vr.resolveVariable(context,
095: "ListPortalsBean");
096: if ((obj != null) && (obj instanceof ListPortalsBean)) {
097: ListPortalsBean portalsbean = (ListPortalsBean) obj;
098: portalId = portalsbean.getCurrentPortal();
099: setSessionAttribute(ATTR_SELECTED_PORTAL, portalId);
100: }
101: }
102:
103: //initialize add to desktop selections
104: addChannelSelections[0].setLabel((String) rbMap
105: .get("createTab.addchannels.yes"));
106: addChannelSelections[0].setValue(YES);
107: addChannelSelections[1].setLabel((String) rbMap
108: .get("createTab.addchannels.no"));
109: addChannelSelections[1].setValue(NO);
110: setAddChannel(NO);
111: }
112:
113: private void resetBean() {
114: displayError = Boolean.FALSE;
115: tabContainers = null;
116: parentContainer = null;
117: tabTitle = null;
118: tabDesc = null;
119: containerName = null;
120: emptyTabContainers = false;
121: removable = true;
122: renamable = true;
123: predefined = true;
124: addChannels = false;
125: addChannel = NO;
126: assignable = null;
127: }
128:
129: public String getTabTitle() {
130: return tabTitle;
131: }
132:
133: public void setTabTitle(String title) {
134: tabTitle = title;
135: }
136:
137: public String getTabDesc() {
138: return tabDesc;
139: }
140:
141: public void setTabDesc(String desc) {
142: tabDesc = desc;
143: }
144:
145: public boolean getRemovable() {
146: return removable;
147: }
148:
149: public void setRemovable(boolean value) {
150: removable = value;
151: }
152:
153: public boolean getRenamable() {
154: return renamable;
155: }
156:
157: public void setRenamable(boolean value) {
158: renamable = value;
159: }
160:
161: public boolean getPredefined() {
162: return predefined;
163: }
164:
165: public void setPredefined(boolean value) {
166: predefined = value;
167: }
168:
169: public String getContainerName() {
170: return containerName;
171: }
172:
173: public void setContainerName(String name) {
174: containerName = name;
175: setContainer(name);
176: }
177:
178: public String getParentContainer() {
179: if (parentContainer == null) {
180: FacesContext context = FacesContext.getCurrentInstance();
181:
182: VariableResolver vr = context.getApplication()
183: .getVariableResolver();
184: Object obj = (Object) vr.resolveVariable(context,
185: "EditPropertiesBean");
186: if ((obj != null) && (obj instanceof EditPropertiesBean)) {
187: EditPropertiesBean epropsbean = (EditPropertiesBean) obj;
188: parentContainer = epropsbean.getChannelName();
189: }
190: if (parentContainer == null) {
191: if (tabContainers == null) {
192: setupTabContainers();
193: }
194: parentContainer = (String) tabContainers[0].getValue();
195: }
196: }
197: return parentContainer;
198: }
199:
200: public void setParentContainer(String value) {
201: parentContainer = value;
202: }
203:
204: public Option[] getTabContainers() {
205: return tabContainers;
206: }
207:
208: private Option[] setupTabContainers() {
209: Set tcs = null;
210: try {
211: ObjectName objName = AdminClientUtil
212: .getDisplayProfileMBeanObjectName(
213: AdminClientUtil.DEFAULT_DOMAIN, portalId);
214:
215: // Setting the params and signature
216: Object[] params = { dn, Boolean.TRUE };
217: String[] signature = { "java.lang.String",
218: "java.lang.Boolean" };
219:
220: // Invoke the get method on the portal's dp mbean
221: tcs = (Set) getMBeanServerConnection().invoke(objName,
222: "getExistingTabContainers", params, signature);
223: } catch (MBeanException me) {
224: log(Level.SEVERE,
225: "CreateTabBean.setupTabContainers(): Failed to getexisting tab containers ");
226: if (me.getCause() instanceof PSMBeanException) {
227: log(
228: Level.SEVERE,
229: "Exception in CreateTabBean.setupTabContainers()",
230: me);
231: }
232: } catch (Exception e) {
233: log(
234: Level.SEVERE,
235: "CreateTabBean.setupTabContainers(): Failed to get existing tab containers ",
236: e);
237: }
238: if (tcs != null && !tcs.isEmpty()) {
239: tabContainers = new Option[tcs.size()];
240: int index = 0;
241: for (Iterator i = tcs.iterator(); i.hasNext();) {
242: String next = (String) i.next();
243: tabContainers[index] = new Option(next, next);
244: index++;
245: }
246: emptyTabContainers = false;
247: } else {
248: tabContainers = new Option[1];
249: tabContainers[0] = new Option("", "");
250: emptyTabContainers = true;
251: }
252: return tabContainers;
253: }
254:
255: public Object[] getAddChannelSelections() {
256: return (Object[]) addChannelSelections;
257: }
258:
259: public Object getAddChannel() {
260: return addChannel;
261: }
262:
263: public void setAddChannel(Object val) {
264: addChannel = val;
265: if (addChannel.equals(YES)) {
266: assignable = getAssignableChannels();
267: }
268:
269: }
270:
271: public boolean getAddChannels() {
272: if (addChannel.equals(YES)) {
273: addChannels = true;
274: } else {
275: addChannels = false;
276: }
277: return addChannels;
278: }
279:
280: public Option[] getExistingChannels() {
281:
282: Option[] channels = null;
283: if (assignable != null) {
284: log(Level.FINEST, "Assignable Set: " + assignable);
285: channels = new Option[assignable.size()];
286: Iterator itr = assignable.iterator();
287: int ctr = 0;
288: while (itr.hasNext()) {
289: String channelName = (String) itr.next();
290: channels[ctr] = new Option(channelName, channelName);
291: ctr++;
292: }
293: }
294:
295: return channels;
296:
297: }
298:
299: public Set getAssignableChannels() {
300: Set asgnChannels = null;
301: // Setting the params and signature
302: String[] signature = { "java.lang.String", "java.lang.String",
303: "java.lang.String", "java.lang.String" };
304: Object[] params = { getCurrentDN(), parentContainer,
305: containerName, TABLE_PROVIDER_NAME };
306:
307: try {
308:
309: log(Level.FINEST,
310: "Invoking get method on Display Profile MBean");
311:
312: // Get the Display Profile MBean Object Name
313: ObjectName objName = AdminClientUtil
314: .getDisplayProfileMBeanObjectName(
315: AdminClientUtil.DEFAULT_DOMAIN,
316: getPortalId());
317: // Invoke the get method on the portal Desktop MBean
318: asgnChannels = (Set) getMBeanServerConnection().invoke(
319: objName,
320: "getNonExistentContainerAssignableChannels",
321: params, signature);
322: } catch (MBeanException me) {
323: log(Level.SEVERE,
324: "CreateTabBean.createTab(): Failed to create tab ",
325: me);
326: if (me.getCause() instanceof PSMBeanException) {
327: PSMBeanException psmbe = (PSMBeanException) me
328: .getCause();
329: String i18nKey = psmbe.getErrorKey();
330: Object[] tokens = psmbe.getTokens();
331: setupAlert(tokens, "createTab.assignable.fetch.failed",
332: i18nKey, "error");
333: }
334: } catch (Exception e) {
335: log(
336: Level.SEVERE,
337: "CreateTabBean.getExistingChannels(): Error Fetching Data",
338: e);
339: setupAlert(null, "createTab.assignable.fetch.failed", "",
340: "error");
341: }
342: return asgnChannels;
343: }
344:
345: private boolean emptyTabContainers = false;
346:
347: public boolean isEmptyTabContainers() {
348: return emptyTabContainers;
349: }
350:
351: //============================================================
352: // Implementation methods for StateHolder
353: //============================================================
354: public boolean handleEvent(WizardEvent event) {
355: switch (event.getNavigationEvent()) {
356: case WizardEvent.PREVIOUS:
357: break;
358:
359: case WizardEvent.NEXT:
360: String id = event.getWizard().getCurrentStep().getId();
361: log(Level.FINEST, "CreateChannel.handleEvent(), event id: "
362: + id);
363: if (id.equals("step2a")) {
364: if (assignable == null) {
365: return false;
366: }
367: }
368: break;
369: case WizardEvent.CANCEL:
370: case WizardEvent.CLOSE:
371: resetBean();
372: break;
373: case WizardEvent.FINISH:
374: createTab();
375: if (getAddChannels()) {
376: setContainer(parentContainer + "/" + containerName);
377: add();
378: }
379: break;
380: default:
381: break;
382: }
383:
384: return true;
385: }
386:
387: //============================================================
388: // Implementation methods for StateHolder
389: //============================================================
390: public boolean isTransient() {
391: return false;
392: }
393:
394: public void setTransient(boolean flag) {
395: }
396:
397: public Object saveState(FacesContext context) {
398: return null;
399: }
400:
401: public void restoreState(FacesContext context, Object state) {
402: }
403:
404: public void validateChannelName(FacesContext contex,
405: UIComponent component, Object value) {
406: String sm = (String) rbMap.get("error.validation.summary");
407: String dm = (String) rbMap.get("error.containername.detail");
408: String cn = (String) value;
409: for (int i = 0; i < cn.length(); i++) {
410: char c = cn.charAt(i);
411: if (!Character.isLetterOrDigit(c) && (c != '_')) {
412: throw new ValidatorException(new FacesMessage(sm, dm));
413: }
414: }
415: }
416:
417: public void validateDN(FacesContext context, UIComponent component,
418: Object value) {
419: //has to call setuptabcontainers here as a workaround
420: //because alert getter is always called first on the next page.
421: //to display the alert if the tabcontainers are empty.
422: String sm = (String) rbMap
423: .get("createTab.notabcontainer.summary");
424: String dm = (String) rbMap
425: .get("createTab.notabcontainer.detail");
426: String cdn = (String) value;
427: String cpid = (String) getSessionAttribute(ListPortalsBean.ATTR_SELECTED_PORTAL);
428: if (!dn.equals(cdn) || !cpid.equals(portalId)) {
429: dn = cdn;
430: portalId = cpid;
431: }
432: setupTabContainers();
433: if (isEmptyTabContainers()) {
434: throw new ValidatorException(new FacesMessage(sm, dm));
435: }
436: }
437:
438: /**
439: * createTab
440: */
441: public String createTab() {
442: Map props = new HashMap();
443: props.put("removable", new Boolean(removable));
444: props.put("renamable", new Boolean(renamable));
445: props.put("predefined", new Boolean(predefined));
446: Map tabProps = new HashMap();
447: tabProps.put(TAB_PROPERTIES, props);
448: tabProps.put(TAB_TITLE, tabTitle);
449: tabProps.put(TAB_DESCRIPTION, tabDesc);
450: String tabContainer = parentContainer;
451: if (isEmptyTabContainers() || parentContainer.equals("_root")) {
452: tabContainer = null;
453: }
454: FacesContext context = FacesContext.getCurrentInstance();
455: try {
456: ObjectName objName = AdminClientUtil
457: .getDisplayProfileMBeanObjectName(
458: AdminClientUtil.DEFAULT_DOMAIN, portalId);
459:
460: // Setting the params and signature
461: Object[] params = { dn, containerName, TABLE_PROVIDER_NAME,
462: tabContainer, tabProps };
463: String[] signature = { "java.lang.String",
464: "java.lang.String", "java.lang.String",
465: "java.lang.String", "java.util.Map" };
466:
467: // Invoke the get method on the portal's dp mbean
468: getMBeanServerConnection().invoke(objName, "createTab",
469: params, signature);
470: setupAlert(null, "createTab.summary",
471: "createTab.completed", "information");
472:
473: VariableResolver vr = context.getApplication()
474: .getVariableResolver();
475: Object obj = (Object) vr.resolveVariable(context,
476: "AddChannelBean");
477: if ((obj != null) && (obj instanceof AddChannelBean)) {
478: AddChannelBean addbean = (AddChannelBean) obj;
479: addbean.setContainer(containerName);
480: }
481: } catch (MBeanException me) {
482: log(Level.SEVERE,
483: "CreateTabBean.createTab(): Failed to create tab ",
484: me);
485: if (me.getCause() instanceof PSMBeanException) {
486: PSMBeanException psmbe = (PSMBeanException) me
487: .getCause();
488: String i18nKey = psmbe.getErrorKey();
489: Object[] tokens = psmbe.getTokens();
490: setupAlert(tokens, "error.createTab.failed", i18nKey,
491: "error");
492: }
493: } catch (Exception e) {
494: log(Level.SEVERE,
495: "CreateTabBean.createTab(): Failed to create tab",
496: e);
497: setupAlert(null, "error.createTab.failed", null,
498: "information");
499: }
500: return "done";
501: }
502:
503: //============================================================
504: // Cancel operation
505: //============================================================
506:
507: public String getCancelText() {
508: return cancelText;
509: }
510:
511: public void setCancelText(String val) {
512: cancelText = val;
513: }
514:
515: public String cancel() {
516: setCancelText((String) rbMap.get("cancel.button"));
517: return "goDesktopManager";
518: }
519:
520: //============================================================
521: // Alert methods
522: //============================================================
523: public Boolean getDisplayError() {
524: Boolean show = displayError;
525: displayError = Boolean.FALSE;
526: return show;
527: }
528:
529: public void setDisplayError(Boolean value) {
530: displayError = value;
531: }
532:
533: public String getAlertSummary() {
534: return alertSummary;
535: }
536:
537: public void setAlertSummary(String summary) {
538: alertSummary = summary;
539: }
540:
541: public String getAlertDetail() {
542: return alertDetail;
543: }
544:
545: public void setAlertDetail(String detail) {
546: alertDetail = detail;
547: }
548:
549: public String getAlertType() {
550: return alertType;
551: }
552:
553: public void setAlertType(String type) {
554: alertType = type;
555: }
556:
557: //============================================================
558: // Alert Utility methods
559: //============================================================
560:
561: public void setupAlert(Object[] tokens, String summary,
562: String detail, String type) {
563:
564: String sm = (String) rbMap.get(summary);
565: String dm = (String) rbMap.get(detail);
566:
567: if (tokens != null) {
568: MessageFormat mf = new MessageFormat(dm);
569: dm = mf.format(tokens);
570: }
571: setDisplayError(Boolean.TRUE);
572: setAlertSummary(sm);
573: setAlertDetail(dm);
574: setAlertType(type);
575: }
576:
577: }
|