001: /**
002: * $Id: NewPropertyBean.java,v 1.3 2005/10/07 00:03:48 pd109850 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.Collections;
016: import java.util.Iterator;
017: import java.util.Map;
018: import java.util.logging.Level;
019:
020: import com.sun.portal.admin.console.common.PortalBaseBean;
021:
022: import javax.faces.context.FacesContext;
023: import javax.faces.application.FacesMessage;
024: import javax.faces.component.UIComponent;
025: import javax.faces.validator.Validator;
026: import javax.faces.validator.ValidatorException;
027:
028: import com.sun.web.ui.component.RadioButton;
029: import com.sun.web.ui.event.WizardEvent;
030: import com.sun.web.ui.event.WizardEventListener;
031: import com.sun.web.ui.component.WizardStep;
032: import com.sun.web.ui.model.Option;
033:
034: import javax.management.MBeanServerConnection;
035: import javax.management.ObjectName;
036: import javax.management.MBeanException;
037:
038: import com.sun.portal.admin.common.util.AdminClientUtil;
039: import com.sun.portal.admin.common.PSMBeanException;
040: import com.sun.portal.admin.common.DesktopConstants;
041:
042: import com.sun.data.provider.DataProvider;
043: import com.sun.data.provider.impl.ObjectListDataProvider;
044:
045: public class NewPropertyBean extends PortalBaseBean implements
046: WizardEventListener {
047:
048: //resource bundle name
049: public static final String RB_NAME = "desktop";
050: //keys in resource bundle
051: public static final String STRING_TYPE = "wizard.newProperty.label.string";
052: public static final String INT_TYPE = "wizard.newProperty.label.integer";
053: public static final String BOOL_TYPE = "wizard.newProperty.label.bool";
054: public static final String COLLECTION_TYPE = "wizard.newProperty.label.collection";
055: public static final String TRUE_VALUE_LABEL = "wizard.newProperty.label.true";
056: public static final String FALSE_VALUE_LABEL = "wizard.newProperty.label.false";
057:
058: /** Creates a new instance of NewPropertyBean */
059: public NewPropertyBean() {
060: }
061:
062: private String name = null;
063:
064: public String getName() {
065: return name;
066: }
067:
068: public void setName(String name) {
069: this .name = name;
070: }
071:
072: private String value = null;
073:
074: public String getValue() {
075: if (isBool() && value == null) {
076: value = "true";
077: }
078: return value;
079: }
080:
081: public void setValue(String value) {
082: this .value = value;
083: }
084:
085: private short type = DesktopConstants.TYPE_STRING_PROPERTY;
086:
087: public String getType() {
088: String t = null;
089: if (type == DesktopConstants.TYPE_STRING_PROPERTY) {
090: t = STRING_TYPE;
091: } else if (type == DesktopConstants.TYPE_INTEGER_PROPERTY) {
092: t = INT_TYPE;
093: } else if (type == DesktopConstants.TYPE_BOOLEAN_PROPERTY) {
094: t = BOOL_TYPE;
095: } else if (type == DesktopConstants.TYPE_COLLECTION_PROPERTY) {
096: t = COLLECTION_TYPE;
097: }
098: return t;
099: }
100:
101: public void setType(String type) {
102: if (STRING_TYPE.equals(type)) {
103: this .type = DesktopConstants.TYPE_STRING_PROPERTY;
104: } else if (INT_TYPE.equals(type)) {
105: this .type = DesktopConstants.TYPE_INTEGER_PROPERTY;
106: } else if (BOOL_TYPE.equals(type)) {
107: this .type = DesktopConstants.TYPE_BOOLEAN_PROPERTY;
108: } else if (COLLECTION_TYPE.equals(type)) {
109: this .type = DesktopConstants.TYPE_COLLECTION_PROPERTY;
110: } else {
111: this .type = DesktopConstants.TYPE_UNKNOWN_PROPERTY;
112: }
113: }
114:
115: public boolean isString() {
116: return type == DesktopConstants.TYPE_STRING_PROPERTY;
117: }
118:
119: public boolean isInt() {
120: return type == DesktopConstants.TYPE_INTEGER_PROPERTY;
121: }
122:
123: public boolean isBool() {
124: return type == DesktopConstants.TYPE_BOOLEAN_PROPERTY;
125: }
126:
127: public boolean isCollection() {
128: return type == DesktopConstants.TYPE_COLLECTION_PROPERTY;
129: }
130:
131: private Option[] typeOptions = null;
132:
133: public Option[] getTypeOptions() {
134: if (typeOptions == null) {
135: Map desktop = getResourceStringMap(RB_NAME);
136: typeOptions = new Option[] {
137: new Option(STRING_TYPE, (String) desktop
138: .get(STRING_TYPE)),
139: new Option(INT_TYPE, (String) desktop.get(INT_TYPE)),
140: new Option(BOOL_TYPE, (String) desktop
141: .get(BOOL_TYPE)),
142: new Option(COLLECTION_TYPE, (String) desktop
143: .get(COLLECTION_TYPE)) };
144: }
145: return typeOptions;
146: }
147:
148: private Option[] valueOptions = null;
149:
150: public Option[] getValueOptions() {
151: if (valueOptions == null) {
152: Map desktop = getResourceStringMap(RB_NAME);
153: valueOptions = new Option[] {
154: new Option("true", (String) desktop
155: .get(TRUE_VALUE_LABEL)),
156: new Option("false", (String) desktop
157: .get(FALSE_VALUE_LABEL)) };
158: }
159: return valueOptions;
160: }
161:
162: private Boolean advanced = Boolean.FALSE;
163:
164: public String getAdvanced() {
165: return advanced.toString();
166: }
167:
168: public void setAdvanced(String adv) {
169: advanced = new Boolean(adv);
170: }
171:
172: private Option[] advOptions = null;
173:
174: public Option[] getAdvOptions() {
175: if (advOptions == null) {
176: Map desktop = getResourceStringMap(RB_NAME);
177: advOptions = new Option[] {
178: new Option("true", (String) desktop
179: .get(TRUE_VALUE_LABEL)),
180: new Option("false", (String) desktop
181: .get(FALSE_VALUE_LABEL)) };
182: }
183: return advOptions;
184: }
185:
186: private String alertSummary = null;
187:
188: public String getAlertSummary() {
189: return alertSummary;
190: }
191:
192: private String alertDetail = null;
193:
194: public String getAlertDetail() {
195: return alertDetail;
196: }
197:
198: private String alertType = null;
199:
200: public String getAlertType() {
201: return alertType;
202: }
203:
204: public void createNewProperty() {
205: Object v = null;
206: switch (type) {
207: case DesktopConstants.TYPE_STRING_PROPERTY:
208: v = value == null ? "" : value;
209: break;
210: case DesktopConstants.TYPE_INTEGER_PROPERTY:
211: try {
212: v = new Integer(value);
213: } catch (NumberFormatException e) {
214: //should never happen, its already validated
215: }
216: break;
217: case DesktopConstants.TYPE_BOOLEAN_PROPERTY:
218: v = new Boolean(value);
219: break;
220: case DesktopConstants.TYPE_COLLECTION_PROPERTY:
221: v = Collections.EMPTY_MAP;
222: break;
223: default:
224: //should never happen, not entered by user (only selected)
225: break;
226: }
227: String dn = (String) getSessionAttribute(ATTR_CURRENT_LOCATION_DN);
228: try {
229: // Get the Domain MBean object
230: ObjectName objName = AdminClientUtil
231: .getDisplayProfileMBeanObjectName(
232: AdminClientUtil.DEFAULT_DOMAIN,
233: getPortalId());
234:
235: //get the edit proprties bean from the session
236: //we depend on the bean being in session and this page
237: //being invoked from it to have proper context setup and initialized
238: FacesContext ctx = FacesContext.getCurrentInstance();
239: EditPropertiesBean epb = (EditPropertiesBean) ctx
240: .getApplication().getVariableResolver()
241: .resolveVariable(ctx, "EditPropertiesBean");
242:
243: // Setting the params and signature
244: Object[] params = {
245: epb.getChannelName(),
246: epb.getRpn(),
247: dn,
248: epb.getClient(),
249: epb.getLang() + "_" + epb.getCountry() + "_"
250: + epb.getVariant(), name, v, advanced };
251: String[] signature = { "java.lang.String", //fqcn
252: "java.lang.String", //rpn
253: "java.lang.String", //dn
254: "java.lang.String", //client
255: "java.lang.String", //locale
256: "java.lang.String", //name
257: "java.lang.Object", //value
258: "java.lang.Boolean" //advanced
259: };
260:
261: // Invoke the get method on the portal's dp mbean
262: getMBeanServerConnection().invoke(objName,
263: "createNodeProperty", params, signature);
264: alertType = "information";
265: alertSummary = getLocalizedString(RB_NAME,
266: "wizard.newProperty.success.message");
267:
268: //force the reset of the opener form
269: epb.reset();
270: } catch (MBeanException me) {
271: log(Level.SEVERE,
272: "NewPropertyBean.createNewProperty(): Failed to create property "
273: + name + " for " + getPortalId(), me);
274: String er = null;
275: if (me.getCause() instanceof PSMBeanException) {
276: er = me.getCause().getMessage();
277: } else {
278: er = me.getMessage();
279: }
280: log(Level.SEVERE,
281: "NewPropertyBean.createNewProperty(): Exception message is: "
282: + er);
283: alertType = "error";
284: alertSummary = getLocalizedString(RB_NAME,
285: "wizard.newProperty.error.message");
286: alertDetail = getLocalizedString(RB_NAME,
287: "wizard.newProperty.error.detail")
288: + er;
289: } catch (Exception e) {
290: log(Level.SEVERE,
291: "NewPropertyBean.createNewProperty(): Failed to create property "
292: + name + " for " + getPortalId(), e);
293: log(Level.SEVERE,
294: "Exception in NewPropertyBean.createNewProperty()",
295: e);
296: alertType = "error";
297: alertSummary = getLocalizedString(RB_NAME,
298: "wizard.newProperty.error.message");
299: alertDetail = getLocalizedString(RB_NAME,
300: "wizard.newProperty.error.detail")
301: + e.getMessage();
302: }
303: }
304:
305: /**
306: * The DP-api does not enforce any restrictions and allows whatever valid
307: * xml allows. Admin enforces that collection name does not contain
308: * EditPropertiesBean.COLLECTION_NAME_SEPARATOR which is a pipe ('|')
309: * For xml char validation, we depend on dp-api to thorw an exception
310: */
311: public void validateName(FacesContext context,
312: UIComponent component, Object value)
313: throws ValidatorException {
314: String string = value.toString();
315:
316: //check if name contains '|'
317: char invalid = EditPropertiesBean.COLLECTION_NAME_SEPARATOR
318: .charAt(0);
319: int index = string.indexOf(invalid);
320: if (index != -1) {
321: String msgString = getLocalizedString(RB_NAME,
322: "wizard.newProperty.error.invalidName");
323: FacesMessage msg = new FacesMessage(msgString + " '"
324: + invalid + "' ");
325: throw new ValidatorException(msg);
326: }
327:
328: //check for duplicate names
329: FacesContext ctx = FacesContext.getCurrentInstance();
330: EditPropertiesBean epb = (EditPropertiesBean) ctx
331: .getApplication().getVariableResolver()
332: .resolveVariable(ctx, "EditPropertiesBean");
333: DataProvider properties = epb.getProperties();
334: if (properties != null
335: && properties instanceof ObjectListDataProvider) {
336: Iterator iter = ((ObjectListDataProvider) properties)
337: .getList().iterator();
338: while (iter.hasNext()) {
339: Property prop = (Property) iter.next();
340: if (prop.getName().equals(string)) {
341: String msgString = getLocalizedString(RB_NAME,
342: "wizard.newProperty.error.duplicateName");
343: FacesMessage msg = new FacesMessage(msgString);
344: throw new ValidatorException(msg);
345: }
346: }
347: }
348: }
349:
350: public void validateIntValue(FacesContext context,
351: UIComponent component, Object value)
352: throws ValidatorException {
353: try {
354: Integer i = new Integer((String) value);
355: } catch (NumberFormatException e) {
356: String msgString = getLocalizedString(RB_NAME,
357: "wizard.newProperty.error.invalidIntValue");
358: FacesMessage msg = new FacesMessage(msgString);
359: throw new ValidatorException(msg);
360: }
361: }
362:
363: private void resetBean() {
364: name = null;
365: value = null;
366: type = DesktopConstants.TYPE_STRING_PROPERTY;
367: advanced = Boolean.FALSE;
368: }
369:
370: ///////////begin wizard evert listner impl////////////
371: public boolean handleEvent(WizardEvent event) {
372: switch (event.getNavigationEvent()) {
373: case WizardEvent.FINISH:
374: //create new property
375: createNewProperty();
376: break;
377: case WizardEvent.CANCEL:
378: case WizardEvent.CLOSE:
379: resetBean();
380: break;
381: default:
382: break;
383: }
384: return true;
385: }
386:
387: public boolean isTransient() {
388: return false;
389: }
390:
391: public void setTransient(boolean flag) {
392: }
393:
394: public Object saveState(FacesContext context) {
395: return null;
396: }
397:
398: public void restoreState(FacesContext context, Object state) {
399: }
400: ///////////end wizard evern listner impl////////////
401:
402: }
|