001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.admin.console.search;
013:
014: import java.util.*;
015: import java.util.logging.Level;
016: import java.io.*;
017:
018: import javax.faces.context.FacesContext;
019: import javax.faces.application.FacesMessage;
020: import javax.faces.component.UIComponent;
021: import javax.faces.validator.*;
022: import javax.faces.el.ValueBinding;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.management.*;
025:
026: import com.sun.web.ui.model.*;
027: import com.sun.web.ui.event.*;
028: import com.sun.web.ui.component.*;
029:
030: import com.sun.data.provider.*;
031: import com.sun.data.provider.impl.ObjectListDataProvider;
032:
033: import com.sun.cacao.agent.JmxClient;
034:
035: import com.sun.portal.admin.common.util.AdminClientUtil;
036: import com.sun.portal.admin.console.common.PSBaseBean;
037:
038: public class CreateDatabaseBean extends PSBaseBean {
039:
040: public String name = "";
041: public TableRowGroup tableRowGroup = null;
042: public TableSelectPhaseListener tspl = null;
043:
044: public CreateDatabaseBean() {
045: name = (String) getSessionAttribute("search.createresourcedescription.name");
046: if (name == null) {
047: name = "";
048: setSessionAttribute("search.createdatabase.name", name);
049: }
050:
051: ObjectListDataProvider attributes = (ObjectListDataProvider) getSessionAttribute("search.createdatabase.attributes");
052: if (attributes == null) {
053: attributes = new ObjectListDataProvider(new ArrayList());
054: attributes.setObjectType(DatabaseAttributeBean.class);
055:
056: setSessionAttribute("search.createdatabase.attributes",
057: attributes);
058: }
059: tspl = new TableSelectPhaseListener();
060: }
061:
062: public boolean getShowError() {
063: String value = (String) getSessionAttribute("search.createdatabase.showerror");
064: if (value == null || value.equals("false")) {
065: return false;
066: } else {
067: return true;
068: }
069: }
070:
071: public String getName() {
072: return name;
073: }
074:
075: public void setName(String name) {
076: this .name = name;
077: }
078:
079: public ObjectListDataProvider getAttributes() {
080: return (ObjectListDataProvider) getSessionAttribute("search.createdatabase.attributes");
081: }
082:
083: public void setAttributes(ObjectListDataProvider attributes) {
084: setSessionAttribute("search.createdatabase.attributes",
085: attributes);
086: }
087:
088: public TableRowGroup getTableRowGroup() {
089: return tableRowGroup;
090: }
091:
092: public void setTableRowGroup(TableRowGroup tableRowGroup) {
093: this .tableRowGroup = tableRowGroup;
094: }
095:
096: public Object getSelected() {
097: return tspl.getSelected(getTableRow());
098: }
099:
100: public void setSelected(Object object) {
101: tspl.setSelected(getTableRow(), object);
102: }
103:
104: public void validate(FacesContext fc, UIComponent component,
105: Object value) throws ValidatorException {
106: String id = component.getId();
107: if (id.equals("name")) {
108: if (!Validators.isAlphaNumeric((String) value)) {
109: throw new ValidatorException(new FacesMessage(
110: FacesMessage.SEVERITY_ERROR,
111: getLocalizedString("search",
112: "search.error.error"),
113: getLocalizedString("search",
114: "search.error.alphanumeric")));
115: }
116: if (isDuplicate((String) value)) {
117: throw new ValidatorException(new FacesMessage(
118: FacesMessage.SEVERITY_ERROR,
119: getLocalizedString("search",
120: "search.error.error"),
121: getLocalizedString("search",
122: "search.error.duplicatedatabase")));
123: }
124: }
125: }
126:
127: public String addAttribute() {
128: ObjectListDataProvider attributes = (ObjectListDataProvider) getSessionAttribute("search.createdatabase.attributes");
129: attributes.commitChanges();
130: if (attributes.canAppendRow()) {
131: attributes.appendRow();
132: attributes.commitChanges();
133: setSessionAttribute("search.createdatabase.attributes",
134: attributes);
135: }
136: return null;
137: }
138:
139: public String deleteAttribute() {
140: ObjectListDataProvider attributes = (ObjectListDataProvider) getSessionAttribute("search.createdatabase.attributes");
141: attributes.commitChanges();
142:
143: RowKey[] rowKeys = tableRowGroup.getRenderedRowKeys();
144: if (rowKeys != null) {
145: for (int index = 0; index < rowKeys.length; index++) {
146: RowKey rowKey = rowKeys[index];
147: if (tspl.isSelected(rowKey)
148: && attributes.isRowAvailable(rowKey)
149: && attributes.canRemoveRow(rowKey)) {
150: attributes.removeRow(rowKey);
151: }
152: }
153: attributes.commitChanges();
154: setSessionAttribute("search.createdatabase.attributes",
155: attributes);
156: tableRowGroup.setFirst(0);
157: tspl.clear();
158: }
159:
160: return null;
161: }
162:
163: public String create() {
164: try {
165: LinkedList path = new LinkedList();
166: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
167: path
168: .addFirst((String) getSessionAttribute("search.server.selected"));
169: path.addFirst("database");
170: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
171: AdminClientUtil.SEARCH_DATABASE_MBEAN_TYPE, path);
172:
173: Properties p = new Properties();
174: ObjectListDataProvider attributes = (ObjectListDataProvider) getSessionAttribute("search.createdatabase.attributes");
175: attributes.commitChanges();
176: List l = attributes.getList();
177: for (int index = 0; index < l.size(); index++) {
178: DatabaseAttributeBean dab = (DatabaseAttributeBean) l
179: .get(index);
180: if (!dab.key.equals("")) {
181: p.setProperty(dab.key, dab.value);
182: }
183: }
184:
185: Object[] params = { name, p };
186: String[] signatures = { "java.lang.String",
187: "java.util.Properties" };
188: getMBeanServerConnection().invoke(on, "create", params,
189: signatures);
190:
191: setSessionAttribute("search.createdatabase.showerror",
192: "false");
193: return "gotoDatabasesHome";
194: } catch (Exception e) {
195: log(Level.SEVERE, "CreateDatabaseBean.create()", e);
196: setSessionAttribute("search.createdatabase.showerror",
197: "true");
198: return null;
199: }
200: }
201:
202: public String cancel() {
203: setSessionAttribute("search.createdatabase.showerror", "false");
204: return "gotoDatabasesHome";
205: }
206:
207: private boolean isDuplicate(String value) {
208: try {
209: LinkedList path = new LinkedList();
210: path.addFirst(AdminClientUtil.DEFAULT_DOMAIN);
211: path
212: .addFirst((String) getSessionAttribute("search.server.selected"));
213: path.addFirst("database");
214: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
215: AdminClientUtil.SEARCH_DATABASE_MBEAN_TYPE, path);
216: Object[] params = {};
217: String[] signatures = {};
218: ArrayList data = (ArrayList) getMBeanServerConnection()
219: .invoke(on, "retrieveAll", params, signatures);
220: for (int index = 0; index < data.size(); index++) {
221: String name = (String) data.get(index);
222: if (value.equals(name)) {
223: return true;
224: }
225: }
226: } catch (Exception e) {
227: }
228: return false;
229: }
230:
231: private RowKey getTableRow() {
232: FacesContext fc = FacesContext.getCurrentInstance();
233: ValueBinding vb = fc.getApplication().createValueBinding(
234: "#{databaseAttribute.tableRow}");
235: return (RowKey) vb.getValue(fc);
236: }
237:
238: }
|