001: /**
002: * $Id: CreateTemplateBean.java,v 1.7 2006/06/01 18:10:55 sorensen 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.ssoa;
014:
015: import java.util.*;
016: import java.io.*;
017: import java.util.logging.Level;
018: import com.sun.data.provider.impl.ObjectListDataProvider;
019: import com.sun.web.ui.model.Option;
020: import com.sun.web.ui.event.WizardEvent;
021: import com.sun.web.ui.event.WizardEventListener;
022:
023: import javax.faces.application.FacesMessage;
024: import javax.faces.component.UIComponent;
025: import javax.faces.context.FacesContext;
026: import javax.faces.event.ActionEvent;
027: import javax.faces.validator.ValidatorException;
028: import javax.management.MBeanServerConnection;
029:
030: public class CreateTemplateBean extends ListTemplatesBean {
031:
032: private String basis;
033: private String name;
034: private Option[] availableTemplates;
035: public static String GENERIC_TEMPLATE = "GENERIC_TEMPLATE";
036:
037: private void resetAvailableTemplates() {
038: availableTemplates = new Option[templatesList.size()];
039: for (int i = 0; i < templatesList.size(); i++) {
040: TemplateNameBean tnb = (TemplateNameBean) templatesList
041: .get(i);
042: availableTemplates[i] = new Option(tnb.getName(), tnb
043: .getName());
044: }
045: // Make sure that basis is set to null for options
046: if ((basis == null) && (templatesList.size() > 0)) {
047: TemplateNameBean tnb = (TemplateNameBean) templatesList
048: .get(0);
049: basis = tnb.getName();
050: log(Level.FINEST,
051: "CreateTemplateBean.resetAvailableTemplates setting basis to "
052: + basis);
053: }
054: }
055:
056: public String getBasis() {
057: if (basis == null) {
058: // If the basis is null, check first if it is set in the session.
059: basis = (String) getSessionAttribute(ATTR_SELECTED_TEMPLATE);
060: if (basis == null) {
061: basis = GENERIC_TEMPLATE;
062: }
063: }
064: return basis;
065: }
066:
067: public void setBasis(String basis) {
068: this .basis = basis;
069: }
070:
071: public String getName() {
072: return name;
073: }
074:
075: public void setName(String name) {
076: this .name = name;
077: }
078:
079: // Reset the available templates every time this is called
080: // in order to catch any other actions (delete) that could occur between
081: // runnings of the template wizard
082: public Option[] getAvailableTemplates() {
083: log(Level.FINEST,
084: "CreateTemplateBean.getAvailableTemplates start");
085: if (availableTemplates == null) {
086: resetTemplateNames();
087: resetAvailableTemplates();
088: }
089: return availableTemplates;
090: }
091:
092: public String create() {
093: Map basisAttributes = null;
094: MBeanServerConnection connection = getMBeanServerConnection();
095: try {
096: // Create a new template with these properties
097: String[] signature = { String.class.getName(),
098: String.class.getName() };
099: Object[] params = { name, basis };
100: // This can throw AttributeNotFoundException, MBeanException, InstanceNotFoundException
101: // ReflectionException, RuntimeOperationsException, or IOException
102: connection.invoke(objectName, "createTemplate", params,
103: signature);
104: } catch (Exception ex) {
105: log(Level.SEVERE,
106: "CreateTemplateBean.create: Exception when"
107: + " creating new template " + name, ex);
108: // Put alert stuff here
109: }
110: return cancel();
111: }
112:
113: //============================================================
114: // validator for unique template name
115: //============================================================
116: public void validateName(FacesContext ctx, UIComponent comp,
117: Object value) {
118: log(Level.FINEST, "CreateTemplatesBean.validateName start");
119: String sm = (String) rbMap.get("error.validation.summary");
120: String cn = (String) value;
121: // Make sure no illegal characters are used
122: // Allow alphanumerics, dash, and underscore only
123: if (cn != null && cn.matches(".*[^a-zA-Z0-9-_].*")) {
124: String cm = (String) rbMap
125: .get("error.meta.illegal.characters");
126: throw new ValidatorException(new FacesMessage(sm, cm));
127: }
128:
129: // Make sure this isn't in the list of availableTemplate options
130: for (int i = 0; i < templatesList.size(); i++) {
131: TemplateNameBean tnb = (TemplateNameBean) templatesList
132: .get(i);
133: if (value.equals(tnb.getName())) {
134: String dm = (String) rbMap.get("error.duplicate");
135: throw new ValidatorException(new FacesMessage(sm, dm));
136: }
137: }
138: }
139:
140: public String cancel() {
141: log(Level.FINEST, "CreateTemplateBean.cancel start");
142: // Reset the bean
143: basis = null;
144: name = null;
145: availableTemplates = null;
146: return "cancel";
147: }
148:
149: }
|